/
ItemStackListFactory.java
118 lines (100 loc) · 3.71 KB
/
ItemStackListFactory.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
package mezz.jei.plugins.vanilla.ingredients;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import mezz.jei.util.ErrorUtil;
import mezz.jei.util.Log;
import mezz.jei.util.StackHelper;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
public class ItemStackListFactory {
private ItemStackListFactory() {
}
public static List<ItemStack> create(StackHelper stackHelper) {
final List<ItemStack> itemList = new ArrayList<ItemStack>();
final Set<String> itemNameSet = new HashSet<String>();
for (CreativeTabs creativeTab : CreativeTabs.CREATIVE_TAB_ARRAY) {
List<ItemStack> creativeTabItemStacks = new ArrayList<ItemStack>();
try {
creativeTab.displayAllRelevantItems(creativeTabItemStacks);
} catch (RuntimeException e) {
Log.error("Creative tab crashed while getting items. Some items from this tab will be missing from the item list. {}", creativeTab, e);
} catch (LinkageError e) {
Log.error("Creative tab crashed while getting items. Some items from this tab will be missing from the item list. {}", creativeTab, e);
}
for (ItemStack itemStack : creativeTabItemStacks) {
if (itemStack == null) {
Log.error("Found a null itemStack in creative tab: {}", creativeTab);
} else if (itemStack.getItem() == null) {
Log.error("Found a null item in an itemStack from creative tab: {}", creativeTab);
} else {
addItemStack(stackHelper, itemStack, itemList, itemNameSet);
}
}
}
for (Block block : ForgeRegistries.BLOCKS) {
addBlockAndSubBlocks(stackHelper, block, itemList, itemNameSet);
}
for (Item item : ForgeRegistries.ITEMS) {
addItemAndSubItems(stackHelper, item, itemList, itemNameSet);
}
return itemList;
}
private static void addItemAndSubItems(StackHelper stackHelper, @Nullable Item item, List<ItemStack> itemList, Set<String> itemNameSet) {
if (item == null) {
return;
}
List<ItemStack> items = stackHelper.getSubtypes(item, 1);
for (ItemStack stack : items) {
if (stack != null) {
addItemStack(stackHelper, stack, itemList, itemNameSet);
}
}
}
private static void addBlockAndSubBlocks(StackHelper stackHelper, @Nullable Block block, List<ItemStack> itemList, Set<String> itemNameSet) {
if (block == null) {
return;
}
Item item = Item.getItemFromBlock(block);
if (item == null) {
return;
}
for (CreativeTabs itemTab : item.getCreativeTabs()) {
List<ItemStack> subBlocks = new ArrayList<ItemStack>();
block.getSubBlocks(item, itemTab, subBlocks);
for (ItemStack subBlock : subBlocks) {
if (subBlock == null) {
Log.error("Found null subBlock of {}", block);
} else if (subBlock.getItem() == null) {
Log.error("Found subBlock of {} with null item", block);
} else {
addItemStack(stackHelper, subBlock, itemList, itemNameSet);
}
}
}
}
private static void addItemStack(StackHelper stackHelper, ItemStack stack, List<ItemStack> itemList, Set<String> itemNameSet) {
String itemKey = null;
try {
itemKey = stackHelper.getUniqueIdentifierForStack(stack, StackHelper.UidMode.FULL);
} catch (RuntimeException e) {
String stackInfo = ErrorUtil.getItemStackInfo(stack);
Log.error("Couldn't get unique name for itemStack {}", stackInfo, e);
} catch (LinkageError e) {
String stackInfo = ErrorUtil.getItemStackInfo(stack);
Log.error("Couldn't get unique name for itemStack {}", stackInfo, e);
}
if (itemKey != null) {
if (itemNameSet.contains(itemKey)) {
return;
}
itemNameSet.add(itemKey);
itemList.add(stack);
}
}
}