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

422 Zeilen
12 KiB
Java

2011-01-14 14:31:10 +01:00
package net.minecraft.server;
2013-03-13 23:33:27 +01:00
import java.util.Random;
2011-01-14 14:31:10 +01:00
public final class ItemStack {
2011-01-29 22:50:29 +01:00
public int count;
2011-01-14 14:31:10 +01:00
public int b;
2011-01-29 22:50:29 +01:00
public int id;
2011-11-20 09:01:14 +01:00
public NBTTagCompound tag;
2011-09-26 09:07:06 +02:00
private int damage;
private EntityItemFrame f;
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) {
this(block.id, 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) {
this(block.id, i, j);
2011-01-14 14:31:10 +01:00
}
public ItemStack(Item item) {
2011-01-29 22:50:29 +01:00
this(item.id, 1, 0);
2011-01-14 14:31:10 +01:00
}
2011-01-29 22:50:29 +01:00
public ItemStack(Item item, int i) {
this(item.id, 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) {
this(item.id, i, j);
2011-01-14 14:31:10 +01:00
}
2011-01-29 22:50:29 +01:00
public ItemStack(int i, int j, int k) {
this.count = 0;
this.f = null;
2011-01-29 22:50:29 +01:00
this.id = i;
this.count = j;
this.setData(k); // CraftBukkit
2013-03-13 23:33:27 +01:00
if (this.damage < 0) {
this.damage = 0;
}
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;
}
private ItemStack() {
2011-01-29 22:50:29 +01:00
this.count = 0;
this.f = null;
2011-01-14 14:31:10 +01:00
}
2011-01-29 22:50:29 +01:00
public ItemStack a(int i) {
2011-11-20 09:01:14 +01:00
ItemStack itemstack = new ItemStack(this.id, i, this.damage);
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() {
2011-01-29 22:50:29 +01:00
return Item.byId[this.id];
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) {
2011-04-20 22:47:26 +02:00
entityhuman.a(StatisticList.E[this.id], 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) {
nbttagcompound.setShort("id", (short) this.id);
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) {
this.id = 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;
}
2011-11-20 09:01:14 +01:00
if (nbttagcompound.hasKey("tag")) {
// CraftBukkit - clear name from compound and make defensive copy as this data may be coming from the save thread
this.tag = (NBTTagCompound) nbttagcompound.getCompound("tag").clone().setName("");
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() {
return Item.byId[this.id].getMaxDurability() > 0;
2011-01-14 14:31:10 +01:00
}
public boolean usesData() {
2013-03-13 23:33:27 +01:00
return Item.byId[this.id].m();
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) {
// CraftBukkit start - filter out data for items that shouldn't have it
if (!this.usesData()) {
2013-03-16 19:26:29 +01:00
i = 0;
}
// Filter wool to avoid confusing the client
if (this.id == Block.WOOL.id) {
2013-03-16 19:26:29 +01:00
i = Math.min(15, i);
}
// CraftBukkit end
2013-03-13 23:33:27 +01:00
this.damage = i;
if (this.damage < 0) {
this.damage = 0;
}
2011-05-26 14:48:22 +02:00
}
2013-03-13 23:33:27 +01:00
public int l() {
return Item.byId[this.id].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()) {
if (this.isDamaged(i, entityliving.aE())) {
entityliving.a(this);
if (entityliving instanceof EntityHuman) {
((EntityHuman) entityliving).a(StatisticList.F[this.id], 1);
}
2011-03-31 22:40:00 +02:00
2013-03-13 23:33:27 +01:00
--this.count;
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) {
boolean flag = Item.byId[this.id].a(this, entityliving, (EntityLiving) entityhuman);
if (flag) {
2011-04-20 22:47:26 +02:00
entityhuman.a(StatisticList.E[this.id], 1);
2011-03-31 22:40:00 +02:00
}
2011-01-14 14:31:10 +01:00
}
2012-07-29 09:33:13 +02:00
public void a(World world, int i, int j, int k, int l, EntityHuman entityhuman) {
boolean flag = Item.byId[this.id].a(this, world, i, j, k, l, entityhuman);
2011-03-31 22:40:00 +02:00
if (flag) {
2011-04-20 22:47:26 +02:00
entityhuman.a(StatisticList.E[this.id], 1);
2011-03-31 22:40:00 +02:00
}
2011-01-14 14:31:10 +01:00
}
public int a(Entity entity) {
2011-01-29 22:50:29 +01:00
return Item.byId[this.id].a(entity);
2011-01-14 14:31:10 +01:00
}
public boolean b(Block block) {
2012-02-29 22:31:04 +01:00
return Item.byId[this.id].canDestroySpecialBlock(block);
2011-01-14 14:31:10 +01:00
}
2012-07-29 09:33:13 +02:00
public boolean a(EntityLiving entityliving) {
return Item.byId[this.id].a(this, entityliving);
2011-01-14 14:31:10 +01:00
}
public ItemStack cloneItemStack() {
2011-11-20 09:01:14 +01:00
ItemStack itemstack = new ItemStack(this.id, this.count, this.damage);
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) {
2011-11-20 09:01:14 +01:00
return this.count != itemstack.count ? false : (this.id != itemstack.id ? 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) {
2011-01-29 22:50:29 +01:00
return this.id == itemstack.id && this.damage == itemstack.damage;
2011-01-14 14:31:10 +01:00
}
2012-07-29 09:33:13 +02:00
public String a() {
2012-12-20 05:03:52 +01:00
return Item.byId[this.id].d(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() {
return this.count + "x" + Item.byId[this.id].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) {
if (this.b > 0) {
--this.b;
}
Item.byId[this.id].a(this, world, entity, i, flag);
}
2012-03-01 11:49:23 +01:00
public void a(World world, EntityHuman entityhuman, int i) {
entityhuman.a(StatisticList.D[this.id], i);
2011-09-15 02:23:52 +02:00
Item.byId[this.id].d(this, world, entityhuman);
2011-05-26 14:48:22 +02:00
}
2013-03-13 23:33:27 +01:00
public int n() {
2012-12-20 05:03:52 +01:00
return this.getItem().c_(this);
2011-09-15 02:23:52 +02:00
}
2013-03-13 23:33:27 +01:00
public EnumAnimation o() {
2012-12-20 05:03:52 +01:00
return this.getItem().b_(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() {
return this.tag == null ? null : (NBTTagList) this.tag.get("ench");
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() {
2012-12-20 05:03:52 +01:00
String s = this.getItem().l(this);
if (this.tag != null && this.tag.hasKey("display")) {
NBTTagCompound nbttagcompound = this.tag.getCompound("display");
if (nbttagcompound.hasKey("Name")) {
s = nbttagcompound.getString("Name");
}
}
return s;
}
public void c(String s) {
if (this.tag == null) {
2013-03-13 23:33:27 +01:00
this.tag = new NBTTagCompound("tag");
}
if (!this.tag.hasKey("display")) {
this.tag.setCompound("display", new NBTTagCompound());
}
this.tag.getCompound("display").setString("Name", s);
}
2013-03-13 23:33:27 +01:00
public boolean hasName() {
return this.tag == null ? false : (!this.tag.hasKey("display") ? false : this.tag.getCompound("display").hasKey("Name"));
}
2013-03-13 23:33:27 +01:00
public boolean w() {
2012-12-20 05:03:52 +01:00
return !this.getItem().d_(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
}
if (!this.tag.hasKey("ench")) {
this.tag.set("ench", new NBTTagList("ench"));
2011-11-20 09:01:14 +01:00
}
NBTTagList nbttaglist = (NBTTagList) this.tag.get("ench");
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() {
2011-11-20 09:01:14 +01:00
return this.tag != null && this.tag.hasKey("ench");
}
public void a(String s, NBTBase nbtbase) {
if (this.tag == null) {
this.setTag(new NBTTagCompound());
}
this.tag.set(s, nbtbase);
}
2013-03-13 23:33:27 +01:00
public boolean y() {
return this.getItem().y();
}
2013-03-13 23:33:27 +01:00
public boolean z() {
return this.f != null;
}
public void a(EntityItemFrame entityitemframe) {
this.f = entityitemframe;
}
2013-03-13 23:33:27 +01:00
public EntityItemFrame A() {
return this.f;
}
public int getRepairCost() {
return this.hasTag() && this.tag.hasKey("RepairCost") ? this.tag.getInt("RepairCost") : 0;
}
public void setRepairCost(int i) {
if (!this.hasTag()) {
2013-03-13 23:33:27 +01:00
this.tag = new NBTTagCompound("tag");
}
this.tag.setInt("RepairCost", i);
}
2011-01-14 14:31:10 +01:00
}