| 1 | package net.minecraft.world.entity.monster.zombie;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.resources.Identifier;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.sounds.SoundEvent;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.util.TimeUtil;
|
| 10 | import net.minecraft.util.valueproviders.UniformInt;
|
| 11 | import net.minecraft.world.Difficulty;
|
| 12 | import net.minecraft.world.DifficultyInstance;
|
| 13 | import net.minecraft.world.damagesource.DamageSource;
|
| 14 | import net.minecraft.world.entity.EntityAttachment;
|
| 15 | import net.minecraft.world.entity.EntityAttachments;
|
| 16 | import net.minecraft.world.entity.EntityDimensions;
|
| 17 | import net.minecraft.world.entity.EntityReference;
|
| 18 | import net.minecraft.world.entity.EntitySelector;
|
| 19 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 20 | import net.minecraft.world.entity.EntityType;
|
| 21 | import net.minecraft.world.entity.EquipmentSlot;
|
| 22 | import net.minecraft.world.entity.LivingEntity;
|
| 23 | import net.minecraft.world.entity.NeutralMob;
|
| 24 | import net.minecraft.world.entity.Pose;
|
| 25 | import net.minecraft.world.entity.ai.attributes.AttributeInstance;
|
| 26 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 27 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 28 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 29 | import net.minecraft.world.entity.ai.goal.SpearUseGoal;
|
| 30 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 31 | import net.minecraft.world.entity.ai.goal.ZombieAttackGoal;
|
| 32 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 33 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 34 | import net.minecraft.world.entity.ai.goal.target.ResetUniversalAngerTargetGoal;
|
| 35 | import net.minecraft.world.entity.player.Player;
|
| 36 | import net.minecraft.world.item.ItemStack;
|
| 37 | import net.minecraft.world.item.Items;
|
| 38 | import net.minecraft.world.level.Level;
|
| 39 | import net.minecraft.world.level.LevelAccessor;
|
| 40 | import net.minecraft.world.level.LevelReader;
|
| 41 | import net.minecraft.world.level.block.Blocks;
|
| 42 | import net.minecraft.world.level.pathfinder.PathType;
|
| 43 | import net.minecraft.world.level.storage.ValueInput;
|
| 44 | import net.minecraft.world.level.storage.ValueOutput;
|
| 45 | import net.minecraft.world.phys.AABB;
|
| 46 | import org.jspecify.annotations.Nullable;
|
| 47 |
|
| 48 | public class ZombifiedPiglin extends Zombie implements NeutralMob {
|
| 49 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.49F, 0.98F)
|
| 50 | .withEyeHeight(0.78F)
|
| 51 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.VEHICLE, 0.0F, 0.1875F, 0.0F));
|
| 52 | private static final Identifier SPEED_MODIFIER_ATTACKING_ID = Identifier.withDefaultNamespace("attacking");
|
| 53 | private static final AttributeModifier SPEED_MODIFIER_ATTACKING = new AttributeModifier(
|
| 54 | SPEED_MODIFIER_ATTACKING_ID, 0.05, AttributeModifier.Operation.ADD_VALUE
|
| 55 | );
|
| 56 | private static final UniformInt FIRST_ANGER_SOUND_DELAY = TimeUtil.rangeOfSeconds(0, 1);
|
| 57 | private int playFirstAngerSoundIn;
|
| 58 | private static final UniformInt PERSISTENT_ANGER_TIME = TimeUtil.rangeOfSeconds(20, 39);
|
| 59 | private long persistentAngerEndTime;
|
| 60 | private @Nullable EntityReference<LivingEntity> persistentAngerTarget;
|
| 61 | private static final int ALERT_RANGE_Y = 10;
|
| 62 | private static final UniformInt ALERT_INTERVAL = TimeUtil.rangeOfSeconds(4, 6);
|
| 63 | private int ticksUntilNextAlert;
|
| 64 |
|
| 65 | public ZombifiedPiglin(final EntityType<? extends ZombifiedPiglin> type, final Level level) {
|
| 66 | super(type, level);
|
| 67 | this.setPathfindingMalus(PathType.LAVA, 8.0F);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected void addBehaviourGoals() {
|
| 72 | this.goalSelector.addGoal(1, new SpearUseGoal<>(this, 1.0, 1.0, 10.0F, 2.0F));
|
| 73 | this.goalSelector.addGoal(2, new ZombieAttackGoal(this, 1.0, false));
|
| 74 | this.goalSelector.addGoal(7, new WaterAvoidingRandomStrollGoal(this, 1.0));
|
| 75 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this).setAlertOthers());
|
| 76 | this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.class, 10, true, false, this::isAngryAt));
|
| 77 | this.targetSelector.addGoal(3, new ResetUniversalAngerTargetGoal<>(this, true));
|
| 78 | }
|
| 79 |
|
| 80 | public static AttributeSupplier.Builder createAttributes() {
|
| 81 | return Zombie.createAttributes().add(Attributes.SPAWN_REINFORCEMENTS_CHANCE, 0.0).add(Attributes.MOVEMENT_SPEED, 0.23F).add(Attributes.ATTACK_DAMAGE, 5.0);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 86 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected boolean convertsInWater() {
|
| 91 | return false;
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected void customServerAiStep(final ServerLevel level) {
|
| 96 | AttributeInstance speed = this.getAttribute(Attributes.MOVEMENT_SPEED);
|
| 97 | if (this.isAngry()) {
|
| 98 | if (!this.isBaby() && !speed.hasModifier(SPEED_MODIFIER_ATTACKING_ID)) {
|
| 99 | speed.addTransientModifier(SPEED_MODIFIER_ATTACKING);
|
| 100 | }
|
| 101 |
|
| 102 | this.maybePlayFirstAngerSound();
|
| 103 | } else if (speed.hasModifier(SPEED_MODIFIER_ATTACKING_ID)) {
|
| 104 | speed.removeModifier(SPEED_MODIFIER_ATTACKING_ID);
|
| 105 | }
|
| 106 |
|
| 107 | this.updatePersistentAnger(level, true);
|
| 108 | if (this.getTarget() != null) {
|
| 109 | this.maybeAlertOthers();
|
| 110 | }
|
| 111 |
|
| 112 | super.customServerAiStep(level);
|
| 113 | }
|
| 114 |
|
| 115 | private void maybePlayFirstAngerSound() {
|
| 116 | if (this.playFirstAngerSoundIn > 0) {
|
| 117 | this.playFirstAngerSoundIn--;
|
| 118 | if (this.playFirstAngerSoundIn == 0) {
|
| 119 | this.playAngerSound();
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | private void maybeAlertOthers() {
|
| 125 | if (this.ticksUntilNextAlert > 0) {
|
| 126 | this.ticksUntilNextAlert--;
|
| 127 | } else {
|
| 128 | if (this.getSensing().hasLineOfSight(this.getTarget())) {
|
| 129 | this.alertOthers();
|
| 130 | }
|
| 131 |
|
| 132 | this.ticksUntilNextAlert = ALERT_INTERVAL.sample(this.random);
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | private void alertOthers() {
|
| 137 | double within = this.getAttributeValue(Attributes.FOLLOW_RANGE);
|
| 138 | AABB searchAabb = AABB.unitCubeFromLowerCorner(this.position()).inflate(within, 10.0, within);
|
| 139 | this.level()
|
| 140 | .getEntitiesOfClass(ZombifiedPiglin.class, searchAabb, EntitySelector.NO_SPECTATORS)
|
| 141 | .stream()
|
| 142 | .filter(other -> other != this)
|
| 143 | .filter(other -> other.getTarget() == null)
|
| 144 | .filter(other -> !other.isAlliedTo(this.getTarget()))
|
| 145 | .forEach(other -> other.setTarget(this.getTarget()));
|
| 146 | }
|
| 147 |
|
| 148 | private void playAngerSound() {
|
| 149 | this.playSound(SoundEvents.ZOMBIFIED_PIGLIN_ANGRY, this.getSoundVolume() * 2.0F, this.getVoicePitch() * 1.8F);
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public void setTarget(final @Nullable LivingEntity target) {
|
| 154 | if (this.getTarget() == null && target != null) {
|
| 155 | this.playFirstAngerSoundIn = FIRST_ANGER_SOUND_DELAY.sample(this.random);
|
| 156 | this.ticksUntilNextAlert = ALERT_INTERVAL.sample(this.random);
|
| 157 | }
|
| 158 |
|
| 159 | super.setTarget(target);
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | public void startPersistentAngerTimer() {
|
| 164 | this.setTimeToRemainAngry(PERSISTENT_ANGER_TIME.sample(this.random));
|
| 165 | }
|
| 166 |
|
| 167 | public static boolean checkZombifiedPiglinSpawnRules(
|
| 168 | final EntityType<ZombifiedPiglin> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 169 | ) {
|
| 170 | return level.getDifficulty() != Difficulty.PEACEFUL && !level.getBlockState(pos.below()).is(Blocks.NETHER_WART_BLOCK);
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public boolean checkSpawnObstruction(final LevelReader level) {
|
| 175 | return level.isUnobstructed(this) && !level.containsAnyLiquid(this.getBoundingBox());
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 180 | super.addAdditionalSaveData(output);
|
| 181 | this.addPersistentAngerSaveData(output);
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 186 | super.readAdditionalSaveData(input);
|
| 187 | this.readPersistentAngerSaveData(this.level(), input);
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public void setPersistentAngerEndTime(final long endTime) {
|
| 192 | this.persistentAngerEndTime = endTime;
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public long getPersistentAngerEndTime() {
|
| 197 | return this.persistentAngerEndTime;
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public void setPersistentAngerTarget(final @Nullable EntityReference<LivingEntity> persistentAngerTarget) {
|
| 202 | this.persistentAngerTarget = persistentAngerTarget;
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | protected SoundEvent getAmbientSound() {
|
| 207 | return this.isAngry() ? SoundEvents.ZOMBIFIED_PIGLIN_ANGRY : SoundEvents.ZOMBIFIED_PIGLIN_AMBIENT;
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 212 | return SoundEvents.ZOMBIFIED_PIGLIN_HURT;
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | protected SoundEvent getDeathSound() {
|
| 217 | return SoundEvents.ZOMBIFIED_PIGLIN_DEATH;
|
| 218 | }
|
| 219 |
|
| 220 | @Override
|
| 221 | public void populateDefaultEquipmentSlots(final RandomSource random, final DifficultyInstance difficulty) {
|
| 222 | this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(random.nextInt(20) == 0 ? Items.GOLDEN_SPEAR : Items.GOLDEN_SWORD));
|
| 223 | }
|
| 224 |
|
| 225 | @Override
|
| 226 | protected void randomizeReinforcementsChance() {
|
| 227 | this.getAttribute(Attributes.SPAWN_REINFORCEMENTS_CHANCE).setBaseValue(0.0);
|
| 228 | }
|
| 229 |
|
| 230 | @Override
|
| 231 | public @Nullable EntityReference<LivingEntity> getPersistentAngerTarget() {
|
| 232 | return this.persistentAngerTarget;
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | public boolean isPreventingPlayerRest(final ServerLevel level, final Player player) {
|
| 237 | return this.isAngryAt(player, level);
|
| 238 | }
|
| 239 |
|
| 240 | @Override
|
| 241 | public boolean wantsToPickUp(final ServerLevel level, final ItemStack itemStack) {
|
| 242 | return this.canHoldItem(itemStack);
|
| 243 | }
|
| 244 | }
|
| 245 |
|