package net.minecraft.world.item;

import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
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.entity.ai.attributes.Attributes;
import com.google.common.collect.ImmutableMultimap;
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.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attribute;
import com.google.common.collect.Multimap;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.core.dispenser.DispenseItemBehavior;
import java.util.UUID;

public class ArmorItem extends Item implements Wearable {
    private static final UUID[] ARMOR_MODIFIER_UUID_PER_SLOT;
    public static final DispenseItemBehavior DISPENSE_ITEM_BEHAVIOR;
    protected final EquipmentSlot slot;
    private final int defense;
    private final float toughness;
    protected final float knockbackResistance;
    protected final ArmorMaterial material;
    private final Multimap<Attribute, AttributeModifier> defaultModifiers;
    
    public static boolean dispenseArmor(final BlockSource fv, final ItemStack bki) {
        final BlockPos fu3 = fv.getPos().relative(fv.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING));
        final List<LivingEntity> list4 = fv.getLevel().<LivingEntity>getEntitiesOfClass(LivingEntity.class, new AABB(fu3), EntitySelector.NO_SPECTATORS.and(new EntitySelector.MobCanWearArmorEntitySelector(bki)));
        if (list4.isEmpty()) {
            return false;
        }
        final LivingEntity aoy5 = list4.get(0);
        final EquipmentSlot aor6 = Mob.getEquipmentSlotForItem(bki);
        final ItemStack bki2 = bki.split(1);
        aoy5.setItemSlot(aor6, bki2);
        if (aoy5 instanceof Mob) {
            ((Mob)aoy5).setDropChance(aor6, 2.0f);
            ((Mob)aoy5).setPersistenceRequired();
        }
        return true;
    }
    
    public ArmorItem(final ArmorMaterial bie, final EquipmentSlot aor, final Properties a) {
        super(a.defaultDurability(bie.getDurabilityForSlot(aor)));
        this.material = bie;
        this.slot = aor;
        this.defense = bie.getDefenseForSlot(aor);
        this.toughness = bie.getToughness();
        this.knockbackResistance = bie.getKnockbackResistance();
        DispenserBlock.registerBehavior(this, ArmorItem.DISPENSE_ITEM_BEHAVIOR);
        final ImmutableMultimap.Builder<Attribute, AttributeModifier> builder5 = ImmutableMultimap.<Attribute, AttributeModifier>builder();
        final UUID uUID6 = ArmorItem.ARMOR_MODIFIER_UUID_PER_SLOT[aor.getIndex()];
        builder5.put(Attributes.ARMOR, new AttributeModifier(uUID6, "Armor modifier", this.defense, AttributeModifier.Operation.ADDITION));
        builder5.put(Attributes.ARMOR_TOUGHNESS, new AttributeModifier(uUID6, "Armor toughness", this.toughness, AttributeModifier.Operation.ADDITION));
        if (bie == ArmorMaterials.NETHERITE) {
            builder5.put(Attributes.KNOCKBACK_RESISTANCE, new AttributeModifier(uUID6, "Armor knockback resistance", this.knockbackResistance, AttributeModifier.Operation.ADDITION));
        }
        this.defaultModifiers = builder5.build();
    }
    
    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 bki1, final ItemStack bki2) {
        return this.material.getRepairIngredient().test(bki2) || super.isValidRepairItem(bki1, bki2);
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bqb, final Player bec, final InteractionHand anf) {
        final ItemStack bki5 = bec.getItemInHand(anf);
        final EquipmentSlot aor6 = Mob.getEquipmentSlotForItem(bki5);
        final ItemStack bki6 = bec.getItemBySlot(aor6);
        if (bki6.isEmpty()) {
            bec.setItemSlot(aor6, bki5.copy());
            bki5.setCount(0);
            return InteractionResultHolder.<ItemStack>sidedSuccess(bki5, bqb.isClientSide());
        }
        return InteractionResultHolder.<ItemStack>fail(bki5);
    }
    
    @Override
    public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(final EquipmentSlot aor) {
        if (aor == this.slot) {
            return this.defaultModifiers;
        }
        return super.getDefaultAttributeModifiers(aor);
    }
    
    public int getDefense() {
        return this.defense;
    }
    
    public float getToughness() {
        return this.toughness;
    }
    
    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 fv, final ItemStack bki) {
                return ArmorItem.dispenseArmor(fv, bki) ? bki : super.execute(fv, bki);
            }
        };
    }
}
