3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-12-21 22:10:10 +01:00
Paper/src/main/java/net/minecraft/server/ItemStack.java

326 Zeilen
8.9 KiB
Java

2011-01-14 14:31:10 +01:00
package net.minecraft.server;
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;
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.id = i;
this.count = j;
this.setData(k); // CraftBukkit
2011-01-14 14:31:10 +01:00
}
// CraftBukkit - used to create a new ItemStack, specifying the enchantments at time of creation.
public ItemStack(int id, int count, int data, NBTTagList enchantments) {
this(id, count, data);
// taken from .addEnchantment
if (this.tag == null) {
this.setTag(new NBTTagCompound());
}
this.tag.set("ench", enchantments); // modify this part to use passed in enchantments list
}
// CraftBukkit end
2011-09-15 02:23:52 +02:00
public static ItemStack a(NBTTagCompound nbttagcompound) {
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;
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
}
public boolean placeItem(EntityHuman entityhuman, World world, int i, int j, int k, int l) {
boolean flag = this.getItem().a(this, entityhuman, world, i, j, k, l);
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().a(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);
}
public NBTTagCompound b(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);
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");
2011-11-20 09:01:14 +01:00
if (nbttagcompound.hasKey("tag")) {
this.tag = nbttagcompound.getCompound("tag");
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() {
return this.getMaxStackSize() > 1 && (!this.d() || !this.f());
2011-01-14 14:31:10 +01:00
}
public boolean d() {
return Item.byId[this.id].getMaxDurability() > 0;
2011-01-14 14:31:10 +01:00
}
public boolean usesData() {
2011-11-20 09:01:14 +01:00
return Item.byId[this.id].e();
2011-01-14 14:31:10 +01:00
}
public boolean f() {
2011-01-29 22:50:29 +01:00
return this.d() && this.damage > 0;
2011-01-14 14:31:10 +01:00
}
public int g() {
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) {
2011-09-26 09:59:03 +02:00
this.damage = (this.id > 0) && (this.id < 256) ? Item.byId[this.id].filterData(i) : i; // CraftBukkit
2011-05-26 14:48:22 +02:00
}
2011-01-14 14:31:10 +01:00
public int i() {
return Item.byId[this.id].getMaxDurability();
2011-01-14 14:31:10 +01:00
}
2011-11-20 09:01:14 +01:00
public void damage(int i, EntityLiving entityliving) {
2011-01-29 22:50:29 +01:00
if (this.d()) {
2011-11-20 09:01:14 +01:00
if (i > 0 && entityliving instanceof EntityHuman) {
int j = EnchantmentManager.getDurabilityEnchantmentLevel(((EntityHuman) entityliving).inventory);
2011-11-20 09:01:14 +01:00
if (j > 0 && entityliving.world.random.nextInt(j + 1) > 0) {
return;
}
}
2011-01-29 22:50:29 +01:00
this.damage += i;
if (this.damage > this.i()) {
2011-11-20 09:01:14 +01:00
entityliving.c(this);
if (entityliving instanceof EntityHuman) {
((EntityHuman) entityliving).a(StatisticList.F[this.id], 1);
2011-03-31 22:40:00 +02:00
}
2011-01-29 22:50:29 +01:00
--this.count;
if (this.count < 0) {
this.count = 0;
}
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
}
2011-03-31 22:40:00 +02:00
public void a(int i, int j, int k, int l, EntityHuman entityhuman) {
boolean flag = Item.byId[this.id].a(this, i, j, k, l, 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
}
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) {
2011-01-29 22:50:29 +01:00
return Item.byId[this.id].a(block);
2011-01-14 14:31:10 +01:00
}
2011-01-29 22:50:29 +01:00
public void a(EntityHuman entityhuman) {}
2011-01-14 14:31:10 +01:00
2011-03-31 22:40:00 +02:00
public void a(EntityLiving entityliving) {
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
if (!itemstack.tag.equals(this.tag)) {
return itemstack;
}
}
return itemstack;
2011-01-14 14:31:10 +01:00
}
public static boolean equals(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
}
2011-09-15 02:23:52 +02:00
public String k() {
return Item.byId[this.id].a(this);
}
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() {
2011-09-15 02:23:52 +02:00
return this.count + "x" + Item.byId[this.id].b() + "@" + 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);
}
2011-09-15 02:23:52 +02:00
public void c(World world, EntityHuman entityhuman) {
2011-05-26 14:48:22 +02:00
entityhuman.a(StatisticList.D[this.id], this.count);
2011-09-15 02:23:52 +02:00
Item.byId[this.id].d(this, world, entityhuman);
2011-05-26 14:48:22 +02:00
}
public boolean c(ItemStack itemstack) {
return this.id == itemstack.id && this.count == itemstack.count && this.damage == itemstack.damage;
}
2011-09-15 02:23:52 +02:00
public int l() {
return this.getItem().c(this);
}
public EnumAnimation m() {
2011-11-20 09:01:14 +01:00
return this.getItem().d(this);
2011-09-15 02:23:52 +02:00
}
public void a(World world, EntityHuman entityhuman, int i) {
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) {
2011-11-20 09:01:14 +01:00
if (Item.byId[this.id].getMaxStackSize() != 1) {
throw new IllegalArgumentException("Cannot add tag data to a stackable item");
} else {
this.tag = nbttagcompound;
}
}
public boolean q() {
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
}
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");
}
2011-01-14 14:31:10 +01:00
}