summaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftInventory.java65
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftInventoryPlayer.java26
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftItemStack.java2
3 files changed, 51 insertions, 42 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/CraftInventory.java
index c74ecea3..b79413d6 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftInventory.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftInventory.java
@@ -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;
- }
-
- // 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;
+ } 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 );
}
-
- // It fits partially
- partialItem.setAmount( maxAmount );
- item.setAmount( amount + partialAmount - maxAmount );
}
}
return leftover;
}
+
+ private int getMaxItemStack() {
+ return getInventory().c();
+ }
+
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftInventoryPlayer.java b/src/main/java/org/bukkit/craftbukkit/CraftInventoryPlayer.java
index 3ee882fc..666698b9 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftInventoryPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftInventoryPlayer.java
@@ -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 );
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/CraftItemStack.java
index d1571ac4..710bad4f 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftItemStack.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftItemStack.java
@@ -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