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.EntityType;
import net.minecraft.world.entity.monster.Skeleton;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.entity.global.LightningBolt;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.ai.goal.Goal;

public class SkeletonTrapGoal extends Goal {
    private final SkeletonHorse horse;
    
    public SkeletonTrapGoal(final SkeletonHorse auj) {
        this.horse = auj;
    }
    
    @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 ajj2 = this.horse.level.getCurrentDifficultyAt(new BlockPos(this.horse));
        this.horse.setTrap(false);
        this.horse.setTamed(true);
        this.horse.setAge(0);
        ((ServerLevel)this.horse.level).addGlobalEntity(new LightningBolt(this.horse.level, this.horse.getX(), this.horse.getY(), this.horse.getZ(), true));
        final Skeleton axg3 = this.createSkeleton(ajj2, this.horse);
        axg3.startRiding(this.horse);
        for (int integer4 = 0; integer4 < 3; ++integer4) {
            final AbstractHorse aue5 = this.createHorse(ajj2);
            final Skeleton axg4 = this.createSkeleton(ajj2, aue5);
            axg4.startRiding(aue5);
            aue5.push(this.horse.getRandom().nextGaussian() * 0.5, 0.0, this.horse.getRandom().nextGaussian() * 0.5);
        }
    }
    
    private AbstractHorse createHorse(final DifficultyInstance ajj) {
        final SkeletonHorse auj3 = EntityType.SKELETON_HORSE.create(this.horse.level);
        auj3.finalizeSpawn(this.horse.level, ajj, MobSpawnType.TRIGGERED, null, null);
        auj3.setPos(this.horse.getX(), this.horse.getY(), this.horse.getZ());
        auj3.invulnerableTime = 60;
        auj3.setPersistenceRequired();
        auj3.setTamed(true);
        auj3.setAge(0);
        auj3.level.addFreshEntity(auj3);
        return auj3;
    }
    
    private Skeleton createSkeleton(final DifficultyInstance ajj, final AbstractHorse aue) {
        final Skeleton axg4 = EntityType.SKELETON.create(aue.level);
        axg4.finalizeSpawn(aue.level, ajj, MobSpawnType.TRIGGERED, null, null);
        axg4.setPos(aue.getX(), aue.getY(), aue.getZ());
        axg4.invulnerableTime = 60;
        axg4.setPersistenceRequired();
        if (axg4.getItemBySlot(EquipmentSlot.HEAD).isEmpty()) {
            axg4.setItemSlot(EquipmentSlot.HEAD, new ItemStack(Items.IRON_HELMET));
        }
        axg4.setItemSlot(EquipmentSlot.MAINHAND, EnchantmentHelper.enchantItem(axg4.getRandom(), axg4.getMainHandItem(), (int)(5.0f + ajj.getSpecialMultiplier() * axg4.getRandom().nextInt(18)), false));
        axg4.setItemSlot(EquipmentSlot.HEAD, EnchantmentHelper.enchantItem(axg4.getRandom(), axg4.getItemBySlot(EquipmentSlot.HEAD), (int)(5.0f + ajj.getSpecialMultiplier() * axg4.getRandom().nextInt(18)), false));
        axg4.level.addFreshEntity(axg4);
        return axg4;
    }
}
