/
Config.java
475 lines (388 loc) · 16.2 KB
/
Config.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package mezz.jei.config;
import com.google.common.collect.ImmutableMap;
import mezz.jei.Internal;
import mezz.jei.util.Log;
import mezz.jei.util.StackHelper;
import mezz.jei.util.Translator;
import mezz.jei.util.color.ColorGetter;
import mezz.jei.util.color.ColorNamer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import javax.annotation.Nonnull;
import java.awt.*;
import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class Config {
private static final String configKeyPrefix = "config.jei";
private static File jeiConfigurationDir;
public static final String CATEGORY_SEARCH = "search";
public static final String CATEGORY_ADVANCED = "advanced";
public static final String CATEGORY_SEARCH_COLORS = "searchColors";
private static LocalizedConfiguration config;
private static Configuration worldConfig;
private static LocalizedConfiguration itemBlacklistConfig;
private static LocalizedConfiguration searchColorsConfig;
// advanced
private static boolean debugModeEnabled = false;
private static boolean debugItemEnabled = false;
private static boolean hideMissingModelsEnabled = true;
private static boolean hideLaggyModelsEnabled = true;
private static boolean deleteItemsInCheatModeEnabled = true;
private static boolean colorSearchEnabled = false;
// search
private static boolean prefixRequiredForModNameSearch = true;
private static boolean prefixRequiredForTooltipSearch = false;
private static boolean prefixRequiredForOreDictSearch = true;
private static boolean prefixRequiredForCreativeTabSearch = true;
private static boolean prefixRequiredForColorSearch = true;
// per-world
private static final boolean defaultOverlayEnabled = true;
private static final boolean defaultCheatItemsEnabled = false;
private static final boolean defaultEditModeEnabled = false;
private static final String defaultFilterText = "";
private static boolean overlayEnabled = defaultOverlayEnabled;
private static boolean cheatItemsEnabled = defaultCheatItemsEnabled;
private static boolean editModeEnabled = defaultEditModeEnabled;
@Nonnull
private static String filterText = defaultFilterText;
// item blacklist
private static final Set<String> itemBlacklist = new HashSet<>();
private static final String[] defaultItemBlacklist = new String[]{};
private Config() {
}
public static boolean isOverlayEnabled() {
return overlayEnabled;
}
public static void toggleOverlayEnabled() {
overlayEnabled = !overlayEnabled;
final String worldCategory = SessionData.getWorldUid();
Property property = worldConfig.get(worldCategory, "overlayEnabled", overlayEnabled);
property.set(overlayEnabled);
if (worldConfig.hasChanged()) {
worldConfig.save();
}
}
public static boolean isCheatItemsEnabled() {
return cheatItemsEnabled;
}
public static boolean isEditModeEnabled() {
return editModeEnabled;
}
public static boolean isDebugModeEnabled() {
return debugModeEnabled;
}
public static boolean isDebugItemEnabled() {
return debugItemEnabled;
}
public static boolean isDeleteItemsInCheatModeActive() {
return deleteItemsInCheatModeEnabled && cheatItemsEnabled && SessionData.isJeiOnServer();
}
public static boolean isHideMissingModelsEnabled() {
return hideMissingModelsEnabled;
}
public static boolean isHideLaggyModelsEnabled() {
return hideLaggyModelsEnabled;
}
public static boolean isColorSearchEnabled() {
return colorSearchEnabled;
}
public static boolean isPrefixRequiredForModNameSearch() {
return prefixRequiredForModNameSearch;
}
public static boolean isPrefixRequiredForTooltipSearch() {
return prefixRequiredForTooltipSearch;
}
public static boolean isPrefixRequiredForOreDictSearch() {
return prefixRequiredForOreDictSearch;
}
public static boolean isPrefixRequiredForCreativeTabSearch() {
return prefixRequiredForCreativeTabSearch;
}
public static boolean isPrefixRequiredForColorSearch() {
return prefixRequiredForColorSearch;
}
public static boolean setFilterText(@Nonnull String filterText) {
String lowercaseFilterText = filterText.toLowerCase();
if (Config.filterText.equals(lowercaseFilterText)) {
return false;
}
Config.filterText = lowercaseFilterText;
return true;
}
@Nonnull
public static String getFilterText() {
return filterText;
}
public static void saveFilterText() {
final String worldCategory = SessionData.getWorldUid();
Property property = worldConfig.get(worldCategory, "filterText", defaultFilterText);
property.set(Config.filterText);
if (worldConfig.hasChanged()) {
worldConfig.save();
}
}
public static LocalizedConfiguration getConfig() {
return config;
}
public static Configuration getWorldConfig() {
return worldConfig;
}
public static void preInit(@Nonnull FMLPreInitializationEvent event) {
jeiConfigurationDir = new File(event.getModConfigurationDirectory(), Constants.MOD_ID);
if (!jeiConfigurationDir.exists()) {
try {
if (!jeiConfigurationDir.mkdir()) {
Log.error("Could not create config directory {}", jeiConfigurationDir);
return;
}
} catch (SecurityException e) {
Log.error("Could not create config directory {}", jeiConfigurationDir, e);
return;
}
}
final File configFile = new File(jeiConfigurationDir, "jei.cfg");
final File itemBlacklistConfigFile = new File(jeiConfigurationDir, "itemBlacklist.cfg");
final File searchColorsConfigFile = new File(jeiConfigurationDir, "searchColors.cfg");
{
final File oldConfigFile = event.getSuggestedConfigurationFile();
if (oldConfigFile.exists()) {
try {
if (!oldConfigFile.renameTo(configFile)) {
Log.error("Could not move old config file {}", oldConfigFile);
}
} catch (SecurityException e) {
Log.error("Could not move old config file {}", oldConfigFile, e);
}
}
}
{
final File oldItemBlacklistConfigFile = new File(event.getModConfigurationDirectory(), Constants.MOD_ID + "-itemBlacklist.cfg");
if (oldItemBlacklistConfigFile.exists()) {
try {
if (!oldItemBlacklistConfigFile.renameTo(itemBlacklistConfigFile)) {
Log.error("Could not move old config file {}", oldItemBlacklistConfigFile);
}
} catch (SecurityException e) {
Log.error("Could not move old config file {}", oldItemBlacklistConfigFile, e);
}
}
}
config = new LocalizedConfiguration(configKeyPrefix, configFile, "0.2.0");
itemBlacklistConfig = new LocalizedConfiguration(configKeyPrefix, itemBlacklistConfigFile, "0.1.0");
searchColorsConfig = new LocalizedConfiguration(configKeyPrefix, searchColorsConfigFile, "0.1.0");
syncConfig();
syncItemBlacklistConfig();
}
public static void startJei() {
final File worldConfigFile = new File(jeiConfigurationDir, "worldSettings.cfg");
worldConfig = new Configuration(worldConfigFile, "0.1.0");
syncWorldConfig();
syncSearchColorsConfig();
}
public static boolean syncAllConfig() {
boolean needsReload = false;
if (syncConfig()) {
needsReload = true;
}
if (syncItemBlacklistConfig()) {
needsReload = true;
}
if (syncWorldConfig()) {
needsReload = true;
}
if (syncSearchColorsConfig()) {
needsReload = true;
}
return needsReload;
}
private static boolean syncConfig() {
boolean needsReload = false;
config.addCategory(CATEGORY_SEARCH);
config.addCategory(CATEGORY_ADVANCED);
ConfigCategory modeCategory = config.getCategory("mode");
if (modeCategory != null) {
config.removeCategory(modeCategory);
}
ConfigCategory addonsCategory = config.getCategory("addons");
if (addonsCategory != null) {
config.removeCategory(addonsCategory);
}
ConfigCategory interfaceCategory = config.getCategory("interface");
if (interfaceCategory != null) {
config.removeCategory(interfaceCategory);
}
prefixRequiredForModNameSearch = config.getBoolean(CATEGORY_SEARCH, "atPrefixRequiredForModName", prefixRequiredForModNameSearch);
prefixRequiredForTooltipSearch = config.getBoolean(CATEGORY_SEARCH, "prefixRequiredForTooltipSearch", prefixRequiredForTooltipSearch);
prefixRequiredForOreDictSearch = config.getBoolean(CATEGORY_SEARCH, "prefixRequiredForOreDictSearch", prefixRequiredForOreDictSearch);
prefixRequiredForCreativeTabSearch = config.getBoolean(CATEGORY_SEARCH, "prefixRequiredForCreativeTabSearch", prefixRequiredForCreativeTabSearch);
prefixRequiredForColorSearch = config.getBoolean(CATEGORY_SEARCH, "prefixRequiredForColorSearch", prefixRequiredForColorSearch);
if (config.getCategory(CATEGORY_SEARCH).hasChanged()) {
needsReload = true;
}
ConfigCategory categoryAdvanced = config.getCategory(CATEGORY_ADVANCED);
categoryAdvanced.remove("nbtKeyIgnoreList");
deleteItemsInCheatModeEnabled = config.getBoolean(CATEGORY_ADVANCED, "deleteItemsInCheatModeEnabled", deleteItemsInCheatModeEnabled);
{
Property property = config.get(CATEGORY_ADVANCED, "deleteItemsInCheatModeEnabled", deleteItemsInCheatModeEnabled);
property.setShowInGui(false);
}
hideMissingModelsEnabled = config.getBoolean(CATEGORY_ADVANCED, "hideMissingModelsEnabled", hideMissingModelsEnabled);
if (categoryAdvanced.get("hideMissingModelsEnabled").hasChanged()) {
needsReload = true;
}
hideLaggyModelsEnabled = config.getBoolean(CATEGORY_ADVANCED, "hideLaggyModelsEnabled", hideLaggyModelsEnabled);
if (categoryAdvanced.get("hideLaggyModelsEnabled").hasChanged()) {
needsReload = true;
}
colorSearchEnabled = config.getBoolean(CATEGORY_ADVANCED, "colorSearchEnabled", colorSearchEnabled);
if (categoryAdvanced.get("colorSearchEnabled").hasChanged()) {
needsReload = true;
}
debugModeEnabled = config.getBoolean(CATEGORY_ADVANCED, "debugModeEnabled", debugModeEnabled);
{
Property property = config.get(CATEGORY_ADVANCED, "debugModeEnabled", debugModeEnabled);
property.setShowInGui(false);
}
debugItemEnabled = config.getBoolean(CATEGORY_ADVANCED, "debugItemEnabled", debugItemEnabled);
{
Property property = config.get(CATEGORY_ADVANCED, "debugItemEnabled", debugItemEnabled);
property.setShowInGui(false);
property.setRequiresMcRestart(true);
}
// migrate item blacklist to new file
if (config.hasKey(CATEGORY_ADVANCED, "itemBlacklist")) {
Property oldItemBlacklistProperty = config.get(CATEGORY_ADVANCED, "itemBlacklist", defaultItemBlacklist);
String[] itemBlacklistArray = oldItemBlacklistProperty.getStringList();
Property newItemBlacklistProperty = itemBlacklistConfig.get(CATEGORY_ADVANCED, "itemBlacklist", defaultItemBlacklist);
newItemBlacklistProperty.set(itemBlacklistArray);
categoryAdvanced.remove("itemBlacklist");
}
final boolean configChanged = config.hasChanged();
if (configChanged) {
config.save();
}
return needsReload;
}
private static boolean syncItemBlacklistConfig() {
itemBlacklistConfig.addCategory(CATEGORY_ADVANCED);
String[] itemBlacklistArray = itemBlacklistConfig.getStringList("itemBlacklist", CATEGORY_ADVANCED, defaultItemBlacklist);
itemBlacklist.clear();
Collections.addAll(itemBlacklist, itemBlacklistArray);
final boolean configChanged = itemBlacklistConfig.hasChanged();
if (configChanged) {
itemBlacklistConfig.save();
}
return configChanged;
}
private static boolean syncWorldConfig() {
boolean needsReload = false;
final String worldCategory = SessionData.getWorldUid();
Property property = worldConfig.get(worldCategory, "overlayEnabled", defaultOverlayEnabled);
property.setLanguageKey("config.jei.interface.overlayEnabled");
property.setComment(Translator.translateToLocal("config.jei.interface.overlayEnabled.comment"));
property.setShowInGui(false);
overlayEnabled = property.getBoolean();
property = worldConfig.get(worldCategory, "cheatItemsEnabled", defaultCheatItemsEnabled);
property.setLanguageKey("config.jei.mode.cheatItemsEnabled");
property.setComment(Translator.translateToLocal("config.jei.mode.cheatItemsEnabled.comment"));
cheatItemsEnabled = property.getBoolean();
property = worldConfig.get(worldCategory, "editEnabled", defaultEditModeEnabled);
property.setLanguageKey("config.jei.mode.editEnabled");
property.setComment(Translator.translateToLocal("config.jei.mode.editEnabled.comment"));
editModeEnabled = property.getBoolean();
if (property.hasChanged()) {
needsReload = true;
}
property = worldConfig.get(worldCategory, "filterText", defaultFilterText);
property.setShowInGui(false);
filterText = property.getString();
final boolean configChanged = worldConfig.hasChanged();
if (configChanged) {
worldConfig.save();
}
return needsReload;
}
private static boolean syncSearchColorsConfig() {
searchColorsConfig.addCategory(CATEGORY_SEARCH_COLORS);
final String[] searchColorDefaults = ColorGetter.getColorDefaults();
final String[] searchColors = searchColorsConfig.getStringList("searchColors", CATEGORY_SEARCH_COLORS, searchColorDefaults);
final ImmutableMap.Builder<Color, String> searchColorsMapBuilder = ImmutableMap.builder();
for (String entry : searchColors) {
final String[] values = entry.split(":");
if (values.length != 2) {
Log.error("Invalid format for searchColor entry: {}", entry);
} else {
try {
final String name = values[0];
final int colorValue = Integer.decode("0x" + values[1]);
final Color color = new Color(colorValue);
searchColorsMapBuilder.put(color, name);
} catch (NumberFormatException e) {
Log.error("Invalid number format for searchColor entry: {}", entry, e);
}
}
}
final ColorNamer colorNamer = new ColorNamer(searchColorsMapBuilder.build());
Internal.setColorNamer(colorNamer);
final boolean configChanged = searchColorsConfig.hasChanged();
if (configChanged) {
searchColorsConfig.save();
}
return configChanged;
}
private static boolean updateBlacklist() {
Property property = itemBlacklistConfig.get(CATEGORY_ADVANCED, "itemBlacklist", defaultItemBlacklist);
String[] currentBlacklist = itemBlacklist.toArray(new String[itemBlacklist.size()]);
property.set(currentBlacklist);
boolean changed = itemBlacklistConfig.hasChanged();
if (changed) {
itemBlacklistConfig.save();
}
return changed;
}
public static void addItemToConfigBlacklist(@Nonnull ItemStack itemStack, @Nonnull ItemBlacklistType blacklistType) {
final String uid = getItemStackUid(itemStack, blacklistType);
if (itemBlacklist.add(uid)) {
updateBlacklist();
}
}
public static void removeItemFromConfigBlacklist(@Nonnull ItemStack itemStack, @Nonnull ItemBlacklistType blacklistType) {
final String uid = getItemStackUid(itemStack, blacklistType);
if (itemBlacklist.remove(uid)) {
updateBlacklist();
}
}
public static boolean isItemOnConfigBlacklist(@Nonnull ItemStack itemStack) {
for (ItemBlacklistType itemBlacklistType : ItemBlacklistType.VALUES) {
if (isItemOnConfigBlacklist(itemStack, itemBlacklistType)) {
return true;
}
}
return false;
}
public static boolean isItemOnConfigBlacklist(@Nonnull ItemStack itemStack, @Nonnull ItemBlacklistType blacklistType) {
final String uid = getItemStackUid(itemStack, blacklistType);
return itemBlacklist.contains(uid);
}
private static String getItemStackUid(@Nonnull ItemStack itemStack, @Nonnull ItemBlacklistType blacklistType) {
StackHelper stackHelper = Internal.getStackHelper();
switch (blacklistType) {
case ITEM:
return stackHelper.getUniqueIdentifierForStack(itemStack, StackHelper.UidMode.NORMAL);
case WILDCARD:
return stackHelper.getUniqueIdentifierForStack(itemStack, StackHelper.UidMode.WILDCARD);
case MOD_ID:
return stackHelper.getModId(itemStack);
}
return "";
}
public enum ItemBlacklistType {
ITEM, WILDCARD, MOD_ID;
public static final ItemBlacklistType[] VALUES = values();
}
}