Skip to content

Commit a501ef3

Browse files
committed
added config file
1 parent ed594b7 commit a501ef3

File tree

2 files changed

+74
-16
lines changed

2 files changed

+74
-16
lines changed

src/main/java/mezz/jei/IngredientBookmarks.java

+24-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import mezz.jei.api.ingredients.IIngredientHelper;
99
import mezz.jei.api.ingredients.IIngredientRegistry;
1010
import mezz.jei.api.ingredients.IIngredientRenderer;
11+
import mezz.jei.config.Config;
1112
import mezz.jei.gui.ingredients.IIngredientListElement;
1213
import mezz.jei.util.IngredientListElement;
1314
import com.google.common.collect.ImmutableCollection;
@@ -22,34 +23,43 @@ public class IngredientBookmarks implements IIngredientBookmarks {
2223

2324
public IngredientBookmarks(IIngredientRegistry ingredientRegistry) {
2425
this.ingredientRegistry = ingredientRegistry;
26+
27+
String[] bookmarks = Config.getBookmarks();
28+
29+
for (String uniqueId : bookmarks) {
30+
bookmarkIds.add(uniqueId);
31+
bookmarkList.put(uniqueId, findIngredientFromUniqueId(uniqueId));
32+
}
2533
}
2634

2735
@Override
2836
public <V> void toggleIngredientBookmark(V ingredient) {
2937
IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredient);
3038
String uniqueId = ingredientHelper.getUniqueId(ingredient);
3139

32-
// find ingredient in registry
33-
ImmutableCollection<Class> clazzes = ingredientRegistry.getRegisteredIngredientClasses();
34-
for (Class<V> clazz : clazzes) {
35-
ImmutableList<V> iList = ingredientRegistry.getIngredients(clazz);
36-
IIngredientHelper<V> iHelper = ingredientRegistry.getIngredientHelper(clazz);
37-
for (V i : iList) {
38-
if (iHelper.getUniqueId(i).equals(uniqueId)) {
39-
ingredient = i;
40-
break;
41-
}
42-
}
43-
}
44-
4540
// System.out.println(ingredientHelper.getUniqueId(ingredient));
4641
if (bookmarkIds.contains(uniqueId)) {
4742
bookmarkIds.remove(uniqueId);
4843
bookmarkList.remove(uniqueId);
4944
} else {
5045
bookmarkIds.add(uniqueId);
51-
bookmarkList.put(uniqueId, ingredient);
46+
bookmarkList.put(uniqueId, findIngredientFromUniqueId(uniqueId));
47+
}
48+
Config.updateBookmarks(bookmarkIds.toArray(new String[bookmarkIds.size()]));
49+
}
50+
51+
private <V> V findIngredientFromUniqueId(String uniqueId) {
52+
ImmutableCollection<Class> classes = ingredientRegistry.getRegisteredIngredientClasses();
53+
for (Class<V> clazz : classes) {
54+
ImmutableList<V> iList = ingredientRegistry.getIngredients(clazz);
55+
IIngredientHelper<V> iHelper = ingredientRegistry.getIngredientHelper(clazz);
56+
for (V i : iList) {
57+
if (iHelper.getUniqueId(i).equals(uniqueId)) {
58+
return i;
59+
}
60+
}
5261
}
62+
return null;
5363
}
5464

5565
@Override

src/main/java/mezz/jei/config/Config.java

+50-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.Collections;
77
import java.util.EnumSet;
88
import java.util.HashSet;
9+
import java.util.LinkedList;
10+
import java.util.List;
911
import java.util.Locale;
1012
import java.util.Set;
1113

