| 1 | package net.minecraft.world.entity.animal.dolphin;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Objects;
|
| 6 | import java.util.function.Predicate;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.particles.ParticleOptions;
|
| 9 | import net.minecraft.core.particles.ParticleTypes;
|
| 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.FluidTags;
|
| 17 | import net.minecraft.tags.ItemTags;
|
| 18 | import net.minecraft.tags.StructureTags;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 | import net.minecraft.world.DifficultyInstance;
|
| 21 | import net.minecraft.world.InteractionHand;
|
| 22 | import net.minecraft.world.InteractionResult;
|
| 23 | import net.minecraft.world.damagesource.DamageSource;
|
| 24 | import net.minecraft.world.effect.MobEffectInstance;
|
| 25 | import net.minecraft.world.effect.MobEffects;
|
| 26 | import net.minecraft.world.entity.AgeableMob;
|
| 27 | import net.minecraft.world.entity.EntityAttachment;
|
| 28 | import net.minecraft.world.entity.EntityAttachments;
|
| 29 | import net.minecraft.world.entity.EntityDimensions;
|
| 30 | import net.minecraft.world.entity.EntityEvent;
|
| 31 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 32 | import net.minecraft.world.entity.EntityType;
|
| 33 | import net.minecraft.world.entity.EntityTypes;
|
| 34 | import net.minecraft.world.entity.EquipmentSlot;
|
| 35 | import net.minecraft.world.entity.LivingEntity;
|
| 36 | import net.minecraft.world.entity.Mob;
|
| 37 | import net.minecraft.world.entity.MoverType;
|
| 38 | import net.minecraft.world.entity.Pose;
|
| 39 | import net.minecraft.world.entity.SpawnGroupData;
|
| 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.SmoothSwimmingLookControl;
|
| 43 | import net.minecraft.world.entity.ai.control.SmoothSwimmingMoveControl;
|
| 44 | import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
|
| 45 | import net.minecraft.world.entity.ai.goal.BreathAirGoal;
|
| 46 | import net.minecraft.world.entity.ai.goal.DolphinJumpGoal;
|
| 47 | import net.minecraft.world.entity.ai.goal.FollowPlayerRiddenEntityGoal;
|
| 48 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 49 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 50 | import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
|
| 51 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 52 | import net.minecraft.world.entity.ai.goal.RandomSwimmingGoal;
|
| 53 | import net.minecraft.world.entity.ai.goal.TryFindLiquidGoal;
|
| 54 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 55 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 56 | import net.minecraft.world.entity.ai.navigation.WaterBoundPathNavigation;
|
| 57 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 58 | import net.minecraft.world.entity.ai.util.DefaultRandomPos;
|
| 59 | import net.minecraft.world.entity.animal.AgeableWaterCreature;
|
| 60 | import net.minecraft.world.entity.animal.nautilus.AbstractNautilus;
|
| 61 | import net.minecraft.world.entity.item.ItemEntity;
|
| 62 | import net.minecraft.world.entity.monster.Guardian;
|
| 63 | import net.minecraft.world.entity.player.Player;
|
| 64 | import net.minecraft.world.entity.vehicle.boat.AbstractBoat;
|
| 65 | import net.minecraft.world.item.ItemStack;
|
| 66 | import net.minecraft.world.level.Level;
|
| 67 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 68 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 69 | import net.minecraft.world.level.storage.ValueInput;
|
| 70 | import net.minecraft.world.level.storage.ValueOutput;
|
| 71 | import net.minecraft.world.phys.Vec3;
|
| 72 | import org.jspecify.annotations.Nullable;
|
| 73 |
|
| 74 | public class Dolphin extends AgeableWaterCreature {
|
| 75 | private static final EntityDataAccessor<Boolean> GOT_FISH = SynchedEntityData.defineId(Dolphin.class, EntityDataSerializers.BOOLEAN);
|
| 76 | private static final EntityDataAccessor<Integer> MOISTNESS_LEVEL = SynchedEntityData.defineId(Dolphin.class, EntityDataSerializers.INT);
|
| 77 | private static final TargetingConditions SWIM_WITH_PLAYER_TARGETING = TargetingConditions.forNonCombat().range(10.0).ignoreLineOfSight();
|
| 78 | public static final int TOTAL_AIR_SUPPLY = 4800;
|
| 79 | private static final int TOTAL_MOISTNESS_LEVEL = 2400;
|
| 80 | public static final Predicate<ItemEntity> ALLOWED_ITEMS = e -> !e.hasPickUpDelay() && e.isAlive() && e.isInWater();
|
| 81 | public static final float BABY_SCALE = 0.65F;
|
| 82 | private static final EntityDimensions BABY_DIMENSIONS = EntityTypes.DOLPHIN
|
| 83 | .getDimensions()
|
| 84 | .scale(0.65F)
|
| 85 | .withEyeHeight(0.09375F)
|
| 86 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.3125F, 0.0F));
|
| 87 | private static final boolean DEFAULT_GOT_FISH = false;
|
| 88 | private @Nullable BlockPos treasurePos;
|
| 89 |
|
| 90 | public Dolphin(final EntityType<? extends Dolphin> type, final Level level) {
|
| 91 | super(type, level);
|
| 92 | this.moveControl = new SmoothSwimmingMoveControl<>(this, 85, 10, 0.02F, 0.1F, true);
|
| 93 | this.lookControl = new SmoothSwimmingLookControl(this, 10);
|
| 94 | this.setCanPickUpLoot(true);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 99 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 100 | ) {
|
| 101 | this.setAirSupply(this.getMaxAirSupply());
|
| 102 | this.setXRot(0.0F);
|
| 103 | SpawnGroupData spawnGroupData = Objects.requireNonNullElseGet(groupData, () -> new AgeableMob.AgeableMobGroupData(0.1F));
|
| 104 | return super.finalizeSpawn(level, difficulty, spawnReason, spawnGroupData);
|
| 105 | }
|
| 106 |
|
| 107 | public @Nullable Dolphin getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 108 | return EntityTypes.DOLPHIN.create(level, EntitySpawnReason.BREEDING);
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public float getAgeScale() {
|
| 113 | return this.isBaby() ? 0.65F : 1.0F;
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | protected void handleAirSupply(final int preTickAirSupply) {
|
| 118 | }
|
| 119 |
|
| 120 | public boolean gotFish() {
|
| 121 | return this.entityData.get(GOT_FISH);
|
| 122 | }
|
| 123 |
|
| 124 | public void setGotFish(final boolean gotFish) {
|
| 125 | this.entityData.set(GOT_FISH, gotFish);
|
| 126 | }
|
| 127 |
|
| 128 | public int getMoistnessLevel() {
|
| 129 | return this.entityData.get(MOISTNESS_LEVEL);
|
| 130 | }
|
| 131 |
|
| 132 | public void setMoisntessLevel(final int level) {
|
| 133 | this.entityData.set(MOISTNESS_LEVEL, level);
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 138 | super.defineSynchedData(entityData);
|
| 139 | entityData.define(GOT_FISH, false);
|
| 140 | entityData.define(MOISTNESS_LEVEL, 2400);
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 145 | super.addAdditionalSaveData(output);
|
| 146 | output.putBoolean("GotFish", this.gotFish());
|
| 147 | output.putInt("Moistness", this.getMoistnessLevel());
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 152 | super.readAdditionalSaveData(input);
|
| 153 | this.setGotFish(input.getBooleanOr("GotFish", false));
|
| 154 | this.setMoisntessLevel(input.getIntOr("Moistness", 2400));
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | protected void registerGoals() {
|
| 159 | this.goalSelector.addGoal(0, new BreathAirGoal(this));
|
| 160 | this.goalSelector.addGoal(0, new TryFindLiquidGoal(this, FluidTags.DOLPHIN_TRIES_TO_FIND));
|
| 161 | this.goalSelector.addGoal(1, new Dolphin.DolphinSwimToTreasureGoal(this));
|
| 162 | this.goalSelector.addGoal(2, new Dolphin.DolphinSwimWithPlayerGoal(this, 4.0));
|
| 163 | this.goalSelector.addGoal(4, new RandomSwimmingGoal(this, 1.0, 10));
|
| 164 | this.goalSelector.addGoal(4, new RandomLookAroundGoal(this));
|
| 165 | this.goalSelector.addGoal(5, new LookAtPlayerGoal(this, Player.class, 6.0F));
|
| 166 | this.goalSelector.addGoal(5, new DolphinJumpGoal(this, 10));
|
| 167 | this.goalSelector.addGoal(6, new MeleeAttackGoal(this, 1.2F, true));
|
| 168 | this.goalSelector.addGoal(7, new Dolphin.MoveToItemGoal());
|
| 169 | this.goalSelector.addGoal(8, new Dolphin.PlayWithItemsGoal());
|
| 170 | this.goalSelector.addGoal(8, new FollowPlayerRiddenEntityGoal(this, AbstractBoat.class));
|
| 171 | this.goalSelector.addGoal(8, new FollowPlayerRiddenEntityGoal(this, AbstractNautilus.class));
|
| 172 | this.goalSelector.addGoal(9, new AvoidEntityGoal<>(this, Guardian.class, 8.0F, 1.0, 1.0));
|
| 173 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this, Guardian.class).setAlertOthers());
|
| 174 | }
|
| 175 |
|
| 176 | public static AttributeSupplier.Builder createAttributes() {
|
| 177 | return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0).add(Attributes.MOVEMENT_SPEED, 1.2F).add(Attributes.ATTACK_DAMAGE, 3.0);
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | protected PathNavigation createNavigation(final Level level) {
|
| 182 | return new WaterBoundPathNavigation(this, level);
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public void playAttackSound() {
|
| 187 | this.playSound(SoundEvents.DOLPHIN_ATTACK, 1.0F, 1.0F);
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public boolean canAttack(final LivingEntity target) {
|
| 192 | return !this.isBaby() && super.canAttack(target);
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public int getMaxAirSupply() {
|
| 197 | return 4800;
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | protected int increaseAirSupply(final int currentSupply) {
|
| 202 | return this.getMaxAirSupply();
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | public int getMaxHeadXRot() {
|
| 207 | return 1;
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public int getMaxHeadYRot() {
|
| 212 | return 1;
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | protected boolean canDispenserEquipIntoSlot(final EquipmentSlot slot) {
|
| 217 | return slot == EquipmentSlot.MAINHAND && this.canPickUpLoot();
|
| 218 | }
|
| 219 |
|
| 220 | @Override
|
| 221 | protected void pickUpItem(final ServerLevel level, final ItemEntity entity) {
|
| 222 | if (this.getItemBySlot(EquipmentSlot.MAINHAND).isEmpty()) {
|
| 223 | ItemStack itemStack = entity.getItem();
|
| 224 | if (this.canHoldItem(itemStack)) {
|
| 225 | this.onItemPickup(entity);
|
| 226 | this.setItemSlot(EquipmentSlot.MAINHAND, itemStack);
|
| 227 | this.setGuaranteedDrop(EquipmentSlot.MAINHAND);
|
| 228 | this.take(entity, itemStack.getCount());
|
| 229 | entity.discard();
|
| 230 | }
|
| 231 | }
|
| 232 | }
|
| 233 |
|
| 234 | @Override
|
| 235 | public void tick() {
|
| 236 | super.tick();
|
| 237 | if (this.isNoAi()) {
|
| 238 | this.setAirSupply(this.getMaxAirSupply());
|
| 239 | } else {
|
| 240 | if (this.isInWaterOrRain()) {
|
| 241 | this.setMoisntessLevel(2400);
|
| 242 | } else {
|
| 243 | this.setMoisntessLevel(this.getMoistnessLevel() - 1);
|
| 244 | if (this.getMoistnessLevel() <= 0) {
|
| 245 | this.hurt(this.damageSources().dryOut(), 1.0F);
|
| 246 | }
|
| 247 |
|
| 248 | if (this.onGround()) {
|
| 249 | this.setDeltaMovement(this.getDeltaMovement().add((this.random.nextFloat() * 2.0F - 1.0F) * 0.2F, 0.5, (this.random.nextFloat() * 2.0F - 1.0F) * 0.2F));
|
| 250 | this.setYRot(this.random.nextFloat() * 360.0F);
|
| 251 | this.setOnGround(false);
|
| 252 | this.needsSync = true;
|
| 253 | }
|
| 254 | }
|
| 255 |
|
| 256 | if (this.level().isClientSide() && this.isInWater() && this.getDeltaMovement().lengthSqr() > 0.03) {
|
| 257 | Vec3 viewVector = this.getViewVector(0.0F);
|
| 258 | float c = Mth.cos(this.getYRot() * (float) (Math.PI / 180.0)) * 0.3F;
|
| 259 | float s = Mth.sin(this.getYRot() * (float) (Math.PI / 180.0)) * 0.3F;
|
| 260 | float multiplier = 1.2F - this.random.nextFloat() * 0.7F;
|
| 261 |
|
| 262 | for (int i = 0; i < 2; i++) {
|
| 263 | this.level()
|
| 264 | .addParticle(
|
| 265 | ParticleTypes.DOLPHIN,
|
| 266 | this.getX() - viewVector.x * multiplier + c,
|
| 267 | this.getY() - viewVector.y,
|
| 268 | this.getZ() - viewVector.z * multiplier + s,
|
| 269 | 0.0,
|
| 270 | 0.0,
|
| 271 | 0.0
|
| 272 | );
|
| 273 | this.level()
|
| 274 | .addParticle(
|
| 275 | ParticleTypes.DOLPHIN,
|
| 276 | this.getX() - viewVector.x * multiplier - c,
|
| 277 | this.getY() - viewVector.y,
|
| 278 | this.getZ() - viewVector.z * multiplier - s,
|
| 279 | 0.0,
|
| 280 | 0.0,
|
| 281 | 0.0
|
| 282 | );
|
| 283 | }
|
| 284 | }
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 290 | if (id == 38) {
|
| 291 | this.addParticlesAroundSelf(ParticleTypes.HAPPY_VILLAGER);
|
| 292 | } else {
|
| 293 | super.handleEntityEvent(id);
|
| 294 | }
|
| 295 | }
|
| 296 |
|
| 297 | private void addParticlesAroundSelf(final ParticleOptions particle) {
|
| 298 | for (int i = 0; i < 7; i++) {
|
| 299 | double xa = this.random.nextGaussian() * 0.01;
|
| 300 | double ya = this.random.nextGaussian() * 0.01;
|
| 301 | double za = this.random.nextGaussian() * 0.01;
|
| 302 | this.level().addParticle(particle, this.getRandomX(1.0), this.getRandomY() + 0.2, this.getRandomZ(1.0), xa, ya, za);
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | @Override
|
| 307 | protected InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 308 | ItemStack itemStack = player.getItemInHand(hand);
|
| 309 | if (!itemStack.isEmpty() && itemStack.is(ItemTags.FISHES)) {
|
| 310 | if (!this.level().isClientSide()) {
|
| 311 | this.playSound(SoundEvents.DOLPHIN_EAT, 1.0F, 1.0F);
|
| 312 | }
|
| 313 |
|
| 314 | if (this.canAgeUp()) {
|
| 315 | itemStack.consume(1, player);
|
| 316 | this.ageUp(getSpeedUpSecondsWhenFeeding(-this.age), true);
|
| 317 | } else {
|
| 318 | this.setGotFish(true);
|
| 319 | itemStack.consume(1, player);
|
| 320 | }
|
| 321 |
|
| 322 | return InteractionResult.SUCCESS;
|
| 323 | } else {
|
| 324 | return super.mobInteract(player, hand);
|
| 325 | }
|
| 326 | }
|
| 327 |
|
| 328 | @Override
|
| 329 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 330 | return SoundEvents.DOLPHIN_HURT;
|
| 331 | }
|
| 332 |
|
| 333 | @Override
|
| 334 | protected @Nullable SoundEvent getDeathSound() {
|
| 335 | return SoundEvents.DOLPHIN_DEATH;
|
| 336 | }
|
| 337 |
|
| 338 | @Override
|
| 339 | protected @Nullable SoundEvent getAmbientSound() {
|
| 340 | return this.isInWater() ? SoundEvents.DOLPHIN_AMBIENT_WATER : SoundEvents.DOLPHIN_AMBIENT;
|
| 341 | }
|
| 342 |
|
| 343 | @Override
|
| 344 | protected SoundEvent getSwimSplashSound() {
|
| 345 | return SoundEvents.DOLPHIN_SPLASH;
|
| 346 | }
|
| 347 |
|
| 348 | @Override
|
| 349 | protected SoundEvent getSwimSound() {
|
| 350 | return SoundEvents.DOLPHIN_SWIM;
|
| 351 | }
|
| 352 |
|
| 353 | protected boolean closeToNextPos() {
|
| 354 | BlockPos target = this.getNavigation().getTargetPos();
|
| 355 | return target != null ? target.closerToCenterThan(this.position(), 12.0) : false;
|
| 356 | }
|
| 357 |
|
| 358 | @Override
|
| 359 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 360 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 361 | }
|
| 362 |
|
| 363 | @Override
|
| 364 | protected void travelInWater(final Vec3 input, final double baseGravity, final boolean isFalling, final double oldY) {
|
| 365 | this.moveRelative(this.getSpeed(), input);
|
| 366 | this.move(MoverType.SELF, this.getDeltaMovement());
|
| 367 | this.setDeltaMovement(this.getDeltaMovement().scale(0.9));
|
| 368 | if (this.getTarget() == null) {
|
| 369 | this.setDeltaMovement(this.getDeltaMovement().add(0.0, -0.005, 0.0));
|
| 370 | }
|
| 371 | }
|
| 372 |
|
| 373 | @Override
|
| 374 | public boolean canBeLeashed() {
|
| 375 | return true;
|
| 376 | }
|
| 377 |
|
| 378 | private static class DolphinSwimToTreasureGoal extends Goal {
|
| 379 | private final Dolphin dolphin;
|
| 380 | private boolean stuck;
|
| 381 |
|
| 382 | public DolphinSwimToTreasureGoal(final Dolphin dolphin) {
|
| 383 | this.dolphin = dolphin;
|
| 384 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 385 | }
|
| 386 |
|
| 387 | @Override
|
| 388 | public boolean isInterruptable() {
|
| 389 | return false;
|
| 390 | }
|
| 391 |
|
| 392 | @Override
|
| 393 | public boolean canUse() {
|
| 394 | return this.dolphin.gotFish() && this.dolphin.getAirSupply() >= 100;
|
| 395 | }
|
| 396 |
|
| 397 | @Override
|
| 398 | public boolean canContinueToUse() {
|
| 399 | BlockPos treasurePos = this.dolphin.treasurePos;
|
| 400 | return treasurePos == null
|
| 401 | ? false
|
| 402 | : !BlockPos.containing(treasurePos.getX(), this.dolphin.getY(), treasurePos.getZ()).closerToCenterThan(this.dolphin.position(), 4.0)
|
| 403 | && !this.stuck
|
| 404 | && this.dolphin.getAirSupply() >= 100;
|
| 405 | }
|
| 406 |
|
| 407 | @Override
|
| 408 | public void start() {
|
| 409 | if (this.dolphin.level() instanceof ServerLevel) {
|
| 410 | ServerLevel level = (ServerLevel)this.dolphin.level();
|
| 411 | this.stuck = false;
|
| 412 | this.dolphin.getNavigation().stop();
|
| 413 | BlockPos dolphinPos = this.dolphin.blockPosition();
|
| 414 | BlockPos treasurePos = level.findNearestMapStructure(StructureTags.DOLPHIN_LOCATED, dolphinPos, 50, false);
|
| 415 | if (treasurePos != null) {
|
| 416 | this.dolphin.treasurePos = treasurePos;
|
| 417 | level.broadcastEntityEvent(this.dolphin, (byte)38);
|
| 418 | } else {
|
| 419 | this.stuck = true;
|
| 420 | }
|
| 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | @Override
|
| 425 | public void stop() {
|
| 426 | BlockPos treasurePos = this.dolphin.treasurePos;
|
| 427 | if (treasurePos == null
|
| 428 | || BlockPos.containing(treasurePos.getX(), this.dolphin.getY(), treasurePos.getZ()).closerToCenterThan(this.dolphin.position(), 4.0)
|
| 429 | || this.stuck) {
|
| 430 | this.dolphin.setGotFish(false);
|
| 431 | }
|
| 432 | }
|
| 433 |
|
| 434 | @Override
|
| 435 | public void tick() {
|
| 436 | if (this.dolphin.treasurePos != null) {
|
| 437 | Level level = this.dolphin.level();
|
| 438 | if (this.dolphin.closeToNextPos() || this.dolphin.getNavigation().isDone()) {
|
| 439 | Vec3 treasurePos = Vec3.atCenterOf(this.dolphin.treasurePos);
|
| 440 | Vec3 nextPos = DefaultRandomPos.getPosTowards(this.dolphin, 16, 1, treasurePos, (float) (Math.PI / 8));
|
| 441 | if (nextPos == null) {
|
| 442 | nextPos = DefaultRandomPos.getPosTowards(this.dolphin, 8, 4, treasurePos, (float) (Math.PI / 2));
|
| 443 | }
|
| 444 |
|
| 445 | if (nextPos != null) {
|
| 446 | BlockPos next = BlockPos.containing(nextPos);
|
| 447 | if (!level.getFluidState(next).is(FluidTags.WATER) || !level.getBlockState(next).isPathfindable(PathComputationType.WATER)) {
|
| 448 | nextPos = DefaultRandomPos.getPosTowards(this.dolphin, 8, 5, treasurePos, (float) (Math.PI / 2));
|
| 449 | }
|
| 450 | }
|
| 451 |
|
| 452 | if (nextPos == null) {
|
| 453 | this.stuck = true;
|
| 454 | return;
|
| 455 | }
|
| 456 |
|
| 457 | this.dolphin.getLookControl().setLookAt(nextPos.x, nextPos.y, nextPos.z, this.dolphin.getMaxHeadYRot() + 20, this.dolphin.getMaxHeadXRot());
|
| 458 | this.dolphin.getNavigation().moveTo(nextPos.x, nextPos.y, nextPos.z, 1.3);
|
| 459 | if (level.getRandom().nextInt(this.adjustedTickDelay(80)) == 0) {
|
| 460 | level.broadcastEntityEvent(this.dolphin, (byte)38);
|
| 461 | }
|
| 462 | }
|
| 463 | }
|
| 464 | }
|
| 465 | }
|
| 466 |
|
| 467 | private static class DolphinSwimWithPlayerGoal extends Goal {
|
| 468 | private final Dolphin dolphin;
|
| 469 | private final double speedModifier;
|
| 470 | private @Nullable Player player;
|
| 471 |
|
| 472 | public DolphinSwimWithPlayerGoal(final Dolphin dolphin, final double speedModifier) {
|
| 473 | this.dolphin = dolphin;
|
| 474 | this.speedModifier = speedModifier;
|
| 475 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 476 | }
|
| 477 |
|
| 478 | @Override
|
| 479 | public boolean canUse() {
|
| 480 | this.player = getServerLevel(this.dolphin).getNearestPlayer(Dolphin.SWIM_WITH_PLAYER_TARGETING, this.dolphin);
|
| 481 | return this.player == null ? false : this.player.isSwimming() && this.dolphin.getTarget() != this.player;
|
| 482 | }
|
| 483 |
|
| 484 | @Override
|
| 485 | public boolean canContinueToUse() {
|
| 486 | return this.player != null && this.player.isSwimming() && this.dolphin.distanceToSqr(this.player) < 256.0;
|
| 487 | }
|
| 488 |
|
| 489 | @Override
|
| 490 | public void start() {
|
| 491 | this.player.addEffect(new MobEffectInstance(MobEffects.DOLPHINS_GRACE, 100), this.dolphin);
|
| 492 | }
|
| 493 |
|
| 494 | @Override
|
| 495 | public void stop() {
|
| 496 | this.player = null;
|
| 497 | this.dolphin.getNavigation().stop();
|
| 498 | }
|
| 499 |
|
| 500 | @Override
|
| 501 | public void tick() {
|
| 502 | this.dolphin.getLookControl().setLookAt(this.player, this.dolphin.getMaxHeadYRot() + 20, this.dolphin.getMaxHeadXRot());
|
| 503 | if (this.dolphin.distanceToSqr(this.player) < 6.25) {
|
| 504 | this.dolphin.getNavigation().stop();
|
| 505 | } else {
|
| 506 | this.dolphin.getNavigation().moveTo(this.player, this.speedModifier);
|
| 507 | }
|
| 508 |
|
| 509 | if (this.player.isSwimming() && this.player.level().getRandom().nextInt(6) == 0) {
|
| 510 | this.player.addEffect(new MobEffectInstance(MobEffects.DOLPHINS_GRACE, 100), this.dolphin);
|
| 511 | }
|
| 512 | }
|
| 513 | }
|
| 514 |
|
| 515 | private abstract class ItemGoal extends Goal {
|
| 516 | protected void drop(final ItemStack itemStack) {
|
| 517 | if (!itemStack.isEmpty()) {
|
| 518 | double yHandPos = Dolphin.this.getEyeY() - 0.3F;
|
| 519 | ItemEntity thrownItem = new ItemEntity(Dolphin.this.level(), Dolphin.this.getX(), yHandPos, Dolphin.this.getZ(), itemStack);
|
| 520 | thrownItem.setPickUpDelay(40);
|
| 521 | thrownItem.setThrower(Dolphin.this);
|
| 522 | float pow = 0.3F;
|
| 523 | float dir = Dolphin.this.random.nextFloat() * (float) (Math.PI * 2);
|
| 524 | float pow2 = 0.02F * Dolphin.this.random.nextFloat();
|
| 525 | thrownItem.setDeltaMovement(
|
| 526 | 0.3F * -Mth.sin(Dolphin.this.getYRot() * (float) (Math.PI / 180.0)) * Mth.cos(Dolphin.this.getXRot() * (float) (Math.PI / 180.0)) + Mth.cos(dir) * pow2,
|
| 527 | 0.3F * Mth.sin(Dolphin.this.getXRot() * (float) (Math.PI / 180.0)) * 1.5F,
|
| 528 | 0.3F * Mth.cos(Dolphin.this.getYRot() * (float) (Math.PI / 180.0)) * Mth.cos(Dolphin.this.getXRot() * (float) (Math.PI / 180.0)) + Mth.sin(dir) * pow2
|
| 529 | );
|
| 530 | Dolphin.this.level().addFreshEntity(thrownItem);
|
| 531 | }
|
| 532 | }
|
| 533 |
|
| 534 | protected boolean dropItem() {
|
| 535 | ItemStack itemStack = Dolphin.this.getItemBySlot(EquipmentSlot.MAINHAND);
|
| 536 | if (itemStack.isEmpty()) {
|
| 537 | return false;
|
| 538 | }
|
| 539 |
|
| 540 | this.drop(itemStack);
|
| 541 | Dolphin.this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
|
| 542 | return true;
|
| 543 | }
|
| 544 | }
|
| 545 |
|
| 546 | private class MoveToItemGoal extends Dolphin.ItemGoal {
|
| 547 | private int cooldown;
|
| 548 |
|
| 549 | public MoveToItemGoal() {
|
| 550 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 551 | }
|
| 552 |
|
| 553 | @Override
|
| 554 | public boolean canUse() {
|
| 555 | if (this.cooldown > Dolphin.this.tickCount) {
|
| 556 | return false;
|
| 557 | }
|
| 558 |
|
| 559 | List<ItemEntity> items = Dolphin.this.level()
|
| 560 | .getEntitiesOfClass(ItemEntity.class, Dolphin.this.getBoundingBox().inflate(8.0, 8.0, 8.0), Dolphin.ALLOWED_ITEMS);
|
| 561 | return !items.isEmpty();
|
| 562 | }
|
| 563 |
|
| 564 | @Override
|
| 565 | public void stop() {
|
| 566 | if (this.dropItem()) {
|
| 567 | this.cooldown = Dolphin.this.tickCount + Dolphin.this.random.nextInt(100);
|
| 568 | }
|
| 569 | }
|
| 570 |
|
| 571 | @Override
|
| 572 | public void start() {
|
| 573 | List<ItemEntity> items = Dolphin.this.level()
|
| 574 | .getEntitiesOfClass(ItemEntity.class, Dolphin.this.getBoundingBox().inflate(8.0, 8.0, 8.0), Dolphin.ALLOWED_ITEMS);
|
| 575 | if (!items.isEmpty()) {
|
| 576 | Dolphin.this.getNavigation().moveTo(items.get(0), 1.2F);
|
| 577 | Dolphin.this.playSound(SoundEvents.DOLPHIN_PLAY, 1.0F, 1.0F);
|
| 578 | }
|
| 579 |
|
| 580 | this.cooldown = 0;
|
| 581 | }
|
| 582 |
|
| 583 | @Override
|
| 584 | public void tick() {
|
| 585 | List<ItemEntity> items = Dolphin.this.level()
|
| 586 | .getEntitiesOfClass(ItemEntity.class, Dolphin.this.getBoundingBox().inflate(8.0, 8.0, 8.0), Dolphin.ALLOWED_ITEMS);
|
| 587 | if (!items.isEmpty()) {
|
| 588 | Dolphin.this.getNavigation().moveTo(items.get(0), 1.2F);
|
| 589 | }
|
| 590 | }
|
| 591 | }
|
| 592 |
|
| 593 | private class PlayWithItemsGoal extends Dolphin.ItemGoal {
|
| 594 | @Override
|
| 595 | public boolean canUse() {
|
| 596 | return !Dolphin.this.getItemBySlot(EquipmentSlot.MAINHAND).isEmpty();
|
| 597 | }
|
| 598 |
|
| 599 | @Override
|
| 600 | public void stop() {
|
| 601 | this.dropItem();
|
| 602 | }
|
| 603 |
|
| 604 | @Override
|
| 605 | public void tick() {
|
| 606 | this.dropItem();
|
| 607 | }
|
| 608 | }
|
| 609 | }
|
| 610 |
|