From 9923f26698a3d830b30f8060401009bf147f1a7c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 29 Aug 2020 02:57:50 +0200 Subject: [PATCH] Config fixes, reusing the config objects --- .../lib/architecture/ButtonPlugin.java | 11 +++----- .../lib/architecture/Component.java | 25 ++++++++++--------- .../lib/architecture/ConfigData.java | 3 ++- .../lib/architecture/IHaveConfig.java | 12 ++++++--- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java index a6d7e2e..d7c4714 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java @@ -25,9 +25,9 @@ import java.util.Stack; @HasConfig(global = true) public abstract class ButtonPlugin extends JavaPlugin { @Getter //Needs to be static as we don't know the plugin when a command is handled - private static Command2MC command2MC = new Command2MC(); + private static final Command2MC command2MC = new Command2MC(); @Getter(AccessLevel.PROTECTED) - private IHaveConfig iConfig; + private final IHaveConfig iConfig = new IHaveConfig(this::saveConfig); private CommentedConfiguration yaml; @Getter(AccessLevel.PROTECTED) private IHaveConfig data; //TODO @@ -35,8 +35,7 @@ public abstract class ButtonPlugin extends JavaPlugin { * Used to unregister components in the right order - and to reload configs */ @Getter - private Stack> componentStack = new Stack<>(); - ; + private final Stack> componentStack = new Stack<>(); protected abstract void pluginEnable(); @@ -72,8 +71,7 @@ public abstract class ButtonPlugin extends JavaPlugin { return false; var section = config.getConfigurationSection("global"); if (section == null) section = config.createSection("global"); - if (iConfig != null) iConfig.reset(section); - else iConfig = new IHaveConfig(section, this::saveConfig); + iConfig.reset(section); return true; } @@ -85,7 +83,6 @@ public abstract class ButtonPlugin extends JavaPlugin { pluginDisable(); if (ConfigData.saveNow(getConfig())) getLogger().info("Saved configuration changes."); - iConfig = null; //Clearing the hashmap is not enough, we need to update the section as well getCommand2MC().unregisterCommands(this); } catch (Exception e) { TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e); diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/Component.java b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/Component.java index 886e16b..c214064 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/Component.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/Component.java @@ -31,9 +31,7 @@ public abstract class Component { @Getter @NonNull private TP plugin; - @NonNull - private @Getter - IHaveConfig config; + private @Getter final IHaveConfig config = new IHaveConfig(null); private @Getter IHaveConfig data; //TODO public final ConfigData shouldBeEnabled() { @@ -83,6 +81,7 @@ public abstract class Component { return false; } component.plugin = plugin; + component.config.setSaveAction(plugin::saveConfig); updateConfig(plugin, component); component.register(plugin); components.put(component.getClass(), component); @@ -161,11 +160,8 @@ public abstract class Component { var configSect = compconf.getConfigurationSection(component.getClassName()); if (configSect == null) configSect = compconf.createSection(component.getClassName()); - if (component.config != null) component.config.reset(configSect); - else component.config = new IHaveConfig(configSect, plugin::saveConfig); - } else //Testing - if (component.config == null) - component.config = new IHaveConfig(null, plugin::saveConfig); + component.config.reset(configSect); + } //Testing: it's already set } /** @@ -192,7 +188,7 @@ public abstract class Component { * * @param plugin Plugin object */ - @SuppressWarnings({"unused", "WeakerAccess"}) + @SuppressWarnings({"unused"}) protected void register(JavaPlugin plugin) { } @@ -203,7 +199,7 @@ public abstract class Component { * * @param plugin Plugin object */ - @SuppressWarnings({"WeakerAccess", "unused"}) + @SuppressWarnings({"unused"}) protected void unregister(JavaPlugin plugin) { } @@ -257,10 +253,15 @@ public abstract class Component { var cs = c.getConfigurationSection(key); if (cs == null) cs = c.createSection(key); val res = cs.getValues(false).entrySet().stream().filter(e -> e.getValue() instanceof ConfigurationSection) - .collect(Collectors.toMap(Map.Entry::getKey, kv -> new IHaveConfig((ConfigurationSection) kv.getValue(), getPlugin()::saveConfig))); + .collect(Collectors.toMap(Map.Entry::getKey, kv -> { + var conf = new IHaveConfig(getPlugin()::saveConfig); + conf.reset((ConfigurationSection) kv.getValue()); + return conf; + })); if (res.size() == 0) { for (val entry : defaultProvider.entrySet()) { - val conf = new IHaveConfig(cs.createSection(entry.getKey()), getPlugin()::saveConfig); + val conf = new IHaveConfig(getPlugin()::saveConfig); + conf.reset(cs.createSection(entry.getKey())); entry.getValue().accept(conf); res.put(entry.getKey(), conf); } diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.java b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.java index c7202ad..78452cc 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.java @@ -30,7 +30,8 @@ public class ConfigData { private String path; protected final T def; private final Object primitiveDef; - private final Runnable saveAction; + @Setter(AccessLevel.PACKAGE) + private Runnable saveAction; /** * The parameter is of a primitive type as returned by {@link YamlConfiguration#get(String)} */ diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java index 54f3091..d2d092a 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java @@ -23,15 +23,14 @@ public final class IHaveConfig { */ @Getter private ConfigurationSection config; - private final Runnable saveAction; + private Runnable saveAction; /** * May be used in testing. * - * @param section May be null for testing + * @param saveAction What to do to save the config to disk. Don't use get methods until it's non-null. */ - IHaveConfig(ConfigurationSection section, Runnable saveAction) { - config = section; + IHaveConfig(Runnable saveAction) { this.saveAction = saveAction; } @@ -183,6 +182,11 @@ public final class IHaveConfig { datamap.forEach((path, data) -> data.reset(config)); } + void setSaveAction(Runnable saveAction) { + this.saveAction = saveAction; + datamap.forEach((path, data) -> data.setSaveAction(saveAction)); + } + /** * Generates the config YAML. *