-
Notifications
You must be signed in to change notification settings - Fork 497
/
ItemList.java
318 lines (259 loc) · 7.66 KB
/
ItemList.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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.WeakHashMap;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import net.minecraftforge.oredict.OreDictionary;
import buildcraft.BuildCraftCore;
import buildcraft.api.items.IList;
import buildcraft.core.lib.inventory.StackHelper;
import buildcraft.core.lib.items.ItemBuildCraft;
import buildcraft.core.lib.utils.NBTUtils;
public class ItemList extends ItemBuildCraft implements IList {
private static final WeakHashMap<ItemStack, StackLine[]> LINE_CACHE = new WeakHashMap<ItemStack, StackLine[]>();
public static class StackLine {
public boolean oreWildcard = false;
public boolean subitemsWildcard = false;
public boolean isOre;
private ItemStack[] stacks = new ItemStack[7];
private ArrayList<ItemStack> ores = new ArrayList<ItemStack>();
private ArrayList<ItemStack> relatedItems = new ArrayList<ItemStack>();
public ItemStack getStack(int index) {
if (index == 0 || (!oreWildcard && !subitemsWildcard)) {
if (index < 7) {
return stacks[index];
} else {
return null;
}
} else if (oreWildcard) {
if (ores.size() >= index) {
return ores.get(index - 1);
} else {
return null;
}
} else {
if (relatedItems.size() >= index) {
return relatedItems.get(index - 1);
} else {
return null;
}
}
}
public void setStack(int slot, ItemStack stack) {
stacks[slot] = stack;
if (stack != null) {
stacks[slot] = stacks[slot].copy();
stacks[slot].stackSize = 1;
}
if (slot == 0) {
relatedItems.clear();
ores.clear();
if (stack == null) {
isOre = false;
} else {
if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) {
setClientPreviewLists();
} else {
isOre = OreDictionary.getOreIDs(stacks[0]).length > 0;
}
}
}
}
public void writeToNBT(NBTTagCompound nbt) {
nbt.setBoolean("ore", oreWildcard);
nbt.setBoolean("sub", subitemsWildcard);
for (int i = 0; i < 7; ++i) {
if (stacks[i] != null) {
NBTTagCompound stackNBT = new NBTTagCompound();
stacks[i].writeToNBT(stackNBT);
nbt.setTag("stacks[" + i + "]", stackNBT);
}
}
}
public void readFromNBT(NBTTagCompound nbt) {
oreWildcard = nbt.getBoolean("ore");
subitemsWildcard = nbt.getBoolean("sub");
for (int i = 0; i < 7; ++i) {
if (nbt.hasKey("stacks[" + i + "]")) {
setStack(i, ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("stacks[" + i + "]")));
}
}
}
private boolean classMatch(Item base, Item matched) {
if (base.getClass() == Item.class) {
return base == matched;
} else if (base.getClass() == matched.getClass()) {
if (base instanceof ItemBlock) {
Block baseBlock = ((ItemBlock) base).field_150939_a;
Block matchedBlock = ((ItemBlock) matched).field_150939_a;
if (baseBlock.getClass() == Block.class) {
return baseBlock == matchedBlock;
} else {
return baseBlock.equals(matchedBlock);
}
} else {
return true;
}
} else {
return false;
}
}
private boolean oreMatch(ItemStack base, ItemStack matched) {
int[] oreIds = OreDictionary.getOreIDs(base);
int[] matchesIds = OreDictionary.getOreIDs(matched);
for (int stackId : oreIds) {
for (int matchId : matchesIds) {
if (stackId == matchId) {
return true;
}
}
}
return false;
}
private void setClientPreviewLists() {
Item baseItem = stacks [0].getItem();
int[] oreIds = OreDictionary.getOreIDs(stacks[0]);
isOre = oreIds.length > 0;
for (Object o : Item.itemRegistry) {
Item item = (Item) o;
boolean classMatch = classMatch(baseItem, item);
List<ItemStack> list = new ArrayList<ItemStack>();
try {
for (CreativeTabs tab : item.getCreativeTabs()) {
item.getSubItems(item, tab, list);
}
} catch (Exception e) {
}
if (list.size() > 0) {
for (ItemStack stack : list) {
if (classMatch && relatedItems.size() <= 7 && !StackHelper.isMatchingItemOrList(stacks[0], stack)) {
relatedItems.add(stack);
}
if (isOre && ores.size() <= 7 && !StackHelper.isMatchingItemOrList(stacks[0], stack)
&& oreMatch(stacks[0], stack)) {
ores.add(stack);
}
}
}
}
}
public boolean matches(ItemStack item) {
if (subitemsWildcard) {
if (stacks[0] == null) {
return false;
}
return classMatch(stacks[0].getItem(), item.getItem());
} else if (oreWildcard) {
if (stacks[0] == null) {
return false;
}
return oreMatch(stacks[0], item);
} else {
for (ItemStack stack : stacks) {
if (stack != null && StackHelper.isMatchingItem(stack, item, true, false)) {
return true;
}
}
return false;
}
}
}
public ItemList() {
super();
setMaxStackSize(1);
}
@Override
public IIcon getIconIndex(ItemStack stack) {
return icons[NBTUtils.getItemData(stack).hasKey("written") ? 1 : 0];
}
@Override
public String[] getIconNames() {
return new String[]{ "list/clean", "list/used" };
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
if (!world.isRemote) {
player.openGui(BuildCraftCore.instance, GuiIds.LIST, world, 0, 0, 0);
}
return stack;
}
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean advanced) {
NBTTagCompound nbt = NBTUtils.getItemData(stack);
if (nbt.hasKey("label")) {
list.add(nbt.getString("label"));
}
}
public static void saveLine(ItemStack stack, StackLine line, int index) {
NBTTagCompound nbt = NBTUtils.getItemData(stack);
nbt.setBoolean("written", true);
NBTTagCompound lineNBT = new NBTTagCompound();
line.writeToNBT(lineNBT);
nbt.setTag("line[" + index + "]", lineNBT);
}
public static void saveLabel(ItemStack stack, String text) {
NBTTagCompound nbt = NBTUtils.getItemData(stack);
nbt.setString("label", text);
}
public static StackLine[] getLines(ItemStack stack) {
if (LINE_CACHE.containsKey(stack)) {
return LINE_CACHE.get(stack);
}
StackLine[] result = new StackLine[6];
for (int i = 0; i < 6; ++i) {
result[i] = new StackLine();
}
NBTTagCompound nbt = NBTUtils.getItemData(stack);
if (nbt.hasKey("written")) {
for (int i = 0; i < 6; ++i) {
result[i].readFromNBT(nbt.getCompoundTag("line[" + i + "]"));
}
}
LINE_CACHE.put(stack, result);
return result;
}
@Override
public boolean setName(ItemStack stack, String name) {
saveLabel(stack, name);
return true;
}
@Override
public String getName(ItemStack stack) {
return getLabel(stack);
}
@Override
public String getLabel(ItemStack stack) {
return NBTUtils.getItemData(stack).getString("label");
}
@Override
public boolean matches(ItemStack stackList, ItemStack item) {
StackLine[] lines = getLines(stackList);
if (lines != null) {
for (int i = 0; i < lines.length; i++) {
if (lines[i] != null && lines[i].matches(item)) {
return true;
}
}
}
return false;
}
}