craftbukkit/src/main/java/net/minecraft/server/PlayerInventory.java

540 lines
15 KiB
Java
Raw Normal View History

2011-01-29 22:50:29 +01:00
package net.minecraft.server;
2013-03-13 17:33:27 -05:00
import java.util.concurrent.Callable;
// CraftBukkit start
import java.util.List;
import org.bukkit.craftbukkit.entity.CraftHumanEntity;
import org.bukkit.entity.HumanEntity;
// CraftBukkit end
public class PlayerInventory implements IInventory {
2011-01-29 22:50:29 +01:00
public ItemStack[] items = new ItemStack[36];
public ItemStack[] armor = new ItemStack[4];
2013-07-01 06:03:00 -05:00
public int itemInHandIndex;
2012-02-29 22:31:04 +01:00
public EntityHuman player;
2012-07-29 02:33:13 -05:00
private ItemStack g;
2013-07-01 06:03:00 -05:00
public boolean e;
2011-01-29 22:50:29 +01:00
2014-03-20 22:26:30 -06:00
// CraftBukkit start - add fields and methods
public List<HumanEntity> transaction = new java.util.ArrayList<HumanEntity>();
private int maxStack = MAX_STACK;
2012-02-29 22:31:04 +01:00
2011-01-29 22:50:29 +01:00
public ItemStack[] getContents() {
return this.items;
2011-01-29 22:50:29 +01:00
}
public ItemStack[] getArmorContents() {
return this.armor;
2011-01-29 22:50:29 +01:00
}
public void onOpen(CraftHumanEntity who) {
transaction.add(who);
}
public void onClose(CraftHumanEntity who) {
transaction.remove(who);
}
2012-02-29 22:31:04 +01:00
public List<HumanEntity> getViewers() {
return transaction;
}
public org.bukkit.inventory.InventoryHolder getOwner() {
2012-02-29 22:31:04 +01:00
return this.player.getBukkitEntity();
}
public void setMaxStackSize(int size) {
maxStack = size;
}
2011-01-29 22:50:29 +01:00
// CraftBukkit end
public PlayerInventory(EntityHuman entityhuman) {
2012-02-29 22:31:04 +01:00
this.player = entityhuman;
2011-01-29 22:50:29 +01:00
}
public ItemStack getItemInHand() {
return this.itemInHandIndex < 9 && this.itemInHandIndex >= 0 ? this.items[this.itemInHandIndex] : null;
2011-03-31 21:40:00 +01:00
}
public static int getHotbarSize() {
2011-03-31 21:40:00 +01:00
return 9;
2011-01-29 22:50:29 +01:00
}
2013-11-04 07:07:38 -06:00
private int c(Item item) {
for (int i = 0; i < this.items.length; ++i) {
if (this.items[i] != null && this.items[i].getItem() == item) {
return i;
2011-01-29 22:50:29 +01:00
}
}
return -1;
}
private int firstPartial(ItemStack itemstack) {
for (int i = 0; i < this.items.length; ++i) {
2013-11-04 07:07:38 -06:00
if (this.items[i] != null && this.items[i].getItem() == itemstack.getItem() && this.items[i].isStackable() && this.items[i].count < this.items[i].getMaxStackSize() && this.items[i].count < this.getMaxStackSize() && (!this.items[i].usesData() || this.items[i].getData() == itemstack.getData()) && ItemStack.equals(this.items[i], itemstack)) {
2011-01-29 22:50:29 +01:00
return i;
}
}
return -1;
}
2013-03-24 23:22:32 -05:00
// CraftBukkit start - Watch method above! :D
public int canHold(ItemStack itemstack) {
int remains = itemstack.count;
for (int i = 0; i < this.items.length; ++i) {
if (this.items[i] == null) return itemstack.count;
// Taken from firstPartial(ItemStack)
2013-11-04 07:07:38 -06:00
if (this.items[i] != null && this.items[i].getItem() == itemstack.getItem() && this.items[i].isStackable() && this.items[i].count < this.items[i].getMaxStackSize() && this.items[i].count < this.getMaxStackSize() && (!this.items[i].usesData() || this.items[i].getData() == itemstack.getData()) && ItemStack.equals(this.items[i], itemstack)) {
remains -= (this.items[i].getMaxStackSize() < this.getMaxStackSize() ? this.items[i].getMaxStackSize() : this.getMaxStackSize()) - this.items[i].count;
}
if (remains <= 0) return itemstack.count;
}
return itemstack.count - remains;
}
// CraftBukkit end
2014-03-20 22:26:30 -06:00
public int getFirstEmptySlotIndex() {
for (int i = 0; i < this.items.length; ++i) {
if (this.items[i] == null) {
2011-01-29 22:50:29 +01:00
return i;
}
}
return -1;
}
2013-11-04 07:07:38 -06:00
public int a(Item item, int i) {
int j = 0;
2013-11-04 07:07:38 -06:00
int k;
ItemStack itemstack;
2013-11-04 07:07:38 -06:00
for (k = 0; k < this.items.length; ++k) {
itemstack = this.items[k];
if (itemstack != null && (item == null || itemstack.getItem() == item) && (i <= -1 || itemstack.getData() == i)) {
j += itemstack.count;
this.items[k] = null;
}
}
2013-11-04 07:07:38 -06:00
for (k = 0; k < this.armor.length; ++k) {
itemstack = this.armor[k];
if (itemstack != null && (item == null || itemstack.getItem() == item) && (i <= -1 || itemstack.getData() == i)) {
j += itemstack.count;
this.armor[k] = null;
}
}
2013-07-01 06:03:00 -05:00
if (this.g != null) {
2013-11-04 07:07:38 -06:00
if (item != null && this.g.getItem() != item) {
return j;
2013-07-01 06:03:00 -05:00
}
2013-11-04 07:07:38 -06:00
if (i > -1 && this.g.getData() != i) {
return j;
2013-07-01 06:03:00 -05:00
}
2013-11-04 07:07:38 -06:00
j += this.g.count;
2013-07-01 06:03:00 -05:00
this.setCarried((ItemStack) null);
}
2013-11-04 07:07:38 -06:00
return j;
}
2011-05-26 13:48:22 +01:00
private int e(ItemStack itemstack) {
2013-11-04 07:07:38 -06:00
Item item = itemstack.getItem();
int i = itemstack.count;
int j;
2011-01-29 22:50:29 +01:00
2011-11-20 00:01:14 -08:00
if (itemstack.getMaxStackSize() == 1) {
2014-03-20 22:26:30 -06:00
j = this.getFirstEmptySlotIndex();
2013-11-04 07:07:38 -06:00
if (j < 0) {
return i;
2011-11-20 00:01:14 -08:00
} else {
2013-11-04 07:07:38 -06:00
if (this.items[j] == null) {
this.items[j] = ItemStack.b(itemstack);
2011-11-20 00:01:14 -08:00
}
2011-01-29 22:50:29 +01:00
2011-11-20 00:01:14 -08:00
return 0;
}
2011-01-29 22:50:29 +01:00
} else {
2013-11-04 07:07:38 -06:00
j = this.firstPartial(itemstack);
if (j < 0) {
2014-03-20 22:26:30 -06:00
j = this.getFirstEmptySlotIndex();
2011-01-29 22:50:29 +01:00
}
2013-11-04 07:07:38 -06:00
if (j < 0) {
return i;
2011-11-20 00:01:14 -08:00
} else {
2013-11-04 07:07:38 -06:00
if (this.items[j] == null) {
this.items[j] = new ItemStack(item, 0, itemstack.getData());
2012-01-12 23:10:13 +01:00
if (itemstack.hasTag()) {
2013-11-04 07:07:38 -06:00
this.items[j].setTag((NBTTagCompound) itemstack.getTag().clone());
2012-01-12 23:10:13 +01:00
}
2011-11-20 00:01:14 -08:00
}
2011-01-29 22:50:29 +01:00
2013-11-04 07:07:38 -06:00
int k = i;
2011-01-29 22:50:29 +01:00
2013-11-04 07:07:38 -06:00
if (i > this.items[j].getMaxStackSize() - this.items[j].count) {
k = this.items[j].getMaxStackSize() - this.items[j].count;
2011-11-20 00:01:14 -08:00
}
2011-01-29 22:50:29 +01:00
2013-11-04 07:07:38 -06:00
if (k > this.getMaxStackSize() - this.items[j].count) {
k = this.getMaxStackSize() - this.items[j].count;
2011-11-20 00:01:14 -08:00
}
2013-11-04 07:07:38 -06:00
if (k == 0) {
return i;
2011-11-20 00:01:14 -08:00
} else {
2013-11-04 07:07:38 -06:00
i -= k;
this.items[j].count += k;
this.items[j].c = 5;
return i;
2011-11-20 00:01:14 -08:00
}
2011-01-29 22:50:29 +01:00
}
}
}
2013-03-13 17:33:27 -05:00
public void k() {
for (int i = 0; i < this.items.length; ++i) {
2011-05-26 13:48:22 +01:00
if (this.items[i] != null) {
2012-02-29 22:31:04 +01:00
this.items[i].a(this.player.world, this.player, i, this.itemInHandIndex == i);
2011-01-29 22:50:29 +01:00
}
}
}
2013-11-04 07:07:38 -06:00
public boolean a(Item item) {
int i = this.c(item);
2011-01-29 22:50:29 +01:00
2013-11-04 07:07:38 -06:00
if (i < 0) {
2011-01-29 22:50:29 +01:00
return false;
} else {
2013-11-04 07:07:38 -06:00
if (--this.items[i].count <= 0) {
this.items[i] = null;
2011-01-29 22:50:29 +01:00
}
return true;
}
}
2013-11-04 07:07:38 -06:00
public boolean b(Item item) {
int i = this.c(item);
2011-09-15 01:23:52 +01:00
2013-11-04 07:07:38 -06:00
return i >= 0;
2011-09-15 01:23:52 +01:00
}
public boolean pickup(ItemStack itemstack) {
2013-11-04 07:07:38 -06:00
if (itemstack != null && itemstack.count != 0 && itemstack.getItem() != null) {
2013-03-13 17:33:27 -05:00
try {
int i;
if (itemstack.i()) {
2014-03-20 22:26:30 -06:00
i = this.getFirstEmptySlotIndex();
2013-03-13 17:33:27 -05:00
if (i >= 0) {
this.items[i] = ItemStack.b(itemstack);
2013-07-01 06:03:00 -05:00
this.items[i].c = 5;
2013-03-13 17:33:27 -05:00
itemstack.count = 0;
return true;
} else if (this.player.abilities.canInstantlyBuild) {
itemstack.count = 0;
return true;
} else {
return false;
}
} else {
do {
i = itemstack.count;
itemstack.count = this.e(itemstack);
} while (itemstack.count > 0 && itemstack.count < i);
if (itemstack.count == i && this.player.abilities.canInstantlyBuild) {
itemstack.count = 0;
return true;
} else {
return itemstack.count < i;
}
}
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.a(throwable, "Adding item to inventory");
CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Item being added");
2013-11-04 07:07:38 -06:00
crashreportsystemdetails.a("Item ID", Integer.valueOf(Item.b(itemstack.getItem())));
2013-03-13 17:33:27 -05:00
crashreportsystemdetails.a("Item data", Integer.valueOf(itemstack.getData()));
crashreportsystemdetails.a("Item name", (Callable) (new CrashReportItemName(this, itemstack)));
throw new ReportedException(crashreport);
2011-11-20 00:01:14 -08:00
}
2013-11-04 07:07:38 -06:00
} else {
return false;
2011-01-29 22:50:29 +01:00
}
}
public ItemStack splitStack(int i, int j) {
ItemStack[] aitemstack = this.items;
2011-01-29 22:50:29 +01:00
if (i >= this.items.length) {
aitemstack = this.armor;
i -= this.items.length;
2011-01-29 22:50:29 +01:00
}
if (aitemstack[i] != null) {
ItemStack itemstack;
if (aitemstack[i].count <= j) {
itemstack = aitemstack[i];
aitemstack[i] = null;
return itemstack;
} else {
itemstack = aitemstack[i].a(j);
if (aitemstack[i].count == 0) {
aitemstack[i] = null;
}
return itemstack;
}
} else {
return null;
}
}
2012-03-01 10:49:23 +00:00
public ItemStack splitWithoutUpdate(int i) {
ItemStack[] aitemstack = this.items;
if (i >= this.items.length) {
aitemstack = this.armor;
i -= this.items.length;
}
if (aitemstack[i] != null) {
ItemStack itemstack = aitemstack[i];
aitemstack[i] = null;
return itemstack;
} else {
return null;
}
}
public void setItem(int i, ItemStack itemstack) {
ItemStack[] aitemstack = this.items;
2011-01-29 22:50:29 +01:00
if (i >= aitemstack.length) {
i -= aitemstack.length;
aitemstack = this.armor;
2011-01-29 22:50:29 +01:00
}
aitemstack[i] = itemstack;
}
public float a(Block block) {
float f = 1.0F;
if (this.items[this.itemInHandIndex] != null) {
f *= this.items[this.itemInHandIndex].a(block);
2011-01-29 22:50:29 +01:00
}
return f;
}
public NBTTagList a(NBTTagList nbttaglist) {
int i;
NBTTagCompound nbttagcompound;
for (i = 0; i < this.items.length; ++i) {
if (this.items[i] != null) {
2011-01-29 22:50:29 +01:00
nbttagcompound = new NBTTagCompound();
nbttagcompound.setByte("Slot", (byte) i);
2012-02-29 22:31:04 +01:00
this.items[i].save(nbttagcompound);
nbttaglist.add(nbttagcompound);
2011-01-29 22:50:29 +01:00
}
}
for (i = 0; i < this.armor.length; ++i) {
if (this.armor[i] != null) {
2011-01-29 22:50:29 +01:00
nbttagcompound = new NBTTagCompound();
nbttagcompound.setByte("Slot", (byte) (i + 100));
2012-02-29 22:31:04 +01:00
this.armor[i].save(nbttagcompound);
nbttaglist.add(nbttagcompound);
2011-01-29 22:50:29 +01:00
}
}
return nbttaglist;
}
public void b(NBTTagList nbttaglist) {
this.items = new ItemStack[36];
this.armor = new ItemStack[4];
2011-01-29 22:50:29 +01:00
for (int i = 0; i < nbttaglist.size(); ++i) {
2013-11-04 07:07:38 -06:00
NBTTagCompound nbttagcompound = nbttaglist.get(i);
int j = nbttagcompound.getByte("Slot") & 255;
2013-01-17 03:28:44 -06:00
ItemStack itemstack = ItemStack.createStack(nbttagcompound);
2011-01-29 22:50:29 +01:00
2011-09-15 01:23:52 +01:00
if (itemstack != null) {
if (j >= 0 && j < this.items.length) {
this.items[j] = itemstack;
2011-01-29 22:50:29 +01:00
}
if (j >= 100 && j < this.armor.length + 100) {
this.armor[j - 100] = itemstack;
2011-01-29 22:50:29 +01:00
}
}
}
}
public int getSize() {
return this.items.length + 4;
2011-01-29 22:50:29 +01:00
}
public ItemStack getItem(int i) {
ItemStack[] aitemstack = this.items;
2011-01-29 22:50:29 +01:00
if (i >= aitemstack.length) {
i -= aitemstack.length;
aitemstack = this.armor;
2011-01-29 22:50:29 +01:00
}
return aitemstack[i];
}
2013-11-04 07:07:38 -06:00
public String getInventoryName() {
2012-03-01 10:49:23 +00:00
return "container.inventory";
2011-01-29 22:50:29 +01:00
}
2013-11-04 07:07:38 -06:00
public boolean k_() {
2013-03-13 17:33:27 -05:00
return false;
}
public int getMaxStackSize() {
2013-11-04 07:07:38 -06:00
return 64;
2011-01-29 22:50:29 +01:00
}
public boolean b(Block block) {
2013-11-04 07:07:38 -06:00
if (block.getMaterial().isAlwaysDestroyable()) {
2011-01-29 22:50:29 +01:00
return true;
} else {
ItemStack itemstack = this.getItem(this.itemInHandIndex);
2011-01-29 22:50:29 +01:00
return itemstack != null ? itemstack.b(block) : false;
}
}
2013-11-04 07:07:38 -06:00
public ItemStack d(int i) {
return this.armor[i];
}
2013-03-13 17:33:27 -05:00
public int l() {
2011-01-29 22:50:29 +01:00
int i = 0;
2012-11-06 06:05:28 -06:00
for (int j = 0; j < this.armor.length; ++j) {
if (this.armor[j] != null && this.armor[j].getItem() instanceof ItemArmor) {
2013-03-13 17:33:27 -05:00
int k = ((ItemArmor) this.armor[j].getItem()).c;
2011-01-29 22:50:29 +01:00
2012-11-06 06:05:28 -06:00
i += k;
2011-01-29 22:50:29 +01:00
}
}
2011-11-20 00:01:14 -08:00
return i;
2011-01-29 22:50:29 +01:00
}
2013-07-01 06:03:00 -05:00
public void a(float f) {
f /= 4.0F;
if (f < 1.0F) {
f = 1.0F;
2011-11-20 00:01:14 -08:00
}
2013-07-01 06:03:00 -05:00
for (int i = 0; i < this.armor.length; ++i) {
if (this.armor[i] != null && this.armor[i].getItem() instanceof ItemArmor) {
this.armor[i].damage((int) f, this.player);
if (this.armor[i].count == 0) {
this.armor[i] = null;
2011-01-29 22:50:29 +01:00
}
}
}
}
2013-03-13 17:33:27 -05:00
public void m() {
2011-01-29 22:50:29 +01:00
int i;
for (i = 0; i < this.items.length; ++i) {
if (this.items[i] != null) {
2013-11-04 07:07:38 -06:00
this.player.a(this.items[i], true, false);
this.items[i] = null;
2011-01-29 22:50:29 +01:00
}
}
for (i = 0; i < this.armor.length; ++i) {
if (this.armor[i] != null) {
2013-11-04 07:07:38 -06:00
this.player.a(this.armor[i], true, false);
this.armor[i] = null;
2011-01-29 22:50:29 +01:00
}
}
}
public void update() {
2011-03-31 21:40:00 +01:00
this.e = true;
2011-01-29 22:50:29 +01:00
}
2012-02-29 22:31:04 +01:00
public void setCarried(ItemStack itemstack) {
2012-07-29 02:33:13 -05:00
this.g = itemstack;
2011-01-29 22:50:29 +01:00
}
2012-02-29 22:31:04 +01:00
public ItemStack getCarried() {
2012-05-31 10:15:00 -05:00
// CraftBukkit start
2012-07-29 02:33:13 -05:00
if (this.g != null && this.g.count == 0) {
2012-05-31 10:15:00 -05:00
this.setCarried(null);
}
// CraftBukkit end
2012-07-29 02:33:13 -05:00
return this.g;
2011-01-29 22:50:29 +01:00
}
2013-03-13 17:33:27 -05:00
public boolean a(EntityHuman entityhuman) {
2014-03-20 22:26:30 -06:00
return this.player.dead ? false : entityhuman.f(this.player) <= 64.0D;
2011-01-29 22:50:29 +01:00
}
2011-05-26 13:48:22 +01:00
public boolean c(ItemStack itemstack) {
2012-11-06 06:05:28 -06:00
int i;
2012-07-29 02:33:13 -05:00
2012-11-06 06:05:28 -06:00
for (i = 0; i < this.armor.length; ++i) {
if (this.armor[i] != null && this.armor[i].doMaterialsMatch(itemstack)) {
2011-05-26 13:48:22 +01:00
return true;
}
}
2012-11-06 06:05:28 -06:00
for (i = 0; i < this.items.length; ++i) {
if (this.items[i] != null && this.items[i].doMaterialsMatch(itemstack)) {
2011-05-26 13:48:22 +01:00
return true;
}
}
return false;
}
2011-09-15 01:23:52 +01:00
2012-07-29 02:33:13 -05:00
public void startOpen() {}
2011-09-15 01:23:52 +01:00
2013-11-04 07:07:38 -06:00
public void l_() {}
2013-03-13 17:33:27 -05:00
public boolean b(int i, ItemStack itemstack) {
return true;
}
2011-11-20 00:01:14 -08:00
2012-07-29 02:33:13 -05:00
public void b(PlayerInventory playerinventory) {
2011-11-20 00:01:14 -08:00
int i;
for (i = 0; i < this.items.length; ++i) {
this.items[i] = ItemStack.b(playerinventory.items[i]);
2011-11-20 00:01:14 -08:00
}
for (i = 0; i < this.armor.length; ++i) {
this.armor[i] = ItemStack.b(playerinventory.armor[i]);
2011-11-20 00:01:14 -08:00
}
2012-12-19 22:03:52 -06:00
this.itemInHandIndex = playerinventory.itemInHandIndex;
2011-11-20 00:01:14 -08:00
}
2011-01-29 22:50:29 +01:00
}