From 7289385a3349b075e2f8fa820fb56ab3321760c1 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 11 Mar 2019 16:50:24 +0100 Subject: [PATCH] Automatic config saving --- ...igotmc_spigot_api_1_12_2_R0_1_SNAPSHOT.xml | 6 ++--- .../lib/architecture/ButtonPlugin.java | 2 +- .../lib/architecture/Component.java | 4 +-- .../lib/architecture/ConfigData.java | 26 ++++++++++++++++--- .../lib/architecture/IHaveConfig.java | 16 +++++++----- ButtonProcessor/ButtonProcessor.iml | 1 + 6 files changed, 40 insertions(+), 15 deletions(-) diff --git a/.idea/libraries/Maven__org_spigotmc_spigot_api_1_12_2_R0_1_SNAPSHOT.xml b/.idea/libraries/Maven__org_spigotmc_spigot_api_1_12_2_R0_1_SNAPSHOT.xml index b6f88ae..42f0d6f 100644 --- a/.idea/libraries/Maven__org_spigotmc_spigot_api_1_12_2_R0_1_SNAPSHOT.xml +++ b/.idea/libraries/Maven__org_spigotmc_spigot_api_1_12_2_R0_1_SNAPSHOT.xml @@ -1,13 +1,13 @@ - + - + - + \ No newline at end of file diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java index 602aefa..381bfc6 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java @@ -42,7 +42,7 @@ public abstract class ButtonPlugin extends JavaPlugin { public final void onEnable() { var section = super.getConfig().getConfigurationSection("global"); if (section == null) section = super.getConfig().createSection("global"); - iConfig = new IHaveConfig(section); + iConfig = new IHaveConfig(section, this::saveConfig); try { pluginEnable(); } catch (Exception e) { diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java index d9a15c3..43e77fa 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java @@ -147,9 +147,9 @@ public abstract class Component { var configSect = compconf.getConfigurationSection(component.getClassName()); if (configSect == null) configSect = compconf.createSection(component.getClassName()); - component.config = new IHaveConfig(configSect); + component.config = new IHaveConfig(configSect, plugin::saveConfig); } else //Testing - component.config = new IHaveConfig(null); + component.config = new IHaveConfig(null, plugin::saveConfig); } /** diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java index a86c071..b2961a1 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java @@ -1,11 +1,17 @@ package buttondevteam.lib.architecture; +import buttondevteam.core.MainPlugin; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; +import org.bukkit.Bukkit; +import org.bukkit.configuration.Configuration; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; +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.Function; @@ -16,7 +22,8 @@ import java.util.function.Function; */ @RequiredArgsConstructor(access = AccessLevel.PACKAGE) //@AllArgsConstructor(access = AccessLevel.PACKAGE) -public class ConfigData { //TODO: Save after a while +public class ConfigData { + private static final HashMap saveTasks= new HashMap<>(); /** * May be null for testing */ @@ -24,6 +31,7 @@ public class ConfigData { //TODO: Save after a while private final String path; private final T def; private final Object primitiveDef; + private final Runnable saveAction; /** * The parameter is of a primitive type as returned by {@link YamlConfiguration#get(String)} */ @@ -39,13 +47,14 @@ public class ConfigData { //TODO: Save after a while private T value; private boolean saved = false; - public ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function getter, Function setter) { + public ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function getter, Function setter, Runnable saveAction) { this.config = config; this.path = path; this.def = def; this.primitiveDef = primitiveDef; this.getter = getter; this.setter = setter; + this.saveAction=saveAction; } @SuppressWarnings("unchecked") @@ -89,8 +98,19 @@ public class ConfigData { //TODO: Save after a while if (setter != null && value != null) val = setter.apply(value); else val = value; - if (config != null) + if (config != null) { config.set(path, val); + if(!saveTasks.containsKey(config.getRoot())) { + synchronized (saveTasks) { + saveTasks.put(config.getRoot(), Bukkit.getScheduler().runTaskLaterAsynchronously(MainPlugin.Instance, () -> { + synchronized (saveTasks) { + saveTasks.remove(config.getRoot()); + saveAction.run(); + } + }, 100)); + } + } + } this.value = value; } } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java index 98fa241..241e4f5 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java @@ -3,6 +3,8 @@ package buttondevteam.lib.architecture; import lombok.Getter; import lombok.val; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitTask; import java.util.HashMap; import java.util.function.Function; @@ -15,14 +17,16 @@ public final class IHaveConfig { private final HashMap> datamap = new HashMap<>(); @Getter private ConfigurationSection config; + private final Runnable saveAction; /** * May be used in testing. * * @param section May be null for testing */ - IHaveConfig(ConfigurationSection section) { + IHaveConfig(ConfigurationSection section, Runnable saveAction) { config = section; + this.saveAction=saveAction; } /** @@ -36,7 +40,7 @@ public final class IHaveConfig { @SuppressWarnings("unchecked") public ConfigData getData(String path, T def) { ConfigData data = datamap.get(path); - if (data == null) datamap.put(path, data = new ConfigData<>(config, path, def, def)); + if (data == null) datamap.put(path, data = new ConfigData<>(config, path, def, def, saveAction)); return (ConfigData) data; } @@ -54,7 +58,7 @@ public final class IHaveConfig { public ConfigData getData(String path, T def, Function getter, Function setter) { ConfigData data = datamap.get(path); if (data == null) - datamap.put(path, data = new ConfigData<>(config, path, def, setter.apply(def), getter, setter)); + datamap.put(path, data = new ConfigData<>(config, path, def, setter.apply(def), getter, setter, saveAction)); return (ConfigData) data; } @@ -72,7 +76,7 @@ public final class IHaveConfig { public ConfigData getDataPrimDef(String path, Object primitiveDef, Function getter, Function setter) { ConfigData data = datamap.get(path); if (data == null) - datamap.put(path, data = new ConfigData<>(config, path, getter.apply(primitiveDef), primitiveDef, getter, setter)); + datamap.put(path, data = new ConfigData<>(config, path, getter.apply(primitiveDef), primitiveDef, getter, setter, saveAction)); return (ConfigData) data; } @@ -89,7 +93,7 @@ public final class IHaveConfig { ConfigData data = datamap.get(path); if (data == null) { val defval = def.get(); - datamap.put(path, data = new ConfigData<>(config, path, defval, defval)); + datamap.put(path, data = new ConfigData<>(config, path, defval, defval, saveAction)); } return (ConfigData) data; } @@ -109,7 +113,7 @@ public final class IHaveConfig { ConfigData data = datamap.get(path); if (data == null) { val defval = def.get(); - datamap.put(path, data = new ConfigData<>(config, path, defval, setter.apply(defval), getter, setter)); + datamap.put(path, data = new ConfigData<>(config, path, defval, setter.apply(defval), getter, setter, saveAction)); } return (ConfigData) data; } diff --git a/ButtonProcessor/ButtonProcessor.iml b/ButtonProcessor/ButtonProcessor.iml index 3c6a51e..b606543 100755 --- a/ButtonProcessor/ButtonProcessor.iml +++ b/ButtonProcessor/ButtonProcessor.iml @@ -14,6 +14,7 @@ +