Fixed some bugs with inventory

By: Erik Broes <erikbroes@grum.nl>
This commit is contained in:
CraftBukkit/Spigot 2011-01-09 23:41:05 +01:00
parent 31ed08bab5
commit 87747a1304
3 changed files with 51 additions and 42 deletions

View file

@ -26,12 +26,12 @@ public class CraftInventory implements org.bukkit.Inventory {
return getInventory().b();
}
public ItemStack getItem(int index) {
public CraftItemStack getItem(int index) {
return new CraftItemStack(getInventory().a(index));
}
public ItemStack[] getContents() {
ItemStack[] items = new ItemStack[getSize()];
public CraftItemStack[] getContents() {
CraftItemStack[] items = new CraftItemStack[getSize()];
net.minecraft.server.ItemStack[] mcItems = getInventory().getContents();
for (int i = 0; i < mcItems.length; i++ ) {
@ -127,7 +127,8 @@ public class CraftInventory implements org.bukkit.Inventory {
public int firstPartial(int materialId) {
ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) {
if (inventory[i].getAmount() <= inventory[i].getMaxStackSize()) {
ItemStack item = inventory[i];
if (item != null && item.getTypeID() == materialId && item.getAmount() < item.getMaxStackSize()) {
return i;
}
}
@ -141,8 +142,8 @@ public class CraftInventory implements org.bukkit.Inventory {
public int firstPartial(ItemStack item) {
return firstPartial(item.getTypeID());
}
public HashMap<Integer,ItemStack> addItem(ItemStack... items) {
public HashMap<Integer, ItemStack> addItem(ItemStack... items) {
HashMap<Integer,ItemStack> leftover = new HashMap<Integer,ItemStack>();
/* TODO: some optimization
@ -165,31 +166,43 @@ public class CraftInventory implements org.bukkit.Inventory {
if (firstFree == -1) {
// No space at all!
leftover.put(i, item);
break;
} else {
// Just store it
setItem( firstFree, item );
// More than a single stack!
if (item.getAmount() > getMaxItemStack()) {
setItem( firstFree, new ItemStack(item.getTypeID(), getMaxItemStack()));
item.setAmount(item.getAmount() - getMaxItemStack());
} else {
// Just store it
setItem( firstFree, item );
break;
}
}
break;
} else {
// So, apparently it might only partially fit, well lets do just that
ItemStack partialItem = getItem(firstPartial);
int amount = item.getAmount();
int partialAmount = partialItem.getAmount();
int maxAmount = partialItem.getMaxStackSize();
// Check if it fully fits
if (amount + partialAmount <= maxAmount) {
partialItem.setAmount( amount + partialAmount );
break;
}
// It fits partially
partialItem.setAmount( maxAmount );
item.setAmount( amount + partialAmount - maxAmount );
}
// So, apparently it might only partially fit, well lets do just that
ItemStack partialItem = getItem(firstPartial);
int amount = item.getAmount();
int partialAmount = partialItem.getAmount();
int maxAmount = partialItem.getMaxStackSize();
// Check if it fully fits
if (amount + partialAmount <= maxAmount) {
partialItem.setAmount( amount + partialAmount );
break;
}
// It fits partially
partialItem.setAmount( maxAmount );
item.setAmount( amount + partialAmount - maxAmount );
}
}
return leftover;
}
private int getMaxItemStack() {
return getInventory().c();
}
}

View file

@ -1,7 +1,5 @@
package org.bukkit.craftbukkit;
import java.util.ArrayList;
import net.minecraft.server.InventoryPlayer;
import org.bukkit.ItemStack;
@ -16,36 +14,34 @@ public class CraftInventoryPlayer extends CraftInventory implements PlayerInvent
return (InventoryPlayer) inventory;
}
public ArrayList<ItemStack> getArmorContents() {
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
for (net.minecraft.server.ItemStack item : getInventory().getArmorContents()) {
ItemStack i = null;
if (item != null) {
i = new CraftItemStack(item);
}
items.add(i);
public CraftItemStack[] getArmorContents() {
net.minecraft.server.ItemStack[] mcItems = getInventory().getArmorContents();
CraftItemStack[] items = new CraftItemStack[mcItems.length];
for (int i = 0; i < mcItems.length; i++ ) {
items[i] = new CraftItemStack(mcItems[i]);
}
return items;
}
public ItemStack getItemInHand() {
public CraftItemStack getItemInHand() {
return new CraftItemStack( getInventory().e() );
}
public ItemStack getHelmet() {
public CraftItemStack getHelmet() {
return getItem( getSize() - 4 );
}
public ItemStack getChestplate() {
public CraftItemStack getChestplate() {
return getItem( getSize() - 3 );
}
public ItemStack getLeggings() {
public CraftItemStack getLeggings() {
return getItem( getSize() - 2 );
}
public ItemStack getBoots() {
public CraftItemStack getBoots() {
return getItem( getSize() - 1 );
}

View file

@ -48,7 +48,7 @@ public class CraftItemStack extends ItemStack {
@Override
public int getAmount() {
super.setAmount(item != null ? item.a : 0); // sync, needed?
return item.a;
return (item != null ? item.a : 0);
}
@Override