| 1 | package net.minecraft.world.entity.monster;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.sounds.SoundEvent;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.tags.DamageTypeTags;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.damagesource.DamageSource;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 14 | import net.minecraft.world.entity.EntityType;
|
| 15 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 16 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 17 | import net.minecraft.world.entity.ai.goal.ClimbOnTopOfPowderSnowGoal;
|
| 18 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 19 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 20 | import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
|
| 21 | import net.minecraft.world.entity.ai.goal.RandomStrollGoal;
|
| 22 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 23 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 24 | import net.minecraft.world.entity.player.Player;
|
| 25 | import net.minecraft.world.level.Level;
|
| 26 | import net.minecraft.world.level.LevelAccessor;
|
| 27 | import net.minecraft.world.level.LevelReader;
|
| 28 | import net.minecraft.world.level.block.InfestedBlock;
|
| 29 | import net.minecraft.world.level.block.state.BlockState;
|
| 30 | import net.minecraft.world.level.gamerules.GameRules;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | public class Silverfish extends Monster {
|
| 34 | private Silverfish.@Nullable SilverfishWakeUpFriendsGoal friendsGoal;
|
| 35 |
|
| 36 | public Silverfish(final EntityType<? extends Silverfish> type, final Level level) {
|
| 37 | super(type, level);
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected void registerGoals() {
|
| 42 | this.friendsGoal = new Silverfish.SilverfishWakeUpFriendsGoal(this);
|
| 43 | this.goalSelector.addGoal(1, new FloatGoal(this));
|
| 44 | this.goalSelector.addGoal(1, new ClimbOnTopOfPowderSnowGoal(this, this.level()));
|
| 45 | this.goalSelector.addGoal(3, this.friendsGoal);
|
| 46 | this.goalSelector.addGoal(4, new MeleeAttackGoal(this, 1.0, false));
|
| 47 | this.goalSelector.addGoal(5, new Silverfish.SilverfishMergeWithStoneGoal(this));
|
| 48 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this).setAlertOthers());
|
| 49 | this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.class, true));
|
| 50 | }
|
| 51 |
|
| 52 | public static AttributeSupplier.Builder createAttributes() {
|
| 53 | return Monster.createMonsterAttributes().add(Attributes.MAX_HEALTH, 8.0).add(Attributes.MOVEMENT_SPEED, 0.25).add(Attributes.ATTACK_DAMAGE, 1.0);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected Entity.MovementEmission getMovementEmission() {
|
| 58 | return Entity.MovementEmission.EVENTS;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected SoundEvent getAmbientSound() {
|
| 63 | return SoundEvents.SILVERFISH_AMBIENT;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 68 | return SoundEvents.SILVERFISH_HURT;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected SoundEvent getDeathSound() {
|
| 73 | return SoundEvents.SILVERFISH_DEATH;
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 78 | this.playSound(SoundEvents.SILVERFISH_STEP, 0.15F, 1.0F);
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 83 | if (this.isInvulnerableTo(level, source)) {
|
| 84 | return false;
|
| 85 | }
|
| 86 |
|
| 87 | if ((source.getEntity() != null || source.is(DamageTypeTags.ALWAYS_TRIGGERS_SILVERFISH)) && this.friendsGoal != null) {
|
| 88 | this.friendsGoal.notifyHurt();
|
| 89 | }
|
| 90 |
|
| 91 | return super.hurtServer(level, source, damage);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public void tick() {
|
| 96 | this.yBodyRot = this.getYRot();
|
| 97 | super.tick();
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public void setYBodyRot(final float yBodyRot) {
|
| 102 | this.setYRot(yBodyRot);
|
| 103 | super.setYBodyRot(yBodyRot);
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public float getWalkTargetValue(final BlockPos pos, final LevelReader level) {
|
| 108 | return InfestedBlock.isCompatibleHostBlock(level.getBlockState(pos.below())) ? 10.0F : super.getWalkTargetValue(pos, level);
|
| 109 | }
|
| 110 |
|
| 111 | public static boolean checkSilverfishSpawnRules(
|
| 112 | final EntityType<Silverfish> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 113 | ) {
|
| 114 | if (!checkAnyLightMonsterSpawnRules(type, level, spawnReason, pos, random)) {
|
| 115 | return false;
|
| 116 | }
|
| 117 |
|
| 118 | if (EntitySpawnReason.isSpawner(spawnReason)) {
|
| 119 | return true;
|
| 120 | }
|
| 121 |
|
| 122 | Player nearestPlayer = level.getNearestPlayer(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, 5.0, true);
|
| 123 | return nearestPlayer == null;
|
| 124 | }
|
| 125 |
|
| 126 | private static class SilverfishMergeWithStoneGoal extends RandomStrollGoal {
|
| 127 | private @Nullable Direction selectedDirection;
|
| 128 | private boolean doMerge;
|
| 129 |
|
| 130 | public SilverfishMergeWithStoneGoal(final Silverfish silverfish) {
|
| 131 | super(silverfish, 1.0, 10);
|
| 132 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public boolean canUse() {
|
| 137 | if (this.mob.getTarget() != null) {
|
| 138 | return false;
|
| 139 | }
|
| 140 |
|
| 141 | if (!this.mob.getNavigation().isDone()) {
|
| 142 | return false;
|
| 143 | }
|
| 144 |
|
| 145 | RandomSource random = this.mob.getRandom();
|
| 146 | if (getServerLevel(this.mob).getGameRules().get(GameRules.MOB_GRIEFING) && random.nextInt(reducedTickDelay(10)) == 0) {
|
| 147 | this.selectedDirection = Direction.getRandom(random);
|
| 148 | BlockPos pos = BlockPos.containing(this.mob.getX(), this.mob.getY() + 0.5, this.mob.getZ()).relative(this.selectedDirection);
|
| 149 | BlockState blockState = this.mob.level().getBlockState(pos);
|
| 150 | if (InfestedBlock.isCompatibleHostBlock(blockState)) {
|
| 151 | this.doMerge = true;
|
| 152 | return true;
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | this.doMerge = false;
|
| 157 | return super.canUse();
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public boolean canContinueToUse() {
|
| 162 | return this.doMerge ? false : super.canContinueToUse();
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | public void start() {
|
| 167 | if (!this.doMerge) {
|
| 168 | super.start();
|
| 169 | } else {
|
| 170 | LevelAccessor level = this.mob.level();
|
| 171 | BlockPos pos = BlockPos.containing(this.mob.getX(), this.mob.getY() + 0.5, this.mob.getZ()).relative(this.selectedDirection);
|
| 172 | BlockState blockState = level.getBlockState(pos);
|
| 173 | if (InfestedBlock.isCompatibleHostBlock(blockState)) {
|
| 174 | level.setBlockAndUpdate(pos, InfestedBlock.infestedStateByHost(blockState));
|
| 175 | this.mob.spawnAnim();
|
| 176 | this.mob.discard();
|
| 177 | }
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | private static class SilverfishWakeUpFriendsGoal extends Goal {
|
| 183 | private final Silverfish silverfish;
|
| 184 | private int lookForFriends;
|
| 185 |
|
| 186 | public SilverfishWakeUpFriendsGoal(final Silverfish silverfish) {
|
| 187 | this.silverfish = silverfish;
|
| 188 | }
|
| 189 |
|
| 190 | public void notifyHurt() {
|
| 191 | if (this.lookForFriends == 0) {
|
| 192 | this.lookForFriends = this.adjustedTickDelay(20);
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | public boolean canUse() {
|
| 198 | return this.lookForFriends > 0;
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | public void tick() {
|
| 203 | this.lookForFriends--;
|
| 204 | if (this.lookForFriends <= 0) {
|
| 205 | Level level = this.silverfish.level();
|
| 206 | RandomSource random = this.silverfish.getRandom();
|
| 207 | BlockPos basePos = this.silverfish.blockPosition();
|
| 208 |
|
| 209 | for (int yOff = 0; yOff <= 5 && yOff >= -5; yOff = (yOff <= 0 ? 1 : 0) - yOff) {
|
| 210 | for (int xOff = 0; xOff <= 10 && xOff >= -10; xOff = (xOff <= 0 ? 1 : 0) - xOff) {
|
| 211 | for (int zOff = 0; zOff <= 10 && zOff >= -10; zOff = (zOff <= 0 ? 1 : 0) - zOff) {
|
| 212 | BlockPos testPos = basePos.offset(xOff, yOff, zOff);
|
| 213 | BlockState blockState = level.getBlockState(testPos);
|
| 214 | if (blockState.getBlock() instanceof InfestedBlock infestedBlock) {
|
| 215 | if (getServerLevel(level).getGameRules().get(GameRules.MOB_GRIEFING)) {
|
| 216 | level.destroyBlock(testPos, true, this.silverfish);
|
| 217 | } else {
|
| 218 | level.setBlockAndUpdate(testPos, infestedBlock.hostStateByInfested(level.getBlockState(testPos)));
|
| 219 | }
|
| 220 |
|
| 221 | if (random.nextBoolean()) {
|
| 222 | return;
|
| 223 | }
|
| 224 | }
|
| 225 | }
|
| 226 | }
|
| 227 | }
|
| 228 | }
|
| 229 | }
|
| 230 | }
|
| 231 | }
|
| 232 |
|