| 1 | package net.minecraft.world.entity.animal.turtle;
|
| 2 |
|
| 3 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 6 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 7 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.server.level.ServerPlayer;
|
| 10 | import net.minecraft.sounds.SoundEvent;
|
| 11 | import net.minecraft.sounds.SoundEvents;
|
| 12 | import net.minecraft.sounds.SoundSource;
|
| 13 | import net.minecraft.stats.Stats;
|
| 14 | import net.minecraft.tags.FluidTags;
|
| 15 | import net.minecraft.tags.ItemTags;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 | import net.minecraft.util.RandomSource;
|
| 18 | import net.minecraft.world.DifficultyInstance;
|
| 19 | import net.minecraft.world.damagesource.DamageSource;
|
| 20 | import net.minecraft.world.entity.AgeableMob;
|
| 21 | import net.minecraft.world.entity.EntityAttachment;
|
| 22 | import net.minecraft.world.entity.EntityAttachments;
|
| 23 | import net.minecraft.world.entity.EntityDimensions;
|
| 24 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 25 | import net.minecraft.world.entity.EntityType;
|
| 26 | import net.minecraft.world.entity.EntityTypes;
|
| 27 | import net.minecraft.world.entity.ExperienceOrb;
|
| 28 | import net.minecraft.world.entity.LightningBolt;
|
| 29 | import net.minecraft.world.entity.MoverType;
|
| 30 | import net.minecraft.world.entity.Pose;
|
| 31 | import net.minecraft.world.entity.SpawnGroupData;
|
| 32 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 33 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 34 | import net.minecraft.world.entity.ai.control.MoveControl;
|
| 35 | import net.minecraft.world.entity.ai.goal.BreedGoal;
|
| 36 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 37 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 38 | import net.minecraft.world.entity.ai.goal.MoveToBlockGoal;
|
| 39 | import net.minecraft.world.entity.ai.goal.PanicGoal;
|
| 40 | import net.minecraft.world.entity.ai.goal.RandomStrollGoal;
|
| 41 | import net.minecraft.world.entity.ai.goal.TemptGoal;
|
| 42 | import net.minecraft.world.entity.ai.navigation.AmphibiousPathNavigation;
|
| 43 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 44 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 45 | import net.minecraft.world.entity.ai.util.DefaultRandomPos;
|
| 46 | import net.minecraft.world.entity.animal.Animal;
|
| 47 | import net.minecraft.world.entity.player.Player;
|
| 48 | import net.minecraft.world.item.ItemStack;
|
| 49 | import net.minecraft.world.level.Level;
|
| 50 | import net.minecraft.world.level.LevelAccessor;
|
| 51 | import net.minecraft.world.level.LevelReader;
|
| 52 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 53 | import net.minecraft.world.level.block.Block;
|
| 54 | import net.minecraft.world.level.block.Blocks;
|
| 55 | import net.minecraft.world.level.block.TurtleEggBlock;
|
| 56 | import net.minecraft.world.level.block.state.BlockState;
|
| 57 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 58 | import net.minecraft.world.level.gamerules.GameRules;
|
| 59 | import net.minecraft.world.level.pathfinder.PathType;
|
| 60 | import net.minecraft.world.level.storage.ValueInput;
|
| 61 | import net.minecraft.world.level.storage.ValueOutput;
|
| 62 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 63 | import net.minecraft.world.phys.Vec3;
|
| 64 | import org.jspecify.annotations.Nullable;
|
| 65 |
|
| 66 | public class Turtle extends Animal {
|
| 67 | private static final EntityDataAccessor<Boolean> HAS_EGG = SynchedEntityData.defineId(Turtle.class, EntityDataSerializers.BOOLEAN);
|
| 68 | private static final EntityDataAccessor<Boolean> LAYING_EGG = SynchedEntityData.defineId(Turtle.class, EntityDataSerializers.BOOLEAN);
|
| 69 | private static final float BABY_SCALE = 0.3F;
|
| 70 | private static final EntityDimensions BABY_DIMENSIONS = EntityTypes.TURTLE
|
| 71 | .getDimensions()
|
| 72 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, EntityTypes.TURTLE.getHeight(), -0.25F))
|
| 73 | .scale(0.3F);
|
| 74 | private static final boolean DEFAULT_HAS_EGG = false;
|
| 75 | private int layEggCounter;
|
| 76 | public static final TargetingConditions.Selector BABY_ON_LAND_SELECTOR = (target, level) -> target.isBaby() && !target.isInWater();
|
| 77 | private BlockPos homePos = BlockPos.ZERO;
|
| 78 | private @Nullable BlockPos travelPos;
|
| 79 | private boolean goingHome;
|
| 80 |
|
| 81 | public Turtle(final EntityType<? extends Turtle> type, final Level level) {
|
| 82 | super(type, level);
|
| 83 | this.setPathfindingMalus(PathType.WATER, 0.0F);
|
| 84 | this.setPathfindingMalus(PathType.DOOR_IRON_CLOSED, -1.0F);
|
| 85 | this.setPathfindingMalus(PathType.DOOR_WOOD_CLOSED, -1.0F);
|
| 86 | this.setPathfindingMalus(PathType.DOOR_OPEN, -1.0F);
|
| 87 | this.moveControl = new Turtle.TurtleMoveControl<>(this);
|
| 88 | }
|
| 89 |
|
| 90 | public void setHomePos(final BlockPos pos) {
|
| 91 | this.homePos = pos;
|
| 92 | }
|
| 93 |
|
| 94 | public BlockPos getHomePos() {
|
| 95 | return this.homePos;
|
| 96 | }
|
| 97 |
|
| 98 | public boolean hasEgg() {
|
| 99 | return this.entityData.get(HAS_EGG);
|
| 100 | }
|
| 101 |
|
| 102 | private void setHasEgg(final boolean onOff) {
|
| 103 | this.entityData.set(HAS_EGG, onOff);
|
| 104 | }
|
| 105 |
|
| 106 | public boolean isLayingEgg() {
|
| 107 | return this.entityData.get(LAYING_EGG);
|
| 108 | }
|
| 109 |
|
| 110 | private void setLayingEgg(final boolean on) {
|
| 111 | this.layEggCounter = on ? 1 : 0;
|
| 112 | this.entityData.set(LAYING_EGG, on);
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 117 | super.defineSynchedData(entityData);
|
| 118 | entityData.define(HAS_EGG, false);
|
| 119 | entityData.define(LAYING_EGG, false);
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 124 | super.addAdditionalSaveData(output);
|
| 125 | output.store("home_pos", BlockPos.CODEC, this.homePos);
|
| 126 | output.putBoolean("has_egg", this.hasEgg());
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 131 | this.setHomePos(input.read("home_pos", BlockPos.CODEC).orElse(this.blockPosition()));
|
| 132 | super.readAdditionalSaveData(input);
|
| 133 | this.setHasEgg(input.getBooleanOr("has_egg", false));
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 138 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 139 | ) {
|
| 140 | this.setHomePos(this.blockPosition());
|
| 141 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 142 | }
|
| 143 |
|
| 144 | public static boolean checkTurtleSpawnRules(
|
| 145 | final EntityType<Turtle> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 146 | ) {
|
| 147 | return pos.getY() < level.getSeaLevel() + 4 && TurtleEggBlock.onSand(level, pos) && isBrightEnoughToSpawn(level, pos);
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | protected void registerGoals() {
|
| 152 | this.goalSelector.addGoal(0, new Turtle.TurtlePanicGoal(this, 1.2));
|
| 153 | this.goalSelector.addGoal(1, new Turtle.TurtleBreedGoal(this, 1.0));
|
| 154 | this.goalSelector.addGoal(1, new Turtle.TurtleLayEggGoal(this, 1.0));
|
| 155 | this.goalSelector.addGoal(2, new TemptGoal(this, 1.1, i -> i.is(ItemTags.TURTLE_FOOD), false));
|
| 156 | this.goalSelector.addGoal(3, new Turtle.TurtleGoToWaterGoal(this, 1.0));
|
| 157 | this.goalSelector.addGoal(4, new Turtle.TurtleGoHomeGoal(this, 1.0));
|
| 158 | this.goalSelector.addGoal(7, new Turtle.TurtleTravelGoal(this, 1.0));
|
| 159 | this.goalSelector.addGoal(8, new LookAtPlayerGoal(this, Player.class, 8.0F));
|
| 160 | this.goalSelector.addGoal(9, new Turtle.TurtleRandomStrollGoal(this, 1.0, 100));
|
| 161 | }
|
| 162 |
|
| 163 | public static AttributeSupplier.Builder createAttributes() {
|
| 164 | return Animal.createAnimalAttributes().add(Attributes.MAX_HEALTH, 30.0).add(Attributes.MOVEMENT_SPEED, 0.25).add(Attributes.STEP_HEIGHT, 1.0);
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public boolean isPushedByFluid() {
|
| 169 | return false;
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public int getAmbientSoundInterval() {
|
| 174 | return 200;
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | protected @Nullable SoundEvent getAmbientSound() {
|
| 179 | return !this.isInWater() && this.onGround() && !this.isBaby() ? SoundEvents.TURTLE_AMBIENT_LAND : super.getAmbientSound();
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | protected void playSwimSound(final float volume) {
|
| 184 | super.playSwimSound(volume * 1.5F);
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | protected SoundEvent getSwimSound() {
|
| 189 | return SoundEvents.TURTLE_SWIM;
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | protected @Nullable SoundEvent getHurtSound(final DamageSource source) {
|
| 194 | return this.isBaby() ? SoundEvents.TURTLE_HURT_BABY : SoundEvents.TURTLE_HURT;
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | protected @Nullable SoundEvent getDeathSound() {
|
| 199 | return this.isBaby() ? SoundEvents.TURTLE_DEATH_BABY : SoundEvents.TURTLE_DEATH;
|
| 200 | }
|
| 201 |
|
| 202 | @Override
|
| 203 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 204 | SoundEvent sound = this.isBaby() ? SoundEvents.TURTLE_SHAMBLE_BABY : SoundEvents.TURTLE_SHAMBLE;
|
| 205 | this.playSound(sound, 0.15F, 1.0F);
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public boolean canFallInLove() {
|
| 210 | return super.canFallInLove() && !this.hasEgg();
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | protected float nextStep() {
|
| 215 | return this.moveDist + 0.15F;
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | public float getAgeScale() {
|
| 220 | return this.isBaby() ? 0.3F : 1.0F;
|
| 221 | }
|
| 222 |
|
| 223 | @Override
|
| 224 | protected PathNavigation createNavigation(final Level level) {
|
| 225 | return new Turtle.TurtlePathNavigation(this, level);
|
| 226 | }
|
| 227 |
|
| 228 | @Override
|
| 229 | public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 230 | return EntityTypes.TURTLE.create(level, EntitySpawnReason.BREEDING);
|
| 231 | }
|
| 232 |
|
| 233 | @Override
|
| 234 | public boolean isFood(final ItemStack itemStack) {
|
| 235 | return itemStack.is(ItemTags.TURTLE_FOOD);
|
| 236 | }
|
| 237 |
|
| 238 | @Override
|
| 239 | public float getWalkTargetValue(final BlockPos pos, final LevelReader level) {
|
| 240 | if (!this.goingHome && level.getFluidState(pos).is(FluidTags.WATER)) {
|
| 241 | return 10.0F;
|
| 242 | } else {
|
| 243 | return TurtleEggBlock.onSand(level, pos) ? 10.0F : level.getPathfindingCostFromLightLevels(pos);
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public void aiStep() {
|
| 249 | super.aiStep();
|
| 250 | if (this.isAlive() && this.isLayingEgg() && this.layEggCounter >= 1 && this.layEggCounter % 5 == 0) {
|
| 251 | BlockPos pos = this.blockPosition();
|
| 252 | if (TurtleEggBlock.onSand(this.level(), pos)) {
|
| 253 | this.level().levelEvent(2001, pos, Block.getId(this.level().getBlockState(pos.below())));
|
| 254 | this.gameEvent(GameEvent.ENTITY_ACTION);
|
| 255 | }
|
| 256 | }
|
| 257 | }
|
| 258 |
|
| 259 | @Override
|
| 260 | protected void ageBoundaryReached() {
|
| 261 | super.ageBoundaryReached();
|
| 262 | if (!this.isBaby() && this.level() instanceof ServerLevel level && level.getGameRules().get(GameRules.MOB_DROPS)) {
|
| 263 | this.dropFromGiftLootTable(level, BuiltInLootTables.TURTLE_GROW, this::spawnAtLocation);
|
| 264 | }
|
| 265 | }
|
| 266 |
|
| 267 | @Override
|
| 268 | protected void travelInWater(final Vec3 input, final double baseGravity, final boolean isFalling, final double oldY) {
|
| 269 | this.moveRelative(0.1F, input);
|
| 270 | this.move(MoverType.SELF, this.getDeltaMovement());
|
| 271 | this.setDeltaMovement(this.getDeltaMovement().scale(0.9));
|
| 272 | if (this.getTarget() == null && (!this.goingHome || !this.homePos.closerToCenterThan(this.position(), 20.0))) {
|
| 273 | this.setDeltaMovement(this.getDeltaMovement().add(0.0, -0.005, 0.0));
|
| 274 | }
|
| 275 | }
|
| 276 |
|
| 277 | @Override
|
| 278 | public boolean canBeLeashed() {
|
| 279 | return false;
|
| 280 | }
|
| 281 |
|
| 282 | @Override
|
| 283 | public void thunderHit(final ServerLevel level, final LightningBolt lightningBolt) {
|
| 284 | this.hurtServer(level, this.damageSources().lightningBolt(), Float.MAX_VALUE);
|
| 285 | }
|
| 286 |
|
| 287 | @Override
|
| 288 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 289 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 290 | }
|
| 291 |
|
| 292 | private static class TurtleBreedGoal extends BreedGoal {
|
| 293 | private final Turtle turtle;
|
| 294 |
|
| 295 | public TurtleBreedGoal(final Turtle turtle, final double speedModifier) {
|
| 296 | super(turtle, speedModifier);
|
| 297 | this.turtle = turtle;
|
| 298 | }
|
| 299 |
|
| 300 | @Override
|
| 301 | public boolean canUse() {
|
| 302 | return super.canUse() && !this.turtle.hasEgg();
|
| 303 | }
|
| 304 |
|
| 305 | @Override
|
| 306 | protected void breed() {
|
| 307 | ServerPlayer loveCause = this.animal.getLoveCause();
|
| 308 | if (loveCause == null && this.partner.getLoveCause() != null) {
|
| 309 | loveCause = this.partner.getLoveCause();
|
| 310 | }
|
| 311 |
|
| 312 | if (loveCause != null) {
|
| 313 | loveCause.awardStat(Stats.ANIMALS_BRED);
|
| 314 | CriteriaTriggers.BRED_ANIMALS.trigger(loveCause, this.animal, this.partner, null);
|
| 315 | }
|
| 316 |
|
| 317 | this.turtle.setHasEgg(true);
|
| 318 | this.animal.setAge(6000);
|
| 319 | this.partner.setAge(6000);
|
| 320 | this.animal.resetLove();
|
| 321 | this.partner.resetLove();
|
| 322 | RandomSource random = this.animal.getRandom();
|
| 323 | if (getServerLevel(this.level).getGameRules().get(GameRules.MOB_DROPS)) {
|
| 324 | this.level.addFreshEntity(new ExperienceOrb(this.level, this.animal.getX(), this.animal.getY(), this.animal.getZ(), random.nextInt(7) + 1));
|
| 325 | }
|
| 326 | }
|
| 327 | }
|
| 328 |
|
| 329 | private static class TurtleGoHomeGoal extends Goal {
|
| 330 | private final Turtle turtle;
|
| 331 | private final double speedModifier;
|
| 332 | private boolean stuck;
|
| 333 | private int closeToHomeTryTicks;
|
| 334 | private static final int GIVE_UP_TICKS = 600;
|
| 335 |
|
| 336 | public TurtleGoHomeGoal(final Turtle turtle, final double speedModifier) {
|
| 337 | this.turtle = turtle;
|
| 338 | this.speedModifier = speedModifier;
|
| 339 | }
|
| 340 |
|
| 341 | @Override
|
| 342 | public boolean canUse() {
|
| 343 | if (this.turtle.isBaby()) {
|
| 344 | return false;
|
| 345 | } else if (this.turtle.hasEgg()) {
|
| 346 | return true;
|
| 347 | } else {
|
| 348 | return this.turtle.getRandom().nextInt(reducedTickDelay(700)) != 0 ? false : !this.turtle.homePos.closerToCenterThan(this.turtle.position(), 64.0);
|
| 349 | }
|
| 350 | }
|
| 351 |
|
| 352 | @Override
|
| 353 | public void start() {
|
| 354 | this.turtle.goingHome = true;
|
| 355 | this.stuck = false;
|
| 356 | this.closeToHomeTryTicks = 0;
|
| 357 | }
|
| 358 |
|
| 359 | @Override
|
| 360 | public void stop() {
|
| 361 | this.turtle.goingHome = false;
|
| 362 | }
|
| 363 |
|
| 364 | @Override
|
| 365 | public boolean canContinueToUse() {
|
| 366 | return !this.turtle.homePos.closerToCenterThan(this.turtle.position(), 7.0) && !this.stuck && this.closeToHomeTryTicks <= this.adjustedTickDelay(600);
|
| 367 | }
|
| 368 |
|
| 369 | @Override
|
| 370 | public void tick() {
|
| 371 | BlockPos homePos = this.turtle.homePos;
|
| 372 | boolean closeToHome = homePos.closerToCenterThan(this.turtle.position(), 16.0);
|
| 373 | if (closeToHome) {
|
| 374 | this.closeToHomeTryTicks++;
|
| 375 | }
|
| 376 |
|
| 377 | if (this.turtle.getNavigation().isDone()) {
|
| 378 | Vec3 homePosVec = Vec3.atBottomCenterOf(homePos);
|
| 379 | Vec3 nextPos = DefaultRandomPos.getPosTowards(this.turtle, 16, 3, homePosVec, (float) (Math.PI / 10));
|
| 380 | if (nextPos == null) {
|
| 381 | nextPos = DefaultRandomPos.getPosTowards(this.turtle, 8, 7, homePosVec, (float) (Math.PI / 2));
|
| 382 | }
|
| 383 |
|
| 384 | if (nextPos != null && !closeToHome && !this.turtle.level().getBlockState(BlockPos.containing(nextPos)).is(Blocks.WATER)) {
|
| 385 | nextPos = DefaultRandomPos.getPosTowards(this.turtle, 16, 5, homePosVec, (float) (Math.PI / 2));
|
| 386 | }
|
| 387 |
|
| 388 | if (nextPos == null) {
|
| 389 | this.stuck = true;
|
| 390 | return;
|
| 391 | }
|
| 392 |
|
| 393 | this.turtle.getNavigation().moveTo(nextPos.x, nextPos.y, nextPos.z, this.speedModifier);
|
| 394 | }
|
| 395 | }
|
| 396 | }
|
| 397 |
|
| 398 | private static class TurtleGoToWaterGoal extends MoveToBlockGoal {
|
| 399 | private static final int GIVE_UP_TICKS = 1200;
|
| 400 | private final Turtle turtle;
|
| 401 |
|
| 402 | private TurtleGoToWaterGoal(final Turtle turtle, final double speedModifier) {
|
| 403 | super(turtle, turtle.isBaby() ? 2.0 : speedModifier, 24);
|
| 404 | this.turtle = turtle;
|
| 405 | this.verticalSearchStart = -1;
|
| 406 | }
|
| 407 |
|
| 408 | @Override
|
| 409 | public boolean canContinueToUse() {
|
| 410 | return !this.turtle.isInWater() && this.tryTicks <= 1200 && this.isValidTarget(this.turtle.level(), this.blockPos);
|
| 411 | }
|
| 412 |
|
| 413 | @Override
|
| 414 | public boolean canUse() {
|
| 415 | if (this.turtle.isBaby() && !this.turtle.isInWater()) {
|
| 416 | return super.canUse();
|
| 417 | } else {
|
| 418 | return !this.turtle.goingHome && !this.turtle.isInWater() && !this.turtle.hasEgg() ? super.canUse() : false;
|
| 419 | }
|
| 420 | }
|
| 421 |
|
| 422 | @Override
|
| 423 | public boolean shouldRecalculatePath() {
|
| 424 | return this.tryTicks % 160 == 0;
|
| 425 | }
|
| 426 |
|
| 427 | @Override
|
| 428 | protected boolean isValidTarget(final LevelReader level, final BlockPos pos) {
|
| 429 | return level.getBlockState(pos).is(Blocks.WATER);
|
| 430 | }
|
| 431 | }
|
| 432 |
|
| 433 | private static class TurtleLayEggGoal extends MoveToBlockGoal {
|
| 434 | private final Turtle turtle;
|
| 435 |
|
| 436 | public TurtleLayEggGoal(final Turtle turtle, final double speedModifier) {
|
| 437 | super(turtle, speedModifier, 16);
|
| 438 | this.turtle = turtle;
|
| 439 | }
|
| 440 |
|
| 441 | @Override
|
| 442 | public boolean canUse() {
|
| 443 | return this.turtle.hasEgg() && this.turtle.homePos.closerToCenterThan(this.turtle.position(), 9.0) ? super.canUse() : false;
|
| 444 | }
|
| 445 |
|
| 446 | @Override
|
| 447 | public boolean canContinueToUse() {
|
| 448 | return super.canContinueToUse() && this.turtle.hasEgg() && this.turtle.homePos.closerToCenterThan(this.turtle.position(), 9.0);
|
| 449 | }
|
| 450 |
|
| 451 | @Override
|
| 452 | public void tick() {
|
| 453 | super.tick();
|
| 454 | BlockPos turtlePos = this.turtle.blockPosition();
|
| 455 | if (!this.turtle.isInWater() && this.isReachedTarget()) {
|
| 456 | if (this.turtle.layEggCounter < 1) {
|
| 457 | this.turtle.setLayingEgg(true);
|
| 458 | } else if (this.turtle.layEggCounter > this.adjustedTickDelay(200)) {
|
| 459 | Level level = this.turtle.level();
|
| 460 | level.playSound(null, turtlePos, SoundEvents.TURTLE_LAY_EGG, SoundSource.BLOCKS, 0.3F, 0.9F + level.getRandom().nextFloat() * 0.2F);
|
| 461 | BlockPos eggPos = this.blockPos.above();
|
| 462 | BlockState eggState = Blocks.TURTLE_EGG.defaultBlockState().setValue(TurtleEggBlock.EGGS, this.turtle.random.nextInt(4) + 1);
|
| 463 | level.setBlockAndUpdate(eggPos, eggState);
|
| 464 | level.gameEvent(GameEvent.BLOCK_PLACE, eggPos, GameEvent.Context.of(this.turtle, eggState));
|
| 465 | this.turtle.setHasEgg(false);
|
| 466 | this.turtle.setLayingEgg(false);
|
| 467 | this.turtle.setInLoveTime(600);
|
| 468 | }
|
| 469 |
|
| 470 | if (this.turtle.isLayingEgg()) {
|
| 471 | this.turtle.layEggCounter++;
|
| 472 | }
|
| 473 | }
|
| 474 | }
|
| 475 |
|
| 476 | @Override
|
| 477 | protected boolean isValidTarget(final LevelReader level, final BlockPos pos) {
|
| 478 | return !level.isEmptyBlock(pos.above()) ? false : TurtleEggBlock.isSand(level, pos);
|
| 479 | }
|
| 480 | }
|
| 481 |
|
| 482 | private static class TurtleMoveControl<T extends Turtle> extends MoveControl<T> {
|
| 483 | public TurtleMoveControl(final T turtle) {
|
| 484 | super(turtle);
|
| 485 | }
|
| 486 |
|
| 487 | private void updateSpeed() {
|
| 488 | if (this.mob.isInWater()) {
|
| 489 | this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(0.0, 0.005, 0.0));
|
| 490 | if (!this.mob.getHomePos().closerToCenterThan(this.mob.position(), 16.0)) {
|
| 491 | this.mob.setSpeed(Math.max(this.mob.getSpeed() / 2.0F, 0.08F));
|
| 492 | }
|
| 493 |
|
| 494 | if (this.mob.isBaby()) {
|
| 495 | this.mob.setSpeed(Math.max(this.mob.getSpeed() / 3.0F, 0.06F));
|
| 496 | }
|
| 497 | } else if (this.mob.onGround()) {
|
| 498 | this.mob.setSpeed(Math.max(this.mob.getSpeed() / 2.0F, 0.06F));
|
| 499 | }
|
| 500 | }
|
| 501 |
|
| 502 | @Override
|
| 503 | public void tick() {
|
| 504 | this.updateSpeed();
|
| 505 | if (this.operation == MoveControl.Operation.MOVE_TO && !this.mob.getNavigation().isDone()) {
|
| 506 | double xd = this.wantedX - this.mob.getX();
|
| 507 | double yd = this.wantedY - this.mob.getY();
|
| 508 | double zd = this.wantedZ - this.mob.getZ();
|
| 509 | double dd = Math.sqrt(xd * xd + yd * yd + zd * zd);
|
| 510 | if (dd < 1.0E-5F) {
|
| 511 | this.mob.setSpeed(0.0F);
|
| 512 | } else {
|
| 513 | yd /= dd;
|
| 514 | float yRotD = (float)(Mth.atan2(zd, xd) * 180.0F / (float)Math.PI) - 90.0F;
|
| 515 | this.mob.setYRot(this.rotlerp(this.mob.getYRot(), yRotD, 90.0F));
|
| 516 | this.mob.yBodyRot = this.mob.getYRot();
|
| 517 | float targetSpeed = (float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED));
|
| 518 | this.mob.setSpeed(Mth.lerp(0.125F, this.mob.getSpeed(), targetSpeed));
|
| 519 | this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(0.0, this.mob.getSpeed() * yd * 0.1, 0.0));
|
| 520 | }
|
| 521 | } else {
|
| 522 | this.mob.setSpeed(0.0F);
|
| 523 | }
|
| 524 | }
|
| 525 | }
|
| 526 |
|
| 527 | private static class TurtlePanicGoal extends PanicGoal {
|
| 528 | public TurtlePanicGoal(final Turtle turtle, final double speedModifier) {
|
| 529 | super(turtle, speedModifier);
|
| 530 | }
|
| 531 |
|
| 532 | @Override
|
| 533 | public boolean canUse() {
|
| 534 | if (!this.shouldPanic()) {
|
| 535 | return false;
|
| 536 | } else {
|
| 537 | BlockPos blockPos = this.lookForWater(this.mob.level(), this.mob, 7);
|
| 538 | if (blockPos != null) {
|
| 539 | this.posX = blockPos.getX();
|
| 540 | this.posY = blockPos.getY();
|
| 541 | this.posZ = blockPos.getZ();
|
| 542 | return true;
|
| 543 | } else {
|
| 544 | return this.findRandomPosition();
|
| 545 | }
|
| 546 | }
|
| 547 | }
|
| 548 | }
|
| 549 |
|
| 550 | private static class TurtlePathNavigation extends AmphibiousPathNavigation {
|
| 551 | public TurtlePathNavigation(final Turtle mob, final Level level) {
|
| 552 | super(mob, level);
|
| 553 | }
|
| 554 |
|
| 555 | @Override
|
| 556 | public boolean isStableDestination(final BlockPos pos) {
|
| 557 | return this.mob instanceof Turtle turtle && turtle.travelPos != null
|
| 558 | ? this.level.getBlockState(pos).is(Blocks.WATER)
|
| 559 | : !this.level.getBlockState(pos.below()).isAir();
|
| 560 | }
|
| 561 | }
|
| 562 |
|
| 563 | private static class TurtleRandomStrollGoal extends RandomStrollGoal {
|
| 564 | private final Turtle turtle;
|
| 565 |
|
| 566 | private TurtleRandomStrollGoal(final Turtle turtle, final double speedModifier, final int interval) {
|
| 567 | super(turtle, speedModifier, interval);
|
| 568 | this.turtle = turtle;
|
| 569 | }
|
| 570 |
|
| 571 | @Override
|
| 572 | public boolean canUse() {
|
| 573 | return !this.mob.isInWater() && !this.turtle.goingHome && !this.turtle.hasEgg() ? super.canUse() : false;
|
| 574 | }
|
| 575 | }
|
| 576 |
|
| 577 | private static class TurtleTravelGoal extends Goal {
|
| 578 | private final Turtle turtle;
|
| 579 | private final double speedModifier;
|
| 580 | private boolean stuck;
|
| 581 |
|
| 582 | public TurtleTravelGoal(final Turtle turtle, final double speedModifier) {
|
| 583 | this.turtle = turtle;
|
| 584 | this.speedModifier = speedModifier;
|
| 585 | }
|
| 586 |
|
| 587 | @Override
|
| 588 | public boolean canUse() {
|
| 589 | return !this.turtle.goingHome && !this.turtle.hasEgg() && this.turtle.isInWater();
|
| 590 | }
|
| 591 |
|
| 592 | @Override
|
| 593 | public void start() {
|
| 594 | int xzDist = 512;
|
| 595 | int yDist = 4;
|
| 596 | RandomSource random = this.turtle.random;
|
| 597 | int xt = random.nextInt(1025) - 512;
|
| 598 | int yt = random.nextInt(9) - 4;
|
| 599 | int zt = random.nextInt(1025) - 512;
|
| 600 | if (yt + this.turtle.getY() > this.turtle.level().getSeaLevel() - 1) {
|
| 601 | yt = 0;
|
| 602 | }
|
| 603 |
|
| 604 | this.turtle.travelPos = BlockPos.containing(xt + this.turtle.getX(), yt + this.turtle.getY(), zt + this.turtle.getZ());
|
| 605 | this.stuck = false;
|
| 606 | }
|
| 607 |
|
| 608 | @Override
|
| 609 | public void tick() {
|
| 610 | if (this.turtle.travelPos == null) {
|
| 611 | this.stuck = true;
|
| 612 | } else {
|
| 613 | if (this.turtle.getNavigation().isDone()) {
|
| 614 | Vec3 targetPos = Vec3.atBottomCenterOf(this.turtle.travelPos);
|
| 615 | Vec3 nextPos = DefaultRandomPos.getPosTowards(this.turtle, 16, 3, targetPos, (float) (Math.PI / 10));
|
| 616 | if (nextPos == null) {
|
| 617 | nextPos = DefaultRandomPos.getPosTowards(this.turtle, 8, 7, targetPos, (float) (Math.PI / 2));
|
| 618 | }
|
| 619 |
|
| 620 | if (nextPos != null) {
|
| 621 | int xc = Mth.floor(nextPos.x);
|
| 622 | int zc = Mth.floor(nextPos.z);
|
| 623 | int r = 34;
|
| 624 | if (!this.turtle.level().hasChunksAt(xc - 34, zc - 34, xc + 34, zc + 34)) {
|
| 625 | nextPos = null;
|
| 626 | }
|
| 627 | }
|
| 628 |
|
| 629 | if (nextPos == null) {
|
| 630 | this.stuck = true;
|
| 631 | return;
|
| 632 | }
|
| 633 |
|
| 634 | this.turtle.getNavigation().moveTo(nextPos.x, nextPos.y, nextPos.z, this.speedModifier);
|
| 635 | }
|
| 636 | }
|
| 637 | }
|
| 638 |
|
| 639 | @Override
|
| 640 | public boolean canContinueToUse() {
|
| 641 | return !this.turtle.getNavigation().isDone() && !this.stuck && !this.turtle.goingHome && !this.turtle.isInLove() && !this.turtle.hasEgg();
|
| 642 | }
|
| 643 |
|
| 644 | @Override
|
| 645 | public void stop() {
|
| 646 | this.turtle.travelPos = null;
|
| 647 | super.stop();
|
| 648 | }
|
| 649 | }
|
| 650 | }
|
| 651 |
|