| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.sounds.SoundEvents;
|
| 6 | import net.minecraft.tags.BlockTags;
|
| 7 | import net.minecraft.tags.FluidTags;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.world.entity.animal.dolphin.Dolphin;
|
| 10 | import net.minecraft.world.level.material.FluidState;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 |
|
| 13 | public class DolphinJumpGoal extends JumpGoal {
|
| 14 | private static final int[] STEPS_TO_CHECK = new int[]{0, 1, 4, 5, 6, 7};
|
| 15 | private final Dolphin dolphin;
|
| 16 | private final int interval;
|
| 17 | private boolean breached;
|
| 18 |
|
| 19 | public DolphinJumpGoal(final Dolphin dolphin, final int interval) {
|
| 20 | this.dolphin = dolphin;
|
| 21 | this.interval = reducedTickDelay(interval);
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public boolean canUse() {
|
| 26 | if (this.dolphin.getRandom().nextInt(this.interval) != 0) {
|
| 27 | return false;
|
| 28 | }
|
| 29 |
|
| 30 | Direction motion = this.dolphin.getMotionDirection();
|
| 31 | int stepX = motion.getStepX();
|
| 32 | int stepZ = motion.getStepZ();
|
| 33 | BlockPos dolphinPos = this.dolphin.blockPosition();
|
| 34 |
|
| 35 | for (int i : STEPS_TO_CHECK) {
|
| 36 | if (!this.waterIsClear(dolphinPos, stepX, stepZ, i) || !this.surfaceIsClear(dolphinPos, stepX, stepZ, i)) {
|
| 37 | return false;
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | return true;
|
| 42 | }
|
| 43 |
|
| 44 | private boolean waterIsClear(final BlockPos dolphinPos, final int stepX, final int stepZ, final int currentStep) {
|
| 45 | BlockPos nextPos = dolphinPos.offset(stepX * currentStep, 0, stepZ * currentStep);
|
| 46 | return this.dolphin.level().getFluidState(nextPos).is(FluidTags.WATER) && !this.dolphin.level().getBlockState(nextPos).is(BlockTags.BLOCKS_DOLPHIN_JUMP);
|
| 47 | }
|
| 48 |
|
| 49 | private boolean surfaceIsClear(final BlockPos dolphinPos, final int stepX, final int stepZ, final int currentStep) {
|
| 50 | return this.dolphin.level().getBlockState(dolphinPos.offset(stepX * currentStep, 1, stepZ * currentStep)).isAir()
|
| 51 | && this.dolphin.level().getBlockState(dolphinPos.offset(stepX * currentStep, 2, stepZ * currentStep)).isAir();
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public boolean canContinueToUse() {
|
| 56 | double yd = this.dolphin.getDeltaMovement().y;
|
| 57 | return (!(yd * yd < 0.03F) || this.dolphin.getXRot() == 0.0F || !(Math.abs(this.dolphin.getXRot()) < 10.0F) || !this.dolphin.isInWater())
|
| 58 | && !this.dolphin.onGround();
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public boolean isInterruptable() {
|
| 63 | return false;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public void start() {
|
| 68 | Direction direction = this.dolphin.getMotionDirection();
|
| 69 | this.dolphin.setDeltaMovement(this.dolphin.getDeltaMovement().add(direction.getStepX() * 0.6, 0.7, direction.getStepZ() * 0.6));
|
| 70 | this.dolphin.getNavigation().stop();
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public void stop() {
|
| 75 | this.dolphin.setXRot(0.0F);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void tick() {
|
| 80 | boolean alreadyBreached = this.breached;
|
| 81 | if (!alreadyBreached) {
|
| 82 | FluidState fluidState = this.dolphin.level().getFluidState(this.dolphin.blockPosition());
|
| 83 | this.breached = fluidState.is(FluidTags.WATER);
|
| 84 | }
|
| 85 |
|
| 86 | if (this.breached && !alreadyBreached) {
|
| 87 | this.dolphin.playSound(SoundEvents.DOLPHIN_JUMP, 1.0F, 1.0F);
|
| 88 | }
|
| 89 |
|
| 90 | Vec3 movement = this.dolphin.getDeltaMovement();
|
| 91 | if (movement.y * movement.y < 0.03F && this.dolphin.getXRot() != 0.0F) {
|
| 92 | this.dolphin.setXRot(Mth.rotLerp(0.2F, this.dolphin.getXRot(), 0.0F));
|
| 93 | } else if (movement.length() > 1.0E-5F) {
|
| 94 | double horizontalDistance = movement.horizontalDistance();
|
| 95 | double rotation = Math.atan2(-movement.y, horizontalDistance) * 180.0F / (float)Math.PI;
|
| 96 | this.dolphin.setXRot((float)rotation);
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|