Paper/src/main/java/net/minecraft/server/ItemStack.java

490 Zeilen
15 KiB
Java

2011-01-14 14:31:10 +01:00
package net.minecraft.server;
2013-07-01 13:03:00 +02:00
import java.text.DecimalFormat;
2013-03-13 23:33:27 +01:00
import java.util.Random;
2013-11-04 14:07:38 +01:00
import net.minecraft.util.com.google.common.collect.HashMultimap;
import net.minecraft.util.com.google.common.collect.Multimap;
import org.bukkit.craftbukkit.util.CraftMagicNumbers; // CraftBukkit
2011-01-14 14:31:10 +01:00
public final class ItemStack {
2013-07-01 13:03:00 +02:00
public static final DecimalFormat a = new DecimalFormat("#.###");
2011-01-29 22:50:29 +01:00
public int count;
2013-07-01 13:03:00 +02:00
public int c;
2013-11-04 14:07:38 +01:00
private Item item;
2011-11-20 09:01:14 +01:00
public NBTTagCompound tag;
2011-09-26 09:07:06 +02:00
private int damage;
2013-07-01 13:03:00 +02:00
private EntityItemFrame g;
2011-01-14 14:31:10 +01:00
public ItemStack(Block block) {
this(block, 1);
}
2011-01-29 22:50:29 +01:00
public ItemStack(Block block, int i) {
2013-11-04 14:07:38 +01:00
this(block, i, 0);
2011-01-14 14:31:10 +01:00
}
2011-01-29 22:50:29 +01:00
public ItemStack(Block block, int i, int j) {
2013-11-04 14:07:38 +01:00
this(Item.getItemOf(block), i, j);
2011-01-14 14:31:10 +01:00
}
public ItemStack(Item item) {
2013-11-04 14:07:38 +01:00
this(item, 1);
2011-01-14 14:31:10 +01:00
}
2011-01-29 22:50:29 +01:00
public ItemStack(Item item, int i) {
2013-11-04 14:07:38 +01:00
this(item, i, 0);
2011-01-14 14:31:10 +01:00
}
2011-01-29 22:50:29 +01:00
public ItemStack(Item item, int i, int j) {
2013-11-04 14:07:38 +01:00
this.item = item;
this.count = i;
2013-03-25 05:22:32 +01:00
// CraftBukkit start - Pass to setData to do filtering
2013-11-04 14:07:38 +01:00
this.setData(j);
//this.damage = j;
//if (this.damage < 0) {
// this.damage = 0;
//}
// CraftBukkit end
2011-01-14 14:31:10 +01:00
}
2013-01-17 10:28:44 +01:00
public static ItemStack createStack(NBTTagCompound nbttagcompound) {
2011-09-15 02:23:52 +02:00
ItemStack itemstack = new ItemStack();
itemstack.c(nbttagcompound);
return itemstack.getItem() != null ? itemstack : null;
}
2013-07-01 13:03:00 +02:00
private ItemStack() {}
2011-01-14 14:31:10 +01:00
2011-01-29 22:50:29 +01:00
public ItemStack a(int i) {
2013-11-04 14:07:38 +01:00
ItemStack itemstack = new ItemStack(this.item, i, this.damage);
2011-11-20 09:01:14 +01:00
if (this.tag != null) {
itemstack.tag = (NBTTagCompound) this.tag.clone();
2011-11-20 09:01:14 +01:00
}
2011-01-29 22:50:29 +01:00
this.count -= i;
2011-11-20 09:01:14 +01:00
return itemstack;
2011-01-14 14:31:10 +01:00
}
public Item getItem() {
2013-11-04 14:07:38 +01:00
return this.item;
2011-01-14 14:31:10 +01:00
}
2012-07-29 09:33:13 +02:00
public boolean placeItem(EntityHuman entityhuman, World world, int i, int j, int k, int l, float f, float f1, float f2) {
boolean flag = this.getItem().interactWith(this, entityhuman, world, i, j, k, l, f, f1, f2);
2011-03-31 22:40:00 +02:00
if (flag) {
2014-03-21 05:26:30 +01:00
entityhuman.a(StatisticList.USE_ITEM_COUNT[Item.b(this.item)], 1);
2011-03-31 22:40:00 +02:00
}
return flag;
2011-01-14 14:31:10 +01:00
}
public float a(Block block) {
return this.getItem().getDestroySpeed(this, block);
2011-01-14 14:31:10 +01:00
}
2011-01-29 22:50:29 +01:00
public ItemStack a(World world, EntityHuman entityhuman) {
return this.getItem().a(this, world, entityhuman);
2011-01-14 14:31:10 +01:00
}
2011-09-15 02:23:52 +02:00
public ItemStack b(World world, EntityHuman entityhuman) {
return this.getItem().b(this, world, entityhuman);
}
2012-02-29 22:31:04 +01:00
public NBTTagCompound save(NBTTagCompound nbttagcompound) {
2013-11-04 14:07:38 +01:00
nbttagcompound.setShort("id", (short) Item.b(this.item));
nbttagcompound.setByte("Count", (byte) this.count);
nbttagcompound.setShort("Damage", (short) this.damage);
2011-11-20 09:01:14 +01:00
if (this.tag != null) {
nbttagcompound.set("tag", this.tag.clone()); // CraftBukkit - make defensive copy, data is going to another thread
2011-11-20 09:01:14 +01:00
}
2011-01-14 14:31:10 +01:00
return nbttagcompound;
}
2011-09-15 02:23:52 +02:00
public void c(NBTTagCompound nbttagcompound) {
2013-11-04 14:07:38 +01:00
this.item = Item.d(nbttagcompound.getShort("id"));
this.count = nbttagcompound.getByte("Count");
this.damage = nbttagcompound.getShort("Damage");
2013-03-13 23:33:27 +01:00
if (this.damage < 0) {
this.damage = 0;
}
2013-11-04 14:07:38 +01:00
if (nbttagcompound.hasKeyOfType("tag", 10)) {
// CraftBukkit - make defensive copy as this data may be coming from the save thread
this.tag = (NBTTagCompound) nbttagcompound.getCompound("tag").clone();
2011-11-20 09:01:14 +01:00
}
2011-01-14 14:31:10 +01:00
}
public int getMaxStackSize() {
return this.getItem().getMaxStackSize();
2011-01-14 14:31:10 +01:00
}
public boolean isStackable() {
2013-03-13 23:33:27 +01:00
return this.getMaxStackSize() > 1 && (!this.g() || !this.i());
2011-01-14 14:31:10 +01:00
}
2013-03-13 23:33:27 +01:00
public boolean g() {
2013-11-04 14:07:38 +01:00
return this.item.getMaxDurability() <= 0 ? false : !this.hasTag() || !this.getTag().getBoolean("Unbreakable");
2011-01-14 14:31:10 +01:00
}
public boolean usesData() {
2013-11-04 14:07:38 +01:00
return this.item.n();
2011-01-14 14:31:10 +01:00
}
2013-03-13 23:33:27 +01:00
public boolean i() {
return this.g() && this.damage > 0;
2011-01-14 14:31:10 +01:00
}
2013-03-13 23:33:27 +01:00
public int j() {
2011-01-29 22:50:29 +01:00
return this.damage;
2011-01-14 14:31:10 +01:00
}
public int getData() {
2011-01-29 22:50:29 +01:00
return this.damage;
2011-01-14 14:31:10 +01:00
}
public void setData(int i) {
2013-03-25 05:22:32 +01:00
// CraftBukkit start - Filter out data for items that shouldn't have it
// The crafting system uses this value for a special purpose so we have to allow it
if (i == 32767) {
this.damage = i;
return;
}
// Is this a block?
if (CraftMagicNumbers.getBlock(CraftMagicNumbers.getId(this.getItem())) != Blocks.AIR) {
// If vanilla doesn't use data on it don't allow any
if (!(this.usesData() || this.getItem().usesDurability())) {
i = 0;
}
}
// CraftBukkit end
2013-03-13 23:33:27 +01:00
this.damage = i;
if (this.damage < -1) { // CraftBukkit - don't filter -1, we use it
2013-03-13 23:33:27 +01:00
this.damage = 0;
}
2011-05-26 14:48:22 +02:00
}
2013-03-13 23:33:27 +01:00
public int l() {
2013-11-04 14:07:38 +01:00
return this.item.getMaxDurability();
2011-01-14 14:31:10 +01:00
}
2013-03-13 23:33:27 +01:00
public boolean isDamaged(int i, Random random) {
if (!this.g()) {
return false;
} else {
if (i > 0) {
2012-12-20 05:03:52 +01:00
int j = EnchantmentManager.getEnchantmentLevel(Enchantment.DURABILITY.id, this);
int k = 0;
2011-11-20 09:01:14 +01:00
2012-12-20 05:03:52 +01:00
for (int l = 0; j > 0 && l < i; ++l) {
2013-03-13 23:33:27 +01:00
if (EnchantmentDurability.a(this, j, random)) {
2012-12-20 05:03:52 +01:00
++k;
}
}
i -= k;
if (i <= 0) {
2013-03-13 23:33:27 +01:00
return false;
2011-11-20 09:01:14 +01:00
}
}
2013-03-13 23:33:27 +01:00
this.damage += i;
return this.damage > this.l();
}
}
2012-07-29 09:33:13 +02:00
2013-03-13 23:33:27 +01:00
public void damage(int i, EntityLiving entityliving) {
if (!(entityliving instanceof EntityHuman) || !((EntityHuman) entityliving).abilities.canInstantlyBuild) {
if (this.g()) {
2014-03-21 05:26:30 +01:00
if (this.isDamaged(i, entityliving.aH())) {
2013-03-13 23:33:27 +01:00
entityliving.a(this);
2013-07-01 13:03:00 +02:00
--this.count;
2013-03-13 23:33:27 +01:00
if (entityliving instanceof EntityHuman) {
2013-07-01 13:03:00 +02:00
EntityHuman entityhuman = (EntityHuman) entityliving;
2014-03-21 05:26:30 +01:00
entityhuman.a(StatisticList.BREAK_ITEM_COUNT[Item.b(this.item)], 1);
2013-07-01 13:03:00 +02:00
if (this.count == 0 && this.getItem() instanceof ItemBow) {
2014-03-21 05:26:30 +01:00
entityhuman.bF();
2013-07-01 13:03:00 +02:00
}
2013-03-13 23:33:27 +01:00
}
2011-03-31 22:40:00 +02:00
2013-03-13 23:33:27 +01:00
if (this.count < 0) {
this.count = 0;
}
2011-01-29 22:50:29 +01:00
2013-03-13 23:33:27 +01:00
// CraftBukkit start - Check for item breaking
if (this.count == 0 && entityliving instanceof EntityHuman) {
org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemBreakEvent((EntityHuman) entityliving, this);
}
// CraftBukkit end
2013-03-13 23:33:27 +01:00
this.damage = 0;
}
2011-01-14 14:31:10 +01:00
}
}
}
2011-03-31 22:40:00 +02:00
public void a(EntityLiving entityliving, EntityHuman entityhuman) {
2013-11-04 14:07:38 +01:00
boolean flag = this.item.a(this, entityliving, (EntityLiving) entityhuman);
2011-03-31 22:40:00 +02:00
if (flag) {
2014-03-21 05:26:30 +01:00
entityhuman.a(StatisticList.USE_ITEM_COUNT[Item.b(this.item)], 1);
2011-03-31 22:40:00 +02:00
}
2011-01-14 14:31:10 +01:00
}
2013-11-04 14:07:38 +01:00
public void a(World world, Block block, int i, int j, int k, EntityHuman entityhuman) {
boolean flag = this.item.a(this, world, block, i, j, k, entityhuman);
2011-03-31 22:40:00 +02:00
if (flag) {
2014-03-21 05:26:30 +01:00
entityhuman.a(StatisticList.USE_ITEM_COUNT[Item.b(this.item)], 1);
2011-03-31 22:40:00 +02:00
}
2011-01-14 14:31:10 +01:00
}
public boolean b(Block block) {
2013-11-04 14:07:38 +01:00
return this.item.canDestroySpecialBlock(block);
2011-01-14 14:31:10 +01:00
}
2013-07-01 13:03:00 +02:00
public boolean a(EntityHuman entityhuman, EntityLiving entityliving) {
2013-11-04 14:07:38 +01:00
return this.item.a(this, entityhuman, entityliving);
2011-01-14 14:31:10 +01:00
}
public ItemStack cloneItemStack() {
2013-11-04 14:07:38 +01:00
ItemStack itemstack = new ItemStack(this.item, this.count, this.damage);
2011-11-20 09:01:14 +01:00
if (this.tag != null) {
itemstack.tag = (NBTTagCompound) this.tag.clone();
2011-11-20 09:01:14 +01:00
}
return itemstack;
2011-01-14 14:31:10 +01:00
}
public static boolean equals(ItemStack itemstack, ItemStack itemstack1) {
2012-01-12 23:10:13 +01:00
return itemstack == null && itemstack1 == null ? true : (itemstack != null && itemstack1 != null ? (itemstack.tag == null && itemstack1.tag != null ? false : itemstack.tag == null || itemstack.tag.equals(itemstack1.tag)) : false);
}
public static boolean matches(ItemStack itemstack, ItemStack itemstack1) {
2011-05-26 14:48:22 +02:00
return itemstack == null && itemstack1 == null ? true : (itemstack != null && itemstack1 != null ? itemstack.d(itemstack1) : false);
2011-01-14 14:31:10 +01:00
}
2011-05-26 14:48:22 +02:00
private boolean d(ItemStack itemstack) {
2013-11-04 14:07:38 +01:00
return this.count != itemstack.count ? false : (this.item != itemstack.item ? false : (this.damage != itemstack.damage ? false : (this.tag == null && itemstack.tag != null ? false : this.tag == null || this.tag.equals(itemstack.tag))));
2011-01-14 14:31:10 +01:00
}
public boolean doMaterialsMatch(ItemStack itemstack) {
2013-11-04 14:07:38 +01:00
return this.item == itemstack.item && this.damage == itemstack.damage;
2011-01-14 14:31:10 +01:00
}
2012-07-29 09:33:13 +02:00
public String a() {
2013-11-04 14:07:38 +01:00
return this.item.a(this);
2011-09-15 02:23:52 +02:00
}
2011-01-14 14:31:10 +01:00
public static ItemStack b(ItemStack itemstack) {
return itemstack == null ? null : itemstack.cloneItemStack();
2011-01-14 14:31:10 +01:00
}
public String toString() {
2013-11-04 14:07:38 +01:00
return this.count + "x" + this.item.getName() + "@" + this.damage;
2011-01-14 14:31:10 +01:00
}
2011-05-26 14:48:22 +02:00
public void a(World world, Entity entity, int i, boolean flag) {
2013-07-01 13:03:00 +02:00
if (this.c > 0) {
--this.c;
2011-05-26 14:48:22 +02:00
}
2013-11-04 14:07:38 +01:00
this.item.a(this, world, entity, i, flag);
2011-05-26 14:48:22 +02:00
}
2012-03-01 11:49:23 +01:00
public void a(World world, EntityHuman entityhuman, int i) {
2014-03-21 05:26:30 +01:00
entityhuman.a(StatisticList.CRAFT_BLOCK_COUNT[Item.b(this.item)], i);
2013-11-04 14:07:38 +01:00
this.item.d(this, world, entityhuman);
2011-05-26 14:48:22 +02:00
}
2013-03-13 23:33:27 +01:00
public int n() {
2013-07-01 13:03:00 +02:00
return this.getItem().d_(this);
2011-09-15 02:23:52 +02:00
}
2013-03-13 23:33:27 +01:00
public EnumAnimation o() {
2013-11-04 14:07:38 +01:00
return this.getItem().d(this);
2011-09-15 02:23:52 +02:00
}
2012-03-01 11:49:23 +01:00
public void b(World world, EntityHuman entityhuman, int i) {
2011-09-15 02:23:52 +02:00
this.getItem().a(this, world, entityhuman, i);
}
2011-11-20 09:01:14 +01:00
public boolean hasTag() {
2011-11-20 09:01:14 +01:00
return this.tag != null;
}
public NBTTagCompound getTag() {
2011-11-20 09:01:14 +01:00
return this.tag;
}
public NBTTagList getEnchantments() {
2013-11-04 14:07:38 +01:00
return this.tag == null ? null : this.tag.getList("ench", 10);
2011-11-20 09:01:14 +01:00
}
public void setTag(NBTTagCompound nbttagcompound) {
2012-01-12 23:10:13 +01:00
this.tag = nbttagcompound;
2011-11-20 09:01:14 +01:00
}
2013-03-13 23:33:27 +01:00
public String getName() {
2013-11-04 14:07:38 +01:00
String s = this.getItem().n(this);
2013-11-04 14:07:38 +01:00
if (this.tag != null && this.tag.hasKeyOfType("display", 10)) {
NBTTagCompound nbttagcompound = this.tag.getCompound("display");
2013-11-04 14:07:38 +01:00
if (nbttagcompound.hasKeyOfType("Name", 8)) {
s = nbttagcompound.getString("Name");
}
}
return s;
}
2013-11-04 14:07:38 +01:00
public ItemStack c(String s) {
if (this.tag == null) {
2013-11-04 14:07:38 +01:00
this.tag = new NBTTagCompound();
}
2013-11-04 14:07:38 +01:00
if (!this.tag.hasKeyOfType("display", 10)) {
this.tag.set("display", new NBTTagCompound());
}
this.tag.getCompound("display").setString("Name", s);
2013-11-04 14:07:38 +01:00
return this;
}
2013-07-01 13:03:00 +02:00
public void t() {
if (this.tag != null) {
2013-11-04 14:07:38 +01:00
if (this.tag.hasKeyOfType("display", 10)) {
2013-07-01 13:03:00 +02:00
NBTTagCompound nbttagcompound = this.tag.getCompound("display");
nbttagcompound.remove("Name");
if (nbttagcompound.isEmpty()) {
this.tag.remove("display");
if (this.tag.isEmpty()) {
this.setTag((NBTTagCompound) null);
}
}
}
}
}
2013-03-13 23:33:27 +01:00
public boolean hasName() {
2013-11-04 14:07:38 +01:00
return this.tag == null ? false : (!this.tag.hasKeyOfType("display", 10) ? false : this.tag.getCompound("display").hasKeyOfType("Name", 8));
}
public EnumItemRarity w() {
return this.getItem().f(this);
}
2013-07-01 13:03:00 +02:00
public boolean x() {
return !this.getItem().e_(this) ? false : !this.hasEnchantments();
2011-11-20 09:01:14 +01:00
}
public void addEnchantment(Enchantment enchantment, int i) {
2011-11-20 09:01:14 +01:00
if (this.tag == null) {
this.setTag(new NBTTagCompound());
2011-11-20 09:01:14 +01:00
}
2013-11-04 14:07:38 +01:00
if (!this.tag.hasKeyOfType("ench", 9)) {
this.tag.set("ench", new NBTTagList());
2011-11-20 09:01:14 +01:00
}
2013-11-04 14:07:38 +01:00
NBTTagList nbttaglist = this.tag.getList("ench", 10);
2011-11-20 09:01:14 +01:00
NBTTagCompound nbttagcompound = new NBTTagCompound();
nbttagcompound.setShort("id", (short) enchantment.id);
nbttagcompound.setShort("lvl", (short) ((byte) i));
nbttaglist.add(nbttagcompound);
2011-11-20 09:01:14 +01:00
}
public boolean hasEnchantments() {
2013-11-04 14:07:38 +01:00
return this.tag != null && this.tag.hasKeyOfType("ench", 9);
2011-11-20 09:01:14 +01:00
}
public void a(String s, NBTBase nbtbase) {
if (this.tag == null) {
this.setTag(new NBTTagCompound());
}
this.tag.set(s, nbtbase);
}
2013-07-01 13:03:00 +02:00
public boolean z() {
2013-11-04 14:07:38 +01:00
return this.getItem().v();
}
2013-07-01 13:03:00 +02:00
public boolean A() {
return this.g != null;
}
public void a(EntityItemFrame entityitemframe) {
2013-07-01 13:03:00 +02:00
this.g = entityitemframe;
}
2013-07-01 13:03:00 +02:00
public EntityItemFrame B() {
return this.g;
}
public int getRepairCost() {
2013-11-04 14:07:38 +01:00
return this.hasTag() && this.tag.hasKeyOfType("RepairCost", 3) ? this.tag.getInt("RepairCost") : 0;
}
public void setRepairCost(int i) {
if (!this.hasTag()) {
2013-11-04 14:07:38 +01:00
this.tag = new NBTTagCompound();
}
this.tag.setInt("RepairCost", i);
}
2013-07-01 13:03:00 +02:00
public Multimap D() {
Object object;
2013-11-04 14:07:38 +01:00
if (this.hasTag() && this.tag.hasKeyOfType("AttributeModifiers", 9)) {
2013-07-01 13:03:00 +02:00
object = HashMultimap.create();
2013-11-04 14:07:38 +01:00
NBTTagList nbttaglist = this.tag.getList("AttributeModifiers", 10);
2013-07-01 13:03:00 +02:00
for (int i = 0; i < nbttaglist.size(); ++i) {
2013-11-04 14:07:38 +01:00
NBTTagCompound nbttagcompound = nbttaglist.get(i);
2013-07-01 13:03:00 +02:00
AttributeModifier attributemodifier = GenericAttributes.a(nbttagcompound);
if (attributemodifier.a().getLeastSignificantBits() != 0L && attributemodifier.a().getMostSignificantBits() != 0L) {
((Multimap) object).put(nbttagcompound.getString("AttributeName"), attributemodifier);
}
}
} else {
2013-11-04 14:07:38 +01:00
object = this.getItem().k();
2013-07-01 13:03:00 +02:00
}
return (Multimap) object;
}
2013-11-04 14:07:38 +01:00
public void setItem(Item item) {
this.item = item;
}
public IChatBaseComponent E() {
IChatBaseComponent ichatbasecomponent = (new ChatComponentText("[")).a(this.getName()).a("]");
if (this.item != null) {
NBTTagCompound nbttagcompound = new NBTTagCompound();
this.save(nbttagcompound);
2014-03-21 05:26:30 +01:00
ichatbasecomponent.getChatModifier().a(new ChatHoverable(EnumHoverAction.SHOW_ITEM, new ChatComponentText(nbttagcompound.toString())));
ichatbasecomponent.getChatModifier().setColor(this.w().e);
2013-11-04 14:07:38 +01:00
}
return ichatbasecomponent;
}
2011-01-14 14:31:10 +01:00
}