package net.minecraft.world.item;

import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.world.entity.monster.SharedMonsterAttributes;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import com.google.common.collect.Multimap;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.ItemLike;
import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.Entity;
import java.util.function.Predicate;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.DispenserBlock;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockSource;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.core.dispenser.DispenseItemBehavior;
import java.util.UUID;

public class ArmorItem extends Item {
    private static final UUID[] ARMOR_MODIFIER_UUID_PER_SLOT;
    public static final DispenseItemBehavior DISPENSE_ITEM_BEHAVIOR;
    protected final EquipmentSlot slot;
    protected final int defense;
    protected final float toughness;
    protected final ArmorMaterial material;
    
    public static boolean dispenseArmor(final BlockSource fl, final ItemStack ben) {
        final BlockPos fk3 = fl.getPos().relative(fl.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING));
        final List<LivingEntity> list4 = fl.getLevel().<LivingEntity>getEntitiesOfClass(LivingEntity.class, new AABB(fk3), EntitySelector.NO_SPECTATORS.and(new EntitySelector.MobCanWearArmourEntitySelector(ben)));
        if (list4.isEmpty()) {
            return false;
        }
        final LivingEntity akz5 = list4.get(0);
        final EquipmentSlot akv6 = Mob.getEquipmentSlotForItem(ben);
        final ItemStack ben2 = ben.split(1);
        akz5.setItemSlot(akv6, ben2);
        if (akz5 instanceof Mob) {
            ((Mob)akz5).setDropChance(akv6, 2.0f);
            ((Mob)akz5).setPersistenceRequired();
        }
        return true;
    }
    
    public ArmorItem(final ArmorMaterial bch, final EquipmentSlot akv, final Properties a) {
        super(a.defaultDurability(bch.getDurabilityForSlot(akv)));
        this.material = bch;
        this.slot = akv;
        this.defense = bch.getDefenseForSlot(akv);
        this.toughness = bch.getToughness();
        DispenserBlock.registerBehavior(this, ArmorItem.DISPENSE_ITEM_BEHAVIOR);
    }
    
    public EquipmentSlot getSlot() {
        return this.slot;
    }
    
    @Override
    public int getEnchantmentValue() {
        return this.material.getEnchantmentValue();
    }
    
    public ArmorMaterial getMaterial() {
        return this.material;
    }
    
    @Override
    public boolean isValidRepairItem(final ItemStack ben1, final ItemStack ben2) {
        return this.material.getRepairIngredient().test(ben2) || super.isValidRepairItem(ben1, ben2);
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bjw, final Player ayj, final InteractionHand ajk) {
        final ItemStack ben5 = ayj.getItemInHand(ajk);
        final EquipmentSlot akv6 = Mob.getEquipmentSlotForItem(ben5);
        final ItemStack ben6 = ayj.getItemBySlot(akv6);
        if (ben6.isEmpty()) {
            ayj.setItemSlot(akv6, ben5.copy());
            ben5.setCount(0);
            return InteractionResultHolder.<ItemStack>success(ben5);
        }
        return InteractionResultHolder.<ItemStack>fail(ben5);
    }
    
    @Override
    public Multimap<String, AttributeModifier> getDefaultAttributeModifiers(final EquipmentSlot akv) {
        final Multimap<String, AttributeModifier> multimap3 = super.getDefaultAttributeModifiers(akv);
        if (akv == this.slot) {
            multimap3.put(SharedMonsterAttributes.ARMOR.getName(), new AttributeModifier(ArmorItem.ARMOR_MODIFIER_UUID_PER_SLOT[akv.getIndex()], "Armor modifier", this.defense, AttributeModifier.Operation.ADDITION));
            multimap3.put(SharedMonsterAttributes.ARMOR_TOUGHNESS.getName(), new AttributeModifier(ArmorItem.ARMOR_MODIFIER_UUID_PER_SLOT[akv.getIndex()], "Armor toughness", this.toughness, AttributeModifier.Operation.ADDITION));
        }
        return multimap3;
    }
    
    public int getDefense() {
        return this.defense;
    }
    
    static {
        ARMOR_MODIFIER_UUID_PER_SLOT = new UUID[] { UUID.fromString("845DB27C-C624-495F-8C9F-6020A9A58B6B"), UUID.fromString("D8499B04-0E66-4726-AB29-64469D734E0D"), UUID.fromString("9F3D476D-C118-4544-8365-64846904B48E"), UUID.fromString("2AD3F246-FEE1-4E67-B886-69FD380BB150") };
        DISPENSE_ITEM_BEHAVIOR = new DefaultDispenseItemBehavior() {
            @Override
            protected ItemStack execute(final BlockSource fl, final ItemStack ben) {
                return ArmorItem.dispenseArmor(fl, ben) ? ben : super.execute(fl, ben);
            }
        };
    }
}
