diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java index cc055e2f79..0216f9dd1f 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java @@ -381,6 +381,13 @@ public class CraftInventory implements Inventory { return new InventoryIterator(this); } + public ListIterator iterator(int index) { + if (index < 0) { + index += getSize() + 1; // ie, with -1, previous() will return the last element + } + return new InventoryIterator(this, index); + } + public List getViewers() { return this.inventory.getViewers(); } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/InventoryIterator.java b/src/main/java/org/bukkit/craftbukkit/inventory/InventoryIterator.java index cd4a0d0705..e3b5f42ada 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/InventoryIterator.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/InventoryIterator.java @@ -8,13 +8,18 @@ import org.bukkit.inventory.ItemStack; public class InventoryIterator implements ListIterator { private final Inventory inventory; private int nextIndex; - private boolean lastDirection; // true = forward, false = backward + private Boolean lastDirection; // true = forward, false = backward, null = haven't moved yet InventoryIterator(Inventory craftInventory) { this.inventory = craftInventory; this.nextIndex = 0; } + InventoryIterator(Inventory craftInventory, int index) { + this.inventory = craftInventory; + this.nextIndex = index; + } + public boolean hasNext() { return nextIndex < inventory.getSize(); } @@ -42,6 +47,9 @@ public class InventoryIterator implements ListIterator { } public void set(ItemStack item) { + if (lastDirection == null) { + throw new IllegalStateException("No current item!"); + } int i = lastDirection ? nextIndex - 1 : nextIndex; inventory.setItem(i, item); }