/
StackUtil.java
393 lines (331 loc) · 10.8 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package mezz.jei.util;
import com.google.common.collect.Sets;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.registry.GameData;
import net.minecraftforge.oredict.OreDictionary;
import mezz.jei.config.Config;
public class StackUtil {
@Nonnull
public static List<ItemStack> removeDuplicateItemStacks(@Nonnull Iterable<ItemStack> stacks) {
List<ItemStack> newStacks = new ArrayList<>();
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 an ItemStack from "contains" */
@Nullable
public static ItemStack containsStack(@Nullable Iterable<ItemStack> stacks, @Nullable Iterable<ItemStack> contains) {
if (stacks == null || contains == null) {
return null;
}
for (ItemStack containStack : contains) {
ItemStack matchingStack = containsStack(stacks, containStack);
if (matchingStack != null) {
return matchingStack;
}
}
return null;
}
/* 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;
}
@Nonnull
public static List<ItemStack> condenseStacks(Collection<ItemStack> stacks) {
List<ItemStack> condensed = new ArrayList<>();
for (ItemStack stack : stacks) {
if (stack == null) {
continue;
}
if (stack.stackSize <= 0) {
continue;
}
boolean matched = false;
for (ItemStack cached : condensed) {
if ((cached.isItemEqual(stack) && ItemStack.areItemStackTagsEqual(cached, stack))) {
cached.stackSize += stack.stackSize;
matched = true;
}
}
if (!matched) {
ItemStack cached = stack.copy();
condensed.add(cached);
}
}
return condensed;
}
/**
* Counts how many full sets are contained in the passed stock.
* Returns a list of matching stacks from set, or null if there aren't enough for a complete match.
*/
@Nullable
public static List<ItemStack> containsSets(Collection<ItemStack> required, Collection<ItemStack> offered) {
int totalSets = 0;
List<ItemStack> matching = new ArrayList<>();
List<ItemStack> condensedRequired = condenseStacks(required);
List<ItemStack> condensedOffered = condenseStacks(offered);
for (ItemStack req : condensedRequired) {
int reqCount = 0;
for (ItemStack offer : condensedOffered) {
if (isIdentical(req, offer)) {
int stackCount = (int) Math.floor(offer.stackSize / req.stackSize);
reqCount = Math.max(reqCount, stackCount);
}
}
if (reqCount == 0) {
return null;
} else {
matching.add(req);
if (totalSets == 0 || totalSets > reqCount) {
totalSets = reqCount;
}
}
}
return matching;
}
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<String> getOreNames(@Nonnull ItemStack stack) {
List<String> oreNames = new ArrayList<>();
int[] oreIds = OreDictionary.getOreIDs(stack);
for (int oreId : oreIds) {
String oreName = OreDictionary.getOreName(oreId);
oreNames.add(oreName);
}
return oreNames;
}
public static List<List<ItemStack>> expandRecipeInputs(@Nullable List recipeInputs, boolean oreDict) {
List<List<ItemStack>> expandedRecipeInputs = new ArrayList<>();
if (recipeInputs == null) {
return expandedRecipeInputs;
}
for (Object recipeInput : recipeInputs) {
List<ItemStack> inputStacks = toItemStackList(recipeInput);
List<ItemStack> inputExpanded = new ArrayList<>();
for (ItemStack stack : inputStacks) {
if (oreDict) {
List<String> inputOres = getOreNames(stack);
if (inputOres.size() > 0) {
for (String inputOre : inputOres) {
List<ItemStack> ores = OreDictionary.getOres(inputOre);
inputExpanded.addAll(ores);
}
}
}
List<ItemStack> subtypes = getSubtypes(stack);
inputExpanded.addAll(subtypes);
}
removeDuplicateItemStacks(inputExpanded);
expandedRecipeInputs.add(inputExpanded);
}
return expandedRecipeInputs;
}
@Nonnull
public static List<ItemStack> toItemStackList(@Nullable Object stacks) {
List<ItemStack> itemStacksList = new ArrayList<>();
toItemStackList(itemStacksList, stacks);
return removeDuplicateItemStacks(itemStacksList);
}
private static void toItemStackList(@Nonnull List<ItemStack> itemStackList, @Nullable Object input) {
if (input instanceof ItemStack) {
itemStackList.add((ItemStack) input);
} else if (input instanceof String) {
List<ItemStack> stacks = OreDictionary.getOres((String) input);
itemStackList.addAll(stacks);
} else if (input instanceof Iterable) {
for (Object obj : (Iterable) input) {
toItemStackList(itemStackList, obj);
}
} else if (input != null) {
Log.error("Unknown object found: {}", input);
}
}
@Nonnull
public static String getUniqueIdentifierForStack(@Nonnull ItemStack stack) {
return getUniqueIdentifierForStack(stack, false);
}
@Nonnull
public static String getUniqueIdentifierForStack(@Nonnull ItemStack stack, boolean wildcard) {
Item item = stack.getItem();
if (item == null) {
throw new NullItemInStackException();
}
String itemNameString = GameData.getItemRegistry().getNameForObject(item).toString();
int metadata = stack.getMetadata();
if (wildcard || metadata == OreDictionary.WILDCARD_VALUE) {
return itemNameString;
}
StringBuilder itemKey = new StringBuilder(itemNameString).append(':').append(metadata);
if (stack.hasTagCompound()) {
NBTTagCompound nbtTagCompound = stack.getTagCompound();
nbtTagCompound = cleanNbt(nbtTagCompound);
if (!nbtTagCompound.hasNoTags()) {
itemKey.append(':').append(nbtTagCompound);
}
}
return itemKey.toString();
}
@Nonnull
public static List<String> getUniqueIdentifiersWithWildcard(@Nonnull ItemStack itemStack) {
return Arrays.asList(
getUniqueIdentifierForStack(itemStack, false),
getUniqueIdentifierForStack(itemStack, true)
);
}
private static NBTTagCompound cleanNbt(NBTTagCompound nbtTagCompound) {
Set<String> keys = nbtTagCompound.getKeySet();
Set<String> ignoredKeys = Sets.intersection(keys, Config.nbtKeyBlacklist);
if (ignoredKeys.size() == 0) {
return nbtTagCompound;
}
nbtTagCompound = (NBTTagCompound) nbtTagCompound.copy();
for (String ignoredKey : ignoredKeys) {
nbtTagCompound.removeTag(ignoredKey);
}
return nbtTagCompound;
}
public static int addStack(Container container, Collection<Integer> slotIndexes, ItemStack stack, boolean doAdd) {
int added = 0;
// Add to existing stacks first
for (Integer slotIndex : slotIndexes) {
Slot slot = container.getSlot(slotIndex);
if (slot == null) {
continue;
}
ItemStack inventoryStack = slot.getStack();
if (inventoryStack == null || inventoryStack.getItem() == null) {
continue;
}
// Already occupied by different item, skip this slot.
if (!inventoryStack.isStackable() || !inventoryStack.isItemEqual(stack) || !ItemStack.areItemStackTagsEqual(inventoryStack, stack)) {
continue;
}
int remain = stack.stackSize - added;
int space = inventoryStack.getMaxStackSize() - inventoryStack.stackSize;
if (space <= 0) {
continue;
}
// Enough space
if (space >= remain) {
if (doAdd) {
inventoryStack.stackSize += remain;
}
return stack.stackSize;
}
// Not enough space
if (doAdd) {
inventoryStack.stackSize = inventoryStack.getMaxStackSize();
}
added += space;
}
if (added >= stack.stackSize) {
return added;
}
for (Integer slotIndex : slotIndexes) {
Slot slot = container.getSlot(slotIndex);
if (slot == null) {
continue;
}
ItemStack inventoryStack = slot.getStack();
if (inventoryStack != null) {
continue;
}
if (doAdd) {
ItemStack stackToAdd = stack.copy();
stackToAdd.stackSize = stack.stackSize - added;
slot.putStack(stackToAdd);
}
return stack.stackSize;
}
return added;
}
}