minecraft-source/src/net/minecraft/world/item/ArmorItem.java

117 lines
5.1 KiB
Java

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 bek) {
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(bek)));
if (list4.isEmpty()) {
return false;
}
final LivingEntity akw5 = list4.get(0);
final EquipmentSlot aks6 = Mob.getEquipmentSlotForItem(bek);
final ItemStack bek2 = bek.split(1);
akw5.setItemSlot(aks6, bek2);
if (akw5 instanceof Mob) {
((Mob)akw5).setDropChance(aks6, 2.0f);
((Mob)akw5).setPersistenceRequired();
}
return true;
}
public ArmorItem(final ArmorMaterial bce, final EquipmentSlot aks, final Properties a) {
super(a.defaultDurability(bce.getDurabilityForSlot(aks)));
this.material = bce;
this.slot = aks;
this.defense = bce.getDefenseForSlot(aks);
this.toughness = bce.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 bek1, final ItemStack bek2) {
return this.material.getRepairIngredient().test(bek2) || super.isValidRepairItem(bek1, bek2);
}
@Override
public InteractionResultHolder<ItemStack> use(final Level bjt, final Player ayg, final InteractionHand ajh) {
final ItemStack bek5 = ayg.getItemInHand(ajh);
final EquipmentSlot aks6 = Mob.getEquipmentSlotForItem(bek5);
final ItemStack bek6 = ayg.getItemBySlot(aks6);
if (bek6.isEmpty()) {
ayg.setItemSlot(aks6, bek5.copy());
bek5.setCount(0);
return InteractionResultHolder.<ItemStack>success(bek5);
}
return InteractionResultHolder.<ItemStack>fail(bek5);
}
@Override
public Multimap<String, AttributeModifier> getDefaultAttributeModifiers(final EquipmentSlot aks) {
final Multimap<String, AttributeModifier> multimap3 = super.getDefaultAttributeModifiers(aks);
if (aks == this.slot) {
multimap3.put(SharedMonsterAttributes.ARMOR.getName(), new AttributeModifier(ArmorItem.ARMOR_MODIFIER_UUID_PER_SLOT[aks.getIndex()], "Armor modifier", this.defense, AttributeModifier.Operation.ADDITION));
multimap3.put(SharedMonsterAttributes.ARMOR_TOUGHNESS.getName(), new AttributeModifier(ArmorItem.ARMOR_MODIFIER_UUID_PER_SLOT[aks.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 bek) {
return ArmorItem.dispenseArmor(fl, bek) ? bek : super.execute(fl, bek);
}
};
}
}