| 1 | package net.minecraft.world.entity.monster;
|
| 2 |
|
| 3 | import java.util.function.Predicate;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.particles.ColorParticleOption;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.tags.EntityTypeTags;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.world.damagesource.DamageSource;
|
| 13 | import net.minecraft.world.entity.Entity;
|
| 14 | import net.minecraft.world.entity.EntityEvent;
|
| 15 | import net.minecraft.world.entity.EntityType;
|
| 16 | import net.minecraft.world.entity.EntityTypes;
|
| 17 | import net.minecraft.world.entity.LivingEntity;
|
| 18 | import net.minecraft.world.entity.Mob;
|
| 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.goal.FloatGoal;
|
| 22 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 23 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 24 | import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
|
| 25 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 26 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 27 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 28 | import net.minecraft.world.entity.animal.golem.IronGolem;
|
| 29 | import net.minecraft.world.entity.monster.illager.AbstractIllager;
|
| 30 | import net.minecraft.world.entity.npc.villager.AbstractVillager;
|
| 31 | import net.minecraft.world.entity.player.Player;
|
| 32 | import net.minecraft.world.entity.raid.Raider;
|
| 33 | import net.minecraft.world.entity.vehicle.boat.AbstractBoat;
|
| 34 | import net.minecraft.world.level.Level;
|
| 35 | import net.minecraft.world.level.LevelReader;
|
| 36 | import net.minecraft.world.level.block.Block;
|
| 37 | import net.minecraft.world.level.block.LeavesBlock;
|
| 38 | import net.minecraft.world.level.block.state.BlockState;
|
| 39 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 40 | import net.minecraft.world.level.gamerules.GameRules;
|
| 41 | import net.minecraft.world.level.pathfinder.PathType;
|
| 42 | import net.minecraft.world.level.storage.ValueInput;
|
| 43 | import net.minecraft.world.level.storage.ValueOutput;
|
| 44 | import net.minecraft.world.phys.AABB;
|
| 45 | import net.minecraft.world.phys.Vec3;
|
| 46 | import org.jspecify.annotations.Nullable;
|
| 47 |
|
| 48 | public class Ravager extends Raider {
|
| 49 | private static final Predicate<Entity> ROAR_TARGET_WITH_GRIEFING = entity -> !(entity instanceof Ravager) && entity.isAlive();
|
| 50 | private static final Predicate<Entity> ROAR_TARGET_WITHOUT_GRIEFING = entity -> ROAR_TARGET_WITH_GRIEFING.test(entity) && !entity.is(EntityTypes.ARMOR_STAND);
|
| 51 | private static final Predicate<LivingEntity> ROAR_TARGET_ON_CLIENT = e -> !(e instanceof Ravager) && e.isAlive() && e.isLocalInstanceAuthoritative();
|
| 52 | private static final double BASE_MOVEMENT_SPEED = 0.3;
|
| 53 | private static final double ATTACK_MOVEMENT_SPEED = 0.35;
|
| 54 | private static final int STUNNED_COLOR = 8356754;
|
| 55 | private static final float STUNNED_COLOR_BLUE = 0.57254905F;
|
| 56 | private static final float STUNNED_COLOR_GREEN = 0.5137255F;
|
| 57 | private static final float STUNNED_COLOR_RED = 0.49803922F;
|
| 58 | public static final int ATTACK_DURATION = 10;
|
| 59 | public static final int STUN_DURATION = 40;
|
| 60 | private static final int DEFAULT_ATTACK_TICK = 0;
|
| 61 | private static final int DEFAULT_STUN_TICK = 0;
|
| 62 | private static final int DEFAULT_ROAR_TICK = 0;
|
| 63 | private int attackTick = 0;
|
| 64 | private int stunnedTick = 0;
|
| 65 | private int roarTick = 0;
|
| 66 |
|
| 67 | public Ravager(final EntityType<? extends Ravager> type, final Level level) {
|
| 68 | super(type, level);
|
| 69 | this.xpReward = 20;
|
| 70 | this.setPathfindingMalus(PathType.LEAVES, 0.0F);
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected void registerGoals() {
|
| 75 | super.registerGoals();
|
| 76 | this.goalSelector.addGoal(0, new FloatGoal(this));
|
| 77 | this.goalSelector.addGoal(4, new MeleeAttackGoal(this, 1.0, true));
|
| 78 | this.goalSelector.addGoal(5, new WaterAvoidingRandomStrollGoal(this, 0.4));
|
| 79 | this.goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 6.0F));
|
| 80 | this.goalSelector.addGoal(10, new LookAtPlayerGoal(this, Mob.class, 8.0F));
|
| 81 | this.targetSelector.addGoal(2, new HurtByTargetGoal(this, Raider.class).setAlertOthers());
|
| 82 | this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, Player.class, true));
|
| 83 | this.targetSelector.addGoal(4, new NearestAttackableTargetGoal<>(this, AbstractVillager.class, true, (target, level) -> !target.isBaby()));
|
| 84 | this.targetSelector.addGoal(4, new NearestAttackableTargetGoal<>(this, IronGolem.class, true));
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | protected void updateControlFlags() {
|
| 89 | boolean noController = !(this.getControllingPassenger() instanceof Mob) || this.getControllingPassenger().is(EntityTypeTags.RAIDERS);
|
| 90 | boolean notInBoat = !(this.getVehicle() instanceof AbstractBoat);
|
| 91 | this.goalSelector.setControlFlag(Goal.Flag.MOVE, noController);
|
| 92 | this.goalSelector.setControlFlag(Goal.Flag.JUMP, noController && notInBoat);
|
| 93 | this.goalSelector.setControlFlag(Goal.Flag.LOOK, noController);
|
| 94 | this.goalSelector.setControlFlag(Goal.Flag.TARGET, noController);
|
| 95 | }
|
| 96 |
|
| 97 | public static AttributeSupplier.Builder createAttributes() {
|
| 98 | return Monster.createMonsterAttributes()
|
| 99 | .add(Attributes.MAX_HEALTH, 100.0)
|
| 100 | .add(Attributes.MOVEMENT_SPEED, 0.3)
|
| 101 | .add(Attributes.KNOCKBACK_RESISTANCE, 0.75)
|
| 102 | .add(Attributes.ATTACK_DAMAGE, 12.0)
|
| 103 | .add(Attributes.ATTACK_KNOCKBACK, 1.5)
|
| 104 | .add(Attributes.FOLLOW_RANGE, 32.0)
|
| 105 | .add(Attributes.STEP_HEIGHT, 1.0);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 110 | super.addAdditionalSaveData(output);
|
| 111 | output.putInt("AttackTick", this.attackTick);
|
| 112 | output.putInt("StunTick", this.stunnedTick);
|
| 113 | output.putInt("RoarTick", this.roarTick);
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 118 | super.readAdditionalSaveData(input);
|
| 119 | this.attackTick = input.getIntOr("AttackTick", 0);
|
| 120 | this.stunnedTick = input.getIntOr("StunTick", 0);
|
| 121 | this.roarTick = input.getIntOr("RoarTick", 0);
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public SoundEvent getCelebrateSound() {
|
| 126 | return SoundEvents.RAVAGER_CELEBRATE;
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public int getMaxHeadYRot() {
|
| 131 | return 45;
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public void aiStep() {
|
| 136 | super.aiStep();
|
| 137 | if (this.isAlive()) {
|
| 138 | if (this.isImmobile()) {
|
| 139 | this.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.0);
|
| 140 | } else {
|
| 141 | double maxSpeed = this.getTarget() != null ? 0.35 : 0.3;
|
| 142 | double baseValue = this.getAttribute(Attributes.MOVEMENT_SPEED).getBaseValue();
|
| 143 | this.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(Mth.lerp(0.1, baseValue, maxSpeed));
|
| 144 | }
|
| 145 |
|
| 146 | if (this.level() instanceof ServerLevel serverLevel && this.horizontalCollision && serverLevel.getGameRules().get(GameRules.MOB_GRIEFING)) {
|
| 147 | boolean destroyedBlock = false;
|
| 148 | AABB bb = this.getBoundingBox().inflate(0.2);
|
| 149 |
|
| 150 | for (BlockPos pos : BlockPos.betweenClosed(
|
| 151 | Mth.floor(bb.minX), Mth.floor(bb.minY), Mth.floor(bb.minZ), Mth.floor(bb.maxX), Mth.floor(bb.maxY), Mth.floor(bb.maxZ)
|
| 152 | )) {
|
| 153 | BlockState state = serverLevel.getBlockState(pos);
|
| 154 | Block block = state.getBlock();
|
| 155 | if (block instanceof LeavesBlock) {
|
| 156 | destroyedBlock = serverLevel.destroyBlock(pos, true, this) || destroyedBlock;
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | if (!destroyedBlock && this.onGround()) {
|
| 161 | this.jumpFromGround();
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | if (this.roarTick > 0) {
|
| 166 | this.roarTick--;
|
| 167 | if (this.roarTick == 10) {
|
| 168 | this.roar();
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | if (this.attackTick > 0) {
|
| 173 | this.attackTick--;
|
| 174 | }
|
| 175 |
|
| 176 | if (this.stunnedTick > 0) {
|
| 177 | this.stunnedTick--;
|
| 178 | this.stunEffect();
|
| 179 | if (this.stunnedTick == 0) {
|
| 180 | this.playSound(SoundEvents.RAVAGER_ROAR, 1.0F, 1.0F);
|
| 181 | this.roarTick = 20;
|
| 182 | }
|
| 183 | }
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | private void stunEffect() {
|
| 188 | if (this.random.nextInt(6) == 0) {
|
| 189 | double headX = this.getX() - this.getBbWidth() * Math.sin(this.yBodyRot * (float) (Math.PI / 180.0)) + (this.random.nextDouble() * 0.6 - 0.3);
|
| 190 | double headY = this.getY() + this.getBbHeight() - 0.3;
|
| 191 | double headZ = this.getZ() + this.getBbWidth() * Math.cos(this.yBodyRot * (float) (Math.PI / 180.0)) + (this.random.nextDouble() * 0.6 - 0.3);
|
| 192 | this.level().addParticle(ColorParticleOption.create(ParticleTypes.ENTITY_EFFECT, 0.49803922F, 0.5137255F, 0.57254905F), headX, headY, headZ, 0.0, 0.0, 0.0);
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | protected boolean isImmobile() {
|
| 198 | return super.isImmobile() || this.attackTick > 0 || this.stunnedTick > 0 || this.roarTick > 0;
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | public boolean hasLineOfSight(final Entity target) {
|
| 203 | return this.stunnedTick <= 0 && this.roarTick <= 0 ? super.hasLineOfSight(target) : false;
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | protected void blockedByItem(final LivingEntity defender, final DamageSource source, final float damage, final boolean fullyBlocked) {
|
| 208 | if (this.roarTick == 0) {
|
| 209 | if (this.random.nextDouble() < 0.5) {
|
| 210 | this.stunnedTick = 40;
|
| 211 | this.playSound(SoundEvents.RAVAGER_STUNNED, 1.0F, 1.0F);
|
| 212 | this.level().broadcastEntityEvent(this, (byte)39);
|
| 213 | defender.push(this);
|
| 214 | } else {
|
| 215 | this.strongKnockback(defender);
|
| 216 | }
|
| 217 |
|
| 218 | defender.syncVelocity = true;
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | private void roar() {
|
| 223 | if (this.isAlive() && this.level() instanceof ServerLevel level) {
|
| 224 | Predicate<Entity> targetSelector = level.getGameRules().get(GameRules.MOB_GRIEFING) ? ROAR_TARGET_WITH_GRIEFING : ROAR_TARGET_WITHOUT_GRIEFING;
|
| 225 |
|
| 226 | for (LivingEntity entity : this.level().getEntitiesOfClass(LivingEntity.class, this.getBoundingBox().inflate(4.0), targetSelector)) {
|
| 227 | if (!(entity instanceof AbstractIllager)) {
|
| 228 | entity.hurtServer(level, this.damageSources().mobAttack(this), 6.0F);
|
| 229 | }
|
| 230 |
|
| 231 | if (!(entity instanceof Player)) {
|
| 232 | this.strongKnockback(entity);
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | this.gameEvent(GameEvent.ENTITY_ACTION);
|
| 237 | level.broadcastEntityEvent(this, (byte)69);
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | private void applyRoarKnockbackClient() {
|
| 242 | for (LivingEntity entity : this.level().getEntitiesOfClass(LivingEntity.class, this.getBoundingBox().inflate(4.0), ROAR_TARGET_ON_CLIENT)) {
|
| 243 | this.strongKnockback(entity);
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | private void strongKnockback(final Entity entity) {
|
| 248 | double xd = entity.getX() - this.getX();
|
| 249 | double zd = entity.getZ() - this.getZ();
|
| 250 | double dd = Math.max(xd * xd + zd * zd, 0.001);
|
| 251 | entity.push(xd / dd * 4.0, 0.2, zd / dd * 4.0);
|
| 252 | }
|
| 253 |
|
| 254 | @Override
|
| 255 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 256 | if (id == 4) {
|
| 257 | this.attackTick = 10;
|
| 258 | this.playSound(SoundEvents.RAVAGER_ATTACK, 1.0F, 1.0F);
|
| 259 | } else if (id == 39) {
|
| 260 | this.stunnedTick = 40;
|
| 261 | } else if (id == 69) {
|
| 262 | this.addRoarParticleEffects();
|
| 263 | this.applyRoarKnockbackClient();
|
| 264 | }
|
| 265 |
|
| 266 | super.handleEntityEvent(id);
|
| 267 | }
|
| 268 |
|
| 269 | private void addRoarParticleEffects() {
|
| 270 | Vec3 center = this.getBoundingBox().getCenter();
|
| 271 |
|
| 272 | for (int i = 0; i < 40; i++) {
|
| 273 | double velocityX = this.random.nextGaussian() * 0.2;
|
| 274 | double velocityY = this.random.nextGaussian() * 0.2;
|
| 275 | double velocityZ = this.random.nextGaussian() * 0.2;
|
| 276 | this.level().addParticle(ParticleTypes.POOF, center.x, center.y, center.z, velocityX, velocityY, velocityZ);
|
| 277 | }
|
| 278 | }
|
| 279 |
|
| 280 | public int getAttackTick() {
|
| 281 | return this.attackTick;
|
| 282 | }
|
| 283 |
|
| 284 | public int getStunnedTick() {
|
| 285 | return this.stunnedTick;
|
| 286 | }
|
| 287 |
|
| 288 | public int getRoarTick() {
|
| 289 | return this.roarTick;
|
| 290 | }
|
| 291 |
|
| 292 | @Override
|
| 293 | public boolean doHurtTarget(final ServerLevel level, final Entity target) {
|
| 294 | this.attackTick = 10;
|
| 295 | level.broadcastEntityEvent(this, (byte)4);
|
| 296 | this.playSound(SoundEvents.RAVAGER_ATTACK, 1.0F, 1.0F);
|
| 297 | return super.doHurtTarget(level, target);
|
| 298 | }
|
| 299 |
|
| 300 | @Override
|
| 301 | protected @Nullable SoundEvent getAmbientSound() {
|
| 302 | return SoundEvents.RAVAGER_AMBIENT;
|
| 303 | }
|
| 304 |
|
| 305 | @Override
|
| 306 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 307 | return SoundEvents.RAVAGER_HURT;
|
| 308 | }
|
| 309 |
|
| 310 | @Override
|
| 311 | protected SoundEvent getDeathSound() {
|
| 312 | return SoundEvents.RAVAGER_DEATH;
|
| 313 | }
|
| 314 |
|
| 315 | @Override
|
| 316 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 317 | this.playSound(SoundEvents.RAVAGER_STEP, 0.15F, 1.0F);
|
| 318 | }
|
| 319 |
|
| 320 | @Override
|
| 321 | public boolean checkSpawnObstruction(final LevelReader level) {
|
| 322 | return !level.containsAnyLiquid(this.getBoundingBox());
|
| 323 | }
|
| 324 |
|
| 325 | @Override
|
| 326 | public void applyRaidBuffs(final ServerLevel level, final int wave, final boolean isCaptain) {
|
| 327 | }
|
| 328 |
|
| 329 | @Override
|
| 330 | public boolean canBeLeader() {
|
| 331 | return false;
|
| 332 | }
|
| 333 |
|
| 334 | @Override
|
| 335 | protected AABB getAttackBoundingBox(final double horizontalExpansion) {
|
| 336 | AABB defaultBB = super.getAttackBoundingBox(horizontalExpansion);
|
| 337 | return defaultBB.deflate(0.05, 0.0, 0.05);
|
| 338 | }
|
| 339 | }
|
| 340 |
|