From b369954c0588d99e51dd3062d082a69bb6411ab6 Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 20 Aug 2021 16:50:23 +0200 Subject: [PATCH] Save configs from ModMenu-Screen --- .../handler/autosync/AutoSync.java | 20 ++++++++-------- src/main/java/ru/bclib/config/Config.java | 6 +++-- .../java/ru/bclib/config/NamedPathConfig.java | 17 +++++++++++--- .../gui/gridlayout/GridCheckboxCell.java | 9 +++++++- .../java/ru/bclib/gui/gridlayout/GridRow.java | 8 +++++++ .../java/ru/bclib/gui/modmenu/MainScreen.java | 23 ++++++++++++------- .../resources/assets/bclib/lang/en_us.json | 9 +++++++- 7 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/main/java/ru/bclib/api/dataexchange/handler/autosync/AutoSync.java b/src/main/java/ru/bclib/api/dataexchange/handler/autosync/AutoSync.java index 53f98388..dd999d7c 100644 --- a/src/main/java/ru/bclib/api/dataexchange/handler/autosync/AutoSync.java +++ b/src/main/java/ru/bclib/api/dataexchange/handler/autosync/AutoSync.java @@ -32,11 +32,13 @@ public class AutoSync { @Environment(EnvType.CLIENT) public static class ClientConfig extends NamedPathConfig{ - public static final ConfigToken.Bool DEBUG_HASHES = new Bool(true, SYNC_CATEGORY, "debugHashes"); - public static final ConfigToken.Bool ENABLED = new Bool(true, SYNC_CATEGORY, "enabled"); - public static final ConfigToken.Bool ACCEPT_CONFIGS = new Bool(true, "acceptConfigs", "enabled"); - public static final ConfigToken.Bool ACCEPT_FILES = new Bool(true, "acceptFiles", "enabled"); - public static final ConfigToken.Bool ACCEPT_MODS = new Bool(true, "acceptMods", "enabled"); + public static final ConfigToken.Bool ENABLED = new Bool(true, "enabled", SYNC_CATEGORY); + public static final ConfigToken.Bool ACCEPT_CONFIGS = new Bool(true,"acceptConfigs", SYNC_CATEGORY); + public static final ConfigToken.Bool ACCEPT_FILES = new Bool(true,"acceptFiles", SYNC_CATEGORY); + public static final ConfigToken.Bool ACCEPT_MODS = new Bool(true,"acceptMods", SYNC_CATEGORY); + public static final ConfigToken.Bool SYNC_MOD_FOLDER = new Bool(false, "syncModFolder", SYNC_CATEGORY); + public static final ConfigToken.Bool DEBUG_HASHES = new Bool(true, "debugHashes", SYNC_CATEGORY); + public ClientConfig(){ super(BCLib.MOD_ID, "client", false); @@ -64,10 +66,10 @@ public class AutoSync { } public static class ServerConfig extends NamedPathConfig { - public static final ConfigToken.Bool ENABLED = new Bool(true, SYNC_CATEGORY, "enabled"); - public static final ConfigToken.Bool OFFER_CONFIGS = new Bool(true, "offerConfigs", "enabled"); - public static final ConfigToken.Bool OFFER_FILES = new Bool(true, "offerFiles", "enabled"); - public static final ConfigToken.Bool OFFER_MODS = new Bool(true, "offerMods", "enabled"); + public static final ConfigToken.Bool ENABLED = new Bool(true, "enabled", SYNC_CATEGORY); + public static final ConfigToken.Bool OFFER_CONFIGS = new Bool(true,"offerConfigs", SYNC_CATEGORY); + public static final ConfigToken.Bool OFFER_FILES = new Bool(true,"offerFiles", SYNC_CATEGORY); + public static final ConfigToken.Bool OFFER_MODS = new Bool(true,"offerMods", SYNC_CATEGORY); public ServerConfig(){ diff --git a/src/main/java/ru/bclib/config/Config.java b/src/main/java/ru/bclib/config/Config.java index 2901021f..0cedea0e 100644 --- a/src/main/java/ru/bclib/config/Config.java +++ b/src/main/java/ru/bclib/config/Config.java @@ -22,6 +22,7 @@ public abstract class Config { protected final static Map autoSyncConfigs = new HashMap<>(); protected final ConfigKeeper keeper; protected final boolean autoSync; + public final String configID; protected abstract void registerEntries(); @@ -34,12 +35,13 @@ public abstract class Config { } protected Config(String modID, String group, boolean autoSync, boolean diffContent) { + configID = modID + "." + group; this.keeper = new ConfigKeeper(modID, group); this.registerEntries(); this.autoSync = autoSync; if (autoSync) { - final String uid = CONFIG_SYNC_PREFIX + modID + "_" + group; + final String uid = CONFIG_SYNC_PREFIX + configID; final AutoSyncID aid = new AutoSyncID(BCLib.MOD_ID, uid); if (diffContent) DataExchangeAPI.addAutoSyncFile(aid.modID, aid.uniqueID, keeper.getConfigFile(),this::compareForSync); @@ -47,7 +49,7 @@ public abstract class Config { DataExchangeAPI.addAutoSyncFile(aid.modID, aid.uniqueID, keeper.getConfigFile()); autoSyncConfigs.put(aid, this); - BCLib.LOGGER.info("Added Config " + modID + "." + group + " to auto sync (" + (diffContent?"content diff":"file hash") + ")"); + BCLib.LOGGER.info("Added Config " + configID + " to auto sync (" + (diffContent?"content diff":"file hash") + ")"); } } diff --git a/src/main/java/ru/bclib/config/NamedPathConfig.java b/src/main/java/ru/bclib/config/NamedPathConfig.java index b3d47b46..ff92c67d 100644 --- a/src/main/java/ru/bclib/config/NamedPathConfig.java +++ b/src/main/java/ru/bclib/config/NamedPathConfig.java @@ -2,6 +2,10 @@ package ru.bclib.config; import net.minecraft.resources.ResourceLocation; import ru.bclib.BCLib; +import ru.bclib.config.NamedPathConfig.ConfigToken.Bool; +import ru.bclib.config.NamedPathConfig.ConfigToken.Float; +import ru.bclib.config.NamedPathConfig.ConfigToken.Int; +import ru.bclib.config.NamedPathConfig.ConfigToken.Str; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -74,12 +78,19 @@ public class NamedPathConfig extends PathConfig{ } private void set(ConfigToken what, Object value) { - BCLib.LOGGER.error("Accessing " + what + " as general type is not supported."); + if (what instanceof Bool) set(what, (boolean)value); + else if (what instanceof Int) set(what, (int)value); + else if (what instanceof Float) set(what, (float)value); + else if (what instanceof Str) set(what, (String)value); + else BCLib.LOGGER.error("Accessing " + what + " as general type is not supported."); } private Object get(ConfigToken what){ - BCLib.LOGGER.error("Accessing " + what + " as general type is not supported."); - return null; + if (what instanceof Bool) return get((Bool)what); + else if (what instanceof Int) return get((Int)what); + else if (what instanceof Float) return get((Float)what); + else if (what instanceof Str) return get((Str)what); + else return null; } public void set(ConfigToken.Int what, int value) { diff --git a/src/main/java/ru/bclib/gui/gridlayout/GridCheckboxCell.java b/src/main/java/ru/bclib/gui/gridlayout/GridCheckboxCell.java index a34ebbb0..ec1e93f9 100644 --- a/src/main/java/ru/bclib/gui/gridlayout/GridCheckboxCell.java +++ b/src/main/java/ru/bclib/gui/gridlayout/GridCheckboxCell.java @@ -31,13 +31,20 @@ public class GridCheckboxCell extends GridCell{ private boolean checked; GridCheckboxCell(Component text, boolean checked, float alpha, double width, GridValueType widthType, double height) { + this(text, checked, alpha, width, widthType, height, null); + } + + GridCheckboxCell(Component text, boolean checked, float alpha, double width, GridValueType widthType, double height, Consumer onChange) { super(width, height, widthType, null, null); this.componentPlacer = (transform) -> { Checkbox cb = new SignalingCheckBox(transform.left, transform.top, transform.width, transform.height, text, checked, - (state)-> this.checked = state + (state)-> { + this.checked = state; + if (onChange!=null) onChange.accept(state); + } ); cb.setAlpha(alpha); return cb; diff --git a/src/main/java/ru/bclib/gui/gridlayout/GridRow.java b/src/main/java/ru/bclib/gui/gridlayout/GridRow.java index 43a2844e..c0609029 100644 --- a/src/main/java/ru/bclib/gui/gridlayout/GridRow.java +++ b/src/main/java/ru/bclib/gui/gridlayout/GridRow.java @@ -14,6 +14,7 @@ import ru.bclib.gui.gridlayout.GridLayout.VerticalAlignment; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Consumer; import java.util.function.Function; @Environment(EnvType.CLIENT) @@ -86,6 +87,13 @@ public class GridRow extends GridContainer { return cell; } + public GridCheckboxCell addCheckbox(Component text, boolean checked, Font font, Consumer onChange){ + final int width = font.width(text.getVisualOrderText()) + 24 + 2 * 12; + + GridCheckboxCell cell = new GridCheckboxCell(text, checked, 1.0f, width, widthType, 20, onChange); + this.cells.add(cell); + return cell; + } public GridCheckboxCell addCheckbox(Component text, boolean checked, int height) { return addCheckbox(text, checked, 1.0f, height); diff --git a/src/main/java/ru/bclib/gui/modmenu/MainScreen.java b/src/main/java/ru/bclib/gui/modmenu/MainScreen.java index b0770dff..bec9e5de 100644 --- a/src/main/java/ru/bclib/gui/modmenu/MainScreen.java +++ b/src/main/java/ru/bclib/gui/modmenu/MainScreen.java @@ -19,12 +19,14 @@ public class MainScreen extends GridScreen{ } protected TranslatableComponent getComponent(NamedPathConfig config, ConfigToken.Bool token, String type){ - String path = ""; + StringBuilder path = new StringBuilder(); for (String p : token.getPath()){ - path += "." + p; + path.append(".") + .append(p); } - return new TranslatableComponent(type + ".config." + path ); + path.append(".").append(token.getEntry()); + return new TranslatableComponent(type + ".config." + config.configID + path ); } protected void addRow(GridColumn grid, NamedPathConfig config, ConfigToken token){ @@ -37,21 +39,26 @@ public class MainScreen extends GridScreen{ protected void addRow(GridColumn grid, NamedPathConfig config, ConfigToken.Bool token){ GridRow row = grid.addRow(); - row.addCheckbox(getComponent(config, token, "title"), config.get(token), 20); - + row.addCheckbox(getComponent(config, token, "title"), config.get(token), font, (state)-> config.set(token, state)); } + + @Override + public boolean shouldCloseOnEsc() { + return false; + } + @Override protected void initLayout() { final int BUTTON_HEIGHT = 20; - grid.addSpacerRow(20); Configs.CLIENT_CONFIG.getAllOptions().forEach(o -> addRow(grid, Configs.CLIENT_CONFIG, o)); + grid.addSpacerRow(15); GridRow row = grid.addRow(); row.addFiller(); - row.addButton(CommonComponents.GUI_BACK, BUTTON_HEIGHT, font, (button)->{ + row.addButton(CommonComponents.GUI_DONE, BUTTON_HEIGHT, font, (button)->{ + Configs.CLIENT_CONFIG.saveChanges(); onClose(); }); - row.addFiller(); } } diff --git a/src/main/resources/assets/bclib/lang/en_us.json b/src/main/resources/assets/bclib/lang/en_us.json index cc591dd7..466bc574 100644 --- a/src/main/resources/assets/bclib/lang/en_us.json +++ b/src/main/resources/assets/bclib/lang/en_us.json @@ -16,5 +16,12 @@ "title.bclib.confirmrestart": "Restart Required", "message.bclib.confirmrestart": "The requested content was synchronized. You need to restart Minecraft now.", "title.link.bclib.discord": "Discord", - "title.bclib.modmenu.main": "BCLib Settings" + "title.bclib.modmenu.main": "BCLib Settings", + + "title.config.bclib.client.auto_sync.enabled": "Enable Auto-Sync", + "title.config.bclib.client.auto_sync.acceptConfigs": "Accept incoming Confog Files", + "title.config.bclib.client.auto_sync.acceptFiles": "Accept incoming Files", + "title.config.bclib.client.auto_sync.acceptMods": "Accept incoming Mods", + "title.config.bclib.client.auto_sync.syncModFolder": "Sync entire Mod-Folder from Server", + "title.config.bclib.client.auto_sync.debugHashes": "Print Debug-Hashes to Log" } \ No newline at end of file