/
LocalizedConfiguration.java
120 lines (100 loc) · 5.08 KB
/
LocalizedConfiguration.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
package mezz.jei.config;
import java.io.File;
import java.util.Arrays;
import java.util.Locale;
import mezz.jei.util.Translator;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
public class LocalizedConfiguration extends Configuration {
private static final String commentPostfix = ".comment";
private final String keyPrefix;
private final String defaultLocalized;
private final String validLocalized;
public LocalizedConfiguration(String keyPrefix, File file, String configVersion) {
super(file, configVersion);
this.keyPrefix = keyPrefix + '.';
this.defaultLocalized = Translator.translateToLocal(this.keyPrefix + "default");
this.validLocalized = Translator.translateToLocal(this.keyPrefix + "valid");
}
public void addCategory(String categoryName) {
String categoryKey = keyPrefix + categoryName;
String commentKey = categoryKey + commentPostfix;
String comment = Translator.translateToLocal(commentKey);
setCategoryComment(categoryName, comment);
setCategoryLanguageKey(categoryName, categoryKey);
}
public boolean getBoolean(String category, String name, boolean defaultValue) {
String langKey = keyPrefix + category + '.' + name;
String commentKey = langKey + commentPostfix;
String comment = Translator.translateToLocal(commentKey);
return getBoolean(name, category, defaultValue, comment, langKey);
}
public String getString(String name, String category, String defaultValue) {
String langKey = keyPrefix + category + '.' + name;
String commentKey = langKey + commentPostfix;
String comment = Translator.translateToLocal(commentKey);
return getString(name, category, defaultValue, comment, langKey);
}
public String getString(String name, String category, String defaultValue, String[] validValues) {
String langKey = keyPrefix + category + '.' + name;
String commentKey = langKey + commentPostfix;
String comment = Translator.translateToLocal(commentKey);
Property prop = get(category, name, defaultValue);
prop.setLanguageKey(langKey);
prop.setComment(comment + " [" + defaultLocalized + ": " + defaultValue + "] [" + validLocalized + ": " + Arrays.toString(validValues) + ']');
return prop.getString();
}
public <T extends Enum<T>> T getEnum(String name, String category, T defaultValue, T[] validEnumValues) {
String langKey = keyPrefix + category + '.' + name;
String commentKey = langKey + commentPostfix;
String comment = Translator.translateToLocal(commentKey);
Property prop = get(category, name, defaultValue.name());
String[] validValues = new String[validEnumValues.length];
for (int i = 0; i < validEnumValues.length; i++) {
T enumValue = validEnumValues[i];
validValues[i] = enumValue.name().toLowerCase(Locale.ENGLISH);
}
prop.setValidValues(validValues);
prop.setLanguageKey(langKey);
prop.setComment(comment + "\n[" + defaultLocalized + ": " + defaultValue.name().toLowerCase(Locale.ENGLISH) + "]\n[" + validLocalized + ": " + Arrays.toString(prop.getValidValues()) + ']');
String stringValue = prop.getString();
T enumValue = defaultValue;
for (int i = 0; i < validValues.length; i++) {
if (stringValue.equalsIgnoreCase(validValues[i])) {
enumValue = validEnumValues[i];
}
}
return enumValue;
}
public String[] getStringList(String name, String category, String[] defaultValue) {
String langKey = keyPrefix + category + '.' + name;
String commentKey = langKey + commentPostfix;
String comment = Translator.translateToLocal(commentKey);
Property prop = get(category, name, defaultValue);
prop.setLanguageKey(langKey);
prop.setComment(comment + " [" + defaultLocalized + ": " + Arrays.toString(defaultValue) + ']');
return prop.getStringList();
}
public String[] getStringList(String name, String category, String[] defaultValue, String[] validValues) {
String langKey = keyPrefix + category + '.' + name;
String commentKey = langKey + commentPostfix;
String comment = Translator.translateToLocal(commentKey);
Property prop = get(category, name, defaultValue);
prop.setLanguageKey(langKey);
prop.setValidValues(validValues);
prop.setComment(comment + " [" + defaultLocalized + ": " + Arrays.toString(defaultValue) + "] [" + validLocalized + ": " + Arrays.toString(prop.getValidValues()) + ']');
return prop.getStringList();
}
public float getFloat(String name, String category, float defaultValue, float minValue, float maxValue) {
String langKey = keyPrefix + category + '.' + name;
String commentKey = langKey + commentPostfix;
String comment = Translator.translateToLocal(commentKey);
return getFloat(name, category, defaultValue, minValue, maxValue, comment, langKey);
}
public int getInt(String name, String category, int defaultValue, int minValue, int maxValue) {
String langKey = keyPrefix + category + '.' + name;
String commentKey = langKey + commentPostfix;
String comment = Translator.translateToLocal(commentKey);
return getInt(name, category, defaultValue, minValue, maxValue, comment, langKey);
}
}