Only save config if changed, number conversion fixes
And do save config if changed #83
This commit is contained in:
parent
fb9c108c52
commit
89c27a10c6
2 changed files with 31 additions and 29 deletions
|
@ -18,6 +18,7 @@
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
|
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
|
||||||
|
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
|
||||||
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.10" level="project" />
|
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.10" level="project" />
|
||||||
<orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" />
|
<orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" />
|
||||||
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" />
|
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" />
|
||||||
|
|
|
@ -15,7 +15,6 @@ import org.bukkit.scheduler.BukkitTask;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
@ -49,10 +48,6 @@ public class ConfigData<T> {
|
||||||
* The config value should not change outside this instance
|
* The config value should not change outside this instance
|
||||||
*/
|
*/
|
||||||
private T value;
|
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
|
//This constructor is needed because it sets the getter and setter
|
||||||
ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter, Runnable saveAction) {
|
ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter, Runnable saveAction) {
|
||||||
|
@ -85,22 +80,25 @@ public class ConfigData<T> {
|
||||||
@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
|
||||||
Object val = config == null ? null : config.get(path); //config==null: testing
|
Object val;
|
||||||
if (val == null) {
|
if (config == null || !config.isSet(path)) { //Call set() if config == null
|
||||||
val = primitiveDef;
|
val = primitiveDef;
|
||||||
}
|
|
||||||
if (!saved && Objects.equals(val, primitiveDef)) { //String needs .equals()
|
|
||||||
if (def == null && config != null) //In Discord's case def may be null
|
if (def == null && config != null) //In Discord's case def may be null
|
||||||
config.set(path, primitiveDef);
|
setInternal(primitiveDef);
|
||||||
else
|
else
|
||||||
set(def); //Save default value - def is always set
|
set(def); //Save default value - def is always set
|
||||||
saved = true;
|
} else
|
||||||
}
|
val = config.get(path); //config==null: testing
|
||||||
|
if (val == null) //If it's set to null explicitly
|
||||||
|
val = primitiveDef;
|
||||||
BiFunction<Object, Object, Object> convert = (_val, _def) -> {
|
BiFunction<Object, Object, Object> convert = (_val, _def) -> {
|
||||||
if (_val instanceof Number && _def != null)
|
if (_def instanceof Number) //If we expect a number
|
||||||
|
if (_val instanceof Number)
|
||||||
_val = ChromaUtils.convertNumber((Number) _val,
|
_val = ChromaUtils.convertNumber((Number) _val,
|
||||||
(Class<? extends Number>) _def.getClass());
|
(Class<? extends Number>) _def.getClass());
|
||||||
if (_val instanceof List && _def != null && _def.getClass().isArray())
|
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<T>) _val).toArray((T[]) Array.newInstance(_def.getClass().getComponentType(), 0));
|
_val = ((List<T>) _val).toArray((T[]) Array.newInstance(_def.getClass().getComponentType(), 0));
|
||||||
return _val;
|
return _val;
|
||||||
};
|
};
|
||||||
|
@ -121,7 +119,12 @@ public class ConfigData<T> {
|
||||||
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)
|
||||||
|
setInternal(val);
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setInternal(Object val) {
|
||||||
config.set(path, val);
|
config.set(path, val);
|
||||||
if (!saveTasks.containsKey(config.getRoot())) {
|
if (!saveTasks.containsKey(config.getRoot())) {
|
||||||
synchronized (saveTasks) {
|
synchronized (saveTasks) {
|
||||||
|
@ -134,8 +137,6 @@ public class ConfigData<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
private static class SaveTask {
|
private static class SaveTask {
|
||||||
|
|
Loading…
Reference in a new issue