/
ItemRegistry.java
154 lines (127 loc) · 4.09 KB
/
ItemRegistry.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
package mezz.jei;
import com.google.common.collect.ImmutableList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraftforge.fml.common.registry.GameData;
import net.minecraftforge.oredict.OreDictionary;
import mezz.jei.api.IItemRegistry;
import mezz.jei.util.Log;
import mezz.jei.util.ModList;
import mezz.jei.util.StackUtil;
class ItemRegistry implements IItemRegistry {
@Nonnull
private final Set<Object> itemNameSet = new HashSet<>();
@Nonnull
private final ImmutableList<ItemStack> itemList;
@Nonnull
private final ImmutableList<ItemStack> fuels;
@Nonnull
private final ModList modList;
public ItemRegistry() {
this.modList = new ModList();
List<ItemStack> itemList = new ArrayList<>();
List<ItemStack> fuels = new ArrayList<>();
for (Block block : GameData.getBlockRegistry().typeSafeIterable()) {
addBlockAndSubBlocks(block, itemList, fuels);
}
for (Item item : GameData.getItemRegistry().typeSafeIterable()) {
addItemAndSubItems(item, itemList, fuels);
}
this.itemList = ImmutableList.copyOf(itemList);
this.fuels = ImmutableList.copyOf(fuels);
}
@Override
@Nonnull
public ImmutableList<ItemStack> getItemList() {
return itemList;
}
@Override
@Nonnull
public ImmutableList<ItemStack> getFuels() {
return fuels;
}
@Nonnull
@Override
public String getModNameForItem(@Nullable Item item) {
if (item == null) {
return "";
}
return modList.getModNameForItem(item);
}
private void addItemAndSubItems(@Nullable Item item, @Nonnull List<ItemStack> itemList, @Nonnull List<ItemStack> fuels) {
if (item == null) {
return;
}
if (item.getHasSubtypes()) {
ItemStack itemStack = new ItemStack(item, 1, OreDictionary.WILDCARD_VALUE);
List<ItemStack> items = StackUtil.getSubtypes(itemStack);
addItemStacks(items, itemList, fuels);
} else {
ItemStack itemStack = new ItemStack(item);
addItemStack(itemStack, itemList, fuels);
}
}
private void addBlockAndSubBlocks(@Nullable Block block, @Nonnull List<ItemStack> itemList, @Nonnull List<ItemStack> fuels) {
if (block == null) {
return;
}
Item item = Item.getItemFromBlock(block);
if (item == null) {
ItemStack stack = new ItemStack(block);
if (stack.getItem() == null) {
Log.debug("Couldn't get itemStack for block: {}", block.getUnlocalizedName());
return;
}
addItemStack(stack, itemList, fuels);
return;
}
List<ItemStack> subItems = new ArrayList<>();
for (CreativeTabs itemTab : item.getCreativeTabs()) {
subItems.clear();
block.getSubBlocks(item, itemTab, subItems);
addItemStacks(subItems, itemList, fuels);
if (subItems.isEmpty()) {
ItemStack stack = new ItemStack(block);
if (stack.getItem() == null) {
Log.debug("Couldn't get itemStack for block: {}", block.getUnlocalizedName());
return;
}
addItemStack(stack, itemList, fuels);
}
}
}
private void addItemStacks(@Nonnull Iterable<ItemStack> stacks, @Nonnull List<ItemStack> itemList, @Nonnull List<ItemStack> fuels) {
for (ItemStack stack : stacks) {
if (stack != null) {
addItemStack(stack, itemList, fuels);
}
}
}
private void addItemStack(@Nonnull ItemStack stack, @Nonnull List<ItemStack> itemList, @Nonnull List<ItemStack> fuels) {
try {
String itemKey = StackUtil.uniqueIdentifierForStack(stack, false);
if (itemNameSet.contains(itemKey)) {
return;
}
itemNameSet.add(itemKey);
itemList.add(stack);
if (TileEntityFurnace.isItemFuel(stack)) {
fuels.add(stack);
}
} catch (RuntimeException e) {
try {
Log.error("Couldn't create unique name for itemStack {}. Exception: {}", stack, e);
} catch (RuntimeException ignored) {
}
}
}
}