From 89c27a10c6950287d2503590e4ec70f1dcfd4a80 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 6 Dec 2019 03:04:11 +0100 Subject: [PATCH] Only save config if changed, number conversion fixes And do save config if changed #83 --- BuildConfigUpdater/BuildConfigUpdater.iml | 1 + .../lib/architecture/ConfigData.java | 59 ++++++++++--------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/BuildConfigUpdater/BuildConfigUpdater.iml b/BuildConfigUpdater/BuildConfigUpdater.iml index 67aa057..04dfdc0 100644 --- a/BuildConfigUpdater/BuildConfigUpdater.iml +++ b/BuildConfigUpdater/BuildConfigUpdater.iml @@ -18,6 +18,7 @@ + diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java index 403fcee..32221ad 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java @@ -15,7 +15,6 @@ import org.bukkit.scheduler.BukkitTask; import java.lang.reflect.Array; import java.util.HashMap; import java.util.List; -import java.util.Objects; import java.util.function.BiFunction; import java.util.function.Function; @@ -49,10 +48,6 @@ public class ConfigData { * The config value should not change outside this instance */ private T value; - /** - * Whether the default value is saved in the yaml - */ - private boolean saved = false; //This constructor is needed because it sets the getter and setter ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function getter, Function setter, Runnable saveAction) { @@ -85,22 +80,25 @@ public class ConfigData { @SuppressWarnings("unchecked") public T get() { if (value != null) return value; //Speed things up - Object val = config == null ? null : config.get(path); //config==null: testing - if (val == null) { + Object val; + if (config == null || !config.isSet(path)) { //Call set() if config == null val = primitiveDef; - } - if (!saved && Objects.equals(val, primitiveDef)) { //String needs .equals() if (def == null && config != null) //In Discord's case def may be null - config.set(path, primitiveDef); + setInternal(primitiveDef); else set(def); //Save default value - def is always set - saved = true; - } - BiFunction convert=(_val, _def) -> { - if (_val instanceof Number && _def != null) - _val = ChromaUtils.convertNumber((Number) _val, - (Class) _def.getClass()); - if (_val instanceof List && _def != null && _def.getClass().isArray()) + } else + val = config.get(path); //config==null: testing + if (val == null) //If it's set to null explicitly + val = primitiveDef; + BiFunction convert = (_val, _def) -> { + if (_def instanceof Number) //If we expect a number + if (_val instanceof Number) + _val = ChromaUtils.convertNumber((Number) _val, + (Class) _def.getClass()); + else + _val = _def; //If we didn't get a number, return default (which is a number) + else if (_val instanceof List && _def != null && _def.getClass().isArray()) _val = ((List) _val).toArray((T[]) Array.newInstance(_def.getClass().getComponentType(), 0)); return _val; }; @@ -121,20 +119,23 @@ public class ConfigData { if (setter != null && value != null) val = setter.apply(value); else val = value; - if (config != null) { - config.set(path, val); - if (!saveTasks.containsKey(config.getRoot())) { - synchronized (saveTasks) { - saveTasks.put(config.getRoot(), new SaveTask(Bukkit.getScheduler().runTaskLaterAsynchronously(MainPlugin.Instance, () -> { - synchronized (saveTasks) { - saveTasks.remove(config.getRoot()); - saveAction.run(); - } - }, 100), saveAction)); - } + if (config != null) + setInternal(val); + this.value = value; + } + + private void setInternal(Object val) { + config.set(path, val); + if (!saveTasks.containsKey(config.getRoot())) { + synchronized (saveTasks) { + saveTasks.put(config.getRoot(), new SaveTask(Bukkit.getScheduler().runTaskLaterAsynchronously(MainPlugin.Instance, () -> { + synchronized (saveTasks) { + saveTasks.remove(config.getRoot()); + saveAction.run(); + } + }, 100), saveAction)); } } - this.value = value; } @AllArgsConstructor