diff options
author | Celtic Minstrel <celtic.minstrel.ca@some.place> | 2012-03-17 10:37:54 -0400 |
---|---|---|
committer | EvilSeph <evilseph@gmail.com> | 2012-03-20 19:40:44 -0400 |
commit | cd9779196bbdf01a6adadec74dafedb0a24b39a4 (patch) | |
tree | 26dfbbd85f2c61de3199f70f843465688e00885e /src/main | |
parent | 7966531113ded3d91144bdafff9674e32e468333 (diff) | |
download | craftbukkit-cd9779196bbdf01a6adadec74dafedb0a24b39a4.tar craftbukkit-cd9779196bbdf01a6adadec74dafedb0a24b39a4.tar.gz craftbukkit-cd9779196bbdf01a6adadec74dafedb0a24b39a4.tar.lz craftbukkit-cd9779196bbdf01a6adadec74dafedb0a24b39a4.tar.xz craftbukkit-cd9779196bbdf01a6adadec74dafedb0a24b39a4.zip |
[Bleeding] Add iterator() with starting index. Addresses BUKKIT-1246
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java | 7 | ||||
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/inventory/InventoryIterator.java | 10 |
2 files changed, 16 insertions, 1 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java index cc055e2f..0216f9dd 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<ItemStack> 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<HumanEntity> 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 cd4a0d07..e3b5f42a 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<ItemStack> { 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<ItemStack> { } public void set(ItemStack item) { + if (lastDirection == null) { + throw new IllegalStateException("No current item!"); + } int i = lastDirection ? nextIndex - 1 : nextIndex; inventory.setItem(i, item); } |