/
StackUtil.java
166 lines (139 loc) · 4.53 KB
/
StackUtil.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package mezz.jei.util;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.registry.GameData;
import net.minecraftforge.oredict.OreDictionary;
public class StackUtil {
@Nonnull
public static List<ItemStack> removeDuplicateItemStacks(Iterable<ItemStack> stacks) {
List<ItemStack> newStacks = new ArrayList<>();
if (stacks == null) {
return newStacks;
}
for (ItemStack stack : stacks) {
if (stack != null && containsStack(newStacks, stack) == null) {
newStacks.add(stack);
}
}
return newStacks;
}
/* Returns an ItemStack from "stacks" if it isIdentical to "contains" */
@Nullable
public static ItemStack containsStack(@Nullable Iterable<ItemStack> stacks, @Nullable ItemStack contains) {
if (stacks == null || contains == null) {
return null;
}
for (ItemStack stack : stacks) {
if (isIdentical(stack, contains)) {
return stack;
}
}
return null;
}
public static boolean isIdentical(@Nullable ItemStack lhs, @Nullable ItemStack rhs) {
if (lhs == rhs) {
return true;
}
if (lhs == null || rhs == null) {
return false;
}
if (lhs.getItem() != rhs.getItem()) {
return false;
}
if (lhs.getItemDamage() != OreDictionary.WILDCARD_VALUE) {
if (lhs.getItemDamage() != rhs.getItemDamage()) {
return false;
}
}
return ItemStack.areItemStackTagsEqual(lhs, rhs);
}
/**
* Returns all the subtypes of itemStack if it has a wildcard meta value.
*/
@Nonnull
public static List<ItemStack> getSubtypes(@Nonnull ItemStack itemStack) {
List<ItemStack> itemStacks = new ArrayList<>();
Item item = itemStack.getItem();
if (item == null) {
return itemStacks;
}
if (item.getDamage(itemStack) != OreDictionary.WILDCARD_VALUE) {
return Collections.singletonList(itemStack);
}
if (!item.getHasSubtypes()) {
return Collections.singletonList(new ItemStack(item));
}
List<ItemStack> subItems = new ArrayList<>();
for (CreativeTabs itemTab : item.getCreativeTabs()) {
subItems.clear();
item.getSubItems(item, itemTab, subItems);
itemStacks.addAll(subItems);
if (subItems.isEmpty()) {
ItemStack stack = new ItemStack(item);
if (stack.getItem() != null) {
itemStacks.add(stack);
}
}
}
return removeDuplicateItemStacks(itemStacks);
}
public static List<ItemStack> getAllSubtypes(Iterable stacks) {
List<ItemStack> allSubtypes = new ArrayList<>();
getAllSubtypes(allSubtypes, stacks);
return allSubtypes;
}
private static void getAllSubtypes(List<ItemStack> subtypesList, Iterable stacks) {
for (Object obj : stacks) {
if (obj instanceof ItemStack) {
ItemStack itemStack = (ItemStack) obj;
List<ItemStack> subtypes = getSubtypes(itemStack);
subtypesList.addAll(subtypes);
} else if (obj instanceof Iterable) {
getAllSubtypes(subtypesList, (Iterable) obj);
} else if (obj != null) {
Log.error("Unknown object found: {}", obj);
}
}
}
@Nonnull
public static List<ItemStack> toItemStackList(@Nullable Iterable stacks) {
if (stacks == null) {
return Collections.emptyList();
}
List<ItemStack> itemStacksList = new ArrayList<>();
toItemStackList(itemStacksList, stacks);
return removeDuplicateItemStacks(itemStacksList);
}
private static void toItemStackList(@Nonnull List<ItemStack> itemStackList, @Nonnull Iterable input) {
for (Object obj : input) {
if (obj instanceof Iterable) {
toItemStackList(itemStackList, (Iterable) obj);
} else if (obj instanceof ItemStack) {
itemStackList.add((ItemStack) obj);
} else if (obj != null) {
Log.error("Unknown object found: {}", obj);
}
}
}
@Nonnull
public static String uniqueIdentifierForStack(@Nonnull ItemStack stack, boolean wildcard) {
Item item = stack.getItem();
Object itemName = GameData.getItemRegistry().getNameForObject(item);
String itemNameString = String.valueOf(itemName);
int metadata = stack.getMetadata();
if (wildcard || metadata == OreDictionary.WILDCARD_VALUE) {
return itemNameString;
}
StringBuilder itemKey = new StringBuilder(itemNameString).append(':').append(metadata);
if (stack.hasTagCompound()) {
itemKey.append(':').append(stack.getTagCompound());
}
return itemKey.toString();
}
}