@@ -40,6 +42,8 @@ public class Config {
4042
private static LocalizedConfiguration itemBlacklistConfig;
4143
@Nullable
4244
private static LocalizedConfiguration searchColorsConfig;
45+
@Nullable
46+
private static LocalizedConfiguration bookmarksConfig;
4347

4448
// advanced
4549
private static boolean debugModeEnabled = false;
@@ -73,7 +77,11 @@ public class Config {
7377

7478
// item blacklist
7579
private static final Set<String> itemBlacklist = new HashSet<String>();
76-
private static final String[] defaultItemBlacklist = new String[]{};
80+
private static final String[] defaultItemBlacklist = new String[] {};
81+
82+
// bookmarks
83+
private static final String[] defaultBookmarks = new String[] {};
84+
private static String[] bookmarks;
7785

7886
private Config() {
7987

@@ -226,6 +234,8 @@ public static void preInit(FMLPreInitializationEvent event) {
226234
final File itemBlacklistConfigFile = new File(jeiConfigurationDir, "itemBlacklist.cfg");
227235
final File searchColorsConfigFile = new File(jeiConfigurationDir, "searchColors.cfg");
228236
final File worldConfigFile = new File(jeiConfigurationDir, "worldSettings.cfg");
237+
final File bookmarksConfigFile = new File(jeiConfigurationDir, "bookmarks.cfg");
238+
229239
worldConfig = new Configuration(worldConfigFile, "0.1.0");
230240

231241
{
@@ -257,10 +267,12 @@ public static void preInit(FMLPreInitializationEvent event) {
257267
config = new LocalizedConfiguration(configKeyPrefix, configFile, "0.2.0");
258268
itemBlacklistConfig = new LocalizedConfiguration(configKeyPrefix, itemBlacklistConfigFile, "0.1.0");
259269
searchColorsConfig = new LocalizedConfiguration(configKeyPrefix, searchColorsConfigFile, "0.1.0");
270+
bookmarksConfig = new LocalizedConfiguration(configKeyPrefix, bookmarksConfigFile, "0.1.0");
260271

261272
syncConfig();
262273
syncItemBlacklistConfig();
263274
syncSearchColorsConfig();
275+
syncBookmarksConfig();
264276
}
265277

266278
public static boolean syncAllConfig() {
@@ -315,7 +327,7 @@ private static boolean syncConfig() {
315327
searchCategory.remove("prefixRequiredForCreativeTabSearch");
316328
searchCategory.remove("prefixRequiredForColorSearch");
317329

318-
SearchMode[] searchModes = SearchMode.values();
330+
SearchMode[] searchModes = SearchMode.values();
319331
modNameSearchMode = config.getEnum("modNameSearchMode", CATEGORY_SEARCH, defaultModNameSearchMode, searchModes);
320332
tooltipSearchMode = config.getEnum("tooltipSearchMode", CATEGORY_SEARCH, defaultTooltipSearchMode, searchModes);
321333
oreDictSearchMode = config.getEnum("oreDictSearchMode", CATEGORY_SEARCH, defaultOreDictSearchMode, searchModes);
@@ -402,6 +414,42 @@ private static boolean syncItemBlacklistConfig() {
402414
return configChanged;
403415
}
404416

417+
private static boolean syncBookmarksConfig() {
418+
if (bookmarksConfig == null) {
419+
return false;
420+
}
421+
422+
bookmarksConfig.addCategory(CATEGORY_ADVANCED);
423+
424+
bookmarks = bookmarksConfig.getStringList("bookmarks", CATEGORY_ADVANCED, defaultBookmarks);
425+
426+
final boolean configChanged = bookmarksConfig.hasChanged();
427+
if (configChanged) {
428+
bookmarksConfig.save();
429+
}
430+
return configChanged;
431+
}
432+
433+
public static boolean updateBookmarks(String[] bookmarkIds) {
434+
bookmarks = bookmarkIds;
435+
if (bookmarksConfig == null) {
436+
return false;
437+
}
438+
Property property = bookmarksConfig.get(CATEGORY_ADVANCED, "bookmarks", defaultBookmarks);
439+
440+
property.set(bookmarks);
441+
442+
boolean changed = bookmarksConfig.hasChanged();
443+
if (changed) {
444+
bookmarksConfig.save();
445+
}
446+
return changed;
447+
}
448+
449+
public static String[] getBookmarks() {
450+
return bookmarks;
451+
}
452+
405453
public static boolean syncWorldConfig() {
406454
if (worldConfig == null) {
407455
return false;

0 commit comments

Comments
 (0)