Automatic config saving

This commit is contained in:
Norbi Peti 2019-03-11 16:50:24 +01:00
parent 9c15d0ffff
commit 7289385a33
6 changed files with 40 additions and 15 deletions

View file

@ -1,13 +1,13 @@
<component name="libraryTable"> <component name="libraryTable">
<library name="Maven: org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT"> <library name="Maven: org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT">
<CLASSES> <CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/spigotmc/spigot-api/1.12.2-R0.1-SNAPSHOT/spigot-api-1.12.2-R0.1-SNAPSHOT.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/spigotmc/spigot-api/1.12.2-R0.1-SNAPSHOT/spigot-api-1.12.2-R0.1-20180712.012057-156.jar!/" />
</CLASSES> </CLASSES>
<JAVADOC> <JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/spigotmc/spigot-api/1.12.2-R0.1-SNAPSHOT/spigot-api-1.12.2-R0.1-SNAPSHOT-javadoc.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/spigotmc/spigot-api/1.12.2-R0.1-SNAPSHOT/spigot-api-1.12.2-R0.1-20180712.012057-156-javadoc.jar!/" />
</JAVADOC> </JAVADOC>
<SOURCES> <SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/spigotmc/spigot-api/1.12.2-R0.1-SNAPSHOT/spigot-api-1.12.2-R0.1-SNAPSHOT-sources.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/spigotmc/spigot-api/1.12.2-R0.1-SNAPSHOT/spigot-api-1.12.2-R0.1-20180712.012057-156-sources.jar!/" />
</SOURCES> </SOURCES>
</library> </library>
</component> </component>

View file

