| 1 | package net.minecraft.world.entity.monster.illager;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.function.Predicate;
|
| 5 | import net.minecraft.network.chat.Component;
|
| 6 | import net.minecraft.resources.ResourceKey;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 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.EntitySpawnReason;
|
| 15 | import net.minecraft.world.entity.EntityType;
|
| 16 | import net.minecraft.world.entity.EquipmentSlot;
|
| 17 | import net.minecraft.world.entity.LivingEntity;
|
| 18 | import net.minecraft.world.entity.Mob;
|
| 19 | import net.minecraft.world.entity.SpawnGroupData;
|
| 20 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 21 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 22 | import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
|
| 23 | import net.minecraft.world.entity.ai.goal.BreakDoorGoal;
|
| 24 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 25 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 26 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 27 | import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
|
| 28 | import net.minecraft.world.entity.ai.goal.RandomStrollGoal;
|
| 29 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 30 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 31 | import net.minecraft.world.entity.ai.util.GoalUtils;
|
| 32 | import net.minecraft.world.entity.animal.golem.IronGolem;
|
| 33 | import net.minecraft.world.entity.monster.Monster;
|
| 34 | import net.minecraft.world.entity.monster.creaking.Creaking;
|
| 35 | import net.minecraft.world.entity.npc.villager.AbstractVillager;
|
| 36 | import net.minecraft.world.entity.player.Player;
|
| 37 | import net.minecraft.world.entity.raid.Raid;
|
| 38 | import net.minecraft.world.entity.raid.Raider;
|
| 39 | import net.minecraft.world.item.ItemStack;
|
| 40 | import net.minecraft.world.item.Items;
|
| 41 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 42 | import net.minecraft.world.item.enchantment.providers.EnchantmentProvider;
|
| 43 | import net.minecraft.world.item.enchantment.providers.VanillaEnchantmentProviders;
|
| 44 | import net.minecraft.world.level.Level;
|
| 45 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 46 | import net.minecraft.world.level.storage.ValueInput;
|
| 47 | import net.minecraft.world.level.storage.ValueOutput;
|
| 48 | import org.jspecify.annotations.Nullable;
|
| 49 |
|
| 50 | public class Vindicator extends AbstractIllager {
|
| 51 | private static final String TAG_JOHNNY = "Johnny";
|
| 52 | private static final Predicate<Difficulty> DOOR_BREAKING_PREDICATE = d -> d == Difficulty.NORMAL || d == Difficulty.HARD;
|
| 53 | private static final boolean DEFAULT_JOHNNY = false;
|
| 54 | private boolean isJohnny = false;
|
| 55 |
|
| 56 | public Vindicator(final EntityType<? extends Vindicator> type, final Level level) {
|
| 57 | super(type, level);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected void registerGoals() {
|
| 62 | super.registerGoals();
|
| 63 | this.goalSelector.addGoal(0, new FloatGoal(this));
|
| 64 | this.goalSelector.addGoal(1, new AvoidEntityGoal<>(this, Creaking.class, 8.0F, 1.0, 1.2));
|
| 65 | this.goalSelector.addGoal(2, new Vindicator.VindicatorBreakDoorGoal(this));
|
| 66 | this.goalSelector.addGoal(3, new AbstractIllager.RaiderOpenDoorGoal(this));
|
| 67 | this.goalSelector.addGoal(4, new Raider.HoldGroundAttackGoal(this, 10.0F));
|
| 68 | this.goalSelector.addGoal(5, new MeleeAttackGoal(this, 1.0, false));
|
| 69 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this, Raider.class).setAlertOthers());
|
| 70 | this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.class, true));
|
| 71 | this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, AbstractVillager.class, true));
|
| 72 | this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, IronGolem.class, true));
|
| 73 | this.targetSelector.addGoal(4, new Vindicator.VindicatorJohnnyAttackGoal(this));
|
| 74 | this.goalSelector.addGoal(8, new RandomStrollGoal(this, 0.6));
|
| 75 | this.goalSelector.addGoal(9, new LookAtPlayerGoal(this, Player.class, 3.0F, 1.0F));
|
| 76 | this.goalSelector.addGoal(10, new LookAtPlayerGoal(this, Mob.class, 8.0F));
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | protected void customServerAiStep(final ServerLevel level) {
|
| 81 | if (!this.isNoAi() && GoalUtils.hasGroundPathNavigation(this)) {
|
| 82 | boolean canOpenDoors = level.isRaided(this.blockPosition());
|
| 83 | this.getNavigation().setCanOpenDoors(canOpenDoors);
|
| 84 | }
|
| 85 |
|
| 86 | super.customServerAiStep(level);
|
| 87 | }
|
| 88 |
|
| 89 | public static AttributeSupplier.Builder createAttributes() {
|
| 90 | return Monster.createMonsterAttributes()
|
| 91 | .add(Attributes.MOVEMENT_SPEED, 0.35F)
|
| 92 | .add(Attributes.FOLLOW_RANGE, 12.0)
|
| 93 | .add(Attributes.MAX_HEALTH, 24.0)
|
| 94 | .add(Attributes.ATTACK_DAMAGE, 5.0);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 99 | super.addAdditionalSaveData(output);
|
| 100 | if (this.isJohnny) {
|
| 101 | output.putBoolean("Johnny", true);
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public AbstractIllager.IllagerArmPose getArmPose() {
|
| 107 | if (this.isAggressive()) {
|
| 108 | return AbstractIllager.IllagerArmPose.ATTACKING;
|
| 109 | } else {
|
| 110 | return this.isCelebrating() ? AbstractIllager.IllagerArmPose.CELEBRATING : AbstractIllager.IllagerArmPose.CROSSED;
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 116 | super.readAdditionalSaveData(input);
|
| 117 | this.isJohnny = input.getBooleanOr("Johnny", false);
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public SoundEvent getCelebrateSound() {
|
| 122 | return SoundEvents.VINDICATOR_CELEBRATE;
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 127 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 128 | ) {
|
| 129 | SpawnGroupData spawnGroupData = super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 130 | this.getNavigation().setCanOpenDoors(true);
|
| 131 | RandomSource random = level.getRandom();
|
| 132 | this.populateDefaultEquipmentSlots(random, difficulty);
|
| 133 | this.populateDefaultEquipmentEnchantments(level, random, difficulty);
|
| 134 | return spawnGroupData;
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | protected void populateDefaultEquipmentSlots(final RandomSource random, final DifficultyInstance difficulty) {
|
| 139 | if (this.getCurrentRaid() == null) {
|
| 140 | this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.IRON_AXE));
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public void setCustomName(final @Nullable Component name) {
|
| 146 | super.setCustomName(name);
|
| 147 | if (!this.isJohnny && name != null && name.getString().equals("Johnny")) {
|
| 148 | this.isJohnny = true;
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | protected SoundEvent getAmbientSound() {
|
| 154 | return SoundEvents.VINDICATOR_AMBIENT;
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | protected SoundEvent getDeathSound() {
|
| 159 | return SoundEvents.VINDICATOR_DEATH;
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 164 | return SoundEvents.VINDICATOR_HURT;
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public void applyRaidBuffs(final ServerLevel level, final int wave, final boolean isCaptain) {
|
| 169 | ItemStack axe = new ItemStack(Items.IRON_AXE);
|
| 170 | Raid raid = this.getCurrentRaid();
|
| 171 | boolean shouldEnchant = this.random.nextFloat() <= raid.getEnchantOdds();
|
| 172 | if (shouldEnchant) {
|
| 173 | ResourceKey<EnchantmentProvider> provider = wave > raid.getNumGroups(Difficulty.NORMAL)
|
| 174 | ? VanillaEnchantmentProviders.RAID_VINDICATOR_POST_WAVE_5
|
| 175 | : VanillaEnchantmentProviders.RAID_VINDICATOR;
|
| 176 | EnchantmentHelper.enchantItemFromProvider(axe, level.registryAccess(), provider, level.getCurrentDifficultyAt(this.blockPosition()), this.random);
|
| 177 | }
|
| 178 |
|
| 179 | this.setItemSlot(EquipmentSlot.MAINHAND, axe);
|
| 180 | }
|
| 181 |
|
| 182 | private static class VindicatorBreakDoorGoal extends BreakDoorGoal {
|
| 183 | public VindicatorBreakDoorGoal(final Mob mob) {
|
| 184 | super(mob, 6, Vindicator.DOOR_BREAKING_PREDICATE);
|
| 185 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | public boolean canContinueToUse() {
|
| 190 | Vindicator vindicator = (Vindicator)this.mob;
|
| 191 | return vindicator.hasActiveRaid() && super.canContinueToUse();
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | public boolean canUse() {
|
| 196 | Vindicator vindicator = (Vindicator)this.mob;
|
| 197 | return vindicator.hasActiveRaid() && vindicator.random.nextInt(reducedTickDelay(10)) == 0 && super.canUse();
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public void start() {
|
| 202 | super.start();
|
| 203 | this.mob.setNoActionTime(0);
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | private static class VindicatorJohnnyAttackGoal extends NearestAttackableTargetGoal<LivingEntity> {
|
| 208 | public VindicatorJohnnyAttackGoal(final Vindicator mob) {
|
| 209 | super(mob, LivingEntity.class, 0, true, true, (target, level) -> target.attackable());
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public boolean canUse() {
|
| 214 | return ((Vindicator)this.mob).isJohnny && super.canUse();
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public void start() {
|
| 219 | super.start();
|
| 220 | this.mob.setNoActionTime(0);
|
| 221 | }
|
| 222 | }
|
| 223 | }
|
| 224 |
|