-
Notifications
You must be signed in to change notification settings - Fork 497
/
DetailedConfigOption.java
142 lines (116 loc) · 3.82 KB
/
DetailedConfigOption.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
package buildcraft.core.lib.config;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.BCLog;
import buildcraft.core.lib.config.ExpressionCompiler.InvalidExpressionException;
public class DetailedConfigOption {
private static final Set<DetailedConfigOption> allRegistered = new HashSet<>();
private final String id, defaultVal;
String cache;
boolean hasWarned;
private boolean cacheBoolean;
private long cacheLong;
private double cacheDouble;
private Expression cacheExpression;
public DetailedConfigOption(String name, String defultVal) {
this.id = name;
this.defaultVal = defultVal;
allRegistered.add(this);
}
public static void reloadAll() {
allRegistered.forEach(r -> r.reload());
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null) return false;
if (obj.getClass() != getClass()) return false;
return ((DetailedConfigOption) obj).id.equals(id);
}
@Override
public int hashCode() {
return id.hashCode();
}
protected void reload() {
cache = null;
cacheExpression = null;
}
public final String defaultValue() {
return defaultVal;
}
protected boolean refresh() {
return BuildCraftCore.detailedConfigManager.refresh(this, id);
}
public String getAsString() {
refresh();
return cache;
}
public boolean getAsBoolean() {
if (refresh()) cacheBoolean = "true".equals(cache);
return cacheBoolean;
}
public long getAsLong() {
if (refresh()) {
try {
cacheLong = Long.parseLong(cache);
} catch (NumberFormatException nfe) {
BCLog.logger.warn("Invalid option for " + id + ":" + cache + ", wanted an integer! " + nfe.getMessage());
cacheLong = 0;
}
}
return cacheLong;
}
public double getAsDouble() {
if (refresh()) {
try {
cacheDouble = Double.parseDouble(cache);
} catch (NumberFormatException nfe) {
BCLog.logger.warn("Invalid option for " + id + ":" + cache + ", wanted a floating-point! " + nfe.getMessage());
cacheDouble = 0;
}
}
return cacheDouble;
}
public Expression getAsExpression() {
if (refresh()) {
try {
String string = getAsString();
cacheExpression = ExpressionCompiler.compileExpression(string);
} catch (InvalidExpressionException iee) {
BCLog.logger.warn("Invalid expression for " + id + ":" + cache + ", wanted a valid expression!");
BCLog.logger.warn("Error: " + iee.getMessage());
cacheLong = 0;
}
}
return cacheExpression;
}
public int getAsInt() {
return (int) getAsLong();
}
public char getAsChar() {
return (char) getAsLong();
}
public byte getAsByte() {
return (byte) getAsLong();
}
public float getAsFloat() {
return (float) getAsDouble();
}
// Helper methods
public float getAsFloatCapped(float min, float max) {
return Math.min(max, Math.max(min, getAsFloat()));
}
@SideOnly(Side.CLIENT)
public enum ReloadListener implements IResourceManagerReloadListener {
INSTANCE;
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
reloadAll();
}
}
}