3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-12-20 21:40:06 +01:00

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

Dieser Commit ist enthalten in:
Celtic Minstrel 2012-03-17 10:37:54 -04:00 committet von EvilSeph
Ursprung 7966531113
Commit cd9779196b
2 geänderte Dateien mit 16 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -381,6 +381,13 @@ public class CraftInventory implements Inventory {
return new InventoryIterator(this); 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() { public List<HumanEntity> getViewers() {
return this.inventory.getViewers(); return this.inventory.getViewers();
} }

Datei anzeigen

@ -8,13 +8,18 @@ import org.bukkit.inventory.ItemStack;
public class InventoryIterator implements ListIterator<ItemStack> { public class InventoryIterator implements ListIterator<ItemStack> {
private final Inventory inventory; private final Inventory inventory;
private int nextIndex; 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) { InventoryIterator(Inventory craftInventory) {
this.inventory = craftInventory; this.inventory = craftInventory;
this.nextIndex = 0; this.nextIndex = 0;
} }
InventoryIterator(Inventory craftInventory, int index) {
this.inventory = craftInventory;
this.nextIndex = index;
}
public boolean hasNext() { public boolean hasNext() {
return nextIndex < inventory.getSize(); return nextIndex < inventory.getSize();
} }
@ -42,6 +47,9 @@ public class InventoryIterator implements ListIterator<ItemStack> {
} }
public void set(ItemStack item) { public void set(ItemStack item) {
if (lastDirection == null) {
throw new IllegalStateException("No current item!");
}
int i = lastDirection ? nextIndex - 1 : nextIndex; int i = lastDirection ? nextIndex - 1 : nextIndex;
inventory.setItem(i, item); inventory.setItem(i, item);
} }