| 1 | package net.minecraft.world.entity.animal.armadillo;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.IntFunction;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 9 | import net.minecraft.network.codec.StreamCodec;
|
| 10 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 11 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 12 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.sounds.SoundEvent;
|
| 15 | import net.minecraft.sounds.SoundEvents;
|
| 16 | import net.minecraft.tags.BlockTags;
|
| 17 | import net.minecraft.tags.DamageTypeTags;
|
| 18 | import net.minecraft.tags.EntityTypeTags;
|
| 19 | import net.minecraft.tags.ItemTags;
|
| 20 | import net.minecraft.util.ByIdMap;
|
| 21 | import net.minecraft.util.RandomSource;
|
| 22 | import net.minecraft.util.StringRepresentable;
|
| 23 | import net.minecraft.util.TimeUtil;
|
| 24 | import net.minecraft.util.profiling.Profiler;
|
| 25 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 26 | import net.minecraft.world.InteractionHand;
|
| 27 | import net.minecraft.world.InteractionResult;
|
| 28 | import net.minecraft.world.damagesource.DamageSource;
|
| 29 | import net.minecraft.world.entity.AgeableMob;
|
| 30 | import net.minecraft.world.entity.AnimationState;
|
| 31 | import net.minecraft.world.entity.Entity;
|
| 32 | import net.minecraft.world.entity.EntityDimensions;
|
| 33 | import net.minecraft.world.entity.EntityEvent;
|
| 34 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 35 | import net.minecraft.world.entity.EntityType;
|
| 36 | import net.minecraft.world.entity.EntityTypes;
|
| 37 | import net.minecraft.world.entity.LivingEntity;
|
| 38 | import net.minecraft.world.entity.Pose;
|
| 39 | import net.minecraft.world.entity.ai.Brain;
|
| 40 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 41 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 42 | import net.minecraft.world.entity.ai.control.BodyRotationControl;
|
| 43 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 44 | import net.minecraft.world.entity.ai.sensing.SensorType;
|
| 45 | import net.minecraft.world.entity.animal.Animal;
|
| 46 | import net.minecraft.world.entity.player.Player;
|
| 47 | import net.minecraft.world.item.ItemStack;
|
| 48 | import net.minecraft.world.item.Items;
|
| 49 | import net.minecraft.world.level.Level;
|
| 50 | import net.minecraft.world.level.LevelAccessor;
|
| 51 | import net.minecraft.world.level.block.state.BlockState;
|
| 52 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 53 | import net.minecraft.world.level.storage.ValueInput;
|
| 54 | import net.minecraft.world.level.storage.ValueOutput;
|
| 55 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 56 | import org.jspecify.annotations.Nullable;
|
| 57 |
|
| 58 | public class Armadillo extends Animal {
|
| 59 | public static final float BABY_SCALE = 0.6F;
|
| 60 | public static final float MAX_HEAD_ROTATION_EXTENT = 32.5F;
|
| 61 | public static final int SCARE_CHECK_INTERVAL = 80;
|
| 62 | private static final double SCARE_DISTANCE_HORIZONTAL = 7.0;
|
| 63 | private static final double SCARE_DISTANCE_VERTICAL = 2.0;
|
| 64 | private static final EntityDimensions BABY_DIMENSIONS = EntityTypes.ARMADILLO.getDimensions().scale(0.6F).withEyeHeight(0.21875F);
|
| 65 | private static final Brain.Provider<Armadillo> BRAIN_PROVIDER = Brain.provider(
|
| 66 | List.of(SensorType.NEAREST_LIVING_ENTITIES, SensorType.HURT_BY, SensorType.FOOD_TEMPTATIONS, SensorType.NEAREST_ADULT, SensorType.ARMADILLO_SCARE_DETECTED),
|
| 67 | var0 -> ArmadilloAi.getActivities()
|
| 68 | );
|
| 69 | private static final EntityDataAccessor<Armadillo.ArmadilloState> ARMADILLO_STATE = SynchedEntityData.defineId(
|
| 70 | Armadillo.class, EntityDataSerializers.ARMADILLO_STATE
|
| 71 | );
|
| 72 | private long inStateTicks = 0L;
|
| 73 | public final AnimationState rollOutAnimationState = new AnimationState();
|
| 74 | public final AnimationState rollUpAnimationState = new AnimationState();
|
| 75 | public final AnimationState peekAnimationState = new AnimationState();
|
| 76 | private int scuteTime;
|
| 77 | private boolean peekReceivedClient = false;
|
| 78 |
|
| 79 | public Armadillo(final EntityType<? extends Animal> type, final Level level) {
|
| 80 | super(type, level);
|
| 81 | this.getNavigation().setCanFloat(true);
|
| 82 | this.scuteTime = this.pickNextScuteDropTime();
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 87 | return EntityTypes.ARMADILLO.create(level, EntitySpawnReason.BREEDING);
|
| 88 | }
|
| 89 |
|
| 90 | public static AttributeSupplier.Builder createAttributes() {
|
| 91 | return Animal.createAnimalAttributes().add(Attributes.MAX_HEALTH, 12.0).add(Attributes.MOVEMENT_SPEED, 0.14);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 96 | super.defineSynchedData(entityData);
|
| 97 | entityData.define(ARMADILLO_STATE, Armadillo.ArmadilloState.IDLE);
|
| 98 | }
|
| 99 |
|
| 100 | public boolean isScared() {
|
| 101 | return this.entityData.get(ARMADILLO_STATE) != Armadillo.ArmadilloState.IDLE;
|
| 102 | }
|
| 103 |
|
| 104 | public boolean shouldHideInShell() {
|
| 105 | return this.getState().shouldHideInShell(this.inStateTicks);
|
| 106 | }
|
| 107 |
|
| 108 | public boolean shouldSwitchToScaredState() {
|
| 109 | return this.getState() == Armadillo.ArmadilloState.ROLLING && this.inStateTicks > Armadillo.ArmadilloState.ROLLING.animationDuration();
|
| 110 | }
|
| 111 |
|
| 112 | public Armadillo.ArmadilloState getState() {
|
| 113 | return this.entityData.get(ARMADILLO_STATE);
|
| 114 | }
|
| 115 |
|
| 116 | public void switchToState(final Armadillo.ArmadilloState state) {
|
| 117 | this.entityData.set(ARMADILLO_STATE, state);
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 122 | if (ARMADILLO_STATE.equals(accessor)) {
|
| 123 | this.inStateTicks = 0L;
|
| 124 | }
|
| 125 |
|
| 126 | super.onSyncedDataUpdated(accessor);
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | protected Brain<Armadillo> makeBrain(final Brain.Packed packedBrain) {
|
| 131 | return BRAIN_PROVIDER.makeBrain(this, packedBrain);
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public Brain<Armadillo> getBrain() {
|
| 136 | return super.getBrain();
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | protected void customServerAiStep(final ServerLevel level) {
|
| 141 | ProfilerFiller profiler = Profiler.get();
|
| 142 | profiler.push("armadilloBrain");
|
| 143 | this.getBrain().tick(level, this);
|
| 144 | profiler.pop();
|
| 145 | profiler.push("armadilloActivityUpdate");
|
| 146 | ArmadilloAi.updateActivity(this);
|
| 147 | profiler.pop();
|
| 148 | if (this.isAlive() && --this.scuteTime <= 0 && this.shouldDropLoot(level)) {
|
| 149 | if (this.dropFromGiftLootTable(level, BuiltInLootTables.ARMADILLO_SHED, this::spawnAtLocation)) {
|
| 150 | this.playSound(SoundEvents.ARMADILLO_SCUTE_DROP, 1.0F, (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F);
|
| 151 | this.gameEvent(GameEvent.ENTITY_PLACE);
|
| 152 | }
|
| 153 |
|
| 154 | this.scuteTime = this.pickNextScuteDropTime();
|
| 155 | }
|
| 156 |
|
| 157 | super.customServerAiStep(level);
|
| 158 | }
|
| 159 |
|
| 160 | private int pickNextScuteDropTime() {
|
| 161 | return this.random.nextInt(20 * TimeUtil.SECONDS_PER_MINUTE * 5) + 20 * TimeUtil.SECONDS_PER_MINUTE * 5;
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public void tick() {
|
| 166 | super.tick();
|
| 167 | if (this.level().isClientSide()) {
|
| 168 | this.setupAnimationStates();
|
| 169 | }
|
| 170 |
|
| 171 | if (this.isScared()) {
|
| 172 | this.clampHeadRotationToBody();
|
| 173 | }
|
| 174 |
|
| 175 | this.inStateTicks++;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public float getAgeScale() {
|
| 180 | return this.isBaby() ? 0.6F : 1.0F;
|
| 181 | }
|
| 182 |
|
| 183 | @Override
|
| 184 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 185 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 186 | }
|
| 187 |
|
| 188 | private void setupAnimationStates() {
|
| 189 | switch (this.getState()) {
|
| 190 | case IDLE:
|
| 191 | this.rollOutAnimationState.stop();
|
| 192 | this.rollUpAnimationState.stop();
|
| 193 | this.peekAnimationState.stop();
|
| 194 | break;
|
| 195 | case ROLLING:
|
| 196 | this.rollOutAnimationState.stop();
|
| 197 | this.rollUpAnimationState.startIfStopped(this.tickCount);
|
| 198 | this.peekAnimationState.stop();
|
| 199 | break;
|
| 200 | case SCARED:
|
| 201 | this.rollOutAnimationState.stop();
|
| 202 | this.rollUpAnimationState.stop();
|
| 203 | if (this.peekReceivedClient) {
|
| 204 | this.peekAnimationState.stop();
|
| 205 | this.peekReceivedClient = false;
|
| 206 | }
|
| 207 |
|
| 208 | if (this.inStateTicks == 0L) {
|
| 209 | this.peekAnimationState.start(this.tickCount);
|
| 210 | this.peekAnimationState.fastForward(Armadillo.ArmadilloState.SCARED.animationDuration(), 1.0F);
|
| 211 | } else {
|
| 212 | this.peekAnimationState.startIfStopped(this.tickCount);
|
| 213 | }
|
| 214 | break;
|
| 215 | case UNROLLING:
|
| 216 | this.rollOutAnimationState.startIfStopped(this.tickCount);
|
| 217 | this.rollUpAnimationState.stop();
|
| 218 | this.peekAnimationState.stop();
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 224 | if (id == 64 && this.level().isClientSide()) {
|
| 225 | this.peekReceivedClient = true;
|
| 226 | this.level().playLocalSound(this.getX(), this.getY(), this.getZ(), SoundEvents.ARMADILLO_PEEK, this.getSoundSource(), 1.0F, 1.0F, false);
|
| 227 | } else {
|
| 228 | super.handleEntityEvent(id);
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | @Override
|
| 233 | public boolean isFood(final ItemStack itemStack) {
|
| 234 | return itemStack.is(ItemTags.ARMADILLO_FOOD);
|
| 235 | }
|
| 236 |
|
| 237 | public static boolean checkArmadilloSpawnRules(
|
| 238 | final EntityType<Armadillo> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 239 | ) {
|
| 240 | return level.getBlockState(pos.below()).is(BlockTags.ARMADILLO_SPAWNABLE_ON) && isBrightEnoughToSpawn(level, pos);
|
| 241 | }
|
| 242 |
|
| 243 | public boolean isScaredBy(final LivingEntity livingEntity) {
|
| 244 | if (!this.getBoundingBox().inflate(7.0, 2.0, 7.0).intersects(livingEntity.getBoundingBox())) {
|
| 245 | return false;
|
| 246 | } else if (livingEntity.is(EntityTypeTags.UNDEAD)) {
|
| 247 | return true;
|
| 248 | } else if (this.getLastHurtByMob() == livingEntity) {
|
| 249 | return true;
|
| 250 | } else if (livingEntity instanceof Player player) {
|
| 251 | return player.isSpectator() ? false : player.isSprinting() || player.isPassenger();
|
| 252 | } else {
|
| 253 | return false;
|
| 254 | }
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 259 | super.addAdditionalSaveData(output);
|
| 260 | output.store("state", Armadillo.ArmadilloState.CODEC, this.getState());
|
| 261 | output.putInt("scute_time", this.scuteTime);
|
| 262 | }
|
| 263 |
|
| 264 | @Override
|
| 265 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 266 | super.readAdditionalSaveData(input);
|
| 267 | this.switchToState(input.read("state", Armadillo.ArmadilloState.CODEC).orElse(Armadillo.ArmadilloState.IDLE));
|
| 268 | input.getInt("scute_time").ifPresent(time -> this.scuteTime = time);
|
| 269 | }
|
| 270 |
|
| 271 | public void rollUp() {
|
| 272 | if (!this.isScared()) {
|
| 273 | this.stopInPlace();
|
| 274 | this.resetLove();
|
| 275 | this.gameEvent(GameEvent.ENTITY_ACTION);
|
| 276 | this.makeSound(SoundEvents.ARMADILLO_ROLL);
|
| 277 | this.switchToState(Armadillo.ArmadilloState.ROLLING);
|
| 278 | }
|
| 279 | }
|
| 280 |
|
| 281 | public void rollOut() {
|
| 282 | if (this.isScared()) {
|
| 283 | this.gameEvent(GameEvent.ENTITY_ACTION);
|
| 284 | this.makeSound(SoundEvents.ARMADILLO_UNROLL_FINISH);
|
| 285 | this.switchToState(Armadillo.ArmadilloState.IDLE);
|
| 286 | }
|
| 287 | }
|
| 288 |
|
| 289 | @Override
|
| 290 | public boolean hurtServer(final ServerLevel level, final DamageSource source, float damage) {
|
| 291 | if (this.isScared()) {
|
| 292 | damage = (damage - 1.0F) / 2.0F;
|
| 293 | }
|
| 294 |
|
| 295 | return super.hurtServer(level, source, damage);
|
| 296 | }
|
| 297 |
|
| 298 | @Override
|
| 299 | protected void actuallyHurt(final ServerLevel level, final DamageSource source, final float dmg) {
|
| 300 | super.actuallyHurt(level, source, dmg);
|
| 301 | if (!this.isNoAi() && !this.isDeadOrDying()) {
|
| 302 | if (source.getEntity() instanceof LivingEntity) {
|
| 303 | this.getBrain().setMemoryWithExpiry(MemoryModuleType.DANGER_DETECTED_RECENTLY, true, 80L);
|
| 304 | if (this.canStayRolledUp()) {
|
| 305 | this.rollUp();
|
| 306 | }
|
| 307 | } else if (source.is(DamageTypeTags.PANIC_ENVIRONMENTAL_CAUSES)) {
|
| 308 | this.rollOut();
|
| 309 | }
|
| 310 | }
|
| 311 | }
|
| 312 |
|
| 313 | @Override
|
| 314 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 315 | ItemStack itemStack = player.getItemInHand(hand);
|
| 316 | if (itemStack.is(Items.BRUSH) && this.brushOffScute(player, itemStack)) {
|
| 317 | itemStack.hurtAndBreak(16, player, hand.asEquipmentSlot());
|
| 318 | return InteractionResult.SUCCESS;
|
| 319 | } else {
|
| 320 | return this.isScared() ? InteractionResult.FAIL : super.mobInteract(player, hand);
|
| 321 | }
|
| 322 | }
|
| 323 |
|
| 324 | public boolean brushOffScute(final @Nullable Entity interactingEntity, final ItemStack tool) {
|
| 325 | if (this.isBaby()) {
|
| 326 | return false;
|
| 327 | }
|
| 328 |
|
| 329 | if (this.level() instanceof ServerLevel level) {
|
| 330 | this.dropFromEntityInteractLootTable(level, BuiltInLootTables.ARMADILLO_BRUSH, interactingEntity, tool, this::spawnAtLocation);
|
| 331 | this.playSound(SoundEvents.ARMADILLO_BRUSH);
|
| 332 | this.gameEvent(GameEvent.ENTITY_INTERACT);
|
| 333 | }
|
| 334 |
|
| 335 | return true;
|
| 336 | }
|
| 337 |
|
| 338 | public boolean canStayRolledUp() {
|
| 339 | return !this.isPanicking() && !this.isInLiquid() && !this.isLeashed() && !this.isPassenger() && !this.isVehicle();
|
| 340 | }
|
| 341 |
|
| 342 | @Override
|
| 343 | public boolean canFallInLove() {
|
| 344 | return super.canFallInLove() && !this.isScared();
|
| 345 | }
|
| 346 |
|
| 347 | @Override
|
| 348 | protected SoundEvent getAmbientSound() {
|
| 349 | return this.isScared() ? null : SoundEvents.ARMADILLO_AMBIENT;
|
| 350 | }
|
| 351 |
|
| 352 | @Override
|
| 353 | protected void playEatingSound() {
|
| 354 | this.makeSound(SoundEvents.ARMADILLO_EAT);
|
| 355 | }
|
| 356 |
|
| 357 | @Override
|
| 358 | protected SoundEvent getDeathSound() {
|
| 359 | return SoundEvents.ARMADILLO_DEATH;
|
| 360 | }
|
| 361 |
|
| 362 | @Override
|
| 363 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 364 | return this.isScared() ? SoundEvents.ARMADILLO_HURT_REDUCED : SoundEvents.ARMADILLO_HURT;
|
| 365 | }
|
| 366 |
|
| 367 | @Override
|
| 368 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 369 | this.playSound(SoundEvents.ARMADILLO_STEP, 0.15F, 1.0F);
|
| 370 | }
|
| 371 |
|
| 372 | @Override
|
| 373 | public int getMaxHeadYRot() {
|
| 374 | return this.isScared() ? 0 : 32;
|
| 375 | }
|
| 376 |
|
| 377 | @Override
|
| 378 | protected BodyRotationControl createBodyControl() {
|
| 379 | return new BodyRotationControl(this) {
|
| 380 | @Override
|
| 381 | public void clientTick() {
|
| 382 | if (!Armadillo.this.isScared()) {
|
| 383 | super.clientTick();
|
| 384 | }
|
| 385 | }
|
| 386 | };
|
| 387 | }
|
| 388 |
|
| 389 | public enum ArmadilloState implements StringRepresentable {
|
| 390 | IDLE("idle", false, 0, 0) {
|
| 391 | @Override
|
| 392 | public boolean shouldHideInShell(final long ticksInState) {
|
| 393 | return false;
|
| 394 | }
|
| 395 | },
|
| 396 | ROLLING("rolling", true, 10, 1) {
|
| 397 | @Override
|
| 398 | public boolean shouldHideInShell(final long ticksInState) {
|
| 399 | return ticksInState > 5L;
|
| 400 | }
|
| 401 | },
|
| 402 | SCARED("scared", true, 50, 2) {
|
| 403 | @Override
|
| 404 | public boolean shouldHideInShell(final long ticksInState) {
|
| 405 | return true;
|
| 406 | }
|
| 407 | },
|
| 408 | UNROLLING("unrolling", true, 30, 3) {
|
| 409 | @Override
|
| 410 | public boolean shouldHideInShell(final long ticksInState) {
|
| 411 | return ticksInState < 26L;
|
| 412 | }
|
| 413 | };
|
| 414 |
|
| 415 | private static final Codec<Armadillo.ArmadilloState> CODEC = StringRepresentable.fromEnum(Armadillo.ArmadilloState::values);
|
| 416 | private static final IntFunction<Armadillo.ArmadilloState> BY_ID = ByIdMap.continuous(
|
| 417 | Armadillo.ArmadilloState::id, values(), ByIdMap.OutOfBoundsStrategy.ZERO
|
| 418 | );
|
| 419 | public static final StreamCodec<ByteBuf, Armadillo.ArmadilloState> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, Armadillo.ArmadilloState::id);
|
| 420 | private final String name;
|
| 421 | private final boolean isThreatened;
|
| 422 | private final int animationDuration;
|
| 423 | private final int id;
|
| 424 |
|
| 425 | ArmadilloState(final String name, final boolean isThreatened, final int animationDuration, final int id) {
|
| 426 | this.name = name;
|
| 427 | this.isThreatened = isThreatened;
|
| 428 | this.animationDuration = animationDuration;
|
| 429 | this.id = id;
|
| 430 | }
|
| 431 |
|
| 432 | @Override
|
| 433 | public String getSerializedName() {
|
| 434 | return this.name;
|
| 435 | }
|
| 436 |
|
| 437 | private int id() {
|
| 438 | return this.id;
|
| 439 | }
|
| 440 |
|
| 441 | public abstract boolean shouldHideInShell(final long ticksInState);
|
| 442 |
|
| 443 | public boolean isThreatened() {
|
| 444 | return this.isThreatened;
|
| 445 | }
|
| 446 |
|
| 447 | public int animationDuration() {
|
| 448 | return this.animationDuration;
|
| 449 | }
|
| 450 | }
|
| 451 | }
|
| 452 |
|