| 1 | package net.minecraft.world.entity.monster;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.function.BooleanSupplier;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 8 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 9 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.sounds.SoundEvents;
|
| 13 | import net.minecraft.sounds.SoundSource;
|
| 14 | import net.minecraft.tags.BlockTags;
|
| 15 | import net.minecraft.tags.DamageTypeTags;
|
| 16 | import net.minecraft.tags.FluidTags;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.util.RandomSource;
|
| 19 | import net.minecraft.world.Difficulty;
|
| 20 | import net.minecraft.world.damagesource.DamageSource;
|
| 21 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 22 | import net.minecraft.world.entity.EntityType;
|
| 23 | import net.minecraft.world.entity.LivingEntity;
|
| 24 | import net.minecraft.world.entity.Mob;
|
| 25 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 26 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 27 | import net.minecraft.world.entity.ai.control.MoveControl;
|
| 28 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 29 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 30 | import net.minecraft.world.entity.player.Player;
|
| 31 | import net.minecraft.world.entity.projectile.hurtingprojectile.LargeFireball;
|
| 32 | import net.minecraft.world.level.BlockGetter;
|
| 33 | import net.minecraft.world.level.Level;
|
| 34 | import net.minecraft.world.level.LevelAccessor;
|
| 35 | import net.minecraft.world.level.block.state.BlockState;
|
| 36 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 37 | import net.minecraft.world.level.material.FluidState;
|
| 38 | import net.minecraft.world.level.storage.ValueInput;
|
| 39 | import net.minecraft.world.level.storage.ValueOutput;
|
| 40 | import net.minecraft.world.phys.AABB;
|
| 41 | import net.minecraft.world.phys.Vec3;
|
| 42 | import org.jspecify.annotations.Nullable;
|
| 43 |
|
| 44 | public class Ghast extends Mob implements Enemy {
|
| 45 | private static final EntityDataAccessor<Boolean> DATA_IS_CHARGING = SynchedEntityData.defineId(Ghast.class, EntityDataSerializers.BOOLEAN);
|
| 46 | private static final byte DEFAULT_EXPLOSION_POWER = 1;
|
| 47 | private int explosionPower = 1;
|
| 48 |
|
| 49 | public Ghast(final EntityType<? extends Ghast> type, final Level level) {
|
| 50 | super(type, level);
|
| 51 | this.xpReward = 5;
|
| 52 | this.moveControl = new Ghast.GhastMoveControl<>(this, false, () -> false);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected void registerGoals() {
|
| 57 | this.goalSelector.addGoal(5, new Ghast.RandomFloatAroundGoal(this));
|
| 58 | this.goalSelector.addGoal(7, new Ghast.GhastLookGoal(this));
|
| 59 | this.goalSelector.addGoal(7, new Ghast.GhastShootFireballGoal(this));
|
| 60 | this.targetSelector
|
| 61 | .addGoal(1, new NearestAttackableTargetGoal<>(this, Player.class, 10, true, false, (target, level) -> Math.abs(target.getY() - this.getY()) <= 4.0));
|
| 62 | }
|
| 63 |
|
| 64 | public boolean isCharging() {
|
| 65 | return this.entityData.get(DATA_IS_CHARGING);
|
| 66 | }
|
| 67 |
|
| 68 | public void setCharging(final boolean onOff) {
|
| 69 | this.entityData.set(DATA_IS_CHARGING, onOff);
|
| 70 | }
|
| 71 |
|
| 72 | public int getExplosionPower() {
|
| 73 | return this.explosionPower;
|
| 74 | }
|
| 75 |
|
| 76 | private static boolean isReflectedFireball(final DamageSource source) {
|
| 77 | return source.getDirectEntity() instanceof LargeFireball && source.getEntity() instanceof Player;
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public boolean isInvulnerableTo(final ServerLevel level, final DamageSource source) {
|
| 82 | return this.isInvulnerable() && !source.is(DamageTypeTags.BYPASSES_INVULNERABILITY) || !isReflectedFireball(source) && super.isInvulnerableTo(level, source);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | protected void checkFallDamage(final double ya, final boolean onGround, final BlockState onState, final BlockPos pos) {
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public boolean onClimbable() {
|
| 91 | return false;
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public void travel(final Vec3 input) {
|
| 96 | this.travelFlying(input, 0.02F);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 101 | if (isReflectedFireball(source)) {
|
| 102 | super.hurtServer(level, source, 1000.0F);
|
| 103 | return true;
|
| 104 | } else {
|
| 105 | return this.isInvulnerableTo(level, source) ? false : super.hurtServer(level, source, damage);
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 111 | super.defineSynchedData(entityData);
|
| 112 | entityData.define(DATA_IS_CHARGING, false);
|
| 113 | }
|
| 114 |
|
| 115 | public static AttributeSupplier.Builder createAttributes() {
|
| 116 | return Mob.createMobAttributes()
|
| 117 | .add(Attributes.MAX_HEALTH, 10.0)
|
| 118 | .add(Attributes.FOLLOW_RANGE, 100.0)
|
| 119 | .add(Attributes.CAMERA_DISTANCE, 8.0)
|
| 120 | .add(Attributes.FLYING_SPEED, 0.06);
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public SoundSource getSoundSource() {
|
| 125 | return SoundSource.HOSTILE;
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | protected SoundEvent getAmbientSound() {
|
| 130 | return SoundEvents.GHAST_AMBIENT;
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 135 | return SoundEvents.GHAST_HURT;
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected SoundEvent getDeathSound() {
|
| 140 | return SoundEvents.GHAST_DEATH;
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected float getSoundVolume() {
|
| 145 | return 5.0F;
|
| 146 | }
|
| 147 |
|
| 148 | public static boolean checkGhastSpawnRules(
|
| 149 | final EntityType<Ghast> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 150 | ) {
|
| 151 | return level.getDifficulty() != Difficulty.PEACEFUL && random.nextInt(20) == 0 && checkMobSpawnRules(type, level, spawnReason, pos, random);
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public int getMaxSpawnClusterSize() {
|
| 156 | return 1;
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 161 | super.addAdditionalSaveData(output);
|
| 162 | output.putByte("ExplosionPower", (byte)this.explosionPower);
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 167 | super.readAdditionalSaveData(input);
|
| 168 | this.explosionPower = input.getByteOr("ExplosionPower", (byte)1);
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | public boolean supportQuadLeashAsHolder() {
|
| 173 | return true;
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | public double leashElasticDistance() {
|
| 178 | return 10.0;
|
| 179 | }
|
| 180 |
|
| 181 | @Override
|
| 182 | public double leashSnapDistance() {
|
| 183 | return 16.0;
|
| 184 | }
|
| 185 |
|
| 186 | public static void faceMovementDirection(final Mob ghast) {
|
| 187 | if (ghast.getTarget() == null) {
|
| 188 | Vec3 movement = ghast.getDeltaMovement();
|
| 189 | ghast.setYRot(-((float)Mth.atan2(movement.x, movement.z)) * (180.0F / (float)Math.PI));
|
| 190 | ghast.yBodyRot = ghast.getYRot();
|
| 191 | } else {
|
| 192 | LivingEntity target = ghast.getTarget();
|
| 193 | double maxDist = 64.0;
|
| 194 | if (target.distanceToSqr(ghast) < 4096.0) {
|
| 195 | double xdd = target.getX() - ghast.getX();
|
| 196 | double zdd = target.getZ() - ghast.getZ();
|
| 197 | ghast.setYRot(-((float)Mth.atan2(xdd, zdd)) * (180.0F / (float)Math.PI));
|
| 198 | ghast.yBodyRot = ghast.getYRot();
|
| 199 | }
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | public static class GhastLookGoal extends Goal {
|
| 204 | private final Mob ghast;
|
| 205 |
|
| 206 | public GhastLookGoal(final Mob ghast) {
|
| 207 | this.ghast = ghast;
|
| 208 | this.setFlags(EnumSet.of(Goal.Flag.LOOK));
|
| 209 | }
|
| 210 |
|
| 211 | @Override
|
| 212 | public boolean canUse() {
|
| 213 | return true;
|
| 214 | }
|
| 215 |
|
| 216 | @Override
|
| 217 | public boolean requiresUpdateEveryTick() {
|
| 218 | return true;
|
| 219 | }
|
| 220 |
|
| 221 | @Override
|
| 222 | public void tick() {
|
| 223 | Ghast.faceMovementDirection(this.ghast);
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | public static class GhastMoveControl<T extends Mob> extends MoveControl<T> {
|
| 228 | private int floatDuration;
|
| 229 | private final boolean careful;
|
| 230 | private final BooleanSupplier shouldBeStopped;
|
| 231 |
|
| 232 | public GhastMoveControl(final T ghast, final boolean careful, final BooleanSupplier shouldBeStopped) {
|
| 233 | super(ghast);
|
| 234 | this.careful = careful;
|
| 235 | this.shouldBeStopped = shouldBeStopped;
|
| 236 | }
|
| 237 |
|
| 238 | @Override
|
| 239 | public void tick() {
|
| 240 | if (this.shouldBeStopped.getAsBoolean()) {
|
| 241 | this.operation = MoveControl.Operation.WAIT;
|
| 242 | this.mob.stopInPlace();
|
| 243 | }
|
| 244 |
|
| 245 | if (this.operation == MoveControl.Operation.MOVE_TO) {
|
| 246 | if (this.floatDuration-- <= 0) {
|
| 247 | this.floatDuration = this.floatDuration + this.mob.getRandom().nextInt(5) + 2;
|
| 248 | Vec3 travel = new Vec3(this.wantedX - this.mob.getX(), this.wantedY - this.mob.getY(), this.wantedZ - this.mob.getZ());
|
| 249 | if (this.canReach(travel)) {
|
| 250 | this.mob.setDeltaMovement(this.mob.getDeltaMovement().add(travel.normalize().scale(this.mob.getAttributeValue(Attributes.FLYING_SPEED) * 5.0 / 3.0)));
|
| 251 | } else {
|
| 252 | this.operation = MoveControl.Operation.WAIT;
|
| 253 | }
|
| 254 | }
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | private boolean canReach(final Vec3 travel) {
|
| 259 | AABB aabb = this.mob.getBoundingBox();
|
| 260 | AABB aabbAtDestination = aabb.move(travel);
|
| 261 | if (this.careful) {
|
| 262 | for (BlockPos pos : BlockPos.betweenClosed(aabbAtDestination.inflate(1.0))) {
|
| 263 | if (!this.blockTraversalPossible(this.mob.level(), null, null, pos, false, false)) {
|
| 264 | return false;
|
| 265 | }
|
| 266 | }
|
| 267 | }
|
| 268 |
|
| 269 | boolean isInWater = this.mob.isInWater();
|
| 270 | boolean isInLava = this.mob.isInLava();
|
| 271 | Vec3 start = this.mob.position();
|
| 272 | Vec3 end = start.add(travel);
|
| 273 | return BlockGetter.forEachBlockIntersectedBetween(
|
| 274 | start,
|
| 275 | end,
|
| 276 | aabbAtDestination,
|
| 277 | (blockPos, i) -> aabb.intersects(blockPos) ? true : this.blockTraversalPossible(this.mob.level(), start, end, blockPos, isInWater, isInLava)
|
| 278 | );
|
| 279 | }
|
| 280 |
|
| 281 | private boolean blockTraversalPossible(
|
| 282 | final BlockGetter level,
|
| 283 | final @Nullable Vec3 start,
|
| 284 | final @Nullable Vec3 end,
|
| 285 | final BlockPos pos,
|
| 286 | final boolean canPathThroughWater,
|
| 287 | final boolean canPathThroughLava
|
| 288 | ) {
|
| 289 | BlockState state = level.getBlockState(pos);
|
| 290 | if (state.isAir()) {
|
| 291 | return true;
|
| 292 | }
|
| 293 |
|
| 294 | boolean preciseBlockCollisions = start != null && end != null;
|
| 295 | boolean pathNoCollisions = preciseBlockCollisions
|
| 296 | ? !this.mob.collidedWithShapeMovingFrom(start, end, state.getCollisionShape(level, pos).move(new Vec3(pos)).toAabbs())
|
| 297 | : state.getCollisionShape(level, pos).isEmpty();
|
| 298 | if (!this.careful) {
|
| 299 | return pathNoCollisions;
|
| 300 | }
|
| 301 |
|
| 302 | if (state.is(BlockTags.HAPPY_GHAST_AVOIDS)) {
|
| 303 | return false;
|
| 304 | }
|
| 305 |
|
| 306 | FluidState fluidState = level.getFluidState(pos);
|
| 307 | if (!fluidState.isEmpty() && (!preciseBlockCollisions || this.mob.collidedWithFluid(fluidState, pos, start, end))) {
|
| 308 | if (fluidState.is(FluidTags.WATER)) {
|
| 309 | return canPathThroughWater;
|
| 310 | }
|
| 311 |
|
| 312 | if (fluidState.is(FluidTags.LAVA)) {
|
| 313 | return canPathThroughLava;
|
| 314 | }
|
| 315 | }
|
| 316 |
|
| 317 | return pathNoCollisions;
|
| 318 | }
|
| 319 | }
|
| 320 |
|
| 321 | private static class GhastShootFireballGoal extends Goal {
|
| 322 | private final Ghast ghast;
|
| 323 | public int chargeTime;
|
| 324 |
|
| 325 | public GhastShootFireballGoal(final Ghast ghast) {
|
| 326 | this.ghast = ghast;
|
| 327 | }
|
| 328 |
|
| 329 | @Override
|
| 330 | public boolean canUse() {
|
| 331 | return this.ghast.getTarget() != null;
|
| 332 | }
|
| 333 |
|
| 334 | @Override
|
| 335 | public void start() {
|
| 336 | this.chargeTime = 0;
|
| 337 | }
|
| 338 |
|
| 339 | @Override
|
| 340 | public void stop() {
|
| 341 | this.ghast.setCharging(false);
|
| 342 | }
|
| 343 |
|
| 344 | @Override
|
| 345 | public boolean requiresUpdateEveryTick() {
|
| 346 | return true;
|
| 347 | }
|
| 348 |
|
| 349 | @Override
|
| 350 | public void tick() {
|
| 351 | LivingEntity target = this.ghast.getTarget();
|
| 352 | if (target != null) {
|
| 353 | double maxDist = 64.0;
|
| 354 | if (target.distanceToSqr(this.ghast) < 4096.0 && this.ghast.hasLineOfSight(target)) {
|
| 355 | Level level = this.ghast.level();
|
| 356 | this.chargeTime++;
|
| 357 | if (this.chargeTime == 10 && !this.ghast.isSilent()) {
|
| 358 | level.levelEvent(null, 1015, this.ghast.blockPosition(), 0);
|
| 359 | }
|
| 360 |
|
| 361 | if (this.chargeTime == 20) {
|
| 362 | double d = 4.0;
|
| 363 | Vec3 viewVector = this.ghast.getViewVector(1.0F);
|
| 364 | double xdd = target.getX() - (this.ghast.getX() + viewVector.x * 4.0);
|
| 365 | double ydd = target.getY(0.5) - (0.5 + this.ghast.getY(0.5));
|
| 366 | double zdd = target.getZ() - (this.ghast.getZ() + viewVector.z * 4.0);
|
| 367 | Vec3 direction = new Vec3(xdd, ydd, zdd);
|
| 368 | if (!this.ghast.isSilent()) {
|
| 369 | level.levelEvent(null, 1016, this.ghast.blockPosition(), 0);
|
| 370 | }
|
| 371 |
|
| 372 | LargeFireball entity = new LargeFireball(level, this.ghast, direction.normalize(), this.ghast.getExplosionPower());
|
| 373 | entity.setPos(this.ghast.getX() + viewVector.x * 4.0, this.ghast.getY(0.5) + 0.5, entity.getZ() + viewVector.z * 4.0);
|
| 374 | level.addFreshEntity(entity);
|
| 375 | this.chargeTime = -40;
|
| 376 | }
|
| 377 | } else if (this.chargeTime > 0) {
|
| 378 | this.chargeTime--;
|
| 379 | }
|
| 380 |
|
| 381 | this.ghast.setCharging(this.chargeTime > 10);
|
| 382 | }
|
| 383 | }
|
| 384 | }
|
| 385 |
|
| 386 | public static class RandomFloatAroundGoal extends Goal {
|
| 387 | private static final int MAX_ATTEMPTS = 64;
|
| 388 | private final Mob ghast;
|
| 389 | private final int distanceToBlocks;
|
| 390 |
|
| 391 | public RandomFloatAroundGoal(final Mob ghast) {
|
| 392 | this(ghast, 0);
|
| 393 | }
|
| 394 |
|
| 395 | public RandomFloatAroundGoal(final Mob ghast, final int distanceToBlocks) {
|
| 396 | this.ghast = ghast;
|
| 397 | this.distanceToBlocks = distanceToBlocks;
|
| 398 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 399 | }
|
| 400 |
|
| 401 | @Override
|
| 402 | public boolean canUse() {
|
| 403 | MoveControl moveControl = this.ghast.getMoveControl();
|
| 404 | if (!moveControl.hasWanted()) {
|
| 405 | return true;
|
| 406 | }
|
| 407 |
|
| 408 | double xd = moveControl.getWantedX() - this.ghast.getX();
|
| 409 | double yd = moveControl.getWantedY() - this.ghast.getY();
|
| 410 | double zd = moveControl.getWantedZ() - this.ghast.getZ();
|
| 411 | double dd = xd * xd + yd * yd + zd * zd;
|
| 412 | return dd < 1.0 || dd > 3600.0;
|
| 413 | }
|
| 414 |
|
| 415 | @Override
|
| 416 | public boolean canContinueToUse() {
|
| 417 | return false;
|
| 418 | }
|
| 419 |
|
| 420 | @Override
|
| 421 | public void start() {
|
| 422 | Vec3 result = getSuitableFlyToPosition(this.ghast, this.distanceToBlocks);
|
| 423 | this.ghast.getMoveControl().setWantedPosition(result.x(), result.y(), result.z(), 1.0);
|
| 424 | }
|
| 425 |
|
| 426 | public static Vec3 getSuitableFlyToPosition(final Mob mob, final int distanceToBlocks) {
|
| 427 | Level level = mob.level();
|
| 428 | RandomSource random = mob.getRandom();
|
| 429 | Vec3 center = mob.position();
|
| 430 | Vec3 result = null;
|
| 431 |
|
| 432 | for (int i = 0; i < 64; i++) {
|
| 433 | result = chooseRandomPositionWithRestriction(mob, center, random);
|
| 434 | if (result != null && isGoodTarget(level, result, distanceToBlocks)) {
|
| 435 | return result;
|
| 436 | }
|
| 437 | }
|
| 438 |
|
| 439 | if (result == null) {
|
| 440 | result = chooseRandomPosition(center, random);
|
| 441 | }
|
| 442 |
|
| 443 | BlockPos pos = BlockPos.containing(result);
|
| 444 | int heightY = level.getHeight(Heightmap.Types.MOTION_BLOCKING, pos.getX(), pos.getZ());
|
| 445 | if (heightY < pos.getY() && heightY > level.getMinY()) {
|
| 446 | result = new Vec3(result.x(), mob.getY() - Math.abs(mob.getY() - result.y()), result.z());
|
| 447 | }
|
| 448 |
|
| 449 | return result;
|
| 450 | }
|
| 451 |
|
| 452 | private static boolean isGoodTarget(final Level level, final Vec3 target, final int distanceToBlocks) {
|
| 453 | if (distanceToBlocks <= 0) {
|
| 454 | return true;
|
| 455 | }
|
| 456 |
|
| 457 | BlockPos pos = BlockPos.containing(target);
|
| 458 | if (!level.getBlockState(pos).isAir()) {
|
| 459 | return false;
|
| 460 | }
|
| 461 |
|
| 462 | for (Direction dir : Direction.values()) {
|
| 463 | for (int i = 1; i < distanceToBlocks; i++) {
|
| 464 | BlockPos offset = pos.relative(dir, i);
|
| 465 | if (!level.getBlockState(offset).isAir()) {
|
| 466 | return true;
|
| 467 | }
|
| 468 | }
|
| 469 | }
|
| 470 |
|
| 471 | return false;
|
| 472 | }
|
| 473 |
|
| 474 | private static Vec3 chooseRandomPosition(final Vec3 center, final RandomSource random) {
|
| 475 | double xTarget = center.x() + (random.nextFloat() * 2.0F - 1.0F) * 16.0F;
|
| 476 | double yTarget = center.y() + (random.nextFloat() * 2.0F - 1.0F) * 16.0F;
|
| 477 | double zTarget = center.z() + (random.nextFloat() * 2.0F - 1.0F) * 16.0F;
|
| 478 | return new Vec3(xTarget, yTarget, zTarget);
|
| 479 | }
|
| 480 |
|
| 481 | private static @Nullable Vec3 chooseRandomPositionWithRestriction(final Mob mob, final Vec3 center, final RandomSource random) {
|
| 482 | Vec3 target = chooseRandomPosition(center, random);
|
| 483 | return mob.hasHome() && !mob.isWithinHome(target) ? null : target;
|
| 484 | }
|
| 485 | }
|
| 486 | }
|
| 487 |
|