mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-24 18:07:39 +01:00
[Bleeding] Add iterator() with starting index. Addresses BUKKIT-1246
This commit is contained in:
parent
7966531113
commit
cd9779196b
@ -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();
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user