aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/xmod/tinkers/util/TinkersUtils.java
blob: aed5b46e0aa40fab1be5da6744cd25cb170f5fea (plain)
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
package gtPlusPlus.xmod.tinkers.util;

import gtPlusPlus.core.lib.LoadedMods;
import gtPlusPlus.core.util.minecraft.ItemUtils;
import gtPlusPlus.core.util.reflect.ReflectionUtils;
import net.minecraftforge.fluids.Fluid;

public class TinkersUtils {

	private static Object mSmelteryInstance;
	private static Class mSmelteryClassInstance;

	public static Object getSmelteryInstance() {
		if (!LoadedMods.TiCon) {
			return null;
		}
		else {
			if (mSmelteryInstance == null || mSmelteryClassInstance == null) {
				if (mSmelteryClassInstance == null) {
					try {
						mSmelteryClassInstance = Class.forName("tconstruct.library.crafting.Smeltery");
					}
					catch (ClassNotFoundException e) {}
				}
				if (mSmelteryClassInstance != null) {
					try {
						mSmelteryInstance = ReflectionUtils.getField(mSmelteryClassInstance, "instance").get(null);
					}
					catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
					}
				}
			}						
		}		
		if (mSmelteryInstance != null) {
			return mSmelteryInstance;
		}		
		return null;
	}

	public static final boolean isTiConFirstInOD() {
		if (LoadedMods.TiCon) {
			try {
				return (boolean) ReflectionUtils.getField(Class.forName("PHConstruct"), "tconComesFirst").get(null);
			}
			catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | ClassNotFoundException e) {
			}
		}
		return false;
	}

	public static final boolean stopTiconLoadingFirst() {
		if (isTiConFirstInOD()) {
			try {
				ReflectionUtils.setFieldValue(Class.forName("PHConstruct"), "tconComesFirst", false);
				if ((boolean) ReflectionUtils.getField(Class.forName("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(getSmelteryInstance(), "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(getSmelteryInstance(), "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(getSmelteryInstance(), "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(getSmelteryInstance(), "getFuelDuration", new Class[] {Fluid.class}, new Object[] {fluid});
	}

}