| 1 | package net.minecraft.world.entity.animal.fish;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.nbt.CompoundTag;
|
| 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.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.tags.FluidTags;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.world.InteractionHand;
|
| 13 | import net.minecraft.world.InteractionResult;
|
| 14 | import net.minecraft.world.entity.Bucketable;
|
| 15 | import net.minecraft.world.entity.EntitySelector;
|
| 16 | import net.minecraft.world.entity.EntityType;
|
| 17 | import net.minecraft.world.entity.Mob;
|
| 18 | import net.minecraft.world.entity.MoverType;
|
| 19 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 20 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 21 | import net.minecraft.world.entity.ai.control.MoveControl;
|
| 22 | import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
|
| 23 | import net.minecraft.world.entity.ai.goal.PanicGoal;
|
| 24 | import net.minecraft.world.entity.ai.goal.RandomSwimmingGoal;
|
| 25 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 26 | import net.minecraft.world.entity.ai.navigation.WaterBoundPathNavigation;
|
| 27 | import net.minecraft.world.entity.player.Player;
|
| 28 | import net.minecraft.world.item.ItemStack;
|
| 29 | import net.minecraft.world.level.Level;
|
| 30 | import net.minecraft.world.level.block.state.BlockState;
|
| 31 | import net.minecraft.world.level.storage.ValueInput;
|
| 32 | import net.minecraft.world.level.storage.ValueOutput;
|
| 33 | import net.minecraft.world.phys.Vec3;
|
| 34 |
|
| 35 | public abstract class AbstractFish extends WaterAnimal implements Bucketable {
|
| 36 | private static final EntityDataAccessor<Boolean> FROM_BUCKET = SynchedEntityData.defineId(AbstractFish.class, EntityDataSerializers.BOOLEAN);
|
| 37 | private static final boolean DEFAULT_FROM_BUCKET = false;
|
| 38 |
|
| 39 | public AbstractFish(final EntityType<? extends AbstractFish> type, final Level level) {
|
| 40 | super(type, level);
|
| 41 | this.moveControl = new AbstractFish.FishMoveControl<>(this);
|
| 42 | }
|
| 43 |
|
| 44 | public static AttributeSupplier.Builder createAttributes() {
|
| 45 | return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 3.0);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public boolean requiresCustomPersistence() {
|
| 50 | return super.requiresCustomPersistence() || this.fromBucket();
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public boolean removeWhenFarAway(final double distSqr) {
|
| 55 | return !this.fromBucket() && !this.hasCustomName();
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public int getMaxSpawnClusterSize() {
|
| 60 | return 8;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 65 | super.defineSynchedData(entityData);
|
| 66 | entityData.define(FROM_BUCKET, false);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public boolean fromBucket() {
|
| 71 | return this.entityData.get(FROM_BUCKET);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public void setFromBucket(final boolean fromBucket) {
|
| 76 | this.entityData.set(FROM_BUCKET, fromBucket);
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 81 | super.addAdditionalSaveData(output);
|
| 82 | output.putBoolean("FromBucket", this.fromBucket());
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 87 | super.readAdditionalSaveData(input);
|
| 88 | this.setFromBucket(input.getBooleanOr("FromBucket", false));
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | protected void registerGoals() {
|
| 93 | super.registerGoals();
|
| 94 | this.goalSelector.addGoal(0, new PanicGoal(this, 1.25));
|
| 95 | this.goalSelector.addGoal(2, new AvoidEntityGoal<>(this, Player.class, 8.0F, 1.6, 1.4, EntitySelector.NO_SPECTATORS));
|
| 96 | this.goalSelector.addGoal(4, new AbstractFish.FishSwimGoal(this));
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | protected PathNavigation createNavigation(final Level level) {
|
| 101 | return new WaterBoundPathNavigation(this, level);
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected void travelInWater(final Vec3 input, final double baseGravity, final boolean isFalling, final double oldY) {
|
| 106 | this.moveRelative(0.01F, input);
|
| 107 | this.move(MoverType.SELF, this.getDeltaMovement());
|
| 108 | this.setDeltaMovement(this.getDeltaMovement().scale(0.9));
|
| 109 | if (this.getTarget() == null) {
|
| 110 | this.setDeltaMovement(this.getDeltaMovement().add(0.0, -0.005, 0.0));
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public void aiStep() {
|
| 116 | if (!this.isInWater() && this.onGround() && this.verticalCollision) {
|
| 117 | this.setDeltaMovement(this.getDeltaMovement().add((this.random.nextFloat() * 2.0F - 1.0F) * 0.05F, 0.4F, (this.random.nextFloat() * 2.0F - 1.0F) * 0.05F));
|
| 118 | this.setOnGround(false);
|
| 119 | this.needsSync = true;
|
| 120 | this.makeSound(this.getFlopSound());
|
| 121 | }
|
| 122 |
|
| 123 | super.aiStep();
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | protected InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 128 | return Bucketable.bucketMobPickup(player, hand, this).orElse(super.mobInteract(player, hand));
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public void saveToBucketTag(final ItemStack bucket) {
|
| 133 | Bucketable.saveDefaultDataToBucketTag(this, bucket);
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public void loadFromBucketTag(final CompoundTag tag) {
|
| 138 | Bucketable.loadDefaultDataFromBucketTag(this, tag);
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | public SoundEvent getPickupSound() {
|
| 143 | return SoundEvents.BUCKET_FILL_FISH;
|
| 144 | }
|
| 145 |
|
| 146 | protected boolean canRandomSwim() {
|
| 147 | return true;
|
| 148 | }
|
| 149 |
|
| 150 | protected abstract SoundEvent getFlopSound();
|
| 151 |
|
| 152 | @Override
|
| 153 | protected SoundEvent getSwimSound() {
|
| 154 | return SoundEvents.FISH_SWIM;
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 159 | }
|
| 160 |
|
| 161 | private static class FishMoveControl<T extends AbstractFish> extends MoveControl<T> {
|
| 162 | public FishMoveControl(final T fish) {
|
| 163 | super(fish);
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public void tick() {
|
| 168 | if (this.mob.isEyeInFluid(FluidTags.WATER)) {
|
| 169 | this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(0.0, 0.005, 0.0));
|
| 170 | }
|
| 171 |
|
| 172 | if (this.operation == MoveControl.Operation.MOVE_TO && !this.mob.getNavigation().isDone()) {
|
| 173 | float targetSpeed = (float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED));
|
| 174 | this.mob.setSpeed(Mth.lerp(0.125F, this.mob.getSpeed(), targetSpeed));
|
| 175 | double xd = this.wantedX - this.mob.getX();
|
| 176 | double yd = this.wantedY - this.mob.getY();
|
| 177 | double zd = this.wantedZ - this.mob.getZ();
|
| 178 | if (yd != 0.0) {
|
| 179 | double dd = Math.sqrt(xd * xd + yd * yd + zd * zd);
|
| 180 | this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(0.0, this.mob.getSpeed() * (yd / dd) * 0.1, 0.0));
|
| 181 | }
|
| 182 |
|
| 183 | if (xd != 0.0 || zd != 0.0) {
|
| 184 | float yRotD = (float)(Mth.atan2(zd, xd) * 180.0F / (float)Math.PI) - 90.0F;
|
| 185 | this.mob.setYRot(this.rotlerp(this.mob.getYRot(), yRotD, 90.0F));
|
| 186 | this.mob.yBodyRot = this.mob.getYRot();
|
| 187 | }
|
| 188 | } else {
|
| 189 | this.mob.setSpeed(0.0F);
|
| 190 | }
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | private static class FishSwimGoal extends RandomSwimmingGoal {
|
| 195 | private final AbstractFish fish;
|
| 196 |
|
| 197 | public FishSwimGoal(final AbstractFish fish) {
|
| 198 | super(fish, 1.0, 40);
|
| 199 | this.fish = fish;
|
| 200 | }
|
| 201 |
|
| 202 | @Override
|
| 203 | public boolean canUse() {
|
| 204 | return this.fish.canRandomSwim() && super.canUse();
|
| 205 | }
|
| 206 | }
|
| 207 | }
|
| 208 |
|