| 1 | package net.minecraft.world.entity.animal.equine;
|
| 2 |
|
| 3 | import net.minecraft.core.component.DataComponents;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.world.DifficultyInstance;
|
| 6 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 7 | import net.minecraft.world.entity.EntityTypes;
|
| 8 | import net.minecraft.world.entity.EquipmentSlot;
|
| 9 | import net.minecraft.world.entity.LightningBolt;
|
| 10 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 11 | import net.minecraft.world.entity.monster.skeleton.Skeleton;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.item.Items;
|
| 14 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 15 | import net.minecraft.world.item.enchantment.ItemEnchantments;
|
| 16 | import net.minecraft.world.item.enchantment.providers.VanillaEnchantmentProviders;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public class SkeletonTrapGoal extends Goal {
|
| 20 | private static final int LIGHTNING_INVULNERABLE_TICKS = 60;
|
| 21 | private final SkeletonHorse horse;
|
| 22 |
|
| 23 | public SkeletonTrapGoal(final SkeletonHorse horse) {
|
| 24 | this.horse = horse;
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public boolean canUse() {
|
| 29 | return this.horse.level().hasNearbyAlivePlayer(this.horse.getX(), this.horse.getY(), this.horse.getZ(), 10.0);
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public void tick() {
|
| 34 | ServerLevel level = (ServerLevel)this.horse.level();
|
| 35 | DifficultyInstance difficulty = level.getCurrentDifficultyAt(this.horse.blockPosition());
|
| 36 | this.horse.setTrap(false);
|
| 37 | this.horse.setTamed(true);
|
| 38 | this.horse.setAge(0);
|
| 39 | LightningBolt bolt = EntityTypes.LIGHTNING_BOLT.create(level, EntitySpawnReason.TRIGGERED);
|
| 40 | if (bolt != null) {
|
| 41 | bolt.snapTo(this.horse.getX(), this.horse.getY(), this.horse.getZ());
|
| 42 | bolt.setVisualOnly(true);
|
| 43 | level.addFreshEntity(bolt);
|
| 44 | Skeleton skeleton = this.createSkeleton(difficulty, this.horse);
|
| 45 | if (skeleton != null) {
|
| 46 | skeleton.startRiding(this.horse);
|
| 47 | level.addFreshEntityWithPassengers(skeleton);
|
| 48 |
|
| 49 | for (int i = 0; i < 3; i++) {
|
| 50 | AbstractHorse otherHorse = this.createHorse(difficulty);
|
| 51 | if (otherHorse != null) {
|
| 52 | Skeleton otherSkeleton = this.createSkeleton(difficulty, otherHorse);
|
| 53 | if (otherSkeleton != null) {
|
| 54 | otherSkeleton.startRiding(otherHorse);
|
| 55 | otherHorse.push(this.horse.getRandom().triangle(0.0, 1.1485), 0.0, this.horse.getRandom().triangle(0.0, 1.1485));
|
| 56 | level.addFreshEntityWithPassengers(otherHorse);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | private @Nullable AbstractHorse createHorse(final DifficultyInstance difficulty) {
|
| 65 | SkeletonHorse horse = EntityTypes.SKELETON_HORSE.create(this.horse.level(), EntitySpawnReason.TRIGGERED);
|
| 66 | if (horse != null) {
|
| 67 | horse.finalizeSpawn((ServerLevel)this.horse.level(), difficulty, EntitySpawnReason.TRIGGERED, null);
|
| 68 | horse.setPos(this.horse.getX(), this.horse.getY(), this.horse.getZ());
|
| 69 | horse.setInvulnerableTime(60);
|
| 70 | horse.setPersistenceRequired();
|
| 71 | horse.setTamed(true);
|
| 72 | horse.setAge(0);
|
| 73 | }
|
| 74 |
|
| 75 | return horse;
|
| 76 | }
|
| 77 |
|
| 78 | private @Nullable Skeleton createSkeleton(final DifficultyInstance difficulty, final AbstractHorse horse) {
|
| 79 | Skeleton skeleton = EntityTypes.SKELETON.create(horse.level(), EntitySpawnReason.TRIGGERED);
|
| 80 | if (skeleton != null) {
|
| 81 | skeleton.finalizeSpawn((ServerLevel)horse.level(), difficulty, EntitySpawnReason.TRIGGERED, null);
|
| 82 | skeleton.setPos(horse.getX(), horse.getY(), horse.getZ());
|
| 83 | skeleton.setInvulnerableTime(60);
|
| 84 | skeleton.setPersistenceRequired();
|
| 85 | if (skeleton.getItemBySlot(EquipmentSlot.HEAD).isEmpty()) {
|
| 86 | skeleton.setItemSlot(EquipmentSlot.HEAD, new ItemStack(Items.IRON_HELMET));
|
| 87 | }
|
| 88 |
|
| 89 | this.enchant(skeleton, EquipmentSlot.MAINHAND, difficulty);
|
| 90 | this.enchant(skeleton, EquipmentSlot.HEAD, difficulty);
|
| 91 | }
|
| 92 |
|
| 93 | return skeleton;
|
| 94 | }
|
| 95 |
|
| 96 | private void enchant(final Skeleton skeleton, final EquipmentSlot slot, final DifficultyInstance difficulty) {
|
| 97 | ItemStack stack = skeleton.getItemBySlot(slot);
|
| 98 | stack.set(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY);
|
| 99 | EnchantmentHelper.enchantItemFromProvider(
|
| 100 | stack, skeleton.level().registryAccess(), VanillaEnchantmentProviders.MOB_SPAWN_EQUIPMENT, difficulty, skeleton.getRandom()
|
| 101 | );
|
| 102 | skeleton.setItemSlot(slot, stack);
|
| 103 | }
|
| 104 | }
|
| 105 |
|