| 1 | package net.minecraft.world.entity.monster.skeleton;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.sounds.SoundEvent;
|
| 6 | import net.minecraft.sounds.SoundEvents;
|
| 7 | import net.minecraft.tags.ItemTags;
|
| 8 | import net.minecraft.tags.TagKey;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.util.SpecialDates;
|
| 11 | import net.minecraft.world.Difficulty;
|
| 12 | import net.minecraft.world.DifficultyInstance;
|
| 13 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 14 | import net.minecraft.world.entity.EntityType;
|
| 15 | import net.minecraft.world.entity.EquipmentSlot;
|
| 16 | import net.minecraft.world.entity.LivingEntity;
|
| 17 | import net.minecraft.world.entity.PathfinderMob;
|
| 18 | import net.minecraft.world.entity.SpawnGroupData;
|
| 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.AvoidEntityGoal;
|
| 22 | import net.minecraft.world.entity.ai.goal.FleeSunGoal;
|
| 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.RandomLookAroundGoal;
|
| 26 | import net.minecraft.world.entity.ai.goal.RangedBowAttackGoal;
|
| 27 | import net.minecraft.world.entity.ai.goal.RestrictSunGoal;
|
| 28 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 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.animal.golem.IronGolem;
|
| 32 | import net.minecraft.world.entity.animal.turtle.Turtle;
|
| 33 | import net.minecraft.world.entity.animal.wolf.Wolf;
|
| 34 | import net.minecraft.world.entity.monster.Monster;
|
| 35 | import net.minecraft.world.entity.monster.RangedAttackMob;
|
| 36 | import net.minecraft.world.entity.player.Player;
|
| 37 | import net.minecraft.world.entity.projectile.Projectile;
|
| 38 | import net.minecraft.world.entity.projectile.ProjectileUtil;
|
| 39 | import net.minecraft.world.entity.projectile.arrow.AbstractArrow;
|
| 40 | import net.minecraft.world.item.Item;
|
| 41 | import net.minecraft.world.item.ItemStack;
|
| 42 | import net.minecraft.world.item.Items;
|
| 43 | import net.minecraft.world.level.Level;
|
| 44 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 45 | import net.minecraft.world.level.block.Blocks;
|
| 46 | import net.minecraft.world.level.block.state.BlockState;
|
| 47 | import net.minecraft.world.level.storage.ValueInput;
|
| 48 | import org.jspecify.annotations.Nullable;
|
| 49 |
|
| 50 | public abstract class AbstractSkeleton extends Monster implements RangedAttackMob {
|
| 51 | private static final int HARD_ATTACK_INTERVAL = 20;
|
| 52 | private static final int NORMAL_ATTACK_INTERVAL = 40;
|
| 53 | protected static final int INCREASED_HARD_ATTACK_INTERVAL = 50;
|
| 54 | protected static final int INCREASED_NORMAL_ATTACK_INTERVAL = 70;
|
| 55 | private final RangedBowAttackGoal<AbstractSkeleton> bowGoal = new RangedBowAttackGoal<>(this, 1.0, 20, 15.0F);
|
| 56 | private final MeleeAttackGoal meleeGoal = new MeleeAttackGoal(this, 1.2, false) {
|
| 57 | @Override
|
| 58 | public void stop() {
|
| 59 | super.stop();
|
| 60 | AbstractSkeleton.this.setAggressive(false);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public void start() {
|
| 65 | super.start();
|
| 66 | AbstractSkeleton.this.setAggressive(true);
|
| 67 | }
|
| 68 | };
|
| 69 |
|
| 70 | protected AbstractSkeleton(final EntityType<? extends AbstractSkeleton> type, final Level level) {
|
| 71 | super(type, level);
|
| 72 | this.reassessWeaponGoal();
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected void registerGoals() {
|
| 77 | this.goalSelector.addGoal(2, new RestrictSunGoal(this));
|
| 78 | this.goalSelector.addGoal(3, new FleeSunGoal(this, 1.0));
|
| 79 | this.goalSelector.addGoal(3, new AvoidEntityGoal<>(this, Wolf.class, 6.0F, 1.0, 1.2));
|
| 80 | this.goalSelector.addGoal(5, new WaterAvoidingRandomStrollGoal(this, 1.0));
|
| 81 | this.goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 8.0F));
|
| 82 | this.goalSelector.addGoal(6, new RandomLookAroundGoal(this));
|
| 83 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this));
|
| 84 | this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.class, true));
|
| 85 | this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, IronGolem.class, true));
|
| 86 | this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, Turtle.class, 10, true, false, Turtle.BABY_ON_LAND_SELECTOR));
|
| 87 | }
|
| 88 |
|
| 89 | public static AttributeSupplier.Builder createAttributes() {
|
| 90 | return Monster.createMonsterAttributes().add(Attributes.MOVEMENT_SPEED, 0.25);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 95 | this.playSound(this.getStepSound(), 0.15F, 1.0F);
|
| 96 | }
|
| 97 |
|
| 98 | protected abstract SoundEvent getStepSound();
|
| 99 |
|
| 100 | @Override
|
| 101 | public void rideTick() {
|
| 102 | super.rideTick();
|
| 103 | if (this.getControlledVehicle() instanceof PathfinderMob entity) {
|
| 104 | this.yBodyRot = entity.yBodyRot;
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected void populateDefaultEquipmentSlots(final RandomSource random, final DifficultyInstance difficulty) {
|
| 110 | super.populateDefaultEquipmentSlots(random, difficulty);
|
| 111 | this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.BOW));
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 116 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 117 | ) {
|
| 118 | groupData = super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 119 | RandomSource random = level.getRandom();
|
| 120 | this.populateDefaultEquipmentSlots(random, difficulty);
|
| 121 | this.populateDefaultEquipmentEnchantments(level, random, difficulty);
|
| 122 | this.reassessWeaponGoal();
|
| 123 | this.setCanPickUpLoot(random.nextFloat() < 0.55F * difficulty.getSpecialMultiplier());
|
| 124 | if (this.getItemBySlot(EquipmentSlot.HEAD).isEmpty() && SpecialDates.isHalloween() && random.nextFloat() < 0.25F) {
|
| 125 | this.setItemSlot(EquipmentSlot.HEAD, new ItemStack(random.nextFloat() < 0.1F ? Blocks.JACK_O_LANTERN : Blocks.CARVED_PUMPKIN));
|
| 126 | this.setDropChance(EquipmentSlot.HEAD, 0.0F);
|
| 127 | }
|
| 128 |
|
| 129 | return groupData;
|
| 130 | }
|
| 131 |
|
| 132 | public void reassessWeaponGoal() {
|
| 133 | if (this.level() != null && !this.level().isClientSide()) {
|
| 134 | this.goalSelector.removeGoal(this.meleeGoal);
|
| 135 | this.goalSelector.removeGoal(this.bowGoal);
|
| 136 | ItemStack usedWeapon = this.getItemInHand(ProjectileUtil.getWeaponHoldingHand(this, Items.BOW));
|
| 137 | if (usedWeapon.is(Items.BOW)) {
|
| 138 | int minAttackInterval = this.getHardAttackInterval();
|
| 139 | if (this.level().getDifficulty() != Difficulty.HARD) {
|
| 140 | minAttackInterval = this.getAttackInterval();
|
| 141 | }
|
| 142 |
|
| 143 | this.bowGoal.setMinAttackInterval(minAttackInterval);
|
| 144 | this.goalSelector.addGoal(4, this.bowGoal);
|
| 145 | } else {
|
| 146 | this.goalSelector.addGoal(4, this.meleeGoal);
|
| 147 | }
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | protected int getHardAttackInterval() {
|
| 152 | return 20;
|
| 153 | }
|
| 154 |
|
| 155 | protected int getAttackInterval() {
|
| 156 | return 40;
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | public void performRangedAttack(final LivingEntity target, final float power) {
|
| 161 | ItemStack bowItem = this.getItemInHand(ProjectileUtil.getWeaponHoldingHand(this, Items.BOW));
|
| 162 | ItemStack projectile = this.getProjectile(bowItem);
|
| 163 | AbstractArrow arrow = this.getArrow(projectile, power, bowItem);
|
| 164 | double xd = target.getX() - this.getX();
|
| 165 | double yd = target.getY(0.3333333333333333) - arrow.getY();
|
| 166 | double zd = target.getZ() - this.getZ();
|
| 167 | double distanceToTarget = Math.sqrt(xd * xd + zd * zd);
|
| 168 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 169 | Projectile.spawnProjectileUsingShoot(arrow, serverLevel, projectile, xd, yd + distanceToTarget * 0.2F, zd, 1.6F, this.rangedAttackUncertainty(serverLevel));
|
| 170 | }
|
| 171 |
|
| 172 | this.playSound(SoundEvents.SKELETON_SHOOT, 1.0F, 1.0F / (this.getRandom().nextFloat() * 0.4F + 0.8F));
|
| 173 | }
|
| 174 |
|
| 175 | protected AbstractArrow getArrow(final ItemStack projectile, final float power, final @Nullable ItemStack firingWeapon) {
|
| 176 | return ProjectileUtil.getMobArrow(this, projectile, power, firingWeapon);
|
| 177 | }
|
| 178 |
|
| 179 | @Override
|
| 180 | public boolean canUseNonMeleeWeapon(final ItemStack item) {
|
| 181 | return item.getItem() == Items.BOW;
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | public TagKey<Item> getPreferredWeaponType() {
|
| 186 | return ItemTags.SKELETON_PREFERRED_WEAPONS;
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 191 | super.readAdditionalSaveData(input);
|
| 192 | this.reassessWeaponGoal();
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public void onEquipItem(final EquipmentSlot slot, final ItemStack oldStack, final ItemStack stack) {
|
| 197 | super.onEquipItem(slot, oldStack, stack);
|
| 198 | if (!this.level().isClientSide()) {
|
| 199 | this.reassessWeaponGoal();
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | public boolean isShaking() {
|
| 204 | return this.isFullyFrozen();
|
| 205 | }
|
| 206 |
|
| 207 | @Override
|
| 208 | public boolean wantsToPickUp(final ServerLevel level, final ItemStack itemStack) {
|
| 209 | return itemStack.is(ItemTags.SPEARS) ? false : super.wantsToPickUp(level, itemStack);
|
| 210 | }
|
| 211 | }
|
| 212 |
|