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

407 lines
14 KiB
Java

package net.minecraft.world.item;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.tags.Tag;
import com.google.common.collect.HashMultimap;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import com.google.common.collect.Multimap;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.core.NonNullList;
import net.minecraft.world.phys.Vec3;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.level.ClipContext;
import java.util.List;
import net.minecraft.world.entity.Entity;
import net.minecraft.Util;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import com.google.common.collect.Maps;
import net.minecraft.core.Registry;
import net.minecraft.world.food.FoodProperties;
import javax.annotation.Nullable;
import net.minecraft.resources.ResourceLocation;
import java.util.Random;
import java.util.UUID;
import net.minecraft.world.level.block.Block;
import java.util.Map;
import net.minecraft.world.level.ItemLike;
public class Item implements ItemLike {
public static final Map<Block, Item> BY_BLOCK;
private static final ItemPropertyFunction PROPERTY_DAMAGED;
private static final ItemPropertyFunction PROPERTY_DAMAGE;
private static final ItemPropertyFunction PROPERTY_LEFTHANDED;
private static final ItemPropertyFunction PROPERTY_COOLDOWN;
private static final ItemPropertyFunction PROPERTY_CUSTOM_MODEL_DATA;
protected static final UUID BASE_ATTACK_DAMAGE_UUID;
protected static final UUID BASE_ATTACK_SPEED_UUID;
protected static final Random random;
private final Map<ResourceLocation, ItemPropertyFunction> properties;
protected final CreativeModeTab category;
private final Rarity rarity;
private final int maxStackSize;
private final int maxDamage;
private final Item craftingRemainingItem;
@Nullable
private String descriptionId;
@Nullable
private final FoodProperties foodProperties;
public static int getId(final Item bef) {
return (bef == null) ? 0 : Registry.ITEM.getId(bef);
}
public static Item byId(final int integer) {
return Registry.ITEM.byId(integer);
}
@Deprecated
public static Item byBlock(final Block bpe) {
return Item.BY_BLOCK.getOrDefault(bpe, Items.AIR);
}
public Item(final Properties a) {
this.properties = Maps.newHashMap();
this.addProperty(new ResourceLocation("lefthanded"), Item.PROPERTY_LEFTHANDED);
this.addProperty(new ResourceLocation("cooldown"), Item.PROPERTY_COOLDOWN);
this.addProperty(new ResourceLocation("custom_model_data"), Item.PROPERTY_CUSTOM_MODEL_DATA);
this.category = a.category;
this.rarity = a.rarity;
this.craftingRemainingItem = a.craftingRemainingItem;
this.maxDamage = a.maxDamage;
this.maxStackSize = a.maxStackSize;
this.foodProperties = a.foodProperties;
if (this.maxDamage > 0) {
this.addProperty(new ResourceLocation("damaged"), Item.PROPERTY_DAMAGED);
this.addProperty(new ResourceLocation("damage"), Item.PROPERTY_DAMAGE);
}
}
public void onUseTick(final Level bjt, final LivingEntity akw, final ItemStack bek, final int integer) {
}
@Nullable
public ItemPropertyFunction getProperty(final ResourceLocation sm) {
return this.properties.get(sm);
}
public boolean hasProperties() {
return !this.properties.isEmpty();
}
public boolean verifyTagAfterLoad(final CompoundTag jt) {
return false;
}
public boolean canAttackBlock(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg) {
return true;
}
@Override
public Item asItem() {
return this;
}
public final void addProperty(final ResourceLocation sm, final ItemPropertyFunction bej) {
this.properties.put(sm, bej);
}
public InteractionResult useOn(final UseOnContext bfw) {
return InteractionResult.PASS;
}
public float getDestroySpeed(final ItemStack bek, final BlockState byg) {
return 1.0f;
}
public InteractionResultHolder<ItemStack> use(final Level bjt, final Player ayg, final InteractionHand ajh) {
if (!this.isEdible()) {
return InteractionResultHolder.<ItemStack>pass(ayg.getItemInHand(ajh));
}
final ItemStack bek5 = ayg.getItemInHand(ajh);
if (ayg.canEat(this.getFoodProperties().canAlwaysEat())) {
ayg.startUsingItem(ajh);
return InteractionResultHolder.<ItemStack>consume(bek5);
}
return InteractionResultHolder.<ItemStack>fail(bek5);
}
public ItemStack finishUsingItem(final ItemStack bek, final Level bjt, final LivingEntity akw) {
if (this.isEdible()) {
return akw.eat(bjt, bek);
}
return bek;
}
public final int getMaxStackSize() {
return this.maxStackSize;
}
public final int getMaxDamage() {
return this.maxDamage;
}
public boolean canBeDepleted() {
return this.maxDamage > 0;
}
public boolean hurtEnemy(final ItemStack bek, final LivingEntity akw2, final LivingEntity akw3) {
return false;
}
public boolean mineBlock(final ItemStack bek, final Level bjt, final BlockState byg, final BlockPos fk, final LivingEntity akw) {
return false;
}
public boolean canDestroySpecial(final BlockState byg) {
return false;
}
public boolean interactEnemy(final ItemStack bek, final Player ayg, final LivingEntity akw, final InteractionHand ajh) {
return false;
}
public Component getDescription() {
return new TranslatableComponent(this.getDescriptionId(), new Object[0]);
}
@Override
public String toString() {
return Registry.ITEM.getKey(this).getPath();
}
protected String getOrCreateDescriptionId() {
if (this.descriptionId == null) {
this.descriptionId = Util.makeDescriptionId("item", Registry.ITEM.getKey(this));
}
return this.descriptionId;
}
public String getDescriptionId() {
return this.getOrCreateDescriptionId();
}
public String getDescriptionId(final ItemStack bek) {
return this.getDescriptionId();
}
public boolean shouldOverrideMultiplayerNbt() {
return true;
}
@Nullable
public final Item getCraftingRemainingItem() {
return this.craftingRemainingItem;
}
public boolean hasCraftingRemainingItem() {
return this.craftingRemainingItem != null;
}
public void inventoryTick(final ItemStack bek, final Level bjt, final Entity akn, final int integer, final boolean boolean5) {
}
public void onCraftedBy(final ItemStack bek, final Level bjt, final Player ayg) {
}
public boolean isComplex() {
return false;
}
public UseAnim getUseAnimation(final ItemStack bek) {
return bek.getItem().isEdible() ? UseAnim.EAT : UseAnim.NONE;
}
public int getUseDuration(final ItemStack bek) {
if (bek.getItem().isEdible()) {
return this.getFoodProperties().isFastFood() ? 16 : 32;
}
return 0;
}
public void releaseUsing(final ItemStack bek, final Level bjt, final LivingEntity akw, final int integer) {
}
public void appendHoverText(final ItemStack bek, @Nullable final Level bjt, final List<Component> list, final TooltipFlag bft) {
}
public Component getName(final ItemStack bek) {
return new TranslatableComponent(this.getDescriptionId(bek), new Object[0]);
}
public boolean isFoil(final ItemStack bek) {
return bek.isEnchanted();
}
public Rarity getRarity(final ItemStack bek) {
if (!bek.isEnchanted()) {
return this.rarity;
}
switch (this.rarity) {
case COMMON:
case UNCOMMON: {
return Rarity.RARE;
}
case RARE: {
return Rarity.EPIC;
}
default: {
return this.rarity;
}
}
}
public boolean isEnchantable(final ItemStack bek) {
return this.getMaxStackSize() == 1 && this.canBeDepleted();
}
protected static HitResult getPlayerPOVHitResult(final Level bjt, final Player ayg, final ClipContext.Fluid b) {
final float float4 = ayg.xRot;
final float float5 = ayg.yRot;
final Vec3 cvi6 = ayg.getEyePosition(1.0f);
final float float6 = Mth.cos(-float5 * 0.017453292f - 3.1415927f);
final float float7 = Mth.sin(-float5 * 0.017453292f - 3.1415927f);
final float float8 = -Mth.cos(-float4 * 0.017453292f);
final float float9 = Mth.sin(-float4 * 0.017453292f);
final float float10 = float7 * float8;
final float float11 = float9;
final float float12 = float6 * float8;
final double double14 = 5.0;
final Vec3 cvi7 = cvi6.add(float10 * 5.0, float11 * 5.0, float12 * 5.0);
return bjt.clip(new ClipContext(cvi6, cvi7, ClipContext.Block.OUTLINE, b, ayg));
}
public int getEnchantmentValue() {
return 0;
}
public void fillItemCategory(final CreativeModeTab bda, final NonNullList<ItemStack> fy) {
if (this.allowdedIn(bda)) {
fy.add(new ItemStack(this));
}
}
protected boolean allowdedIn(final CreativeModeTab bda) {
final CreativeModeTab bda2 = this.getItemCategory();
return bda2 != null && (bda == CreativeModeTab.TAB_SEARCH || bda == bda2);
}
@Nullable
public final CreativeModeTab getItemCategory() {
return this.category;
}
public boolean isValidRepairItem(final ItemStack bek1, final ItemStack bek2) {
return false;
}
public Multimap<String, AttributeModifier> getDefaultAttributeModifiers(final EquipmentSlot aks) {
return HashMultimap.create();
}
public boolean useOnRelease(final ItemStack bek) {
return bek.getItem() == Items.CROSSBOW;
}
public ItemStack getDefaultInstance() {
return new ItemStack(this);
}
public boolean is(final Tag<Item> aaz) {
return aaz.contains(this);
}
public boolean isEdible() {
return this.foodProperties != null;
}
@Nullable
public FoodProperties getFoodProperties() {
return this.foodProperties;
}
public SoundEvent getDrinkingSound() {
return SoundEvents.GENERIC_DRINK;
}
public SoundEvent getEatingSound() {
return SoundEvents.GENERIC_EAT;
}
static {
BY_BLOCK = Maps.newHashMap();
PROPERTY_DAMAGED = ((bek, bjt, akw) -> bek.isDamaged() ? 1.0f : 0.0f);
PROPERTY_DAMAGE = ((bek, bjt, akw) -> Mth.clamp(bek.getDamageValue() / (float)bek.getMaxDamage(), 0.0f, 1.0f));
PROPERTY_LEFTHANDED = ((bek, bjt, akw) -> (akw == null || akw.getMainArm() == HumanoidArm.RIGHT) ? 0.0f : 1.0f);
PROPERTY_COOLDOWN = ((bek, bjt, akw) -> (akw instanceof Player) ? akw.getCooldowns().getCooldownPercent(bek.getItem(), 0.0f) : 0.0f);
PROPERTY_CUSTOM_MODEL_DATA = ((bek, bjt, akw) -> bek.hasTag() ? ((float)bek.getTag().getInt("CustomModelData")) : 0.0f);
BASE_ATTACK_DAMAGE_UUID = UUID.fromString("CB3F55D3-645C-4F38-A497-9C13A33DB5CF");
BASE_ATTACK_SPEED_UUID = UUID.fromString("FA233E1C-4180-4865-B01B-BCCE9785ACA3");
random = new Random();
}
public static class Properties {
private int maxStackSize;
private int maxDamage;
private Item craftingRemainingItem;
private CreativeModeTab category;
private Rarity rarity;
private FoodProperties foodProperties;
public Properties() {
this.maxStackSize = 64;
this.rarity = Rarity.COMMON;
}
public Properties food(final FoodProperties bah) {
this.foodProperties = bah;
return this;
}
public Properties stacksTo(final int integer) {
if (this.maxDamage > 0) {
throw new RuntimeException("Unable to have damage AND stack.");
}
this.maxStackSize = integer;
return this;
}
public Properties defaultDurability(final int integer) {
return (this.maxDamage == 0) ? this.durability(integer) : this;
}
public Properties durability(final int integer) {
this.maxDamage = integer;
this.maxStackSize = 1;
return this;
}
public Properties craftRemainder(final Item bef) {
this.craftingRemainingItem = bef;
return this;
}
public Properties tab(final CreativeModeTab bda) {
this.category = bda;
return this;
}
public Properties rarity(final Rarity bex) {
this.rarity = bex;
return this;
}
}
}