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
|
package gtPlusPlus.xmod.tinkers.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedHashMap;
import gtPlusPlus.core.lib.LoadedMods;
import gtPlusPlus.core.material.Material;
import gtPlusPlus.core.util.minecraft.ItemUtils;
import gtPlusPlus.core.util.reflect.ReflectionUtils;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
public class TinkersUtils {
private static Object mSmelteryInstance;
private static Class mSmelteryClassInstance;
private static Object mTinkersRegistryInstance;
private static Class mTinkersRegistryClass;
private static final Class mToolMaterialClass;
private static final HashMap<String, Method> mMethodCache = new LinkedHashMap<String, Method>();
static {
setRegistries();
mToolMaterialClass = ReflectionUtils.getClass("tconstruct.library.tools.ToolMaterial");
}
/**
*
* @param aSwitch - The Registry to return
*/
private static Object getTiConDataInstance(int aSwitch) {
if (!LoadedMods.TiCon) {
return null;
}
else {
if (mTinkersRegistryClass == null || mSmelteryClassInstance == null) {
setRegistries();
}
// getSmelteryInstance
if (aSwitch == 0) {
//Set Smeltery Instance
if (mSmelteryInstance == null || mSmelteryClassInstance == null) {
if (mSmelteryClassInstance == null) {
mSmelteryClassInstance = ReflectionUtils.getClass("tconstruct.library.crafting.Smeltery");
}
if (mSmelteryClassInstance != null) {
try {
mSmelteryInstance = ReflectionUtils.getField(mSmelteryClassInstance, "instance").get(null);
}
catch (IllegalArgumentException | IllegalAccessException e) {
}
}
}
//Return Smeltery Instance
if (mSmelteryInstance != null) {
return mSmelteryInstance;
}
}
// getTableCastingInstance || getBasinCastingInstance
else if (aSwitch == 1) {
if (mTinkersRegistryClass == null || mTinkersRegistryInstance == null) {
if (mTinkersRegistryClass == null) {
mTinkersRegistryClass = ReflectionUtils.getClass("tconstruct.library.TConstructRegistry");
}
if (mTinkersRegistryClass != null) {
if (mTinkersRegistryInstance == null) {
try {
mTinkersRegistryInstance = ReflectionUtils.getField(mTinkersRegistryClass, "instance").get(null);
}
catch (IllegalArgumentException | IllegalAccessException e) {
}
}
}
}
//Return Smeltery Instance
if (mTinkersRegistryInstance != null) {
return mTinkersRegistryInstance;
}
}
return null;
}
}
private static void setRegistries() {
if (mTinkersRegistryClass == null) {
mTinkersRegistryClass = ReflectionUtils.getClass("tconstruct.library.TConstructRegistry");
}
if (mSmelteryClassInstance == null) {
mSmelteryClassInstance = ReflectionUtils.getClass("tconstruct.library.crafting.Smeltery");
}
}
public static final boolean isTiConFirstInOD() {
if (LoadedMods.TiCon) {
try {
return (boolean) ReflectionUtils.getField(ReflectionUtils.getClass("PHConstruct"), "tconComesFirst").get(null);
} catch (IllegalArgumentException | IllegalAccessException e) {
}
}
return false;
}
public static final boolean stopTiconLoadingFirst() {
if (isTiConFirstInOD()) {
try {
ReflectionUtils.setFinalFieldValue(ReflectionUtils.getClass("PHConstruct"), "tconComesFirst", false);
if ((boolean) ReflectionUtils.getField(ReflectionUtils.getClass("PHConstruct"), "tconComesFirst").get(null) == false) {
return true;
}
//Did not work, let's see where TiCon uses this and prevent it.
else {
ItemUtils.getNonTinkersDust("", 1);
}
}
catch (Exception e) {}
}
return false;
}
/**
* Add a new fluid as a valid Smeltery fuel.
* @param fluid The fluid.
* @param power The temperature of the fluid. This also influences the melting speed. Lava is 1000.
* @param duration How long one "portion" of liquid fuels the smeltery. Lava is 10.
*/
public static void addSmelteryFuel (Fluid fluid, int power, int duration){
ReflectionUtils.invokeVoid(getTiConDataInstance(0), "addSmelteryFuel", new Class[] {Fluid.class, int.class, int.class}, new Object[] {fluid, power, duration});
}
/**
* Returns true if the liquid is a valid smeltery fuel.
*/
public static boolean isSmelteryFuel (Fluid fluid){
return ReflectionUtils.invoke(getTiConDataInstance(0), "isSmelteryFuel", new Class[] {Fluid.class}, new Object[] {fluid});
}
/**
* Returns the power of a smeltery fuel or 0 if it's not a fuel.
*/
public static int getFuelPower (Fluid fluid){
return (int) ReflectionUtils.invokeNonBool(getTiConDataInstance(0), "getFuelPower", new Class[] {Fluid.class}, new Object[] {fluid});
}
/**
* Returns the duration of a smeltery fuel or 0 if it's not a fuel.
*/
public static int getFuelDuration (Fluid fluid){
return (int) ReflectionUtils.invokeNonBool(getTiConDataInstance(0), "getFuelDuration", new Class[] {Fluid.class}, new Object[] {fluid});
}
public static boolean registerFluidType(String name, Block block, int meta, int baseTemperature, Fluid fluid, boolean isToolpart) {
if (mMethodCache.get("registerFluidType") == null) {
Method m = ReflectionUtils.getMethod(ReflectionUtils.getClass("tconstruct.library.crafting.FluidType"), "registerFluidType", String.class, Block.class, int.class, int.class, Fluid.class, boolean.class);
mMethodCache.put("registerFluidType", m);
}
try {
mMethodCache.get("registerFluidType").invoke(null, name, block, meta, baseTemperature, fluid, isToolpart);
return true;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
return false;
}
}
public static boolean addBaseMeltingRecipes(Material aMaterial) {
return addMelting(aMaterial.getBlock(1), aMaterial.getBlock(), 0, aMaterial.getMeltingPointC(), aMaterial.getFluid(144*9)) &&
addMelting(aMaterial.getIngot(1), aMaterial.getBlock(), 0, aMaterial.getMeltingPointC(), aMaterial.getFluid(144));
}
public static boolean addMelting(ItemStack input, Block block, int metadata, int temperature, FluidStack liquid) {
if (mMethodCache.get("addMelting") == null) {
Method m = ReflectionUtils.getMethod(mSmelteryClassInstance, "addMelting", ItemStack.class, Block.class, int.class, int.class, FluidStack.class);
mMethodCache.put("addMelting", m);
}
try {
mMethodCache.get("addMelting").invoke(null, input, block, metadata, temperature, liquid);
return true;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
return false;
}
}
public static boolean addBaseBasinRecipes(Material aMaterial) {
return addBasinRecipe(aMaterial.getBlock(1), aMaterial.getFluid(144*9), (ItemStack) null, true, 100);
}
public static boolean addBasinRecipe(ItemStack output, FluidStack metal, ItemStack cast, boolean consume, int delay) {
if (mMethodCache.get("addBasinRecipe") == null) {
Method m = ReflectionUtils.getMethod(ReflectionUtils.getClass("tconstruct.library.crafting.LiquidCasting"), "addCastingRecipe", ItemStack.class, FluidStack.class, ItemStack.class, boolean.class, int.class);
mMethodCache.put("addBasinRecipe", m);
}
try {
mMethodCache.get("addBasinRecipe").invoke(getCastingInstance(0), output, metal, cast, consume, delay);
return true;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
return false;
}
}
public static boolean addBaseCastingRecipes(Material aMaterial) {
ItemStack ingotcast = getPattern(1);
return addCastingTableRecipe(aMaterial.getIngot(1), aMaterial.getFluid(144), ingotcast, false, 50);
}
public static boolean addCastingTableRecipe(ItemStack output, FluidStack metal, ItemStack cast, boolean consume, int delay) {
if (mMethodCache.get("addCastingTableRecipe") == null) {
Method m = ReflectionUtils.getMethod(ReflectionUtils.getClass("tconstruct.library.crafting.LiquidCasting"), "addBasinRecipe", ItemStack.class, FluidStack.class, ItemStack.class, boolean.class, int.class);
mMethodCache.put("addCastingTableRecipe", m);
}
try {
mMethodCache.get("addCastingTableRecipe").invoke(getCastingInstance(1), output, metal, cast, consume, delay);
return true;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
return false;
}
}
public static Object getCastingInstance(int aType) {
if (aType == 0) {
return ReflectionUtils.invokeVoid(getTiConDataInstance(1), "getTableCasting", new Class[] {}, new Object[] {});
}
else if (aType == 1) {
return ReflectionUtils.invokeVoid(getTiConDataInstance(1), "getBasinCasting", new Class[] {}, new Object[] {});
}
else {
return null;
}
}
private static Item mTinkerMetalPattern;
public static ItemStack getPattern(int aType) {
if (mTinkerMetalPattern == null) {
Field m = ReflectionUtils.getField(ReflectionUtils.getClass("tconstruct.smeltery.TinkerSmeltery"), "metalPattern");
if (m != null) {
try {
mTinkerMetalPattern = (Item) m.get(null);
} catch (IllegalArgumentException | IllegalAccessException e) {
}
}
}
if (mTinkerMetalPattern != null) {
ItemStack ingotCast = new ItemStack(mTinkerMetalPattern, aType, 0);
return ingotCast;
}
return ItemUtils.getErrorStack(1, "Bad Tinkers Pattern");
}
/**
* Generates Tinkers {@link ToolMaterial}'s reflectively.
* @param name
* @param localizationString
* @param level
* @param durability
* @param speed
* @param damage
* @param handle
* @param reinforced
* @param stonebound
* @param style
* @param primaryColor
* @return
*/
public static Object generateToolMaterial(String name, String localizationString, int level, int durability, int speed, int damage, float handle, int reinforced, float stonebound, String style, int primaryColor) {
try {
Constructor constructor = mToolMaterialClass.getConstructor(String.class, String.class, int.class, int.class, int.class, int.class, float.class, int.class, float.class, String.class, int.class);
Object myObject = constructor.newInstance(name, localizationString, level, durability, speed, damage, handle, reinforced, stonebound, style, primaryColor);
return myObject;
} catch (Throwable t) {
t.printStackTrace();
return null;
}
}
public static void addToolMaterial(int id, Object aToolMaterial) {
setRegistries();
if (mMethodCache.get("addToolMaterial") == null) {
Method m = ReflectionUtils.getMethod(mTinkersRegistryClass, "addToolMaterial", int.class, mToolMaterialClass);
mMethodCache.put("addToolMaterial", m);
}
try {
mMethodCache.get("addToolMaterial").invoke(mTinkersRegistryClass, id, aToolMaterial);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
}
}
public static void addDefaultToolPartMaterial(int id) {
setRegistries();
if (mMethodCache.get("addDefaultToolPartMaterial") == null) {
Method m = ReflectionUtils.getMethod(mTinkersRegistryClass, "addDefaultToolPartMaterial", int.class);
mMethodCache.put("addDefaultToolPartMaterial", m);
}
try {
mMethodCache.get("addDefaultToolPartMaterial").invoke(mTinkersRegistryClass, id);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
}
}
public static void addBowMaterial(int id, int drawspeed, float maxSpeed) {
setRegistries();
if (mMethodCache.get("addBowMaterial") == null) {
Method m = ReflectionUtils.getMethod(mTinkersRegistryClass, "addBowMaterial", int.class, int.class, float.class);
mMethodCache.put("addBowMaterial", m);
}
try {
mMethodCache.get("addBowMaterial").invoke(mTinkersRegistryClass, id, drawspeed, maxSpeed);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
}
}
public static void addArrowMaterial(int id, float mass, float fragility) {
setRegistries();
if (mMethodCache.get("addArrowMaterial") == null) {
Method m = ReflectionUtils.getMethod(mTinkersRegistryClass, "addArrowMaterial", int.class, float.class, float.class);
mMethodCache.put("addArrowMaterial", m);
}
try {
mMethodCache.get("addArrowMaterial").invoke(mTinkersRegistryClass, id, mass, fragility);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
}
}
}
|