-
Notifications
You must be signed in to change notification settings - Fork 497
/
ReflectMjAPI.java
executable file
·148 lines (117 loc) · 3.03 KB
/
ReflectMjAPI.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
package buildcraft.core;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import buildcraft.api.mj.MjBattery;
import buildcraft.core.utils.Utils;
public class ReflectMjAPI {
enum BatteryKind {
Value, Container
}
private static class BatteryField {
public Field field;
public MjBattery battery;
public BatteryKind kind;
public double getEnergyRequested (Object obj) {
try {
double contained = field.getDouble(obj);
double left = contained + battery.maxReceivedPerCycle() > battery
.maxCapacity() ? battery.maxCapacity() - contained : battery
.maxReceivedPerCycle();
if (left > 0) {
return left;
} else {
return battery.minimumConsumption();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return 0;
}
}
public static class BatteryObject {
BatteryField f;
Object o;
public double getEnergyRequested () {
return f.getEnergyRequested(o);
}
public double addEnergy(double watts) {
try {
double e = f.field.getDouble(o);
double max = f.battery.maxCapacity();
double used = 0;
if (e + watts <= max) {
used = watts;
} else {
used = max - e;
}
f.field.setDouble(o, e + used);
return used;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return 0;
}
public double minimumConsumption() {
return f.battery.minimumConsumption();
}
public double maxReceivedPerCycle() {
return f.battery.maxReceivedPerCycle();
}
}
public static BatteryObject getMjBattery (Object o) {
if (o == null)
return null;
BatteryField f = getMjBattery (o.getClass());
if (f == null) {
return null;
} else if (f.kind == BatteryKind.Value) {
BatteryObject obj = new BatteryObject();
obj.o = o;
obj.f = f;
return obj;
} else {
try {
return getMjBattery(f.field.get(o));
} catch (IllegalArgumentException e) {
e.printStackTrace();
return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
}
static Map <Class, BatteryField> MjBatteries = new HashMap <Class, BatteryField> ();
private static BatteryField getMjBattery (Class c) {
if (!MjBatteries.containsKey(c)) {
for (Field f : Utils.getAllFields(c)) {
MjBattery battery = f.getAnnotation (MjBattery.class);
if (battery != null) {
f.setAccessible(true);
BatteryField bField = new BatteryField();
bField.field = f;
bField.battery = battery;
if (f.getType().equals(double.class)) {
bField.kind = BatteryKind.Value;
} else if (f.getType().isPrimitive()) {
throw new RuntimeException(
"MJ battery needs to be object or double type");
} else {
bField.kind = BatteryKind.Container;
}
MjBatteries.put(c, bField);
return bField;
}
}
MjBatteries.put(c, null);
return null;
} else {
return MjBatteries.get(c);
}
}
}