From fbdabf52e7d25471e5e7a41a2cf641f8ad36c05b Mon Sep 17 00:00:00 2001 From: coolGi2007 Date: Wed, 5 Jan 2022 23:07:40 +1030 Subject: [PATCH] Made a way to save 1 config entry at a time --- .../lod/common/wrappers/config/ConfigGui.java | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/com/seibel/lod/common/wrappers/config/ConfigGui.java b/common/src/main/java/com/seibel/lod/common/wrappers/config/ConfigGui.java index b687b6836..029dd50d6 100644 --- a/common/src/main/java/com/seibel/lod/common/wrappers/config/ConfigGui.java +++ b/common/src/main/java/com/seibel/lod/common/wrappers/config/ConfigGui.java @@ -86,6 +86,7 @@ public abstract class ConfigGui private static final Pattern DECIMAL_ONLY_REGEX = Pattern.compile("-?([\\d]+\\.?[\\d]*|[\\d]*\\.?[\\d]+|\\.)"); private static final List entries = new ArrayList<>(); + public static final Map entryMap = new HashMap<>(); // Change these to your own mod private static final String MOD_NAME = ModInfo.NAME; // For file saving and identifying @@ -163,6 +164,7 @@ public abstract class ConfigGui if (field.isAnnotationPresent(ConfigAnnotations.Entry.class) || field.isAnnotationPresent(ConfigAnnotations.Comment.class) || field.isAnnotationPresent(ConfigAnnotations.ScreenEntry.class)) { // If putting in your own mod then put your own check for server sided + entryMap.put((!category.isEmpty() ? category + "." : "") + field.getName(), info); info.category = category; if (!LodCommonMain.serverSided) initClient(field, info, category); @@ -353,24 +355,39 @@ public abstract class ConfigGui // Puts everything into its variable for (EntryInfo info : entries) { if (info.field.isAnnotationPresent(ConfigAnnotations.Entry.class)) { - String itemPath = (info.category.isEmpty() ? "" : info.category + ".") + info.field.getName(); - if (config.contains(itemPath)) { - if (info.field.getType().isEnum()) - info.value = config.getEnum(itemPath, info.varClass); - else - info.value = config.get(itemPath); - } else - config.set(itemPath, info.value); - - try { - info.field.set(null, info.value); - } catch (IllegalAccessException ignored) {} + saveOption(info); } } config.close(); } + // TODO: Use this in the config wrapper singleton + public static void saveOption(String name) { + saveOption(entryMap.get(name)); + } + + public static void saveOption(EntryInfo info) + { + CommentedFileConfig config = CommentedFileConfig.builder(configFilePath.toFile()).autosave().build(); + config.load(); + + String itemPath = (info.category.isEmpty() ? "" : info.category + ".") + info.field.getName(); + if (config.contains(itemPath)) { + if (info.field.getType().isEnum()) + info.value = config.getEnum(itemPath, info.varClass); + else + info.value = config.get(itemPath); + } else + config.set(itemPath, info.value); + + try { + info.field.set(null, info.value); + } catch (IllegalAccessException ignored) {} + + config.close(); + } + public static Screen getScreen(Screen parent, String category) {