Config fixes, actually registering the configs

This commit is contained in:
Norbi Peti 2020-08-30 02:21:34 +02:00
parent 9923f26698
commit 784ad5e1c7
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
4 changed files with 44 additions and 47 deletions

View file

@ -5,7 +5,6 @@ import buttondevteam.lib.ChromaUtils;
import lombok.*; import lombok.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.Configuration; import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
@ -24,14 +23,12 @@ public class ConfigData<T> {
/** /**
* May be null for testing * May be null for testing
*/ */
private ConfigurationSection config; private IHaveConfig config;
@Getter @Getter
@Setter(AccessLevel.PACKAGE) @Setter(AccessLevel.PACKAGE)
private String path; private String path;
protected final T def; protected final T def;
private final Object primitiveDef; private final Object primitiveDef;
@Setter(AccessLevel.PACKAGE)
private 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)}
*/ */
@ -46,7 +43,7 @@ public class ConfigData<T> {
*/ */
private T value; private T value;
ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter, Runnable saveAction) { ConfigData(IHaveConfig config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter) {
if (def == null) { if (def == null) {
if (primitiveDef == null) if (primitiveDef == null)
throw new IllegalArgumentException("Either def or primitiveDef must be set."); throw new IllegalArgumentException("Either def or primitiveDef must be set.");
@ -66,7 +63,6 @@ public class ConfigData<T> {
this.primitiveDef = primitiveDef; this.primitiveDef = primitiveDef;
this.getter = getter; this.getter = getter;
this.setter = setter; this.setter = setter;
this.saveAction = saveAction;
} }
@Override @Override
@ -74,14 +70,14 @@ public class ConfigData<T> {
return "ConfigData{" + "path='" + path + '\'' + ", value=" + value + '}'; return "ConfigData{" + "path='" + path + '\'' + ", value=" + value + '}';
} }
void reset(ConfigurationSection config) { void reset() {
value = null; value = null;
this.config = config;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T get() { public T get() {
if (value != null) return value; //Speed things up if (value != null) return value; //Speed things up
var config = this.config.getConfig();
Object val; Object val;
if (config == null || !config.isSet(path)) { //Call set() if config == null if (config == null || !config.isSet(path)) { //Call set() if config == null
val = primitiveDef; val = primitiveDef;
@ -127,19 +123,21 @@ public class ConfigData<T> {
} }
private void setInternal(Object val) { private void setInternal(Object val) {
config.set(path, val); config.getConfig().set(path, val);
signalChange(config, saveAction); signalChange(config);
} }
static void signalChange(ConfigurationSection config, Runnable saveAction) { static void signalChange(IHaveConfig config) {
if (!saveTasks.containsKey(config.getRoot())) { var cc = config.getConfig();
var sa = config.getSaveAction();
if (!saveTasks.containsKey(cc.getRoot())) {
synchronized (saveTasks) { synchronized (saveTasks) {
saveTasks.put(config.getRoot(), new SaveTask(Bukkit.getScheduler().runTaskLaterAsynchronously(MainPlugin.Instance, () -> { saveTasks.put(cc.getRoot(), new SaveTask(Bukkit.getScheduler().runTaskLaterAsynchronously(MainPlugin.Instance, () -> {
synchronized (saveTasks) { synchronized (saveTasks) {
saveTasks.remove(config.getRoot()); saveTasks.remove(cc.getRoot());
saveAction.run(); sa.run();
} }
}, 100), saveAction)); }, 100), sa));
} }
} }
} }
@ -163,19 +161,18 @@ public class ConfigData<T> {
return false; return false;
} }
public static <T> ConfigData.ConfigDataBuilder<T> builder(ConfigurationSection config, String path, Runnable saveAction) { public static <T> ConfigData.ConfigDataBuilder<T> builder(IHaveConfig config, String path) {
return new ConfigDataBuilder<T>(config, path, saveAction); return new ConfigDataBuilder<T>(config, path);
} }
@RequiredArgsConstructor(access = AccessLevel.PACKAGE) @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public static class ConfigDataBuilder<T> { public static class ConfigDataBuilder<T> {
private final ConfigurationSection config; private final IHaveConfig config;
private final String path; private final String path;
private T def; private T def;
private Object primitiveDef; private Object primitiveDef;
private Function<Object, T> getter; private Function<Object, T> getter;
private Function<T, Object> setter; private Function<T, Object> setter;
private final Runnable saveAction;
/** /**
* The default value to use, as used in code. If not a primitive type, use the {@link #getter(Function)} and {@link #setter(Function)} methods. * The default value to use, as used in code. If not a primitive type, use the {@link #getter(Function)} and {@link #setter(Function)} methods.
@ -233,7 +230,9 @@ public class ConfigData<T> {
* @return A ConfigData instance. * @return A ConfigData instance.
*/ */
public ConfigData<T> build() { public ConfigData<T> build() {
return new ConfigData<>(config, path, def, primitiveDef, getter, setter, saveAction); ConfigData<T> config = new ConfigData<>(this.config, path, def, primitiveDef, getter, setter);
this.config.onConfigBuild(config);
return config;
} }
/** /**
@ -242,9 +241,9 @@ public class ConfigData<T> {
* @return A ReadOnlyConfigData instance. * @return A ReadOnlyConfigData instance.
*/ */
public ReadOnlyConfigData<T> buildReadOnly() { public ReadOnlyConfigData<T> buildReadOnly() {
return new ReadOnlyConfigData<>(config, path, def, primitiveDef, getter, setter, saveAction); return new ReadOnlyConfigData<>(config, path, def, primitiveDef, getter, setter);
} }
public String toString() {return "ConfigData.ConfigDataBuilder(config=" + this.config + ", path=" + this.path + ", def=" + this.def + ", primitiveDef=" + this.primitiveDef + ", getter=" + this.getter + ", setter=" + this.setter + ", saveAction=" + this.saveAction + ")";} public String toString() {return "ConfigData.ConfigDataBuilder(config=" + this.config + ", path=" + this.path + ", def=" + this.def + ", primitiveDef=" + this.primitiveDef + ", getter=" + this.getter + ", setter=" + this.setter + ")";}
} }
} }

View file

@ -3,6 +3,7 @@ package buttondevteam.lib.architecture;
import buttondevteam.core.MainPlugin; import buttondevteam.core.MainPlugin;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import lombok.val; import lombok.val;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -23,6 +24,8 @@ public final class IHaveConfig {
*/ */
@Getter @Getter
private ConfigurationSection config; private ConfigurationSection config;
@Getter
@Setter
private Runnable saveAction; private Runnable saveAction;
/** /**
@ -42,7 +45,11 @@ public final class IHaveConfig {
* @return A ConfigData builder to set how to obtain the value * @return A ConfigData builder to set how to obtain the value
*/ */
public <T> ConfigData.ConfigDataBuilder<T> getConfig(String path) { public <T> ConfigData.ConfigDataBuilder<T> getConfig(String path) {
return ConfigData.builder(config, path, saveAction); return ConfigData.builder(this, path);
}
void onConfigBuild(ConfigData<?> config) {
datamap.put(config.getPath(), config);
} }
/** /**
@ -56,7 +63,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, null, null, saveAction)); if (data == null) datamap.put(path, data = new ConfigData<>(this, path, def, def, null, null));
return (ConfigData<T>) data; return (ConfigData<T>) data;
} }
@ -74,7 +81,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, saveAction)); datamap.put(path, data = new ConfigData<>(this, path, def, setter.apply(def), getter, setter));
return (ConfigData<T>) data; return (ConfigData<T>) data;
} }
@ -92,7 +99,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, saveAction)); datamap.put(path, data = new ConfigData<>(this, path, getter.apply(primitiveDef), primitiveDef, getter, setter));
return (ConfigData<T>) data; return (ConfigData<T>) data;
} }
@ -110,7 +117,7 @@ public final class IHaveConfig {
public <T> ReadOnlyConfigData<T> getReadOnlyDataPrimDef(String path, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter) { public <T> ReadOnlyConfigData<T> getReadOnlyDataPrimDef(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 ReadOnlyConfigData<>(config, path, getter.apply(primitiveDef), primitiveDef, getter, setter, saveAction)); datamap.put(path, data = new ReadOnlyConfigData<>(this, path, getter.apply(primitiveDef), primitiveDef, getter, setter));
return (ReadOnlyConfigData<T>) data; return (ReadOnlyConfigData<T>) data;
} }
@ -127,7 +134,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, null, null, saveAction)); datamap.put(path, data = new ConfigData<>(this, path, defval, defval, null, null));
} }
return (ConfigData<T>) data; return (ConfigData<T>) data;
} }
@ -147,7 +154,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, saveAction)); datamap.put(path, data = new ConfigData<>(this, path, defval, setter.apply(defval), getter, setter));
} }
return (ConfigData<T>) data; return (ConfigData<T>) data;
} }
@ -163,7 +170,7 @@ public final class IHaveConfig {
public <T> ListConfigData<T> getListData(String path) { public <T> ListConfigData<T> getListData(String path) {
ConfigData<?> data = datamap.get(path); ConfigData<?> data = datamap.get(path);
if (data == null) if (data == null)
datamap.put(path, data = new ListConfigData<>(config, path, new ListConfigData.List<T>(), saveAction)); datamap.put(path, data = new ListConfigData<>(this, path, new ListConfigData.List<T>()));
return (ListConfigData<T>) data; return (ListConfigData<T>) data;
} }
@ -171,7 +178,7 @@ public final class IHaveConfig {
* Schedules a save operation. Use after changing the ConfigurationSection directly. * Schedules a save operation. Use after changing the ConfigurationSection directly.
*/ */
public void signalChange() { public void signalChange() {
ConfigData.signalChange(config, saveAction); ConfigData.signalChange(this);
} }
/** /**
@ -179,12 +186,6 @@ public final class IHaveConfig {
*/ */
public void reset(ConfigurationSection config) { public void reset(ConfigurationSection config) {
this.config = config; this.config = config;
datamap.forEach((path, data) -> data.reset(config));
}
void setSaveAction(Runnable saveAction) {
this.saveAction = saveAction;
datamap.forEach((path, data) -> data.setSaveAction(saveAction));
} }
/** /**

View file

@ -1,7 +1,6 @@
package buttondevteam.lib.architecture; package buttondevteam.lib.architecture;
import lombok.val; import lombok.val;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
@ -12,12 +11,12 @@ import java.util.function.UnaryOperator;
public class ListConfigData<T> extends ConfigData<ListConfigData.List<T>> { public class ListConfigData<T> extends ConfigData<ListConfigData.List<T>> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ListConfigData(ConfigurationSection config, String path, List<T> def, Runnable saveAction) { ListConfigData(IHaveConfig config, String path, List<T> def) {
super(config, path, def, new ArrayList<>(def), list -> { super(config, path, def, new ArrayList<>(def), list -> {
var l = new List<>((ArrayList<T>) list); var l = new List<>((ArrayList<T>) list);
l.listConfig = def.listConfig; l.listConfig = def.listConfig;
return l; return l;
}, ArrayList::new, saveAction); }, ArrayList::new);
def.listConfig = this; //Can't make the List class non-static or pass this in the super() constructor def.listConfig = this; //Can't make the List class non-static or pass this in the super() constructor
} }

View file

@ -1,15 +1,13 @@
package buttondevteam.lib.architecture; package buttondevteam.lib.architecture;
import org.bukkit.configuration.ConfigurationSection;
import java.util.function.Function; import java.util.function.Function;
public class ReadOnlyConfigData<T> extends ConfigData<T> { public class ReadOnlyConfigData<T> extends ConfigData<T> {
ReadOnlyConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter, Runnable saveAction) { ReadOnlyConfigData(IHaveConfig config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter) {
super(config, path, def, primitiveDef, getter, setter, saveAction); super(config, path, def, primitiveDef, getter, setter);
} }
ReadOnlyConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Runnable saveAction) { ReadOnlyConfigData(IHaveConfig config, String path, T def, Object primitiveDef) {
super(config, path, def, primitiveDef, null, null, saveAction); super(config, path, def, primitiveDef, null, null);
} }
} }