diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java index 89e47bb355..4866d1aee6 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java @@ -50,6 +50,18 @@ public class CraftInventory implements Inventory { return item.isEmpty() ? null : CraftItemStack.asCraftMirror(item); } + protected ItemStack[] asCraftMirror(List mcItems) { + int size = mcItems.size(); + ItemStack[] items = new ItemStack[size]; + + for (int i = 0; i < size; i++) { + net.minecraft.server.ItemStack mcItem = mcItems.get(i); + items[i] = (mcItem.isEmpty()) ? null : CraftItemStack.asCraftMirror(mcItem); + } + + return items; + } + @Override public ItemStack[] getStorageContents() { return getContents(); @@ -61,14 +73,9 @@ public class CraftInventory implements Inventory { } public ItemStack[] getContents() { - ItemStack[] items = new ItemStack[getSize()]; List mcItems = getInventory().getContents(); - int size = Math.min(items.length, mcItems.size()); - for (int i = 0; i < size; i++) { - items[i] = (mcItems.get(i).isEmpty()) ? null : CraftItemStack.asCraftMirror(mcItems.get(i)); - } - return items; + return asCraftMirror(mcItems); } public void setContents(ItemStack[] items) { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCrafting.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCrafting.java index 24d1804ac7..0f0dd1997f 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCrafting.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCrafting.java @@ -34,10 +34,8 @@ public class CraftInventoryCrafting extends CraftInventory implements CraftingIn @Override public void setContents(ItemStack[] items) { - int resultLen = getResultInventory().getContents().size(); - int len = getMatrixInventory().getContents().size() + resultLen; - if (len > items.length) { - throw new IllegalArgumentException("Invalid inventory size; expected " + len + " or less"); + if (getSize() > items.length) { + throw new IllegalArgumentException("Invalid inventory size; expected " + getSize() + " or less"); } setContents(items[0], Arrays.copyOfRange(items, 1, items.length)); } @@ -88,13 +86,8 @@ public class CraftInventoryCrafting extends CraftInventory implements CraftingIn public ItemStack[] getMatrix() { List matrix = getMatrixInventory().getContents(); - ItemStack[] items = new ItemStack[matrix.size()]; - for (int i = 0; i < matrix.size(); i++) { - items[i] = CraftItemStack.asCraftMirror(matrix.get(i)); - } - - return items; + return asCraftMirror(matrix); } public ItemStack getResult() { @@ -104,13 +97,11 @@ public class CraftInventoryCrafting extends CraftInventory implements CraftingIn } public void setMatrix(ItemStack[] contents) { - if (getMatrixInventory().getContents().size() > contents.length) { - throw new IllegalArgumentException("Invalid inventory size; expected " + getMatrixInventory().getContents().size() + " or less"); + if (getMatrixInventory().getSize() > contents.length) { + throw new IllegalArgumentException("Invalid inventory size; expected " + getMatrixInventory().getSize() + " or less"); } - List mcItems = getMatrixInventory().getContents(); - - for (int i = 0; i < mcItems.size(); i++) { + for (int i = 0; i < getMatrixInventory().getSize(); i++) { if (i < contents.length) { getMatrixInventory().setItem(i, CraftItemStack.asNMSCopy(contents[i])); } else { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryDoubleChest.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryDoubleChest.java index 39c7d6b7a7..f028b339a9 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryDoubleChest.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryDoubleChest.java @@ -1,6 +1,5 @@ package org.bukkit.craftbukkit.inventory; -import net.minecraft.server.ITileEntityContainer; import net.minecraft.server.ITileInventory; import org.bukkit.block.DoubleChest; import org.bukkit.inventory.DoubleChestInventory; @@ -44,11 +43,11 @@ public class CraftInventoryDoubleChest extends CraftInventory implements DoubleC @Override public void setContents(ItemStack[] items) { - if (getInventory().getContents().size() < items.length) { - throw new IllegalArgumentException("Invalid inventory size; expected " + getInventory().getContents().size() + " or less"); + if (getInventory().getSize() < items.length) { + throw new IllegalArgumentException("Invalid inventory size; expected " + getInventory().getSize() + " or less"); } ItemStack[] leftItems = new ItemStack[left.getSize()], rightItems = new ItemStack[right.getSize()]; - System.arraycopy(items, 0, leftItems, 0, Math.min(left.getSize(),items.length)); + System.arraycopy(items, 0, leftItems, 0, Math.min(left.getSize(), items.length)); left.setContents(leftItems); if (items.length >= left.getSize()) { System.arraycopy(items, left.getSize(), rightItems, 0, Math.min(right.getSize(), items.length - left.getSize())); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryPlayer.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryPlayer.java index d8c5364df9..67c54899de 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryPlayer.java @@ -1,7 +1,6 @@ package org.bukkit.craftbukkit.inventory; import com.google.common.base.Preconditions; -import java.util.Arrays; import net.minecraft.server.EntityPlayer; import net.minecraft.server.PacketPlayOutHeldItemSlot; import net.minecraft.server.PacketPlayOutSetSlot; @@ -25,10 +24,9 @@ public class CraftInventoryPlayer extends CraftInventory implements org.bukkit.i @Override public ItemStack[] getStorageContents() { - return Arrays.copyOfRange(getContents(), 0, getInventory().items.size()); + return asCraftMirror(getInventory().items); } - @Override public ItemStack getItemInMainHand() { return CraftItemStack.asCraftMirror(getInventory().getItemInHand()); @@ -148,8 +146,7 @@ public class CraftInventoryPlayer extends CraftInventory implements org.bukkit.i } public ItemStack[] getArmorContents() { - int start = getInventory().items.size(); - return Arrays.copyOfRange(getContents(), start, start + getInventory().armor.size()); + return asCraftMirror(getInventory().armor); } private void setSlots(ItemStack[] items, int baseSlot, int length) { @@ -179,8 +176,7 @@ public class CraftInventoryPlayer extends CraftInventory implements org.bukkit.i @Override public ItemStack[] getExtraContents() { - int start = getInventory().items.size() + getInventory().armor.size(); - return Arrays.copyOfRange(getContents(), start, start + getInventory().extraSlots.size()); + return asCraftMirror(getInventory().extraSlots); } @Override