package net.minecraft.world.entity.animal.horse;

import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.SpawnGroupData;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.monster.Skeleton;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LightningBolt;
import net.minecraft.world.entity.ai.goal.Goal;

public class SkeletonTrapGoal extends Goal {
    private final SkeletonHorse horse;
    
    public SkeletonTrapGoal(final SkeletonHorse azs) {
        this.horse = azs;
    }
    
    @Override
    public boolean canUse() {
        return this.horse.level.hasNearbyAlivePlayer(this.horse.getX(), this.horse.getY(), this.horse.getZ(), 10.0);
    }
    
    @Override
    public void tick() {
        final DifficultyInstance ane2 = this.horse.level.getCurrentDifficultyAt(this.horse.blockPosition());
        this.horse.setTrap(false);
        this.horse.setTamed(true);
        this.horse.setAge(0);
        final LightningBolt aox3 = EntityType.LIGHTNING_BOLT.create(this.horse.level);
        aox3.moveTo(this.horse.getX(), this.horse.getY(), this.horse.getZ());
        aox3.setVisualOnly(true);
        this.horse.level.addFreshEntity(aox3);
        final Skeleton bcj4 = this.createSkeleton(ane2, this.horse);
        bcj4.startRiding(this.horse);
        for (int integer5 = 0; integer5 < 3; ++integer5) {
            final AbstractHorse azm6 = this.createHorse(ane2);
            final Skeleton bcj5 = this.createSkeleton(ane2, azm6);
            bcj5.startRiding(azm6);
            azm6.push(this.horse.getRandom().nextGaussian() * 0.5, 0.0, this.horse.getRandom().nextGaussian() * 0.5);
        }
    }
    
    private AbstractHorse createHorse(final DifficultyInstance ane) {
        final SkeletonHorse azs3 = EntityType.SKELETON_HORSE.create(this.horse.level);
        azs3.finalizeSpawn(this.horse.level, ane, MobSpawnType.TRIGGERED, null, null);
        azs3.setPos(this.horse.getX(), this.horse.getY(), this.horse.getZ());
        azs3.invulnerableTime = 60;
        azs3.setPersistenceRequired();
        azs3.setTamed(true);
        azs3.setAge(0);
        azs3.level.addFreshEntity(azs3);
        return azs3;
    }
    
    private Skeleton createSkeleton(final DifficultyInstance ane, final AbstractHorse azm) {
        final Skeleton bcj4 = EntityType.SKELETON.create(azm.level);
        bcj4.finalizeSpawn(azm.level, ane, MobSpawnType.TRIGGERED, null, null);
        bcj4.setPos(azm.getX(), azm.getY(), azm.getZ());
        bcj4.invulnerableTime = 60;
        bcj4.setPersistenceRequired();
        if (bcj4.getItemBySlot(EquipmentSlot.HEAD).isEmpty()) {
            bcj4.setItemSlot(EquipmentSlot.HEAD, new ItemStack(Items.IRON_HELMET));
        }
        bcj4.setItemSlot(EquipmentSlot.MAINHAND, EnchantmentHelper.enchantItem(bcj4.getRandom(), bcj4.getMainHandItem(), (int)(5.0f + ane.getSpecialMultiplier() * bcj4.getRandom().nextInt(18)), false));
        bcj4.setItemSlot(EquipmentSlot.HEAD, EnchantmentHelper.enchantItem(bcj4.getRandom(), bcj4.getItemBySlot(EquipmentSlot.HEAD), (int)(5.0f + ane.getSpecialMultiplier() * bcj4.getRandom().nextInt(18)), false));
        bcj4.level.addFreshEntity(bcj4);
        return bcj4;
    }
}
