Config fixes, actually registering the configs
This commit is contained in:
parent
9923f26698
commit
784ad5e1c7
4 changed files with 44 additions and 47 deletions
|
@ -5,7 +5,6 @@ import buttondevteam.lib.ChromaUtils;
|
|||
import lombok.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
|
@ -24,14 +23,12 @@ public class ConfigData<T> {
|
|||
/**
|
||||
* May be null for testing
|
||||
*/
|
||||
private ConfigurationSection config;
|
||||
private IHaveConfig config;
|
||||
@Getter
|
||||
@Setter(AccessLevel.PACKAGE)
|
||||
private String path;
|
||||
protected final T def;
|
||||
private final Object primitiveDef;
|
||||
@Setter(AccessLevel.PACKAGE)
|
||||
private Runnable saveAction;
|
||||
/**
|
||||
* 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;
|
||||
|
||||
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 (primitiveDef == null)
|
||||
throw new IllegalArgumentException("Either def or primitiveDef must be set.");
|
||||
|
@ -66,7 +63,6 @@ public class ConfigData<T> {
|
|||
this.primitiveDef = primitiveDef;
|
||||
this.getter = getter;
|
||||
this.setter = setter;
|
||||
this.saveAction = saveAction;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -74,14 +70,14 @@ public class ConfigData<T> {
|
|||
return "ConfigData{" + "path='" + path + '\'' + ", value=" + value + '}';
|
||||
}
|
||||
|
||||
void reset(ConfigurationSection config) {
|
||||
void reset() {
|
||||
value = null;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T get() {
|
||||
if (value != null) return value; //Speed things up
|
||||
var config = this.config.getConfig();
|
||||
Object val;
|
||||
if (config == null || !config.isSet(path)) { //Call set() if config == null
|
||||
val = primitiveDef;
|
||||
|
@ -127,19 +123,21 @@ public class ConfigData<T> {
|
|||
}
|
||||
|
||||
private void setInternal(Object val) {
|
||||
config.set(path, val);
|
||||
signalChange(config, saveAction);
|
||||
config.getConfig().set(path, val);
|
||||
signalChange(config);
|
||||
}
|
||||
|
||||
static void signalChange(ConfigurationSection config, Runnable saveAction) {
|
||||
if (!saveTasks.containsKey(config.getRoot())) {
|
||||
static void signalChange(IHaveConfig config) {
|
||||
var cc = config.getConfig();
|
||||
var sa = config.getSaveAction();
|
||||
if (!saveTasks.containsKey(cc.getRoot())) {
|
||||
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) {
|
||||
saveTasks.remove(config.getRoot());
|
||||
saveAction.run();
|
||||
saveTasks.remove(cc.getRoot());
|
||||
sa.run();
|
||||
}
|
||||
}, 100), saveAction));
|
||||
}, 100), sa));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -163,19 +161,18 @@ public class ConfigData<T> {
|
|||
return false;
|
||||
}
|
||||
|
||||
public static <T> ConfigData.ConfigDataBuilder<T> builder(ConfigurationSection config, String path, Runnable saveAction) {
|
||||
return new ConfigDataBuilder<T>(config, path, saveAction);
|
||||
public static <T> ConfigData.ConfigDataBuilder<T> builder(IHaveConfig config, String path) {
|
||||
return new ConfigDataBuilder<T>(config, path);
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
|
||||
public static class ConfigDataBuilder<T> {
|
||||
private final ConfigurationSection config;
|
||||
private final IHaveConfig config;
|
||||
private final String path;
|
||||
private T def;
|
||||
private Object primitiveDef;
|
||||
private Function<Object, T> getter;
|
||||
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.
|
||||
|
@ -233,7 +230,9 @@ public class ConfigData<T> {
|
|||
* @return A ConfigData instance.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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 + ")";}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package buttondevteam.lib.architecture;
|
|||
import buttondevteam.core.MainPlugin;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.val;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
|
@ -23,6 +24,8 @@ public final class IHaveConfig {
|
|||
*/
|
||||
@Getter
|
||||
private ConfigurationSection config;
|
||||
@Getter
|
||||
@Setter
|
||||
private Runnable saveAction;
|
||||
|
||||
/**
|
||||
|
@ -42,7 +45,11 @@ public final class IHaveConfig {
|
|||
* @return A ConfigData builder to set how to obtain the value
|
||||
*/
|
||||
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")
|
||||
public <T> ConfigData<T> getData(String path, T def) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
ConfigData<?> data = datamap.get(path);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
ConfigData<?> data = datamap.get(path);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
ConfigData<?> data = datamap.get(path);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -127,7 +134,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, null, null, saveAction));
|
||||
datamap.put(path, data = new ConfigData<>(this, path, defval, defval, null, null));
|
||||
}
|
||||
return (ConfigData<T>) data;
|
||||
}
|
||||
|
@ -147,7 +154,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, saveAction));
|
||||
datamap.put(path, data = new ConfigData<>(this, path, defval, setter.apply(defval), getter, setter));
|
||||
}
|
||||
return (ConfigData<T>) data;
|
||||
}
|
||||
|
@ -163,7 +170,7 @@ public final class IHaveConfig {
|
|||
public <T> ListConfigData<T> getListData(String path) {
|
||||
ConfigData<?> data = datamap.get(path);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -171,7 +178,7 @@ public final class IHaveConfig {
|
|||
* Schedules a save operation. Use after changing the ConfigurationSection directly.
|
||||
*/
|
||||
public void signalChange() {
|
||||
ConfigData.signalChange(config, saveAction);
|
||||
ConfigData.signalChange(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -179,12 +186,6 @@ public final class IHaveConfig {
|
|||
*/
|
||||
public void reset(ConfigurationSection 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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package buttondevteam.lib.architecture;
|
||||
|
||||
import lombok.val;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -12,12 +11,12 @@ import java.util.function.UnaryOperator;
|
|||
|
||||
public class ListConfigData<T> extends ConfigData<ListConfigData.List<T>> {
|
||||
@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 -> {
|
||||
var l = new List<>((ArrayList<T>) list);
|
||||
l.listConfig = def.listConfig;
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
package buttondevteam.lib.architecture;
|
||||
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
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) {
|
||||
super(config, path, def, primitiveDef, getter, setter, 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);
|
||||
}
|
||||
|
||||
ReadOnlyConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Runnable saveAction) {
|
||||
super(config, path, def, primitiveDef, null, null, saveAction);
|
||||
ReadOnlyConfigData(IHaveConfig config, String path, T def, Object primitiveDef) {
|
||||
super(config, path, def, primitiveDef, null, null);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue