/
ItemRegistry.java
207 lines (176 loc) · 6.18 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
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
package mezz.jei;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentData;
import net.minecraft.init.Items;
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.fml.common.registry.GameRegistry;
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;
public class ItemRegistry implements IItemRegistry {
@Nonnull
private final Set<String> itemNameSet = new HashSet<>();
@Nonnull
private final ImmutableList<ItemStack> itemList;
@Nonnull
private final ImmutableListMultimap<String, ItemStack> itemsByModId;
@Nonnull
private final ImmutableList<ItemStack> potionIngredients;
@Nonnull
private final ImmutableList<ItemStack> fuels;
@Nonnull
private final ModList modList;
public ItemRegistry() {
this.modList = new ModList();
List<ItemStack> itemListMutable = new ArrayList<>();
List<ItemStack> fuelsMutable = new ArrayList<>();
for (Block block : GameData.getBlockRegistry().typeSafeIterable()) {
addBlockAndSubBlocks(block, itemListMutable, fuelsMutable);
}
for (Item item : GameData.getItemRegistry().typeSafeIterable()) {
addItemAndSubItems(item, itemListMutable, fuelsMutable);
}
addEnchantedBooks(itemListMutable);
this.itemList = ImmutableList.copyOf(itemListMutable);
this.fuels = ImmutableList.copyOf(fuelsMutable);
ImmutableListMultimap.Builder<String, ItemStack> itemsByModIdBuilder = ImmutableListMultimap.builder();
for (ItemStack itemStack : itemListMutable) {
Item item = itemStack.getItem();
if (item != null) {
String modId = GameRegistry.findUniqueIdentifierFor(item).modId.toLowerCase(Locale.ENGLISH);
itemsByModIdBuilder.put(modId, itemStack);
}
}
this.itemsByModId = itemsByModIdBuilder.build();
ImmutableList.Builder<ItemStack> potionIngredientBuilder = ImmutableList.builder();
for (ItemStack itemStack : this.itemList) {
if (itemStack.getItem().isPotionIngredient(itemStack)) {
potionIngredientBuilder.add(itemStack);
}
}
this.potionIngredients = potionIngredientBuilder.build();
}
private void addEnchantedBooks(List<ItemStack> itemList) {
for (Enchantment enchantment : Enchantment.enchantmentsBookList) {
if (enchantment != null && enchantment.type != null) {
EnchantmentData enchantmentData = new EnchantmentData(enchantment, enchantment.getMaxLevel());
ItemStack enchantedBook = Items.enchanted_book.getEnchantedItemStack(enchantmentData);
itemList.add(enchantedBook);
}
}
}
@Override
@Nonnull
public ImmutableList<ItemStack> getItemList() {
return itemList;
}
@Override
@Nonnull
public ImmutableList<ItemStack> getFuels() {
return fuels;
}
@Override
@Nonnull
public ImmutableList<ItemStack> getPotionIngredients() {
return potionIngredients;
}
@Nonnull
@Override
public String getModNameForItem(@Nullable Item item) {
if (item == null) {
Log.error("Null item", new NullPointerException());
return "";
}
return modList.getModNameForItem(item);
}
@Nonnull
@Override
public ImmutableList<ItemStack> getItemListForModId(@Nullable String modId) {
if (modId == null) {
Log.error("Null modId", new NullPointerException());
return ImmutableList.of();
}
String lowerCaseModId = modId.toLowerCase(Locale.ENGLISH);
return itemsByModId.get(lowerCaseModId);
}
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.getUniqueIdentifierForStack(stack);
if (itemNameSet.contains(itemKey)) {
return;
}
itemNameSet.add(itemKey);
itemList.add(stack);
if (TileEntityFurnace.isItemFuel(stack)) {
fuels.add(stack);
}
} catch (RuntimeException e) {
Log.error("Couldn't create unique name for itemStack {}.", stack.getClass(), e);
}
}
}