summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/bukkit/craftbukkit/inventory/InventoryIterator.java
blob: cd4a0d0705514eb66d828be30e3c8c2b0b73b8e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.bukkit.craftbukkit.inventory;

import java.util.ListIterator;

import org.bukkit.inventory.Inventory;
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

    InventoryIterator(Inventory craftInventory) {
        this.inventory = craftInventory;
        this.nextIndex = 0;
    }

    public boolean hasNext() {
        return nextIndex < inventory.getSize();
    }

    public ItemStack next() {
        lastDirection = true;
        return inventory.getItem(nextIndex++);
    }

    public int nextIndex() {
        return nextIndex;
    }

    public boolean hasPrevious() {
        return nextIndex > 0;
    }

    public ItemStack previous() {
        lastDirection = false;
        return inventory.getItem(--nextIndex);
    }

    public int previousIndex() {
        return nextIndex - 1;
    }

    public void set(ItemStack item) {
        int i = lastDirection ? nextIndex - 1 : nextIndex;
        inventory.setItem(i, item);
    }

    public void add(ItemStack item) {
        throw new UnsupportedOperationException("Can't change the size of an inventory!");
    }

    public void remove() {
        throw new UnsupportedOperationException("Can't change the size of an inventory!");
    }
}