aboutsummaryrefslogtreecommitdiff
path: root/src/Java/binnie/core/util/ItemStackSet.java
blob: a154f7edfcaedbed933e9a0d753d60b9a1b7629c (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package binnie.core.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import net.minecraft.item.ItemStack;

public class ItemStackSet
  implements Set<ItemStack>
{
  public String toString()
  {
    return this.itemStacks.toString();
  }
  
  List<ItemStack> itemStacks = new ArrayList();
  
  protected ItemStack getExisting(ItemStack stack)
  {
    for (ItemStack stack2 : this.itemStacks) {
      if (stack2.isItemEqual(stack)) {
        return stack2;
      }
    }
    return null;
  }
  
  public boolean add(ItemStack e)
  {
    if (e != null)
    {
      if (getExisting(e) == null) {
        return this.itemStacks.add(e.copy());
      }
      getExisting(e).stackSize += e.stackSize;
    }
    return false;
  }
  
  public boolean addAll(Collection<? extends ItemStack> c)
  {
    boolean addedAll = true;
    for (ItemStack stack : c) {
      addedAll = (add(stack)) && (addedAll);
    }
    return addedAll;
  }
  
  public void clear()
  {
    this.itemStacks.clear();
  }
  
  public boolean contains(Object o)
  {
    if (!(o instanceof ItemStack)) {
      return false;
    }
    return getExisting((ItemStack)o) != null;
  }
  
  public boolean containsAll(Collection<?> c)
  {
    boolean addedAll = true;
    for (Object o : c) {
      addedAll = (addedAll) && (contains(o));
    }
    return false;
  }
  
  public boolean isEmpty()
  {
    return this.itemStacks.isEmpty();
  }
  
  public Iterator<ItemStack> iterator()
  {
    return this.itemStacks.iterator();
  }
  
  public boolean remove(Object o)
  {
    if (contains(o))
    {
      ItemStack r = (ItemStack)o;
      ItemStack existing = getExisting(r);
      if (existing.stackSize > r.stackSize) {
        existing.stackSize -= r.stackSize;
      } else {
        this.itemStacks.remove(existing);
      }
    }
    return false;
  }
  
  public boolean removeAll(Collection<?> c)
  {
    boolean addedAll = true;
    for (Object o : c)
    {
      boolean removed = remove(o);
      addedAll = (removed) && (addedAll);
    }
    return false;
  }
  
  public boolean retainAll(Collection<?> c)
  {
    return this.itemStacks.retainAll(c);
  }
  
  public int size()
  {
    return this.itemStacks.size();
  }
  
  public Object[] toArray()
  {
    return this.itemStacks.toArray();
  }
  
  public <T> T[] toArray(T[] a)
  {
    return this.itemStacks.toArray(a);
  }
}