| 1 | package net.minecraft.world.entity.monster.zombie;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.EnumSet;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.tags.BiomeTags;
|
| 11 | import net.minecraft.tags.FluidTags;
|
| 12 | import net.minecraft.tags.ItemTags;
|
| 13 | import net.minecraft.tags.TagKey;
|
| 14 | import net.minecraft.util.Mth;
|
| 15 | import net.minecraft.util.RandomSource;
|
| 16 | import net.minecraft.world.Difficulty;
|
| 17 | import net.minecraft.world.DifficultyInstance;
|
| 18 | import net.minecraft.world.InteractionHand;
|
| 19 | import net.minecraft.world.damagesource.DamageSource;
|
| 20 | import net.minecraft.world.entity.EntityAttachment;
|
| 21 | import net.minecraft.world.entity.EntityAttachments;
|
| 22 | import net.minecraft.world.entity.EntityDimensions;
|
| 23 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 24 | import net.minecraft.world.entity.EntityType;
|
| 25 | import net.minecraft.world.entity.EntityTypes;
|
| 26 | import net.minecraft.world.entity.EquipmentSlot;
|
| 27 | import net.minecraft.world.entity.LivingEntity;
|
| 28 | import net.minecraft.world.entity.MoverType;
|
| 29 | import net.minecraft.world.entity.PathfinderMob;
|
| 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.Goal;
|
| 36 | import net.minecraft.world.entity.ai.goal.MoveToBlockGoal;
|
| 37 | import net.minecraft.world.entity.ai.goal.RandomStrollGoal;
|
| 38 | import net.minecraft.world.entity.ai.goal.RangedAttackGoal;
|
| 39 | import net.minecraft.world.entity.ai.goal.ZombieAttackGoal;
|
| 40 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 41 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 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.util.DefaultRandomPos;
|
| 45 | import net.minecraft.world.entity.animal.axolotl.Axolotl;
|
| 46 | import net.minecraft.world.entity.animal.golem.IronGolem;
|
| 47 | import net.minecraft.world.entity.animal.nautilus.ZombieNautilus;
|
| 48 | import net.minecraft.world.entity.animal.turtle.Turtle;
|
| 49 | import net.minecraft.world.entity.monster.RangedAttackMob;
|
| 50 | import net.minecraft.world.entity.npc.villager.AbstractVillager;
|
| 51 | import net.minecraft.world.entity.player.Player;
|
| 52 | import net.minecraft.world.entity.projectile.Projectile;
|
| 53 | import net.minecraft.world.entity.projectile.arrow.ThrownTrident;
|
| 54 | import net.minecraft.world.item.Item;
|
| 55 | import net.minecraft.world.item.ItemStack;
|
| 56 | import net.minecraft.world.item.Items;
|
| 57 | import net.minecraft.world.level.Level;
|
| 58 | import net.minecraft.world.level.LevelAccessor;
|
| 59 | import net.minecraft.world.level.LevelReader;
|
| 60 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 61 | import net.minecraft.world.level.biome.Biome;
|
| 62 | import net.minecraft.world.level.block.Blocks;
|
| 63 | import net.minecraft.world.level.pathfinder.Path;
|
| 64 | import net.minecraft.world.level.pathfinder.PathType;
|
| 65 | import net.minecraft.world.phys.Vec3;
|
| 66 | import org.jspecify.annotations.Nullable;
|
| 67 |
|
| 68 | public class Drowned extends Zombie implements RangedAttackMob {
|
| 69 | public static final float NAUTILUS_SHELL_CHANCE = 0.03F;
|
| 70 | private static final float ZOMBIE_NAUTILUS_JOCKEY_CHANCE = 0.5F;
|
| 71 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.49F, 0.98F)
|
| 72 | .withEyeHeight(0.775F)
|
| 73 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.VEHICLE, 0.0F, 0.1875F, 0.0F));
|
| 74 | private boolean searchingForLand;
|
| 75 | private float rangedAttackUncertainty = -1.0F;
|
| 76 |
|
| 77 | public Drowned(final EntityType<? extends Drowned> type, final Level level) {
|
| 78 | super(type, level);
|
| 79 | this.moveControl = new Drowned.DrownedMoveControl<>(this);
|
| 80 | this.setPathfindingMalus(PathType.WATER, 0.0F);
|
| 81 | }
|
| 82 |
|
| 83 | public static AttributeSupplier.Builder createAttributes() {
|
| 84 | return Zombie.createAttributes().add(Attributes.STEP_HEIGHT, 1.0);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | protected PathNavigation createNavigation(final Level level) {
|
| 89 | return new AmphibiousPathNavigation(this, level);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | protected void addBehaviourGoals() {
|
| 94 | this.goalSelector.addGoal(1, new Drowned.DrownedGoToWaterGoal(this, 1.0));
|
| 95 | this.goalSelector.addGoal(2, new Drowned.DrownedTridentAttackGoal(this, 1.0, 40, 10.0F));
|
| 96 | this.goalSelector.addGoal(2, new Drowned.DrownedAttackGoal(this, 1.0, false));
|
| 97 | this.goalSelector.addGoal(5, new Drowned.DrownedGoToBeachGoal(this, 1.0));
|
| 98 | this.goalSelector.addGoal(6, new Drowned.DrownedSwimUpGoal(this, 1.0, this.level().getSeaLevel()));
|
| 99 | this.goalSelector.addGoal(7, new RandomStrollGoal(this, 1.0));
|
| 100 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this, Drowned.class).setAlertOthers(ZombifiedPiglin.class));
|
| 101 | this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.class, 10, true, false, (target, level) -> this.okTarget(target)));
|
| 102 | this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, AbstractVillager.class, false));
|
| 103 | this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, IronGolem.class, true));
|
| 104 | this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, Axolotl.class, true, false));
|
| 105 | this.targetSelector.addGoal(5, new NearestAttackableTargetGoal<>(this, Turtle.class, 10, true, false, Turtle.BABY_ON_LAND_SELECTOR));
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public SpawnGroupData finalizeSpawn(
|
| 110 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 111 | ) {
|
| 112 | groupData = super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 113 | if (this.getItemBySlot(EquipmentSlot.OFFHAND).isEmpty() && level.getRandom().nextFloat() < 0.03F) {
|
| 114 | this.setItemSlot(EquipmentSlot.OFFHAND, new ItemStack(Items.NAUTILUS_SHELL));
|
| 115 | this.setGuaranteedDrop(EquipmentSlot.OFFHAND);
|
| 116 | }
|
| 117 |
|
| 118 | if ((spawnReason == EntitySpawnReason.NATURAL || spawnReason == EntitySpawnReason.STRUCTURE)
|
| 119 | && this.getMainHandItem().is(Items.TRIDENT)
|
| 120 | && level.getRandom().nextFloat() < 0.5F
|
| 121 | && !this.isBaby()
|
| 122 | && !level.getBiome(this.blockPosition()).is(BiomeTags.MORE_FREQUENT_DROWNED_SPAWNS)) {
|
| 123 | ZombieNautilus zombieNautilus = EntityTypes.ZOMBIE_NAUTILUS.create(this.level(), EntitySpawnReason.JOCKEY);
|
| 124 | if (zombieNautilus != null) {
|
| 125 | if (spawnReason == EntitySpawnReason.STRUCTURE) {
|
| 126 | zombieNautilus.setPersistenceRequired();
|
| 127 | }
|
| 128 |
|
| 129 | zombieNautilus.snapTo(this.getX(), this.getY(), this.getZ(), this.getYRot(), 0.0F);
|
| 130 | zombieNautilus.finalizeSpawn(level, difficulty, spawnReason, null);
|
| 131 | this.startRiding(zombieNautilus, false, false);
|
| 132 | level.addFreshEntity(zombieNautilus);
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | return groupData;
|
| 137 | }
|
| 138 |
|
| 139 | public static boolean checkDrownedSpawnRules(
|
| 140 | final EntityType<Drowned> type, final ServerLevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 141 | ) {
|
| 142 | if (!level.getFluidState(pos.below()).is(FluidTags.WATER) && !EntitySpawnReason.isSpawner(spawnReason)) {
|
| 143 | return false;
|
| 144 | } else {
|
| 145 | Holder<Biome> biome = level.getBiome(pos);
|
| 146 | boolean canMonsterSpawn = level.getDifficulty() != Difficulty.PEACEFUL
|
| 147 | && (EntitySpawnReason.ignoresLightRequirements(spawnReason) || isDarkEnoughToSpawn(level, pos, random))
|
| 148 | && (EntitySpawnReason.isSpawner(spawnReason) || level.getFluidState(pos).is(FluidTags.WATER));
|
| 149 | if (!canMonsterSpawn || !EntitySpawnReason.isSpawner(spawnReason) && spawnReason != EntitySpawnReason.REINFORCEMENT) {
|
| 150 | return biome.is(BiomeTags.MORE_FREQUENT_DROWNED_SPAWNS)
|
| 151 | ? random.nextInt(15) == 0 && canMonsterSpawn
|
| 152 | : random.nextInt(40) == 0 && isDeepEnoughToSpawn(level, pos) && canMonsterSpawn;
|
| 153 | } else {
|
| 154 | return true;
|
| 155 | }
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | private static boolean isDeepEnoughToSpawn(final LevelAccessor level, final BlockPos pos) {
|
| 160 | return pos.getY() < level.getSeaLevel() - 5;
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 165 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | protected SoundEvent getAmbientSound() {
|
| 170 | return this.isInWater() ? SoundEvents.DROWNED_AMBIENT_WATER : SoundEvents.DROWNED_AMBIENT;
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 175 | return this.isInWater() ? SoundEvents.DROWNED_HURT_WATER : SoundEvents.DROWNED_HURT;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | protected SoundEvent getDeathSound() {
|
| 180 | return this.isInWater() ? SoundEvents.DROWNED_DEATH_WATER : SoundEvents.DROWNED_DEATH;
|
| 181 | }
|
| 182 |
|
| 183 | @Override
|
| 184 | protected SoundEvent getStepSound() {
|
| 185 | return SoundEvents.DROWNED_STEP;
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | protected SoundEvent getSwimSound() {
|
| 190 | return SoundEvents.DROWNED_SWIM;
|
| 191 | }
|
| 192 |
|
| 193 | @Override
|
| 194 | protected boolean canSpawnInLiquids() {
|
| 195 | return true;
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | protected void populateDefaultEquipmentSlots(final RandomSource random, final DifficultyInstance difficulty) {
|
| 200 | if (random.nextFloat() > 0.9) {
|
| 201 | int rand = random.nextInt(16);
|
| 202 | if (rand < 10) {
|
| 203 | this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.TRIDENT));
|
| 204 | } else {
|
| 205 | this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.FISHING_ROD));
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | protected boolean canReplaceCurrentItem(final ItemStack newItemStack, final ItemStack currentItemStack, final EquipmentSlot slot) {
|
| 212 | return currentItemStack.is(Items.NAUTILUS_SHELL) ? false : super.canReplaceCurrentItem(newItemStack, currentItemStack, slot);
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | protected boolean convertsInWater() {
|
| 217 | return false;
|
| 218 | }
|
| 219 |
|
| 220 | @Override
|
| 221 | public boolean checkSpawnObstruction(final LevelReader level) {
|
| 222 | return level.isUnobstructed(this);
|
| 223 | }
|
| 224 |
|
| 225 | public boolean okTarget(final @Nullable LivingEntity target) {
|
| 226 | return target != null ? !this.level().isBrightOutside() || target.isInWater() : false;
|
| 227 | }
|
| 228 |
|
| 229 | @Override
|
| 230 | public boolean isPushedByFluid() {
|
| 231 | return !this.isSwimming();
|
| 232 | }
|
| 233 |
|
| 234 | public boolean wantsToSwim() {
|
| 235 | if (this.searchingForLand) {
|
| 236 | return true;
|
| 237 | }
|
| 238 |
|
| 239 | LivingEntity target = this.getTarget();
|
| 240 | return target != null && target.isInWater();
|
| 241 | }
|
| 242 |
|
| 243 | @Override
|
| 244 | protected void travelInWater(final Vec3 input, final double baseGravity, final boolean isFalling, final double oldY) {
|
| 245 | if (this.isUnderWater() && this.wantsToSwim()) {
|
| 246 | this.moveRelative(0.01F, input);
|
| 247 | this.move(MoverType.SELF, this.getDeltaMovement());
|
| 248 | this.setDeltaMovement(this.getDeltaMovement().scale(0.9));
|
| 249 | } else {
|
| 250 | super.travelInWater(input, baseGravity, isFalling, oldY);
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | @Override
|
| 255 | public void updateSwimming() {
|
| 256 | if (!this.level().isClientSide()) {
|
| 257 | this.setSwimming(this.isEffectiveAi() && this.isUnderWater() && this.wantsToSwim());
|
| 258 | }
|
| 259 | }
|
| 260 |
|
| 261 | @Override
|
| 262 | public boolean isVisuallySwimming() {
|
| 263 | return this.isSwimming() && !this.isPassenger();
|
| 264 | }
|
| 265 |
|
| 266 | protected boolean closeToNextPos() {
|
| 267 | Path path = this.getNavigation().getPath();
|
| 268 | if (path != null) {
|
| 269 | BlockPos pos = path.getTarget();
|
| 270 | if (pos != null) {
|
| 271 | double sqrDistToNextPos = this.distanceToSqr(pos.getX(), pos.getY(), pos.getZ());
|
| 272 | if (sqrDistToNextPos < 4.0) {
|
| 273 | return true;
|
| 274 | }
|
| 275 | }
|
| 276 | }
|
| 277 |
|
| 278 | return false;
|
| 279 | }
|
| 280 |
|
| 281 | @Override
|
| 282 | public void performRangedAttack(final LivingEntity target, final float power) {
|
| 283 | ItemStack mainHandItem = this.getMainHandItem();
|
| 284 | ItemStack tridentItemStack = mainHandItem.is(Items.TRIDENT) ? mainHandItem : new ItemStack(Items.TRIDENT);
|
| 285 | ThrownTrident trident = new ThrownTrident(this.level(), this, tridentItemStack);
|
| 286 | double xd = target.getX() - this.getX();
|
| 287 | double yd = target.getY(0.3333333333333333) - trident.getY();
|
| 288 | double zd = target.getZ() - this.getZ();
|
| 289 | double distanceToTarget = Math.sqrt(xd * xd + zd * zd);
|
| 290 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 291 | Projectile.spawnProjectileUsingShoot(
|
| 292 | trident, serverLevel, tridentItemStack, xd, yd + distanceToTarget * 0.2F, zd, 1.6F, this.rangedAttackUncertainty(serverLevel)
|
| 293 | );
|
| 294 | }
|
| 295 |
|
| 296 | this.playSound(SoundEvents.DROWNED_SHOOT, 1.0F, 1.0F / (this.getRandom().nextFloat() * 0.4F + 0.8F));
|
| 297 | }
|
| 298 |
|
| 299 | @Override
|
| 300 | public float rangedAttackUncertainty(final Level level) {
|
| 301 | return this.rangedAttackUncertainty < 0.0F ? RangedAttackMob.super.rangedAttackUncertainty(level) : this.rangedAttackUncertainty;
|
| 302 | }
|
| 303 |
|
| 304 | @VisibleForTesting
|
| 305 | public void setRangedAttackUncertainty(final float rangedAttackUncertainty) {
|
| 306 | this.rangedAttackUncertainty = rangedAttackUncertainty;
|
| 307 | }
|
| 308 |
|
| 309 | @Override
|
| 310 | public TagKey<Item> getPreferredWeaponType() {
|
| 311 | return ItemTags.DROWNED_PREFERRED_WEAPONS;
|
| 312 | }
|
| 313 |
|
| 314 | public void setSearchingForLand(final boolean searchingForLand) {
|
| 315 | this.searchingForLand = searchingForLand;
|
| 316 | }
|
| 317 |
|
| 318 | public boolean isSearchingForLand() {
|
| 319 | return this.searchingForLand;
|
| 320 | }
|
| 321 |
|
| 322 | @Override
|
| 323 | public void rideTick() {
|
| 324 | super.rideTick();
|
| 325 | if (this.getControlledVehicle() instanceof PathfinderMob entity) {
|
| 326 | this.yBodyRot = entity.yBodyRot;
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | @Override
|
| 331 | public boolean wantsToPickUp(final ServerLevel level, final ItemStack itemStack) {
|
| 332 | return itemStack.is(ItemTags.SPEARS) ? false : super.wantsToPickUp(level, itemStack);
|
| 333 | }
|
| 334 |
|
| 335 | private static class DrownedAttackGoal extends ZombieAttackGoal {
|
| 336 | private final Drowned drowned;
|
| 337 |
|
| 338 | public DrownedAttackGoal(final Drowned drowned, final double speedModifier, final boolean trackTarget) {
|
| 339 | super(drowned, speedModifier, trackTarget);
|
| 340 | this.drowned = drowned;
|
| 341 | }
|
| 342 |
|
| 343 | @Override
|
| 344 | public boolean canUse() {
|
| 345 | return super.canUse() && this.drowned.okTarget(this.drowned.getTarget());
|
| 346 | }
|
| 347 |
|
| 348 | @Override
|
| 349 | public boolean canContinueToUse() {
|
| 350 | return super.canContinueToUse() && this.drowned.okTarget(this.drowned.getTarget());
|
| 351 | }
|
| 352 | }
|
| 353 |
|
| 354 | private static class DrownedGoToBeachGoal extends MoveToBlockGoal {
|
| 355 | private final Drowned drowned;
|
| 356 |
|
| 357 | public DrownedGoToBeachGoal(final Drowned drowned, final double speedModifier) {
|
| 358 | super(drowned, speedModifier, 8, 2);
|
| 359 | this.drowned = drowned;
|
| 360 | }
|
| 361 |
|
| 362 | @Override
|
| 363 | public boolean canUse() {
|
| 364 | return super.canUse()
|
| 365 | && !this.drowned.level().isBrightOutside()
|
| 366 | && this.drowned.isInWater()
|
| 367 | && this.drowned.getY() >= this.drowned.level().getSeaLevel() - 3;
|
| 368 | }
|
| 369 |
|
| 370 | @Override
|
| 371 | public boolean canContinueToUse() {
|
| 372 | return super.canContinueToUse();
|
| 373 | }
|
| 374 |
|
| 375 | @Override
|
| 376 | protected boolean isValidTarget(final LevelReader level, final BlockPos pos) {
|
| 377 | BlockPos above = pos.above();
|
| 378 | return level.isEmptyBlock(above) && level.isEmptyBlock(above.above()) ? level.getBlockState(pos).entityCanStandOn(level, pos, this.drowned) : false;
|
| 379 | }
|
| 380 |
|
| 381 | @Override
|
| 382 | public void start() {
|
| 383 | this.drowned.setSearchingForLand(false);
|
| 384 | super.start();
|
| 385 | }
|
| 386 |
|
| 387 | @Override
|
| 388 | public void stop() {
|
| 389 | super.stop();
|
| 390 | }
|
| 391 | }
|
| 392 |
|
| 393 | private static class DrownedGoToWaterGoal extends Goal {
|
| 394 | private final PathfinderMob mob;
|
| 395 | private double wantedX;
|
| 396 | private double wantedY;
|
| 397 | private double wantedZ;
|
| 398 | private final double speedModifier;
|
| 399 | private final Level level;
|
| 400 |
|
| 401 | public DrownedGoToWaterGoal(final PathfinderMob mob, final double speedModifier) {
|
| 402 | this.mob = mob;
|
| 403 | this.speedModifier = speedModifier;
|
| 404 | this.level = mob.level();
|
| 405 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 406 | }
|
| 407 |
|
| 408 | @Override
|
| 409 | public boolean canUse() {
|
| 410 | if (!this.level.isBrightOutside()) {
|
| 411 | return false;
|
| 412 | }
|
| 413 |
|
| 414 | if (this.mob.isInWater()) {
|
| 415 | return false;
|
| 416 | }
|
| 417 |
|
| 418 | Vec3 pos = this.getWaterPos();
|
| 419 | if (pos == null) {
|
| 420 | return false;
|
| 421 | }
|
| 422 |
|
| 423 | this.wantedX = pos.x;
|
| 424 | this.wantedY = pos.y;
|
| 425 | this.wantedZ = pos.z;
|
| 426 | return true;
|
| 427 | }
|
| 428 |
|
| 429 | @Override
|
| 430 | public boolean canContinueToUse() {
|
| 431 | return !this.mob.getNavigation().isDone();
|
| 432 | }
|
| 433 |
|
| 434 | @Override
|
| 435 | public void start() {
|
| 436 | this.mob.getNavigation().moveTo(this.wantedX, this.wantedY, this.wantedZ, this.speedModifier);
|
| 437 | }
|
| 438 |
|
| 439 | private @Nullable Vec3 getWaterPos() {
|
| 440 | RandomSource random = this.mob.getRandom();
|
| 441 | BlockPos pos = this.mob.blockPosition();
|
| 442 |
|
| 443 | for (int i = 0; i < 10; i++) {
|
| 444 | BlockPos randomPos = pos.offset(random.nextInt(20) - 10, 2 - random.nextInt(8), random.nextInt(20) - 10);
|
| 445 | if (this.level.getBlockState(randomPos).is(Blocks.WATER)) {
|
| 446 | return Vec3.atBottomCenterOf(randomPos);
|
| 447 | }
|
| 448 | }
|
| 449 |
|
| 450 | return null;
|
| 451 | }
|
| 452 | }
|
| 453 |
|
| 454 | private static class DrownedMoveControl<T extends Drowned> extends MoveControl<T> {
|
| 455 | public DrownedMoveControl(final T drowned) {
|
| 456 | super(drowned);
|
| 457 | }
|
| 458 |
|
| 459 | @Override
|
| 460 | public void tick() {
|
| 461 | LivingEntity target = this.mob.getTarget();
|
| 462 | if (this.mob.wantsToSwim() && this.mob.isInWater()) {
|
| 463 | if (target != null && target.getY() > this.mob.getY() || this.mob.isSearchingForLand()) {
|
| 464 | this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(0.0, 0.002, 0.0));
|
| 465 | }
|
| 466 |
|
| 467 | if (this.operation != MoveControl.Operation.MOVE_TO || this.mob.getNavigation().isDone()) {
|
| 468 | this.mob.setSpeed(0.0F);
|
| 469 | return;
|
| 470 | }
|
| 471 |
|
| 472 | double xd = this.wantedX - this.mob.getX();
|
| 473 | double yd = this.wantedY - this.mob.getY();
|
| 474 | double zd = this.wantedZ - this.mob.getZ();
|
| 475 | double dd = Math.sqrt(xd * xd + yd * yd + zd * zd);
|
| 476 | yd /= dd;
|
| 477 | float yRotD = (float)(Mth.atan2(zd, xd) * 180.0F / (float)Math.PI) - 90.0F;
|
| 478 | this.mob.setYRot(this.rotlerp(this.mob.getYRot(), yRotD, 90.0F));
|
| 479 | this.mob.yBodyRot = this.mob.getYRot();
|
| 480 | float targetSpeed = (float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED));
|
| 481 | float newSpeed = Mth.lerp(0.125F, this.mob.getSpeed(), targetSpeed);
|
| 482 | this.mob.setSpeed(newSpeed);
|
| 483 | this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(newSpeed * xd * 0.005, newSpeed * yd * 0.1, newSpeed * zd * 0.005));
|
| 484 | } else {
|
| 485 | if (!this.mob.onGround()) {
|
| 486 | this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(0.0, -0.008, 0.0));
|
| 487 | }
|
| 488 |
|
| 489 | super.tick();
|
| 490 | }
|
| 491 | }
|
| 492 | }
|
| 493 |
|
| 494 | private static class DrownedSwimUpGoal extends Goal {
|
| 495 | private final Drowned drowned;
|
| 496 | private final double speedModifier;
|
| 497 | private final int seaLevel;
|
| 498 | private boolean stuck;
|
| 499 |
|
| 500 | public DrownedSwimUpGoal(final Drowned drowned, final double speedModifier, final int seaLevel) {
|
| 501 | this.drowned = drowned;
|
| 502 | this.speedModifier = speedModifier;
|
| 503 | this.seaLevel = seaLevel;
|
| 504 | }
|
| 505 |
|
| 506 | @Override
|
| 507 | public boolean canUse() {
|
| 508 | return !this.drowned.level().isBrightOutside() && this.drowned.isInWater() && this.drowned.getY() < this.seaLevel - 2;
|
| 509 | }
|
| 510 |
|
| 511 | @Override
|
| 512 | public boolean canContinueToUse() {
|
| 513 | return this.canUse() && !this.stuck;
|
| 514 | }
|
| 515 |
|
| 516 | @Override
|
| 517 | public void tick() {
|
| 518 | if (this.drowned.getY() < this.seaLevel - 1 && (this.drowned.getNavigation().isDone() || this.drowned.closeToNextPos())) {
|
| 519 | Vec3 nextPos = DefaultRandomPos.getPosTowards(
|
| 520 | this.drowned, 4, 8, new Vec3(this.drowned.getX(), this.seaLevel - 1, this.drowned.getZ()), (float) (Math.PI / 2)
|
| 521 | );
|
| 522 | if (nextPos == null) {
|
| 523 | this.stuck = true;
|
| 524 | return;
|
| 525 | }
|
| 526 |
|
| 527 | this.drowned.getNavigation().moveTo(nextPos.x, nextPos.y, nextPos.z, this.speedModifier);
|
| 528 | }
|
| 529 | }
|
| 530 |
|
| 531 | @Override
|
| 532 | public void start() {
|
| 533 | this.drowned.setSearchingForLand(true);
|
| 534 | this.stuck = false;
|
| 535 | }
|
| 536 |
|
| 537 | @Override
|
| 538 | public void stop() {
|
| 539 | this.drowned.setSearchingForLand(false);
|
| 540 | }
|
| 541 | }
|
| 542 |
|
| 543 | private static class DrownedTridentAttackGoal extends RangedAttackGoal {
|
| 544 | private final Drowned drowned;
|
| 545 |
|
| 546 | public DrownedTridentAttackGoal(final RangedAttackMob mob, final double speedModifier, final int attackInterval, final float attackRadius) {
|
| 547 | super(mob, speedModifier, attackInterval, attackRadius);
|
| 548 | this.drowned = (Drowned)mob;
|
| 549 | }
|
| 550 |
|
| 551 | @Override
|
| 552 | public boolean canUse() {
|
| 553 | return super.canUse() && this.drowned.getMainHandItem().is(Items.TRIDENT);
|
| 554 | }
|
| 555 |
|
| 556 | @Override
|
| 557 | public void start() {
|
| 558 | super.start();
|
| 559 | this.drowned.setAggressive(true);
|
| 560 | this.drowned.startUsingItem(InteractionHand.MAIN_HAND);
|
| 561 | }
|
| 562 |
|
| 563 | @Override
|
| 564 | public void stop() {
|
| 565 | super.stop();
|
| 566 | this.drowned.stopUsingItem();
|
| 567 | this.drowned.setAggressive(false);
|
| 568 | }
|
| 569 | }
|
| 570 | }
|
| 571 |
|