diff options
author | Lukas Hennig <lukas@wirsindwir.de> | 2017-04-17 18:09:15 +1000 |
---|---|---|
committer | md_5 <git@md-5.net> | 2017-04-17 18:09:15 +1000 |
commit | e04a1793ae35519baf1f9e1b94c812a6e6712d5a (patch) | |
tree | dc7bb7f0a3edfa9d0dd490dfa8343e038c6bcc04 | |
parent | e17c3d285b1026811acc1df8a1299d4ad7a6953a (diff) | |
download | craftbukkit-e04a1793ae35519baf1f9e1b94c812a6e6712d5a.tar craftbukkit-e04a1793ae35519baf1f9e1b94c812a6e6712d5a.tar.gz craftbukkit-e04a1793ae35519baf1f9e1b94c812a6e6712d5a.tar.lz craftbukkit-e04a1793ae35519baf1f9e1b94c812a6e6712d5a.tar.xz craftbukkit-e04a1793ae35519baf1f9e1b94c812a6e6712d5a.zip |
Inventory getContents Cleanup
4 files changed, 25 insertions, 32 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java index 89e47bb3..4866d1ae 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<net.minecraft.server.ItemStack> 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<net.minecraft.server.ItemStack> 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 24d1804a..0f0dd199 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<net.minecraft.server.ItemStack> 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<net.minecraft.server.ItemStack> 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 39c7d6b7..f028b339 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 d8c5364d..67c54899 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 |