Merge and fix
This commit is contained in:
parent
b91c85529d
commit
ce9f4add97
548 changed files with 17517 additions and 16862 deletions
|
@ -1,115 +1,115 @@
|
|||
package ru.betterend.config;
|
||||
|
||||
import ru.betterend.BetterEnd;
|
||||
import ru.betterend.config.ConfigKeeper.BooleanEntry;
|
||||
import ru.betterend.config.ConfigKeeper.Entry;
|
||||
import ru.betterend.config.ConfigKeeper.FloatEntry;
|
||||
import ru.betterend.config.ConfigKeeper.IntegerEntry;
|
||||
import ru.betterend.config.ConfigKeeper.RangeEntry;
|
||||
import ru.betterend.config.ConfigKeeper.StringEntry;
|
||||
|
||||
public abstract class Config {
|
||||
|
||||
protected final ConfigKeeper configKeeper = new ConfigKeeper();
|
||||
|
||||
public abstract void saveChanges();
|
||||
|
||||
public <E extends Entry<?>> E getEntry(String key) {
|
||||
return this.configKeeper.getEntry(key);
|
||||
}
|
||||
|
||||
public <T> T getDefault(String key) {
|
||||
Entry<T> entry = configKeeper.getEntry(key);
|
||||
return entry != null ? entry.getDefault() : null;
|
||||
}
|
||||
|
||||
public String getString(String key) {
|
||||
String str = configKeeper.getValue(key);
|
||||
return str != null ? str : "";
|
||||
}
|
||||
|
||||
public boolean setString(String key, String value) {
|
||||
try {
|
||||
StringEntry entry = configKeeper.getEntry(key);
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getInt(String key) {
|
||||
Integer val = configKeeper.getValue(key);
|
||||
return val != null ? val : 0;
|
||||
}
|
||||
|
||||
public boolean setInt(String key, int value) {
|
||||
try {
|
||||
IntegerEntry entry = configKeeper.getEntry(key);
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public <T extends Comparable<T>> boolean setRanged(String key, T value) {
|
||||
try {
|
||||
RangeEntry<T> entry = configKeeper.getEntry(key);
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException | ClassCastException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public float getFloat(String key) {
|
||||
Float val = configKeeper.getValue(key);
|
||||
return val != null ? val : 0.0F;
|
||||
}
|
||||
|
||||
public boolean setFloat(String key, float value) {
|
||||
try {
|
||||
FloatEntry entry = configKeeper.getEntry(key);
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key) {
|
||||
Boolean val = configKeeper.getValue(key);
|
||||
return val != null ? val : false;
|
||||
}
|
||||
|
||||
public boolean setBoolean(String key, boolean value) {
|
||||
try {
|
||||
BooleanEntry entry = configKeeper.getEntry(key);
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
package ru.betterend.config;
|
||||
|
||||
import ru.betterend.BetterEnd;
|
||||
import ru.betterend.config.ConfigKeeper.BooleanEntry;
|
||||
import ru.betterend.config.ConfigKeeper.Entry;
|
||||
import ru.betterend.config.ConfigKeeper.FloatEntry;
|
||||
import ru.betterend.config.ConfigKeeper.IntegerEntry;
|
||||
import ru.betterend.config.ConfigKeeper.RangeEntry;
|
||||
import ru.betterend.config.ConfigKeeper.StringEntry;
|
||||
|
||||
public abstract class Config {
|
||||
|
||||
protected final ConfigKeeper configKeeper = new ConfigKeeper();
|
||||
|
||||
public abstract void saveChanges();
|
||||
|
||||
public <E extends Entry<?>> E getEntry(String key) {
|
||||
return this.configKeeper.getEntry(key);
|
||||
}
|
||||
|
||||
public <T> T getDefault(String key) {
|
||||
Entry<T> entry = configKeeper.getEntry(key);
|
||||
return entry != null ? entry.getDefault() : null;
|
||||
}
|
||||
|
||||
public String getString(String key) {
|
||||
String str = configKeeper.getValue(key);
|
||||
return str != null ? str : "";
|
||||
}
|
||||
|
||||
public boolean setString(String key, String value) {
|
||||
try {
|
||||
StringEntry entry = configKeeper.getEntry(key);
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getInt(String key) {
|
||||
Integer val = configKeeper.getValue(key);
|
||||
return val != null ? val : 0;
|
||||
}
|
||||
|
||||
public boolean setInt(String key, int value) {
|
||||
try {
|
||||
IntegerEntry entry = configKeeper.getEntry(key);
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public <T extends Comparable<T>> boolean setRanged(String key, T value) {
|
||||
try {
|
||||
RangeEntry<T> entry = configKeeper.getEntry(key);
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException | ClassCastException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public float getFloat(String key) {
|
||||
Float val = configKeeper.getValue(key);
|
||||
return val != null ? val : 0.0F;
|
||||
}
|
||||
|
||||
public boolean setFloat(String key, float value) {
|
||||
try {
|
||||
FloatEntry entry = configKeeper.getEntry(key);
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key) {
|
||||
Boolean val = configKeeper.getValue(key);
|
||||
return val != null ? val : false;
|
||||
}
|
||||
|
||||
public boolean setBoolean(String key, boolean value) {
|
||||
try {
|
||||
BooleanEntry entry = configKeeper.getEntry(key);
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,342 +1,342 @@
|
|||
package ru.betterend.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.minecraft.util.JsonHelper;
|
||||
import ru.betterend.BetterEnd;
|
||||
|
||||
public final class ConfigKeeper {
|
||||
|
||||
private Map<String, Entry<?>> configEntries = new HashMap<>();
|
||||
|
||||
public JsonElement toJson(JsonObject jsonObject) {
|
||||
for (String param : configEntries.keySet()) {
|
||||
jsonObject.addProperty(param, configEntries.get(param).asString());
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
public void fromJson(JsonObject jsonObject) {
|
||||
for (String param : configEntries.keySet()) {
|
||||
if (jsonObject.has(param)) {
|
||||
Entry<?> entry = configEntries.get(param);
|
||||
entry.fromString(JsonHelper.getString(jsonObject, param));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <E extends Entry<?>> E getEntry(String key) {
|
||||
Entry<?> entry = this.configEntries.get(key);
|
||||
if (entry == null) {
|
||||
BetterEnd.LOGGER.warning(String.format("Entry '%s' doesn't exists.", key));
|
||||
return null;
|
||||
}
|
||||
return (E) entry;
|
||||
}
|
||||
|
||||
public <T> T getValue(String key) {
|
||||
Entry<T> entry = this.getEntry(key);
|
||||
if (entry == null) {
|
||||
BetterEnd.LOGGER.warning("Empty value will be returned.");
|
||||
return null;
|
||||
}
|
||||
return entry.getValue();
|
||||
}
|
||||
|
||||
public void set(String key, Entry<?> entry) {
|
||||
configEntries.put(key, entry);
|
||||
}
|
||||
|
||||
public <T extends Entry<?>> void registerEntry(String key, T entry) {
|
||||
configEntries.put(key, entry);
|
||||
}
|
||||
|
||||
public static class BooleanEntry extends Entry<Boolean> {
|
||||
|
||||
public BooleanEntry(Boolean defaultValue, Consumer<Boolean> consumer, Supplier<Boolean> supplier) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Boolean value) {
|
||||
this.setter.accept(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return this.getValue() ? "true" : "false";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(value.equals("true") ? true : false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FloatEntry extends Entry<Float> {
|
||||
|
||||
public FloatEntry(Float defaultValue, Consumer<Float> consumer, Supplier<Float> supplier) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Float value) {
|
||||
this.setter.accept(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Float.toString(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Float.valueOf(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FloatRange extends RangeEntry<Float> {
|
||||
|
||||
public FloatRange(Float defaultValue, Consumer<Float> consumer, Supplier<Float> supplier, Float minVal, Float maxVal) {
|
||||
super(defaultValue, consumer, supplier, minVal, maxVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Float.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Float.toString(getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class IntegerEntry extends Entry<Integer> {
|
||||
|
||||
public IntegerEntry(Integer defaultValue, Consumer<Integer> consumer, Supplier<Integer> supplier) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Integer value) {
|
||||
this.setter.accept(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Integer.toString(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Integer.valueOf(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class IntegerRange extends RangeEntry<Integer> {
|
||||
|
||||
public IntegerRange(Integer defaultValue, Consumer<Integer> consumer, Supplier<Integer> supplier, Integer minVal, Integer maxVal) {
|
||||
super(defaultValue, consumer, supplier, minVal, maxVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Integer.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Integer.toString(getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class StringEntry extends Entry<String> {
|
||||
|
||||
public StringEntry(String defaultValue, Consumer<String> consumer, Supplier<String> supplier) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(String value) {
|
||||
this.setter.accept(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class EnumEntry<T extends Enum<T>> extends Entry<T> {
|
||||
|
||||
public EnumEntry(T defaultValue, Consumer<T> consumer, Supplier<T> supplier) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(T value) {
|
||||
this.setter.accept(value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean setValue(String name) {
|
||||
try {
|
||||
this.setter.accept((T) Enum.valueOf(this.defaultValue.getClass(), name));
|
||||
return true;
|
||||
} catch(IllegalArgumentException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return getValue().name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class RangeEntry<T extends Comparable<T>> extends Entry<T> {
|
||||
|
||||
private final T min, max;
|
||||
|
||||
public RangeEntry(T defaultValue, Consumer<T> consumer, Supplier<T> supplier, T minVal, T maxVal) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
|
||||
this.min = minVal;
|
||||
this.max = maxVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(T value) {
|
||||
this.setter.accept(value.compareTo(min) < 0 ? min : value.compareTo(max) > 0 ? max : value);
|
||||
}
|
||||
|
||||
public T minValue() {
|
||||
return this.min;
|
||||
}
|
||||
|
||||
public T maxValue() {
|
||||
return this.max;
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class Entry<T> {
|
||||
|
||||
protected final T defaultValue;
|
||||
|
||||
protected final Consumer<T> setter;
|
||||
protected final Supplier<T> getter;
|
||||
|
||||
public Entry (T defaultValue, Consumer<T> consumer, Supplier<T> supplier) {
|
||||
this.defaultValue = defaultValue;
|
||||
this.setter = consumer;
|
||||
this.getter = supplier;
|
||||
}
|
||||
|
||||
public abstract T getValue();
|
||||
public abstract void setValue(T value);
|
||||
public abstract T getDefault();
|
||||
public abstract void fromString(String value);
|
||||
public abstract String asString();
|
||||
|
||||
public void setDefault() {
|
||||
this.setter.accept(defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
package ru.betterend.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.minecraft.util.JsonHelper;
|
||||
import ru.betterend.BetterEnd;
|
||||
|
||||
public final class ConfigKeeper {
|
||||
|
||||
private Map<String, Entry<?>> configEntries = new HashMap<>();
|
||||
|
||||
public JsonElement toJson(JsonObject jsonObject) {
|
||||
for (String param : configEntries.keySet()) {
|
||||
jsonObject.addProperty(param, configEntries.get(param).asString());
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
public void fromJson(JsonObject jsonObject) {
|
||||
for (String param : configEntries.keySet()) {
|
||||
if (jsonObject.has(param)) {
|
||||
Entry<?> entry = configEntries.get(param);
|
||||
entry.fromString(JsonHelper.getString(jsonObject, param));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <E extends Entry<?>> E getEntry(String key) {
|
||||
Entry<?> entry = this.configEntries.get(key);
|
||||
if (entry == null) {
|
||||
BetterEnd.LOGGER.warning(String.format("Entry '%s' doesn't exists.", key));
|
||||
return null;
|
||||
}
|
||||
return (E) entry;
|
||||
}
|
||||
|
||||
public <T> T getValue(String key) {
|
||||
Entry<T> entry = this.getEntry(key);
|
||||
if (entry == null) {
|
||||
BetterEnd.LOGGER.warning("Empty value will be returned.");
|
||||
return null;
|
||||
}
|
||||
return entry.getValue();
|
||||
}
|
||||
|
||||
public void set(String key, Entry<?> entry) {
|
||||
configEntries.put(key, entry);
|
||||
}
|
||||
|
||||
public <T extends Entry<?>> void registerEntry(String key, T entry) {
|
||||
configEntries.put(key, entry);
|
||||
}
|
||||
|
||||
public static class BooleanEntry extends Entry<Boolean> {
|
||||
|
||||
public BooleanEntry(Boolean defaultValue, Consumer<Boolean> consumer, Supplier<Boolean> supplier) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Boolean value) {
|
||||
this.setter.accept(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return this.getValue() ? "true" : "false";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(value.equals("true") ? true : false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FloatEntry extends Entry<Float> {
|
||||
|
||||
public FloatEntry(Float defaultValue, Consumer<Float> consumer, Supplier<Float> supplier) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Float value) {
|
||||
this.setter.accept(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Float.toString(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Float.valueOf(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FloatRange extends RangeEntry<Float> {
|
||||
|
||||
public FloatRange(Float defaultValue, Consumer<Float> consumer, Supplier<Float> supplier, Float minVal, Float maxVal) {
|
||||
super(defaultValue, consumer, supplier, minVal, maxVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Float.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Float.toString(getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class IntegerEntry extends Entry<Integer> {
|
||||
|
||||
public IntegerEntry(Integer defaultValue, Consumer<Integer> consumer, Supplier<Integer> supplier) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Integer value) {
|
||||
this.setter.accept(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Integer.toString(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Integer.valueOf(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class IntegerRange extends RangeEntry<Integer> {
|
||||
|
||||
public IntegerRange(Integer defaultValue, Consumer<Integer> consumer, Supplier<Integer> supplier, Integer minVal, Integer maxVal) {
|
||||
super(defaultValue, consumer, supplier, minVal, maxVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Integer.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Integer.toString(getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class StringEntry extends Entry<String> {
|
||||
|
||||
public StringEntry(String defaultValue, Consumer<String> consumer, Supplier<String> supplier) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(String value) {
|
||||
this.setter.accept(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class EnumEntry<T extends Enum<T>> extends Entry<T> {
|
||||
|
||||
public EnumEntry(T defaultValue, Consumer<T> consumer, Supplier<T> supplier) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getValue() {
|
||||
return this.getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(T value) {
|
||||
this.setter.accept(value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean setValue(String name) {
|
||||
try {
|
||||
this.setter.accept((T) Enum.valueOf(this.defaultValue.getClass(), name));
|
||||
return true;
|
||||
} catch(IllegalArgumentException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return getValue().name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class RangeEntry<T extends Comparable<T>> extends Entry<T> {
|
||||
|
||||
private final T min, max;
|
||||
|
||||
public RangeEntry(T defaultValue, Consumer<T> consumer, Supplier<T> supplier, T minVal, T maxVal) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
|
||||
this.min = minVal;
|
||||
this.max = maxVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(T value) {
|
||||
this.setter.accept(value.compareTo(min) < 0 ? min : value.compareTo(max) > 0 ? max : value);
|
||||
}
|
||||
|
||||
public T minValue() {
|
||||
return this.min;
|
||||
}
|
||||
|
||||
public T maxValue() {
|
||||
return this.max;
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class Entry<T> {
|
||||
|
||||
protected final T defaultValue;
|
||||
|
||||
protected final Consumer<T> setter;
|
||||
protected final Supplier<T> getter;
|
||||
|
||||
public Entry (T defaultValue, Consumer<T> consumer, Supplier<T> supplier) {
|
||||
this.defaultValue = defaultValue;
|
||||
this.setter = consumer;
|
||||
this.getter = supplier;
|
||||
}
|
||||
|
||||
public abstract T getValue();
|
||||
public abstract void setValue(T value);
|
||||
public abstract T getDefault();
|
||||
public abstract void fromString(String value);
|
||||
public abstract String asString();
|
||||
|
||||
public void setDefault() {
|
||||
this.setter.accept(defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,79 +1,79 @@
|
|||
package ru.betterend.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import ru.betterend.BetterEnd;
|
||||
import ru.betterend.util.JsonFactory;
|
||||
|
||||
public class ConfigWriter {
|
||||
|
||||
private final static Path GAME_CONFIG_DIR = FabricLoader.getInstance().getConfigDir();
|
||||
public final static File MOD_CONFIG_DIR = new File(GAME_CONFIG_DIR.toFile(), BetterEnd.MOD_ID);
|
||||
private final static File MAIN_CONFIG_FILE = new File(MOD_CONFIG_DIR, "settings.json");
|
||||
|
||||
private static JsonObject mainConfig;
|
||||
|
||||
private JsonObject configObject;
|
||||
private File configFile;
|
||||
|
||||
public JsonObject getConfig() {
|
||||
return configObject;
|
||||
}
|
||||
|
||||
public JsonObject loadConfig(File configFile) {
|
||||
this.configFile = configFile;
|
||||
if (configObject == null) {
|
||||
configObject = load(configFile);
|
||||
}
|
||||
|
||||
return configObject;
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
if (configFile == null || configObject == null) {
|
||||
return;
|
||||
}
|
||||
save(configFile, configObject);
|
||||
}
|
||||
|
||||
public static JsonObject load() {
|
||||
if (mainConfig == null) {
|
||||
mainConfig = load(MAIN_CONFIG_FILE);
|
||||
}
|
||||
return mainConfig;
|
||||
}
|
||||
|
||||
public static JsonObject load(File configFile) {
|
||||
return JsonFactory.getJsonObject(configFile);
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
save(MAIN_CONFIG_FILE, mainConfig);
|
||||
}
|
||||
|
||||
public static void save(JsonElement config) {
|
||||
save(MAIN_CONFIG_FILE, config);
|
||||
}
|
||||
|
||||
public static void save(File configFile, JsonElement config) {
|
||||
JsonFactory.storeJson(configFile, config);
|
||||
}
|
||||
|
||||
public static String scrubFileName(String input) {
|
||||
input = input.replaceAll("[/\\ ]+", "_");
|
||||
input = input.replaceAll("[,:&\"\\|\\<\\>\\?\\*]", "_");
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
static {
|
||||
if (!MOD_CONFIG_DIR.exists()) {
|
||||
MOD_CONFIG_DIR.mkdirs();
|
||||
}
|
||||
}
|
||||
}
|
||||
package ru.betterend.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import ru.betterend.BetterEnd;
|
||||
import ru.betterend.util.JsonFactory;
|
||||
|
||||
public class ConfigWriter {
|
||||
|
||||
private final static Path GAME_CONFIG_DIR = FabricLoader.getInstance().getConfigDir();
|
||||
public final static File MOD_CONFIG_DIR = new File(GAME_CONFIG_DIR.toFile(), BetterEnd.MOD_ID);
|
||||
private final static File MAIN_CONFIG_FILE = new File(MOD_CONFIG_DIR, "settings.json");
|
||||
|
||||
private static JsonObject mainConfig;
|
||||
|
||||
private JsonObject configObject;
|
||||
private File configFile;
|
||||
|
||||
public JsonObject getConfig() {
|
||||
return configObject;
|
||||
}
|
||||
|
||||
public JsonObject loadConfig(File configFile) {
|
||||
this.configFile = configFile;
|
||||
if (configObject == null) {
|
||||
configObject = load(configFile);
|
||||
}
|
||||
|
||||
return configObject;
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
if (configFile == null || configObject == null) {
|
||||
return;
|
||||
}
|
||||
save(configFile, configObject);
|
||||
}
|
||||
|
||||
public static JsonObject load() {
|
||||
if (mainConfig == null) {
|
||||
mainConfig = load(MAIN_CONFIG_FILE);
|
||||
}
|
||||
return mainConfig;
|
||||
}
|
||||
|
||||
public static JsonObject load(File configFile) {
|
||||
return JsonFactory.getJsonObject(configFile);
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
save(MAIN_CONFIG_FILE, mainConfig);
|
||||
}
|
||||
|
||||
public static void save(JsonElement config) {
|
||||
save(MAIN_CONFIG_FILE, config);
|
||||
}
|
||||
|
||||
public static void save(File configFile, JsonElement config) {
|
||||
JsonFactory.storeJson(configFile, config);
|
||||
}
|
||||
|
||||
public static String scrubFileName(String input) {
|
||||
input = input.replaceAll("[/\\ ]+", "_");
|
||||
input = input.replaceAll("[,:&\"\\|\\<\\>\\?\\*]", "_");
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
static {
|
||||
if (!MOD_CONFIG_DIR.exists()) {
|
||||
MOD_CONFIG_DIR.mkdirs();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,34 +1,34 @@
|
|||
package ru.betterend.config;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class MainConfig extends Config {
|
||||
|
||||
private static MainConfig instance;
|
||||
|
||||
public static MainConfig getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new MainConfig();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
private MainConfig() {
|
||||
//TODO: Need to register config params in the Keeper
|
||||
|
||||
JsonObject config = ConfigWriter.load();
|
||||
if (config.size() > 0) {
|
||||
this.configKeeper.fromJson(config);
|
||||
} else {
|
||||
this.configKeeper.toJson(config);
|
||||
ConfigWriter.save();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveChanges() {
|
||||
this.configKeeper.toJson(ConfigWriter.load());
|
||||
ConfigWriter.save();
|
||||
}
|
||||
}
|
||||
package ru.betterend.config;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class MainConfig extends Config {
|
||||
|
||||
private static MainConfig instance;
|
||||
|
||||
public static MainConfig getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new MainConfig();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
private MainConfig() {
|
||||
//TODO: Need to register config params in the Keeper
|
||||
|
||||
JsonObject config = ConfigWriter.load();
|
||||
if (config.size() > 0) {
|
||||
this.configKeeper.fromJson(config);
|
||||
} else {
|
||||
this.configKeeper.toJson(config);
|
||||
ConfigWriter.save();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveChanges() {
|
||||
this.configKeeper.toJson(ConfigWriter.load());
|
||||
ConfigWriter.save();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue