[Bleeding] Add iterator() with starting index. Addresses BUKKIT-1246

This commit is contained in:
Celtic Minstrel 2012-03-17 10:37:54 -04:00 committed by EvilSeph
parent 7966531113
commit cd9779196b
2 changed files with 16 additions and 1 deletions

View File

@ -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();
}

View File

@ -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);
}