-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathItemStackHashStrategy.java
More file actions
148 lines (129 loc) · 4.79 KB
/
ItemStackHashStrategy.java
File metadata and controls
148 lines (129 loc) · 4.79 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gregtech.api.util;
import net.minecraft.item.ItemStack;
import it.unimi.dsi.fastutil.Hash;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
/**
* A configurable generator of hashing strategies, allowing for consideration of select properties of ItemStacks when
* considering equality.
*/
public interface ItemStackHashStrategy extends Hash.Strategy<ItemStack> {
/**
* @return a builder object for producing a custom ItemStackHashStrategy.
*/
static Builder builder() {
return new Builder();
}
/**
* Generates an ItemStackHash configured to compare every aspect of ItemStacks.
*
* @return the ItemStackHashStrategy as described above.
*/
static ItemStackHashStrategy comparingAll() {
return builder().compareItem(true)
.compareCount(true)
.compareDamage(true)
.compareTag(true)
.build();
}
/**
* Generates an ItemStackHash configured to compare every aspect of ItemStacks except the number
* of items in the stack.
*
* @return the ItemStackHashStrategy as described above.
*/
static ItemStackHashStrategy comparingAllButCount() {
return builder().compareItem(true)
.compareDamage(true)
.compareTag(true)
.build();
}
static ItemStackHashStrategy comparingItemDamageCount() {
return builder().compareItem(true)
.compareDamage(true)
.compareCount(true)
.build();
}
/**
* Builder pattern class for generating customized ItemStackHashStrategy
*/
class Builder {
private boolean item, count, damage, tag, meta;
/**
* Defines whether the Item type should be considered for equality.
*
* @param choice {@code true} to consider this property, {@code false} to ignore it.
* @return {@code this}
*/
public Builder compareItem(boolean choice) {
item = choice;
return this;
}
/**
* Defines whether stack size should be considered for equality.
*
* @param choice {@code true} to consider this property, {@code false} to ignore it.
* @return {@code this}
*/
public Builder compareCount(boolean choice) {
count = choice;
return this;
}
/**
* Defines whether damage values should be considered for equality.
*
* @param choice {@code true} to consider this property, {@code false} to ignore it.
* @return {@code this}
*/
public Builder compareDamage(boolean choice) {
damage = choice;
return this;
}
/**
* Defines whether metadata values should be considered for equality.
*
* @param choice {@code true} to consider this property, {@code false} to ignore it.
* @return {@code this}
*/
public Builder compareMetadata(boolean choice) {
meta = choice;
return this;
}
/**
* Defines whether NBT Tags should be considered for equality.
*
* @param choice {@code true} to consider this property, {@code false} to ignore it.
* @return {@code this}
*/
public Builder compareTag(boolean choice) {
tag = choice;
return this;
}
/**
* @return the ItemStackHashStrategy as configured by "compare" methods.
*/
public ItemStackHashStrategy build() {
return new ItemStackHashStrategy() {
@Override
public int hashCode(@Nullable ItemStack o) {
return o == null || o.isEmpty() ? 0 : Objects.hash(
item ? o.getItem() : null,
count ? o.getCount() : null,
damage ? o.getItemDamage() : null,
tag ? o.getTagCompound() : null,
meta ? o.getMetadata() : null);
}
@Override
public boolean equals(@Nullable ItemStack a, @Nullable ItemStack b) {
if (a == null || a.isEmpty()) return b == null || b.isEmpty();
if (b == null || b.isEmpty()) return false;
return (!item || a.getItem() == b.getItem()) &&
(!count || a.getCount() == b.getCount()) &&
(!damage || a.getItemDamage() == b.getItemDamage()) &&
(!meta || a.getMetadata() == b.getMetadata()) &&
(!tag || Objects.equals(a.getTagCompound(), b.getTagCompound()));
}
};
}
}
}