WIP: configuration
This commit is contained in:
parent
92d4ccab6c
commit
9f2b1b50c4
8 changed files with 276 additions and 225 deletions
|
@ -1,6 +1,7 @@
|
||||||
package ru.betterend;
|
package ru.betterend;
|
||||||
|
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||||
import net.fabricmc.loader.api.FabricLoader;
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
import ru.betterend.api.BetterEndPlugin;
|
import ru.betterend.api.BetterEndPlugin;
|
||||||
|
@ -50,6 +51,9 @@ public class BetterEnd implements ModInitializer {
|
||||||
EndStructures.register();
|
EndStructures.register();
|
||||||
|
|
||||||
FabricLoader.getInstance().getEntrypoints("betterend", BetterEndPlugin.class).forEach(BetterEndPlugin::register);
|
FabricLoader.getInstance().getEntrypoints("betterend", BetterEndPlugin.class).forEach(BetterEndPlugin::register);
|
||||||
|
ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
|
||||||
|
CONFIG.saveChanges();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Identifier makeID(String path) {
|
public static Identifier makeID(String path) {
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
package ru.betterend.config;
|
package ru.betterend.config;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import net.minecraft.util.JsonHelper;
|
||||||
import ru.betterend.BetterEnd;
|
import ru.betterend.BetterEnd;
|
||||||
import ru.betterend.config.ConfigKeeper.BooleanEntry;
|
import ru.betterend.config.ConfigKeeper.BooleanEntry;
|
||||||
import ru.betterend.config.ConfigKeeper.Entry;
|
import ru.betterend.config.ConfigKeeper.Entry;
|
||||||
|
@ -11,28 +16,50 @@ import ru.betterend.config.ConfigKeeper.StringEntry;
|
||||||
public abstract class Config {
|
public abstract class Config {
|
||||||
|
|
||||||
protected final ConfigKeeper configKeeper = new ConfigKeeper();
|
protected final ConfigKeeper configKeeper = new ConfigKeeper();
|
||||||
|
protected JsonObject settings;
|
||||||
|
|
||||||
public abstract void saveChanges();
|
public abstract void saveChanges();
|
||||||
|
protected abstract void registerEntries();
|
||||||
|
|
||||||
public <E extends Entry<?>> E getEntry(String key) {
|
@Nullable
|
||||||
return this.configKeeper.getEntry(key);
|
public <E extends Entry<?>> E getEntry(String category, String key) {
|
||||||
|
return this.configKeeper.getEntry(category, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getDefault(String key) {
|
@Nullable
|
||||||
Entry<T> entry = configKeeper.getEntry(key);
|
public <T> T getDefault(String category, String key) {
|
||||||
|
Entry<T> entry = configKeeper.getEntry(category, key);
|
||||||
return entry != null ? entry.getDefault() : null;
|
return entry != null ? entry.getDefault() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getString(String key) {
|
public String getString(String category, String key, String defaultValue) {
|
||||||
String str = configKeeper.getValue(key);
|
String str = configKeeper.getValue(category, key);
|
||||||
return str != null ? str : "";
|
if (str == null) {
|
||||||
|
StringEntry entry = this.configKeeper.registerEntry(category, key, new StringEntry(defaultValue));
|
||||||
|
if (settings != null && settings.has(category)) {
|
||||||
|
JsonObject params = JsonHelper.getObject(settings, category);
|
||||||
|
key += " [default: " + defaultValue + "]";
|
||||||
|
if (params.has(key)) {
|
||||||
|
entry.fromString(JsonHelper.getString(params, key));
|
||||||
|
return entry.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return str != null ? str : defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setString(String key, String value) {
|
@Nullable
|
||||||
|
public String getString(String category, String key) {
|
||||||
|
String str = configKeeper.getValue(category, key);
|
||||||
|
return str != null ? str : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean setString(String category, String key, String value) {
|
||||||
try {
|
try {
|
||||||
StringEntry entry = configKeeper.getEntry(key);
|
StringEntry entry = configKeeper.getEntry(category, key);
|
||||||
|
if (entry == null) return false;
|
||||||
entry.setValue(value);
|
entry.setValue(value);
|
||||||
this.configKeeper.set(key, entry);
|
this.configKeeper.set(category, key, entry);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (NullPointerException ex) {
|
} catch (NullPointerException ex) {
|
||||||
|
@ -42,16 +69,33 @@ public abstract class Config {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getInt(String key) {
|
public int getInt(String category, String key, int defaultValue) {
|
||||||
Integer val = configKeeper.getValue(key);
|
Integer val = configKeeper.getValue(category, key);
|
||||||
|
if (val == null) {
|
||||||
|
IntegerEntry entry = this.configKeeper.registerEntry(category, key, new IntegerEntry(defaultValue));
|
||||||
|
if (settings != null && settings.has(category)) {
|
||||||
|
JsonObject params = JsonHelper.getObject(settings, category);
|
||||||
|
key += " [default: " + defaultValue + "]";
|
||||||
|
if (params.has(key)) {
|
||||||
|
entry.fromString(JsonHelper.getString(params, key));
|
||||||
|
return entry.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return val != null ? val : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getInt(String category, String key) {
|
||||||
|
Integer val = configKeeper.getValue(category, key);
|
||||||
return val != null ? val : 0;
|
return val != null ? val : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setInt(String key, int value) {
|
public boolean setInt(String category, String key, int value) {
|
||||||
try {
|
try {
|
||||||
IntegerEntry entry = configKeeper.getEntry(key);
|
IntegerEntry entry = configKeeper.getEntry(category, key);
|
||||||
|
if (entry == null) return false;
|
||||||
entry.setValue(value);
|
entry.setValue(value);
|
||||||
this.configKeeper.set(key, entry);
|
this.configKeeper.set(category, key, entry);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (NullPointerException ex) {
|
} catch (NullPointerException ex) {
|
||||||
|
@ -61,11 +105,12 @@ public abstract class Config {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Comparable<T>> boolean setRanged(String key, T value) {
|
public <T extends Comparable<T>> boolean setRanged(String category, String key, T value) {
|
||||||
try {
|
try {
|
||||||
RangeEntry<T> entry = configKeeper.getEntry(key);
|
RangeEntry<T> entry = configKeeper.getEntry(category, key);
|
||||||
|
if (entry == null) return false;
|
||||||
entry.setValue(value);
|
entry.setValue(value);
|
||||||
this.configKeeper.set(key, entry);
|
this.configKeeper.set(category, key, entry);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (NullPointerException | ClassCastException ex) {
|
} catch (NullPointerException | ClassCastException ex) {
|
||||||
|
@ -75,16 +120,33 @@ public abstract class Config {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getFloat(String key) {
|
public float getFloat(String category, String key, float defaultValue) {
|
||||||
Float val = configKeeper.getValue(key);
|
Float val = configKeeper.getValue(category, key);
|
||||||
|
if (val == null) {
|
||||||
|
FloatEntry entry = this.configKeeper.registerEntry(category, key, new FloatEntry(defaultValue));
|
||||||
|
if (settings != null && settings.has(category)) {
|
||||||
|
JsonObject params = JsonHelper.getObject(settings, category);
|
||||||
|
key += " [default: " + defaultValue + "]";
|
||||||
|
if (params.has(key)) {
|
||||||
|
entry.fromString(JsonHelper.getString(params, key));
|
||||||
|
return entry.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return val != null ? val : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getFloat(String category, String key) {
|
||||||
|
Float val = configKeeper.getValue(category, key);
|
||||||
return val != null ? val : 0.0F;
|
return val != null ? val : 0.0F;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setFloat(String key, float value) {
|
public boolean setFloat(String category, String key, float value) {
|
||||||
try {
|
try {
|
||||||
FloatEntry entry = configKeeper.getEntry(key);
|
FloatEntry entry = configKeeper.getEntry(category, key);
|
||||||
|
if (entry == null) return false;
|
||||||
entry.setValue(value);
|
entry.setValue(value);
|
||||||
this.configKeeper.set(key, entry);
|
this.configKeeper.set(category, key, entry);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (NullPointerException ex) {
|
} catch (NullPointerException ex) {
|
||||||
|
@ -94,16 +156,33 @@ public abstract class Config {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getBoolean(String key) {
|
public boolean getBoolean(String category, String key, boolean defaultValue) {
|
||||||
Boolean val = configKeeper.getValue(key);
|
Boolean val = configKeeper.getValue(category, key);
|
||||||
|
if (val == null) {
|
||||||
|
BooleanEntry entry = this.configKeeper.registerEntry(category, key, new BooleanEntry(defaultValue));
|
||||||
|
if (settings != null && settings.has(category)) {
|
||||||
|
JsonObject params = JsonHelper.getObject(settings, category);
|
||||||
|
key += " [default: " + defaultValue + "]";
|
||||||
|
if (params.has(key)) {
|
||||||
|
entry.fromString(JsonHelper.getString(params, key));
|
||||||
|
return entry.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return val != null ? val : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getBoolean(String category, String key) {
|
||||||
|
Boolean val = configKeeper.getValue(category, key);
|
||||||
return val != null ? val : false;
|
return val != null ? val : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setBoolean(String key, boolean value) {
|
public boolean setBoolean(String category, String key, boolean value) {
|
||||||
try {
|
try {
|
||||||
BooleanEntry entry = configKeeper.getEntry(key);
|
BooleanEntry entry = configKeeper.getEntry(category, key);
|
||||||
|
if (entry == null) return false;
|
||||||
entry.setValue(value);
|
entry.setValue(value);
|
||||||
this.configKeeper.set(key, entry);
|
this.configKeeper.set(category, key, entry);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (NullPointerException ex) {
|
} catch (NullPointerException ex) {
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
package ru.betterend.config;
|
package ru.betterend.config;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
@ -13,71 +13,76 @@ import ru.betterend.BetterEnd;
|
||||||
|
|
||||||
public final class ConfigKeeper {
|
public final class ConfigKeeper {
|
||||||
|
|
||||||
private Map<String, Entry<?>> configEntries = new HashMap<>();
|
private Map<String, Map<String, Entry<?>>> configEntries = Maps.newHashMap();
|
||||||
|
|
||||||
public JsonElement toJson(JsonObject jsonObject) {
|
public JsonElement toJson(JsonObject jsonObject) {
|
||||||
for (String param : configEntries.keySet()) {
|
for (String category : configEntries.keySet()) {
|
||||||
jsonObject.addProperty(param, configEntries.get(param).asString());
|
Map<String, Entry<?>> entryCategory = this.configEntries.get(category);
|
||||||
|
JsonObject jsonCategory = new JsonObject();
|
||||||
|
entryCategory.forEach((key, param) -> {
|
||||||
|
key += " [default: " + param.getDefault() + "]";
|
||||||
|
jsonCategory.addProperty(key, param.asString());
|
||||||
|
});
|
||||||
|
jsonObject.add(category, jsonCategory);
|
||||||
}
|
}
|
||||||
|
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fromJson(JsonObject jsonObject) {
|
public void fromJson(JsonObject jsonObject) {
|
||||||
for (String param : configEntries.keySet()) {
|
if (jsonObject.size() == 0) return;
|
||||||
if (jsonObject.has(param)) {
|
for (String category : configEntries.keySet()) {
|
||||||
Entry<?> entry = configEntries.get(param);
|
Map<String, Entry<?>> entryCategory = this.configEntries.get(category);
|
||||||
entry.fromString(JsonHelper.getString(jsonObject, param));
|
if (!jsonObject.has(category)) continue;
|
||||||
}
|
JsonObject jsonCategory = jsonObject.getAsJsonObject(category);
|
||||||
|
entryCategory.forEach((key, param) -> {
|
||||||
|
key += " [default: " + param.getDefault() + "]";
|
||||||
|
if (!jsonCategory.has(key)) return;
|
||||||
|
param.fromString(JsonHelper.getString(jsonCategory, key));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <E extends Entry<?>> E getEntry(String key) {
|
public <E extends Entry<?>> E getEntry(String category, String key) {
|
||||||
Entry<?> entry = this.configEntries.get(key);
|
Map<String, Entry<?>> entryCategory = this.configEntries.get(category);
|
||||||
if (entry == null) {
|
if (entryCategory == null) {
|
||||||
BetterEnd.LOGGER.warning(String.format("Entry '%s' doesn't exists.", key));
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (E) entry;
|
return (E) entryCategory.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getValue(String key) {
|
@Nullable
|
||||||
Entry<T> entry = this.getEntry(key);
|
public <T> T getValue(String category, String key) {
|
||||||
|
Entry<T> entry = this.getEntry(category, key);
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
BetterEnd.LOGGER.warning("Empty value will be returned.");
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return entry.getValue();
|
return entry.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set(String key, Entry<?> entry) {
|
public void set(String category, String key, Entry<?> entry) {
|
||||||
configEntries.put(key, entry);
|
Map<String, Entry<?>> entryCategory = this.configEntries.get(category);
|
||||||
|
if (entryCategory != null) {
|
||||||
|
entryCategory.put(key, entry);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Entry<?>> void registerEntry(String key, T entry) {
|
public <T extends Entry<?>> T registerEntry(String category, String key, T entry) {
|
||||||
configEntries.put(key, entry);
|
Map<String, Entry<?>> entryCategory = this.configEntries.get(category);
|
||||||
|
if (entryCategory == null) {
|
||||||
|
entryCategory = Maps.newHashMap();
|
||||||
|
this.configEntries.put(category, entryCategory);
|
||||||
|
}
|
||||||
|
entryCategory.put(key, entry);
|
||||||
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class BooleanEntry extends Entry<Boolean> {
|
public static class BooleanEntry extends Entry<Boolean> {
|
||||||
|
|
||||||
public BooleanEntry(Boolean defaultValue, Consumer<Boolean> consumer, Supplier<Boolean> supplier) {
|
public BooleanEntry(Boolean defaultValue) {
|
||||||
super(defaultValue, consumer, supplier);
|
super(defaultValue);
|
||||||
}
|
|
||||||
|
|
||||||
@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
|
@Override
|
||||||
|
@ -94,23 +99,8 @@ public final class ConfigKeeper {
|
||||||
|
|
||||||
public static class FloatEntry extends Entry<Float> {
|
public static class FloatEntry extends Entry<Float> {
|
||||||
|
|
||||||
public FloatEntry(Float defaultValue, Consumer<Float> consumer, Supplier<Float> supplier) {
|
public FloatEntry(Float defaultValue) {
|
||||||
super(defaultValue, consumer, supplier);
|
super(defaultValue);
|
||||||
}
|
|
||||||
|
|
||||||
@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
|
@Override
|
||||||
|
@ -127,18 +117,8 @@ public final class ConfigKeeper {
|
||||||
|
|
||||||
public static class FloatRange extends RangeEntry<Float> {
|
public static class FloatRange extends RangeEntry<Float> {
|
||||||
|
|
||||||
public FloatRange(Float defaultValue, Consumer<Float> consumer, Supplier<Float> supplier, Float minVal, Float maxVal) {
|
public FloatRange(Float defaultValue, float minVal, float maxVal) {
|
||||||
super(defaultValue, consumer, supplier, minVal, maxVal);
|
super(defaultValue, minVal, maxVal);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Float getValue() {
|
|
||||||
return this.getter.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Float getDefault() {
|
|
||||||
return this.defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -155,18 +135,8 @@ public final class ConfigKeeper {
|
||||||
|
|
||||||
public static class IntegerEntry extends Entry<Integer> {
|
public static class IntegerEntry extends Entry<Integer> {
|
||||||
|
|
||||||
public IntegerEntry(Integer defaultValue, Consumer<Integer> consumer, Supplier<Integer> supplier) {
|
public IntegerEntry(Integer defaultValue) {
|
||||||
super(defaultValue, consumer, supplier);
|
super(defaultValue);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer getValue() {
|
|
||||||
return this.getter.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setValue(Integer value) {
|
|
||||||
this.setter.accept(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -188,23 +158,8 @@ public final class ConfigKeeper {
|
||||||
|
|
||||||
public static class IntegerRange extends RangeEntry<Integer> {
|
public static class IntegerRange extends RangeEntry<Integer> {
|
||||||
|
|
||||||
public IntegerRange(Integer defaultValue, Consumer<Integer> consumer, Supplier<Integer> supplier, Integer minVal, Integer maxVal) {
|
public IntegerRange(Integer defaultValue, int minVal, int maxVal) {
|
||||||
super(defaultValue, consumer, supplier, minVal, maxVal);
|
super(defaultValue, 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
|
@Override
|
||||||
|
@ -212,32 +167,22 @@ public final class ConfigKeeper {
|
||||||
return Integer.toString(getValue());
|
return Integer.toString(getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fromString(String value) {
|
||||||
|
this.setValue(Integer.valueOf(value));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class StringEntry extends Entry<String> {
|
public static class StringEntry extends Entry<String> {
|
||||||
|
|
||||||
public StringEntry(String defaultValue, Consumer<String> consumer, Supplier<String> supplier) {
|
public StringEntry(String defaultValue) {
|
||||||
super(defaultValue, consumer, supplier);
|
super(defaultValue);
|
||||||
}
|
|
||||||
|
|
||||||
@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
|
@Override
|
||||||
public String asString() {
|
public String asString() {
|
||||||
return getValue();
|
return this.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -249,24 +194,14 @@ public final class ConfigKeeper {
|
||||||
|
|
||||||
public static class EnumEntry<T extends Enum<T>> extends Entry<T> {
|
public static class EnumEntry<T extends Enum<T>> extends Entry<T> {
|
||||||
|
|
||||||
public EnumEntry(T defaultValue, Consumer<T> consumer, Supplier<T> supplier) {
|
public EnumEntry(T defaultValue) {
|
||||||
super(defaultValue, consumer, supplier);
|
super(defaultValue);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T getValue() {
|
|
||||||
return this.getter.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setValue(T value) {
|
|
||||||
this.setter.accept(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public boolean setValue(String name) {
|
public boolean setValue(String name) {
|
||||||
try {
|
try {
|
||||||
this.setter.accept((T) Enum.valueOf(this.defaultValue.getClass(), name));
|
this.setValue((T) Enum.valueOf(this.defaultValue.getClass(), name));
|
||||||
return true;
|
return true;
|
||||||
} catch(IllegalArgumentException ex) {
|
} catch(IllegalArgumentException ex) {
|
||||||
BetterEnd.LOGGER.catching(ex);
|
BetterEnd.LOGGER.catching(ex);
|
||||||
|
@ -295,16 +230,15 @@ public final class ConfigKeeper {
|
||||||
|
|
||||||
private final T min, max;
|
private final T min, max;
|
||||||
|
|
||||||
public RangeEntry(T defaultValue, Consumer<T> consumer, Supplier<T> supplier, T minVal, T maxVal) {
|
public RangeEntry(T defaultValue, T minVal, T maxVal) {
|
||||||
super(defaultValue, consumer, supplier);
|
super(defaultValue);
|
||||||
|
|
||||||
this.min = minVal;
|
this.min = minVal;
|
||||||
this.max = maxVal;
|
this.max = maxVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setValue(T value) {
|
public void setValue(T value) {
|
||||||
this.setter.accept(value.compareTo(min) < 0 ? min : value.compareTo(max) > 0 ? max : value);
|
this.value = (value.compareTo(min) < 0 ? min : value.compareTo(max) > 0 ? max : value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T minValue() {
|
public T minValue() {
|
||||||
|
@ -319,24 +253,30 @@ public final class ConfigKeeper {
|
||||||
public static abstract class Entry<T> {
|
public static abstract class Entry<T> {
|
||||||
|
|
||||||
protected final T defaultValue;
|
protected final T defaultValue;
|
||||||
|
protected T value;
|
||||||
|
|
||||||
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 void fromString(String value);
|
||||||
public abstract String asString();
|
public abstract String asString();
|
||||||
|
|
||||||
|
public Entry (T defaultValue) {
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
this.value = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getValue() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(T value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getDefault() {
|
||||||
|
return this.defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
public void setDefault() {
|
public void setDefault() {
|
||||||
this.setter.accept(defaultValue);
|
this.value = defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,52 +14,46 @@ public class ConfigWriter {
|
||||||
|
|
||||||
private final static Path GAME_CONFIG_DIR = FabricLoader.getInstance().getConfigDir();
|
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);
|
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 final File configFile;
|
||||||
private JsonObject configObject;
|
private JsonObject configObject;
|
||||||
private File configFile;
|
|
||||||
|
public ConfigWriter(String configFile) {
|
||||||
|
this.configFile = new File(MOD_CONFIG_DIR, configFile + ".json");
|
||||||
|
this.load();
|
||||||
|
}
|
||||||
|
|
||||||
public JsonObject getConfig() {
|
public JsonObject getConfig() {
|
||||||
return configObject;
|
return configObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonObject loadConfig(File configFile) {
|
|
||||||
this.configFile = configFile;
|
|
||||||
if (configObject == null) {
|
|
||||||
configObject = load(configFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
return configObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveConfig() {
|
public void saveConfig() {
|
||||||
if (configFile == null || configObject == null) {
|
if (configObject == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
save(configFile, configObject);
|
save(configFile, configObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonObject load() {
|
public JsonObject load() {
|
||||||
if (mainConfig == null) {
|
if (configObject == null) {
|
||||||
mainConfig = load(MAIN_CONFIG_FILE);
|
configObject = load(configFile);
|
||||||
}
|
}
|
||||||
return mainConfig;
|
return configObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save() {
|
||||||
|
save(configFile, configObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(JsonElement config) {
|
||||||
|
this.configObject = config.getAsJsonObject();
|
||||||
|
save(configFile, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonObject load(File configFile) {
|
public static JsonObject load(File configFile) {
|
||||||
return JsonFactory.getJsonObject(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) {
|
public static void save(File configFile, JsonElement config) {
|
||||||
JsonFactory.storeJson(configFile, config);
|
JsonFactory.storeJson(configFile, config);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ru.betterend.config;
|
package ru.betterend.config;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import ru.betterend.config.ConfigKeeper.*;
|
||||||
|
|
||||||
public class MainConfig extends Config {
|
public class MainConfig extends Config {
|
||||||
|
|
||||||
|
@ -14,21 +15,36 @@ public class MainConfig extends Config {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private MainConfig() {
|
private final ConfigWriter writer;
|
||||||
//TODO: Need to register config params in the Keeper
|
|
||||||
|
|
||||||
JsonObject config = ConfigWriter.load();
|
private MainConfig() {
|
||||||
if (config.size() > 0) {
|
this.writer = new ConfigWriter("settings");
|
||||||
this.configKeeper.fromJson(config);
|
this.settings = this.writer.load();
|
||||||
|
this.registerEntries();
|
||||||
|
if (settings.size() > 0) {
|
||||||
|
this.configKeeper.fromJson(settings);
|
||||||
} else {
|
} else {
|
||||||
this.configKeeper.toJson(config);
|
this.configKeeper.toJson(settings);
|
||||||
ConfigWriter.save();
|
this.writer.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void registerEntries() {
|
||||||
|
// this.configKeeper.registerEntry("add_armor_and_equipment", new BooleanEntry(true));
|
||||||
|
// this.configKeeper.registerEntry("add_terminite", new BooleanEntry(true));
|
||||||
|
// this.configKeeper.registerEntry("add_terminite_armor", new BooleanEntry(true));
|
||||||
|
// this.configKeeper.registerEntry("add_terminite_tools", new BooleanEntry(true));
|
||||||
|
// this.configKeeper.registerEntry("add_aeternuim", new BooleanEntry(true));
|
||||||
|
// this.configKeeper.registerEntry("add_aeternuim_armor", new BooleanEntry(true));
|
||||||
|
// this.configKeeper.registerEntry("add_aeternuim_tools", new BooleanEntry(true));
|
||||||
|
// this.configKeeper.registerEntry("add_pedestals", new BooleanEntry(true));
|
||||||
|
// this.configKeeper.registerEntry("add_hammers", new BooleanEntry(true));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveChanges() {
|
public void saveChanges() {
|
||||||
this.configKeeper.toJson(ConfigWriter.load());
|
this.configKeeper.toJson(settings);
|
||||||
ConfigWriter.save();
|
this.writer.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,9 +57,12 @@ import ru.betterend.blocks.basis.BlockWallMushroom;
|
||||||
import ru.betterend.blocks.basis.BlockWallPlant;
|
import ru.betterend.blocks.basis.BlockWallPlant;
|
||||||
import ru.betterend.blocks.complex.StoneMaterial;
|
import ru.betterend.blocks.complex.StoneMaterial;
|
||||||
import ru.betterend.blocks.complex.WoodenMaterial;
|
import ru.betterend.blocks.complex.WoodenMaterial;
|
||||||
|
import ru.betterend.config.MainConfig;
|
||||||
import ru.betterend.tab.CreativeTab;
|
import ru.betterend.tab.CreativeTab;
|
||||||
|
|
||||||
public class EndBlocks {
|
public class EndBlocks {
|
||||||
|
private static final MainConfig CONFIG = MainConfig.getInstance();
|
||||||
|
|
||||||
// Terrain //
|
// Terrain //
|
||||||
public static final Block ENDSTONE_DUST = registerBlock("endstone_dust", new BlockEndstoneDust());
|
public static final Block ENDSTONE_DUST = registerBlock("endstone_dust", new BlockEndstoneDust());
|
||||||
public static final Block END_MYCELIUM = registerBlock("end_mycelium", new BlockTerrain(MaterialColor.LIGHT_BLUE));
|
public static final Block END_MYCELIUM = registerBlock("end_mycelium", new BlockTerrain(MaterialColor.LIGHT_BLUE));
|
||||||
|
@ -176,6 +179,9 @@ public class EndBlocks {
|
||||||
public static void register() {}
|
public static void register() {}
|
||||||
|
|
||||||
public static Block registerBlock(Identifier id, Block block) {
|
public static Block registerBlock(Identifier id, Block block) {
|
||||||
|
if (!CONFIG.getBoolean("blocks", id.getPath(), true)) {
|
||||||
|
return block;
|
||||||
|
}
|
||||||
Registry.register(Registry.BLOCK, id, block);
|
Registry.register(Registry.BLOCK, id, block);
|
||||||
EndItems.registerItem(id, new BlockItem(block, new Item.Settings().group(CreativeTab.END_TAB)));
|
EndItems.registerItem(id, new BlockItem(block, new Item.Settings().group(CreativeTab.END_TAB)));
|
||||||
return block;
|
return block;
|
||||||
|
|
|
@ -31,6 +31,7 @@ import net.minecraft.util.math.BlockPointer;
|
||||||
import net.minecraft.util.math.Direction;
|
import net.minecraft.util.math.Direction;
|
||||||
import net.minecraft.util.registry.Registry;
|
import net.minecraft.util.registry.Registry;
|
||||||
import ru.betterend.BetterEnd;
|
import ru.betterend.BetterEnd;
|
||||||
|
import ru.betterend.config.MainConfig;
|
||||||
import ru.betterend.item.EndArmorMaterial;
|
import ru.betterend.item.EndArmorMaterial;
|
||||||
import ru.betterend.item.EndAxe;
|
import ru.betterend.item.EndAxe;
|
||||||
import ru.betterend.item.EndHammer;
|
import ru.betterend.item.EndHammer;
|
||||||
|
@ -42,6 +43,8 @@ import ru.betterend.tab.CreativeTab;
|
||||||
import ru.betterend.util.TagHelper;
|
import ru.betterend.util.TagHelper;
|
||||||
|
|
||||||
public class EndItems {
|
public class EndItems {
|
||||||
|
|
||||||
|
private static final MainConfig CONFIG = MainConfig.getInstance();
|
||||||
private static final List<Item> MOD_BLOCKS = Lists.newArrayList();
|
private static final List<Item> MOD_BLOCKS = Lists.newArrayList();
|
||||||
private static final List<Item> MOD_ITEMS = Lists.newArrayList();
|
private static final List<Item> MOD_ITEMS = Lists.newArrayList();
|
||||||
|
|
||||||
|
@ -100,6 +103,9 @@ public class EndItems {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Item registerItem(Identifier id, Item item) {
|
public static Item registerItem(Identifier id, Item item) {
|
||||||
|
if (!(item instanceof BlockItem) && !CONFIG.getBoolean("items", id.getPath(), true)) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
if (item != Items.AIR) {
|
if (item != Items.AIR) {
|
||||||
Registry.register(Registry.ITEM, id, item);
|
Registry.register(Registry.ITEM, id, item);
|
||||||
if (item instanceof BlockItem)
|
if (item instanceof BlockItem)
|
||||||
|
@ -111,6 +117,9 @@ public class EndItems {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static ToolItem registerTool(String name, ToolItem item) {
|
protected static ToolItem registerTool(String name, ToolItem item) {
|
||||||
|
if (!CONFIG.getBoolean("items", name, true)) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
Registry.register(Registry.ITEM, BetterEnd.makeID(name), item);
|
Registry.register(Registry.ITEM, BetterEnd.makeID(name), item);
|
||||||
MOD_ITEMS.add(item);
|
MOD_ITEMS.add(item);
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package ru.betterend.world.biome;
|
package ru.betterend.world.biome;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
|
||||||
import ru.betterend.config.Config;
|
import ru.betterend.config.Config;
|
||||||
import ru.betterend.config.ConfigWriter;
|
import ru.betterend.config.ConfigWriter;
|
||||||
|
|
||||||
|
@ -13,34 +13,37 @@ public class BiomeConfig extends Config {
|
||||||
|
|
||||||
private final static Path BIOME_CONFIG_DIR = ConfigWriter.MOD_CONFIG_DIR.toPath().resolve("biomes");
|
private final static Path BIOME_CONFIG_DIR = ConfigWriter.MOD_CONFIG_DIR.toPath().resolve("biomes");
|
||||||
|
|
||||||
//private EndBiome biome;
|
public static BiomeConfig getConfig(EndBiome biome) {
|
||||||
private ConfigWriter configWriter;
|
return new BiomeConfig(biome);
|
||||||
private File configFile;
|
}
|
||||||
|
|
||||||
|
private final EndBiome biome;
|
||||||
|
private final ConfigWriter writer;
|
||||||
|
|
||||||
public BiomeConfig(EndBiome biome) {
|
public BiomeConfig(EndBiome biome) {
|
||||||
//this.biome = biome;
|
this.biome = biome;
|
||||||
Identifier biomeId = biome.getID();
|
Identifier biomeId = biome.getID();
|
||||||
String folder = ConfigWriter.scrubFileName(biomeId.toString());
|
String file = ConfigWriter.scrubFileName(biomeId.toString());
|
||||||
this.configFile = new File(BIOME_CONFIG_DIR.toFile(), folder + ".json");
|
this.writer = new ConfigWriter("biomes/" + file);
|
||||||
this.configWriter = new ConfigWriter();
|
this.settings = writer.load();
|
||||||
this.registerEntries();
|
this.registerEntries();
|
||||||
JsonObject config = configWriter.loadConfig(configFile);
|
if (settings.size() > 0) {
|
||||||
if (config.size() > 0) {
|
this.configKeeper.fromJson(settings);
|
||||||
this.configKeeper.fromJson(config);
|
|
||||||
} else {
|
} else {
|
||||||
this.configKeeper.toJson(config);
|
this.configKeeper.toJson(settings);
|
||||||
this.configWriter.saveConfig();
|
this.writer.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerEntries() {
|
@Override
|
||||||
|
protected void registerEntries() {
|
||||||
//TODO: Need to register config params in the Keeper
|
//TODO: Need to register config params in the Keeper
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveChanges() {
|
public void saveChanges() {
|
||||||
this.configKeeper.toJson(configWriter.getConfig());
|
this.configKeeper.toJson(settings);
|
||||||
this.configWriter.saveConfig();
|
this.writer.saveConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue