This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
forked from iguanaman/IguanaTweaksTConstruct
/
RestrictionHelper.java
454 lines (403 loc) · 14.3 KB
/
RestrictionHelper.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package iguanaman.iguanatweakstconstruct.restriction;
import cpw.mods.fml.common.registry.GameRegistry;
import iguanaman.iguanatweakstconstruct.util.Log;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import tconstruct.library.TConstructRegistry;
import tconstruct.library.crafting.PatternBuilder;
import tconstruct.library.crafting.ToolBuilder;
import tconstruct.library.tools.BowstringMaterial;
import tconstruct.library.tools.CustomMaterial;
import tconstruct.library.tools.FletchingMaterial;
import tconstruct.library.tools.ToolMaterial;
import tconstruct.library.util.IPattern;
import tconstruct.tools.TinkerTools;
import tconstruct.tools.items.Pattern;
import java.util.*;
// todo: refactor this properly with a Map or something when i need to restrict more than vanilla
public abstract class RestrictionHelper {
public static Map<String, ItemMeta> configNameToPattern; // holds the names that can be used in the config and maps them to item-meta combinations to retrieve the materials
// this list contains all ALLOWED pattern - material combinations
public static Map<ItemMeta, List<ToolMaterial>> patternMaterialLookup; // item+metadata -> List of applicable materials
public static Map<ItemMeta, List<CustomMaterial>> patternCustomMaterialLookup; // item+metadata -> List of applicable custom materials
static {
configNameToPattern = new HashMap<String, ItemMeta>();
patternMaterialLookup = new HashMap<ItemMeta, List<ToolMaterial>>();
patternCustomMaterialLookup = new HashMap<ItemMeta, List<CustomMaterial>>();
}
public static boolean isRestricted(ItemStack pattern, ToolMaterial material)
{
boolean restricted = true;
List<ToolMaterial> matIDs = patternMaterialLookup.get(new ItemMeta(pattern));
if(matIDs != null)
{
for(ToolMaterial mat : matIDs)
if(mat == material) {
restricted = false;
break;
}
}
return restricted;
}
public static int getPatternIndexFromName(String name)
{
for(int i = 0; i < patternNames.length; i++)
if(patternNames[i].equals(name))
return i;
return -1;
}
public static List<ToolMaterial> getPatternMaterials(ItemStack pattern)
{
return patternMaterialLookup.get(new ItemMeta(pattern));
}
public static List<CustomMaterial> getPatternCustomMaterials(ItemStack pattern)
{
return patternCustomMaterialLookup.get(new ItemMeta(pattern));
}
public static void addRestriction(ItemMeta key, ToolMaterial material)
{
// fetch the material list
List<ToolMaterial> materials = patternMaterialLookup.get(key);
if(materials == null)
{
Log.debug(String.format("Couldn't find lookup entry for %s:%d", key.item.getUnlocalizedName(), key.meta));
return;
}
// find the entry and remove it
ListIterator<ToolMaterial> iter = materials.listIterator();
while(iter.hasNext())
{
ToolMaterial mat = iter.next();
if(mat == material)
{
iter.remove();
}
}
}
// removes all recipes that use a certain tool from the list
public static void clearRecipes(ToolMaterial material)
{
for(List<ToolMaterial> mats : patternMaterialLookup.values()) {
ListIterator<ToolMaterial> iter = mats.listIterator();
while(iter.hasNext())
{
ToolMaterial mat = iter.next();
if(mat == material)
{
iter.remove();
}
}
}
}
public static void addAllowed(ItemMeta key, ToolMaterial material)
{
// fetch the material list
List<ToolMaterial> materials = patternMaterialLookup.get(key);
if(materials == null)
{
Log.debug(String.format("Couldn't find lookup entry for %s:%d", key.item.getUnlocalizedName(), key.meta));
return;
}
// find the entry so we don't have a double entry
ListIterator<ToolMaterial> iter = materials.listIterator();
while(iter.hasNext())
{
ToolMaterial mat = iter.next();
if(mat == material)
{
return;
}
}
// item is not in list. add it
materials.add(material);
}
public static void initPatternParts()
{
Log.info("Loading tool pattern combinations");
// cycle through ALL combinations
for(Map.Entry<List, ItemStack> entry : TConstructRegistry.patternPartMapping.entrySet())
{
Item pattern = (Item)entry.getKey().get(0); // the pattern
Integer meta = (Integer)entry.getKey().get(1); // metadata of the pattern
Integer matID = (Integer)entry.getKey().get(2); // Material-ID of the material needed to craft
String name;
if(pattern == TinkerTools.woodPattern && meta <= patternNames.length)
name = patternNames[meta];
else
name = (new ItemStack(pattern, 1, meta)).getUnlocalizedName();
ItemMeta im = configNameToPattern.get(name);
// not registered in the mapping yet?
if(im == null) {
im = new ItemMeta(pattern, meta);
configNameToPattern.put(name, im);
}
// add material
if(!patternMaterialLookup.containsKey(im))
patternMaterialLookup.put(im, new LinkedList<ToolMaterial>());
patternMaterialLookup.get(im).add(TConstructRegistry.getMaterial(matID));
}
// bowstring and fletchling are treated differently
for(Integer matID : TConstructRegistry.toolMaterials.keySet())
{
// bowstring
CustomMaterial mat = TConstructRegistry.getCustomMaterial(matID, BowstringMaterial.class);
if(mat != null)
{
ItemMeta im = configNameToPattern.get("bowstring");
// not registered in the mapping yet?
if(im == null) {
im = new ItemMeta(TinkerTools.woodPattern, 23);
configNameToPattern.put("bowstring", im);
}
// add material
if(!patternCustomMaterialLookup.containsKey(im))
patternCustomMaterialLookup.put(im, new LinkedList<CustomMaterial>());
patternCustomMaterialLookup.get(im).add(mat);
}
// fletchling
mat = TConstructRegistry.getCustomMaterial(matID, FletchingMaterial.class);
if(mat != null)
{
ItemMeta im = configNameToPattern.get("fletching");
// not registered in the mapping yet?
if(im == null) {
im = new ItemMeta(TinkerTools.woodPattern, 24);
configNameToPattern.put("fletching", im);
}
// add material
if(!patternCustomMaterialLookup.containsKey(im))
patternCustomMaterialLookup.put(im, new LinkedList<CustomMaterial>());
patternCustomMaterialLookup.get(im).add(mat);
}
}
}
// item + metadata combination
public static class ItemMeta {
public Item item;
public Integer meta;
public ItemMeta(Item item, Integer meta) {
this.item = item;
this.meta = meta;
}
public ItemMeta(ItemStack stack)
{
this.item = stack.getItem();
this.meta = stack.getItemDamage();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ItemMeta itemMeta = (ItemMeta) o;
if (item != null ? !item.equals(itemMeta.item) : itemMeta.item != null) return false;
if (meta != null ? !meta.equals(itemMeta.meta) : itemMeta.meta != null) return false;
return true;
}
@Override
public int hashCode() {
int result = item != null ? item.hashCode() : 0;
result = 31 * result + (meta != null ? meta.hashCode() : 0);
return result;
}
}
// the tool parts
public static final String[] patternNames = new String[] {
"ingot", // 0
"rod", // 1
"pickaxe", // 2
"shovel", // 3
"axe", // 4
"swordblade", // 5
"largeguard", // 6
"mediumguard", // 7
"crossbar", // 8
"binding", // 9
"frypan", // 10
"sign", // 11
"knifeblade", // 12
"chisel", // 13
"largerod", // 14
"toughbinding", // 15
"largeplate", // 16
"broadaxe", // 17
"scythe", // 18
"excavator", // 19
"largeblade", // 20
"hammerhead", // 21
"fullguard", // 22
"bowstring", // 23
"fletching", // 24
"arrowhead" // 25
};
public static final String[] defaultRestrictions = new String[]{
// Wood:
"Wood:pickaxe",
"Wood:axe",
"Wood:swordblade",
"Wood:largeguard",
"Wood:mediumguard",
"Wood:frypan",
"Wood:chisel",
"Wood:largerod",
"Wood:toughbinding",
"Wood:largeplate",
"Wood:broadaxe",
"Wood:scythe",
"Wood:excavator",
"Wood:largeblade",
"Wood:hammerhead",
"Wood:fullguard",
// Flint:
"Flint:rod",
"Flint:swordblade",
"Flint:largeguard",
"Flint:mediumguard",
"Flint:crossbar",
"Flint:binding",
"Flint:frypan",
"Flint:sign",
"Flint:largerod",
"Flint:toughbinding",
"Flint:largeplate",
"Flint:broadaxe",
"Flint:scythe",
"Flint:excavator",
"Flint:largeblade",
"Flint:hammerhead",
"Flint:fullguard",
// Bone
"Bone:pickaxe",
"Bone:swordblade",
"Bone:largeguard",
"Bone:mediumguard",
"Bone:binding",
"Bone:frypan",
"Bone:sign",
"Bone:chisel",
"Bone:largerod",
"Bone:toughbinding",
"Bone:largeplate",
"Bone:broadaxe",
"Bone:scythe",
"Bone:excavator",
"Bone:largeblade",
"Bone:hammerhead",
"Bone:fullguard",
// Cactus
"Cactus:pickaxe",
"Cactus:shovel",
"Cactus:axe",
"Cactus:swordblade",
"Cactus:largeguard",
"Cactus:mediumguard",
"Cactus:crossbar",
"Cactus:frypan",
"Cactus:sign",
"Cactus:knifeblade",
"Cactus:chisel",
"Cactus:largerod",
"Cactus:toughbinding",
"Cactus:largeplate",
"Cactus:broadaxe",
"Cactus:scythe",
"Cactus:excavator",
"Cactus:largeblade",
"Cactus:hammerhead",
"Cactus:fullguard",
"Cactus:arrowhead",
// Paper:
"Paper:pickaxe",
"Paper:shovel",
"Paper:axe",
"Paper:swordblade",
"Paper:largeguard",
"Paper:mediumguard",
"Paper:crossbar",
"Paper:frypan",
"Paper:sign",
"Paper:knifeblade",
"Paper:chisel",
"Paper:largerod",
"Paper:toughbinding",
"Paper:largeplate",
"Paper:broadaxe",
"Paper:scythe",
"Paper:excavator",
"Paper:largeblade",
"Paper:hammerhead",
"Paper:fullguard",
"Paper:arrowhead",
// Slime:
"Slime:pickaxe",
"Slime:shovel",
"Slime:axe",
"Slime:swordblade",
"Slime:largeguard",
"Slime:mediumguard",
"Slime:crossbar",
"Slime:frypan",
"Slime:knifeblade",
"Slime:chisel",
"Slime:largerod",
"Slime:toughbinding",
"Slime:largeplate",
"Slime:broadaxe",
"Slime:scythe",
"Slime:excavator",
"Slime:largeblade",
"Slime:hammerhead",
"Slime:fullguard",
"Slime:arrowhead",
// BlueSlime:
"BlueSlime:pickaxe",
"BlueSlime:shovel",
"BlueSlime:axe",
"BlueSlime:swordblade",
"BlueSlime:largeguard",
"BlueSlime:mediumguard",
"BlueSlime:crossbar",
"BlueSlime:frypan",
"BlueSlime:sign",
"BlueSlime:knifeblade",
"BlueSlime:chisel",
"BlueSlime:largerod",
"BlueSlime:toughbinding",
"BlueSlime:largeplate",
"BlueSlime:broadaxe",
"BlueSlime:scythe",
"BlueSlime:excavator",
"BlueSlime:largeblade",
"BlueSlime:hammerhead",
"BlueSlime:fullguard",
"BlueSlime:arrowhead"
};
public static final String[] defaultAllowed = new String[] {
// Wood:
"Wood:rod",
"Wood:binding",
"Wood:sign",
// Flint:
"Flint:pickaxe",
"Flint:knifeblade",
"Flint:arrowhead",
// Bone
"Bone:rod",
"Bone:shovel",
"Bone:axe",
"Bone:knifeblade",
"Bone:arrowhead",
// Cactus
"Cactus:binding",
// Paper:
"Paper:rod",
"Paper:binding",
// Slime:
"Slime:rod",
"Slime:sign",
"Slime:binding",
// BlueSlime
"BlueSlime:rod",
"BlueSlime:binding",
};
}