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

486 Zeilen
13 KiB
Java

2011-01-29 22:50:29 +01:00
package net.minecraft.server;
// 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];
public int itemInHandIndex = 0;
2012-02-29 22:31:04 +01:00
public EntityHuman player;
2011-01-29 22:50:29 +01:00
private ItemStack f;
2011-03-31 22:40:00 +02:00
public boolean e = false;
2011-01-29 22:50:29 +01:00
// CraftBukkit start
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 22:40:00 +02:00
}
public static int getHotbarSize() {
2011-03-31 22:40:00 +02:00
return 9;
2011-01-29 22:50:29 +01:00
}
2012-03-01 11:49:23 +01:00
private int f(int i) {
for (int j = 0; j < this.items.length; ++j) {
if (this.items[j] != null && this.items[j].id == i) {
2011-01-29 22:50:29 +01:00
return j;
}
}
return -1;
}
private int firstPartial(ItemStack itemstack) {
for (int i = 0; i < this.items.length; ++i) {
2012-01-12 23:10:13 +01:00
if (this.items[i] != null && this.items[i].id == itemstack.id && 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;
}
// 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)
if (this.items[i] != null && this.items[i].id == itemstack.id && 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())) {
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
2011-09-15 02:23:52 +02:00
private int m() {
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;
}
2011-05-26 14:48:22 +02:00
private int e(ItemStack itemstack) {
2011-01-29 22:50:29 +01:00
int i = itemstack.id;
int j = itemstack.count;
2011-11-20 09:01:14 +01:00
int k;
2011-01-29 22:50:29 +01:00
2011-11-20 09:01:14 +01:00
if (itemstack.getMaxStackSize() == 1) {
2011-09-15 02:23:52 +02:00
k = this.m();
2011-11-20 09:01:14 +01:00
if (k < 0) {
return j;
} else {
if (this.items[k] == null) {
this.items[k] = ItemStack.b(itemstack);
}
2011-01-29 22:50:29 +01:00
2011-11-20 09:01:14 +01:00
return 0;
}
2011-01-29 22:50:29 +01:00
} else {
2011-11-20 09:01:14 +01:00
k = this.firstPartial(itemstack);
if (k < 0) {
k = this.m();
2011-01-29 22:50:29 +01:00
}
2011-11-20 09:01:14 +01:00
if (k < 0) {
return j;
} else {
if (this.items[k] == null) {
this.items[k] = new ItemStack(i, 0, itemstack.getData());
2012-01-12 23:10:13 +01:00
if (itemstack.hasTag()) {
this.items[k].setTag((NBTTagCompound) itemstack.getTag().clone());
}
2011-11-20 09:01:14 +01:00
}
2011-01-29 22:50:29 +01:00
2011-11-20 09:01:14 +01:00
int l = j;
2011-01-29 22:50:29 +01:00
2011-11-20 09:01:14 +01:00
if (j > this.items[k].getMaxStackSize() - this.items[k].count) {
l = this.items[k].getMaxStackSize() - this.items[k].count;
}
2011-01-29 22:50:29 +01:00
2011-11-20 09:01:14 +01:00
if (l > this.getMaxStackSize() - this.items[k].count) {
l = this.getMaxStackSize() - this.items[k].count;
}
if (l == 0) {
return j;
} else {
j -= l;
this.items[k].count += l;
this.items[k].b = 5;
return j;
}
2011-01-29 22:50:29 +01:00
}
}
}
2011-11-20 09:01:14 +01:00
public void i() {
for (int i = 0; i < this.items.length; ++i) {
2011-05-26 14:48:22 +02: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
}
}
}
2012-03-01 11:49:23 +01:00
public boolean c(int i) {
int j = this.f(i);
2011-01-29 22:50:29 +01:00
if (j < 0) {
return false;
} else {
if (--this.items[j].count <= 0) {
this.items[j] = null;
2011-01-29 22:50:29 +01:00
}
return true;
}
}
2012-03-01 11:49:23 +01:00
public boolean d(int i) {
int j = this.f(i);
2011-09-15 02:23:52 +02:00
return j >= 0;
}
public boolean pickup(ItemStack itemstack) {
2011-05-26 14:48:22 +02:00
int i;
if (itemstack.f()) {
2011-09-15 02:23:52 +02:00
i = this.m();
2011-05-26 14:48:22 +02:00
if (i >= 0) {
2011-05-28 22:50:08 +02:00
this.items[i] = ItemStack.b(itemstack);
2011-05-26 14:48:22 +02:00
this.items[i].b = 5;
itemstack.count = 0;
2011-01-29 22:50:29 +01:00
return true;
2012-02-29 22:31:04 +01:00
} else if (this.player.abilities.canInstantlyBuild) {
2011-11-20 09:01:14 +01:00
itemstack.count = 0;
return true;
2011-05-26 14:48:22 +02:00
} else {
return false;
2011-01-29 22:50:29 +01:00
}
} else {
2011-05-26 14:48:22 +02:00
do {
i = itemstack.count;
itemstack.count = this.e(itemstack);
} while (itemstack.count > 0 && itemstack.count < i);
2012-02-29 22:31:04 +01:00
if (itemstack.count == i && this.player.abilities.canInstantlyBuild) {
2011-11-20 09:01:14 +01:00
itemstack.count = 0;
return true;
} else {
return itemstack.count < i;
}
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 11:49:23 +01: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) {
NBTTagCompound nbttagcompound = (NBTTagCompound) nbttaglist.get(i);
int j = nbttagcompound.getByte("Slot") & 255;
2011-09-15 02:23:52 +02:00
ItemStack itemstack = ItemStack.a(nbttagcompound);
2011-01-29 22:50:29 +01:00
2011-09-15 02:23:52 +02: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];
}
public String getName() {
2012-03-01 11:49:23 +01:00
return "container.inventory";
2011-01-29 22:50:29 +01:00
}
public int getMaxStackSize() {
return maxStack; // CraftBukkit
2011-01-29 22:50:29 +01:00
}
public int a(Entity entity) {
ItemStack itemstack = this.getItem(this.itemInHandIndex);
2011-01-29 22:50:29 +01:00
return itemstack != null ? itemstack.a(entity) : 1;
}
public boolean b(Block block) {
// CraftBukkit start - fixed NPE
if (block == null) {
return false;
}
// CraftBukkit end
2012-02-29 22:31:04 +01:00
if (block.material.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;
}
}
2011-11-20 09:01:14 +01:00
public int j() {
2011-01-29 22:50:29 +01:00
int i = 0;
2011-11-20 09:01:14 +01:00
for (int j = 0; j < this.armor.length; ++j) {
if (this.armor[j] != null && this.armor[j].getItem() instanceof ItemArmor) {
int k = ((ItemArmor) this.armor[j].getItem()).b;
2011-01-29 22:50:29 +01:00
2011-11-20 09:01:14 +01:00
i += k;
2011-01-29 22:50:29 +01:00
}
}
2011-11-20 09:01:14 +01:00
return i;
2011-01-29 22:50:29 +01:00
}
2012-03-01 11:49:23 +01:00
public void e(int i) {
2011-11-20 09:01:14 +01:00
i /= 4;
if (i < 1) {
i = 1;
}
for (int j = 0; j < this.armor.length; ++j) {
if (this.armor[j] != null && this.armor[j].getItem() instanceof ItemArmor) {
2012-02-29 22:31:04 +01:00
this.armor[j].damage(i, this.player);
if (this.armor[j].count == 0) {
2012-02-29 22:31:04 +01:00
this.armor[j].a(this.player);
this.armor[j] = null;
2011-01-29 22:50:29 +01:00
}
}
}
}
2011-11-20 09:01:14 +01:00
public void k() {
2011-01-29 22:50:29 +01:00
int i;
for (i = 0; i < this.items.length; ++i) {
if (this.items[i] != null) {
2012-02-29 22:31:04 +01:00
this.player.a(this.items[i], true);
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) {
2012-02-29 22:31:04 +01:00
this.player.a(this.armor[i], true);
this.armor[i] = null;
2011-01-29 22:50:29 +01:00
}
}
}
public void update() {
2011-03-31 22:40:00 +02: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) {
2011-01-29 22:50:29 +01:00
this.f = itemstack;
2012-02-29 22:31:04 +01:00
this.player.carriedChanged(itemstack);
2011-01-29 22:50:29 +01:00
}
2012-02-29 22:31:04 +01:00
public ItemStack getCarried() {
2012-05-31 17:15:00 +02:00
// CraftBukkit start
if (this.f != null && this.f.count == 0) {
this.setCarried(null);
}
// CraftBukkit end
2011-01-29 22:50:29 +01:00
return this.f;
}
2011-09-15 02:23:52 +02:00
public boolean a(EntityHuman entityhuman) {
2012-03-01 11:49:23 +01:00
return this.player.dead ? false : entityhuman.j(this.player) <= 64.0D;
2011-01-29 22:50:29 +01:00
}
2011-05-26 14:48:22 +02:00
public boolean c(ItemStack itemstack) {
int i;
for (i = 0; i < this.armor.length; ++i) {
if (this.armor[i] != null && this.armor[i].c(itemstack)) {
return true;
}
}
for (i = 0; i < this.items.length; ++i) {
if (this.items[i] != null && this.items[i].c(itemstack)) {
return true;
}
}
return false;
}
2011-09-15 02:23:52 +02:00
2011-11-20 09:01:14 +01:00
public void f() {}
2011-09-15 02:23:52 +02:00
2011-11-20 09:01:14 +01:00
public void g() {}
public void a(PlayerInventory playerinventory) {
2011-11-20 09:01:14 +01:00
int i;
for (i = 0; i < this.items.length; ++i) {
this.items[i] = ItemStack.b(playerinventory.items[i]);
2011-11-20 09:01:14 +01:00
}
for (i = 0; i < this.armor.length; ++i) {
this.armor[i] = ItemStack.b(playerinventory.armor[i]);
2011-11-20 09:01:14 +01:00
}
}
2011-01-29 22:50:29 +01:00
}