@ -42,7 +42,7 @@ public abstract class ButtonPlugin extends JavaPlugin {
public final void onEnable() { public final void onEnable() {
var section = super.getConfig().getConfigurationSection("global"); var section = super.getConfig().getConfigurationSection("global");
if (section == null) section = super.getConfig().createSection("global"); if (section == null) section = super.getConfig().createSection("global");
iConfig = new IHaveConfig(section); iConfig = new IHaveConfig(section, this::saveConfig);
try { try {
pluginEnable(); pluginEnable();
} catch (Exception e) { } catch (Exception e) {

View file

@ -147,9 +147,9 @@ public abstract class Component<TP extends JavaPlugin> {
var configSect = compconf.getConfigurationSection(component.getClassName()); var configSect = compconf.getConfigurationSection(component.getClassName());
if (configSect == null) if (configSect == null)
configSect = compconf.createSection(component.getClassName()); configSect = compconf.createSection(component.getClassName());
component.config = new IHaveConfig(configSect); component.config = new IHaveConfig(configSect, plugin::saveConfig);
} else //Testing } else //Testing
component.config = new IHaveConfig(null); component.config = new IHaveConfig(null, plugin::saveConfig);
} }
/** /**

View file

@ -1,11 +1,17 @@
package buttondevteam.lib.architecture; package buttondevteam.lib.architecture;
import buttondevteam.core.MainPlugin;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
@ -16,7 +22,8 @@ import java.util.function.Function;
*/ */
@RequiredArgsConstructor(access = AccessLevel.PACKAGE) @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
//@AllArgsConstructor(access = AccessLevel.PACKAGE) //@AllArgsConstructor(access = AccessLevel.PACKAGE)
public class ConfigData<T> { //TODO: Save after a while public class ConfigData<T> {
private static final HashMap<Configuration, BukkitTask> saveTasks= new HashMap<>();
/** /**
* May be null for testing * May be null for testing
*/ */
@ -24,6 +31,7 @@ public class ConfigData<T> { //TODO: Save after a while
private final String path; private final String path;
private final T def; private final T def;
private final Object primitiveDef; private final Object primitiveDef;
private final Runnable saveAction;
/** /**
* The parameter is of a primitive type as returned by {@link YamlConfiguration#get(String)} * The parameter is of a primitive type as returned by {@link YamlConfiguration#get(String)}
*/ */
@ -39,13 +47,14 @@ public class ConfigData<T> { //TODO: Save after a while
private T value; private T value;
private boolean saved = false; private boolean saved = false;
public ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter) { public ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter, Runnable saveAction) {
this.config = config; this.config = config;
this.path = path; this.path = path;
this.def = def; this.def = def;
this.primitiveDef = primitiveDef; this.primitiveDef = primitiveDef;
this.getter = getter; this.getter = getter;
this.setter = setter; this.setter = setter;
this.saveAction=saveAction;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -89,8 +98,19 @@ public class ConfigData<T> { //TODO: Save after a while
if (setter != null && value != null) if (setter != null && value != null)
val = setter.apply(value); val = setter.apply(value);
else val = value; else val = value;
if (config != null) if (config != null) {
config.set(path, val); 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; this.value = value;
} }
} }

View file

@ -3,6 +3,8 @@ package buttondevteam.lib.architecture;
import lombok.Getter; import lombok.Getter;
import lombok.val; import lombok.val;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashMap; import java.util.HashMap;
import java.util.function.Function; import java.util.function.Function;
@ -15,14 +17,16 @@ public final class IHaveConfig {
private final HashMap<String, ConfigData<?>> datamap = new HashMap<>(); private final HashMap<String, ConfigData<?>> datamap = new HashMap<>();
@Getter @Getter
private ConfigurationSection config; private ConfigurationSection config;
private final Runnable saveAction;
/** /**
* May be used in testing. * May be used in testing.
* *
* @param section May be null for testing * @param section May be null for testing
*/ */
IHaveConfig(ConfigurationSection section) { IHaveConfig(ConfigurationSection section, Runnable saveAction) {
config = section; config = section;
this.saveAction=saveAction;
} }
/** /**
@ -36,7 +40,7 @@ public final class IHaveConfig {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> ConfigData<T> getData(String path, T def) { public <T> ConfigData<T> getData(String path, T def) {
ConfigData<?> data = datamap.get(path); 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<T>) data; return (ConfigData<T>) data;
} }
@ -54,7 +58,7 @@ public final class IHaveConfig {
public <T> ConfigData<T> getData(String path, T def, Function<Object, T> getter, Function<T, Object> setter) { public <T> ConfigData<T> getData(String path, T def, Function<Object, T> getter, Function<T, Object> setter) {
ConfigData<?> data = datamap.get(path); ConfigData<?> data = datamap.get(path);
if (data == null) 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<T>) data; return (ConfigData<T>) data;
} }
@ -72,7 +76,7 @@ public final class IHaveConfig {
public <T> ConfigData<T> getDataPrimDef(String path, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter) { public <T> ConfigData<T> getDataPrimDef(String path, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter) {
ConfigData<?> data = datamap.get(path); ConfigData<?> data = datamap.get(path);
if (data == null) 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<T>) data; return (ConfigData<T>) data;
} }
@ -89,7 +93,7 @@ public final class IHaveConfig {
ConfigData<?> data = datamap.get(path); ConfigData<?> data = datamap.get(path);
if (data == null) { if (data == null) {
val defval = def.get(); 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<T>) data; return (ConfigData<T>) data;
} }
@ -109,7 +113,7 @@ public final class IHaveConfig {
ConfigData<?> data = datamap.get(path); ConfigData<?> data = datamap.get(path);
if (data == null) { if (data == null) {
val defval = def.get(); 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<T>) data; return (ConfigData<T>) data;
} }

View file

@ -14,6 +14,7 @@
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.21" level="project" /> <orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.21" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.21" level="project" /> <orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.21" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.21" level="project" />
<orderEntry type="library" name="Maven: org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT" level="project" /> <orderEntry type="library" name="Maven: org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.6" level="project" /> <orderEntry type="library" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" /> <orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />