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;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.util.Identifier;
|
||||
import ru.betterend.api.BetterEndPlugin;
|
||||
|
@ -50,6 +51,9 @@ public class BetterEnd implements ModInitializer {
|
|||
EndStructures.register();
|
||||
|
||||
FabricLoader.getInstance().getEntrypoints("betterend", BetterEndPlugin.class).forEach(BetterEndPlugin::register);
|
||||
ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
|
||||
CONFIG.saveChanges();
|
||||
});
|
||||
}
|
||||
|
||||
public static Identifier makeID(String path) {
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
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.config.ConfigKeeper.BooleanEntry;
|
||||
import ru.betterend.config.ConfigKeeper.Entry;
|
||||
|
@ -11,28 +16,50 @@ import ru.betterend.config.ConfigKeeper.StringEntry;
|
|||
public abstract class Config {
|
||||
|
||||
protected final ConfigKeeper configKeeper = new ConfigKeeper();
|
||||
protected JsonObject settings;
|
||||
|
||||
public abstract void saveChanges();
|
||||
protected abstract void registerEntries();
|
||||
|
||||
public <E extends Entry<?>> E getEntry(String key) {
|
||||
return this.configKeeper.getEntry(key);
|
||||
@Nullable
|
||||
public <E extends Entry<?>> E getEntry(String category, String key) {
|
||||
return this.configKeeper.getEntry(category, key);
|
||||
}
|
||||
|
||||
public <T> T getDefault(String key) {
|
||||
Entry<T> entry = configKeeper.getEntry(key);
|
||||
@Nullable
|
||||
public <T> T getDefault(String category, String key) {
|
||||
Entry<T> entry = configKeeper.getEntry(category, key);
|
||||
return entry != null ? entry.getDefault() : null;
|
||||
}
|
||||
|
||||
public String getString(String key) {
|
||||
String str = configKeeper.getValue(key);
|
||||
return str != null ? str : "";
|
||||
public String getString(String category, String key, String defaultValue) {
|
||||
String str = configKeeper.getValue(category, key);
|
||||
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 {
|
||||
StringEntry entry = configKeeper.getEntry(key);
|
||||
StringEntry entry = configKeeper.getEntry(category, key);
|
||||
if (entry == null) return false;
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
this.configKeeper.set(category, key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
|
@ -42,16 +69,33 @@ public abstract class Config {
|
|||
return false;
|
||||
}
|
||||
|
||||
public int getInt(String key) {
|
||||
Integer val = configKeeper.getValue(key);
|
||||
public int getInt(String category, String key, int defaultValue) {
|
||||
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;
|
||||
}
|
||||
|
||||
public boolean setInt(String key, int value) {
|
||||
public boolean setInt(String category, String key, int value) {
|
||||
try {
|
||||
IntegerEntry entry = configKeeper.getEntry(key);
|
||||
IntegerEntry entry = configKeeper.getEntry(category, key);
|
||||
if (entry == null) return false;
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
this.configKeeper.set(category, key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
|
@ -61,11 +105,12 @@ public abstract class Config {
|
|||
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 {
|
||||
RangeEntry<T> entry = configKeeper.getEntry(key);
|
||||
RangeEntry<T> entry = configKeeper.getEntry(category, key);
|
||||
if (entry == null) return false;
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
this.configKeeper.set(category, key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException | ClassCastException ex) {
|
||||
|
@ -75,16 +120,33 @@ public abstract class Config {
|
|||
return false;
|
||||
}
|
||||
|
||||
public float getFloat(String key) {
|
||||
Float val = configKeeper.getValue(key);
|
||||
public float getFloat(String category, String key, float defaultValue) {
|
||||
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;
|
||||
}
|
||||
|
||||
public boolean setFloat(String key, float value) {
|
||||
public boolean setFloat(String category, String key, float value) {
|
||||
try {
|
||||
FloatEntry entry = configKeeper.getEntry(key);
|
||||
FloatEntry entry = configKeeper.getEntry(category, key);
|
||||
if (entry == null) return false;
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
this.configKeeper.set(category, key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
|
@ -94,16 +156,33 @@ public abstract class Config {
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key) {
|
||||
Boolean val = configKeeper.getValue(key);
|
||||
public boolean getBoolean(String category, String key, boolean defaultValue) {
|
||||
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;
|
||||
}
|
||||
|
||||
public boolean setBoolean(String key, boolean value) {
|
||||
public boolean setBoolean(String category, String key, boolean value) {
|
||||
try {
|
||||
BooleanEntry entry = configKeeper.getEntry(key);
|
||||
BooleanEntry entry = configKeeper.getEntry(category, key);
|
||||
if (entry == null) return false;
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(key, entry);
|
||||
this.configKeeper.set(category, key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package ru.betterend.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
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.JsonObject;
|
||||
|
||||
|
@ -13,71 +13,76 @@ import ru.betterend.BetterEnd;
|
|||
|
||||
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) {
|
||||
for (String param : configEntries.keySet()) {
|
||||
jsonObject.addProperty(param, configEntries.get(param).asString());
|
||||
for (String category : configEntries.keySet()) {
|
||||
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;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
if (jsonObject.size() == 0) return;
|
||||
for (String category : configEntries.keySet()) {
|
||||
Map<String, Entry<?>> entryCategory = this.configEntries.get(category);
|
||||
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")
|
||||
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));
|
||||
public <E extends Entry<?>> E getEntry(String category, String key) {
|
||||
Map<String, Entry<?>> entryCategory = this.configEntries.get(category);
|
||||
if (entryCategory == null) {
|
||||
return null;
|
||||
}
|
||||
return (E) entry;
|
||||
return (E) entryCategory.get(key);
|
||||
}
|
||||
|
||||
public <T> T getValue(String key) {
|
||||
Entry<T> entry = this.getEntry(key);
|
||||
@Nullable
|
||||
public <T> T getValue(String category, String key) {
|
||||
Entry<T> entry = this.getEntry(category, 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 void set(String category, String key, Entry<?> 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) {
|
||||
configEntries.put(key, entry);
|
||||
public <T extends Entry<?>> T registerEntry(String category, String key, T 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 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;
|
||||
public BooleanEntry(Boolean defaultValue) {
|
||||
super(defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -94,23 +99,8 @@ public final class ConfigKeeper {
|
|||
|
||||
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;
|
||||
public FloatEntry(Float defaultValue) {
|
||||
super(defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -127,18 +117,8 @@ public final class ConfigKeeper {
|
|||
|
||||
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;
|
||||
public FloatRange(Float defaultValue, float minVal, float maxVal) {
|
||||
super(defaultValue, minVal, maxVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -155,18 +135,8 @@ public final class ConfigKeeper {
|
|||
|
||||
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);
|
||||
public IntegerEntry(Integer defaultValue) {
|
||||
super(defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -188,56 +158,31 @@ public final class ConfigKeeper {
|
|||
|
||||
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));
|
||||
public IntegerRange(Integer defaultValue, int minVal, int maxVal) {
|
||||
super(defaultValue, minVal, maxVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Integer.toString(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Integer.valueOf(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
public StringEntry(String defaultValue) {
|
||||
super(defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return getValue();
|
||||
return this.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -249,24 +194,14 @@ public final class ConfigKeeper {
|
|||
|
||||
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);
|
||||
public EnumEntry(T defaultValue) {
|
||||
super(defaultValue);
|
||||
}
|
||||
|
||||
@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));
|
||||
this.setValue((T) Enum.valueOf(this.defaultValue.getClass(), name));
|
||||
return true;
|
||||
} catch(IllegalArgumentException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
|
@ -295,16 +230,15 @@ public final class ConfigKeeper {
|
|||
|
||||
private final T min, max;
|
||||
|
||||
public RangeEntry(T defaultValue, Consumer<T> consumer, Supplier<T> supplier, T minVal, T maxVal) {
|
||||
super(defaultValue, consumer, supplier);
|
||||
|
||||
public RangeEntry(T defaultValue, T minVal, T maxVal) {
|
||||
super(defaultValue);
|
||||
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);
|
||||
this.value = (value.compareTo(min) < 0 ? min : value.compareTo(max) > 0 ? max : value);
|
||||
}
|
||||
|
||||
public T minValue() {
|
||||
|
@ -319,24 +253,30 @@ public final class ConfigKeeper {
|
|||
public static abstract class Entry<T> {
|
||||
|
||||
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 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() {
|
||||
this.setter.accept(defaultValue);
|
||||
this.value = defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,52 +14,46 @@ 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 final File configFile;
|
||||
private JsonObject configObject;
|
||||
private File configFile;
|
||||
|
||||
public ConfigWriter(String configFile) {
|
||||
this.configFile = new File(MOD_CONFIG_DIR, configFile + ".json");
|
||||
this.load();
|
||||
}
|
||||
|
||||
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) {
|
||||
if (configObject == null) {
|
||||
return;
|
||||
}
|
||||
save(configFile, configObject);
|
||||
}
|
||||
|
||||
public static JsonObject load() {
|
||||
if (mainConfig == null) {
|
||||
mainConfig = load(MAIN_CONFIG_FILE);
|
||||
public JsonObject load() {
|
||||
if (configObject == null) {
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package ru.betterend.config;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import ru.betterend.config.ConfigKeeper.*;
|
||||
|
||||
public class MainConfig extends Config {
|
||||
|
||||
|
@ -14,21 +15,36 @@ public class MainConfig extends Config {
|
|||
return instance;
|
||||
}
|
||||
|
||||
private final ConfigWriter writer;
|
||||
|
||||
private MainConfig() {
|
||||
//TODO: Need to register config params in the Keeper
|
||||
|
||||
JsonObject config = ConfigWriter.load();
|
||||
if (config.size() > 0) {
|
||||
this.configKeeper.fromJson(config);
|
||||
this.writer = new ConfigWriter("settings");
|
||||
this.settings = this.writer.load();
|
||||
this.registerEntries();
|
||||
if (settings.size() > 0) {
|
||||
this.configKeeper.fromJson(settings);
|
||||
} else {
|
||||
this.configKeeper.toJson(config);
|
||||
ConfigWriter.save();
|
||||
this.configKeeper.toJson(settings);
|
||||
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
|
||||
public void saveChanges() {
|
||||
this.configKeeper.toJson(ConfigWriter.load());
|
||||
ConfigWriter.save();
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,9 +57,12 @@ import ru.betterend.blocks.basis.BlockWallMushroom;
|
|||
import ru.betterend.blocks.basis.BlockWallPlant;
|
||||
import ru.betterend.blocks.complex.StoneMaterial;
|
||||
import ru.betterend.blocks.complex.WoodenMaterial;
|
||||
import ru.betterend.config.MainConfig;
|
||||
import ru.betterend.tab.CreativeTab;
|
||||
|
||||
public class EndBlocks {
|
||||
private static final MainConfig CONFIG = MainConfig.getInstance();
|
||||
|
||||
// Terrain //
|
||||
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));
|
||||
|
@ -176,6 +179,9 @@ public class EndBlocks {
|
|||
public static void register() {}
|
||||
|
||||
public static Block registerBlock(Identifier id, Block block) {
|
||||
if (!CONFIG.getBoolean("blocks", id.getPath(), true)) {
|
||||
return block;
|
||||
}
|
||||
Registry.register(Registry.BLOCK, id, block);
|
||||
EndItems.registerItem(id, new BlockItem(block, new Item.Settings().group(CreativeTab.END_TAB)));
|
||||
return block;
|
||||
|
|
|
@ -31,6 +31,7 @@ import net.minecraft.util.math.BlockPointer;
|
|||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import ru.betterend.BetterEnd;
|
||||
import ru.betterend.config.MainConfig;
|
||||
import ru.betterend.item.EndArmorMaterial;
|
||||
import ru.betterend.item.EndAxe;
|
||||
import ru.betterend.item.EndHammer;
|
||||
|
@ -42,6 +43,8 @@ import ru.betterend.tab.CreativeTab;
|
|||
import ru.betterend.util.TagHelper;
|
||||
|
||||
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_ITEMS = Lists.newArrayList();
|
||||
|
||||
|
@ -100,6 +103,9 @@ public class EndItems {
|
|||
}
|
||||
|
||||
public static Item registerItem(Identifier id, Item item) {
|
||||
if (!(item instanceof BlockItem) && !CONFIG.getBoolean("items", id.getPath(), true)) {
|
||||
return item;
|
||||
}
|
||||
if (item != Items.AIR) {
|
||||
Registry.register(Registry.ITEM, id, item);
|
||||
if (item instanceof BlockItem)
|
||||
|
@ -111,6 +117,9 @@ public class EndItems {
|
|||
}
|
||||
|
||||
protected static ToolItem registerTool(String name, ToolItem item) {
|
||||
if (!CONFIG.getBoolean("items", name, true)) {
|
||||
return item;
|
||||
}
|
||||
Registry.register(Registry.ITEM, BetterEnd.makeID(name), item);
|
||||
MOD_ITEMS.add(item);
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package ru.betterend.world.biome;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import ru.betterend.config.Config;
|
||||
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 EndBiome biome;
|
||||
private ConfigWriter configWriter;
|
||||
private File configFile;
|
||||
public static BiomeConfig getConfig(EndBiome biome) {
|
||||
return new BiomeConfig(biome);
|
||||
}
|
||||
|
||||
private final EndBiome biome;
|
||||
private final ConfigWriter writer;
|
||||
|
||||
public BiomeConfig(EndBiome biome) {
|
||||
//this.biome = biome;
|
||||
this.biome = biome;
|
||||
Identifier biomeId = biome.getID();
|
||||
String folder = ConfigWriter.scrubFileName(biomeId.toString());
|
||||
this.configFile = new File(BIOME_CONFIG_DIR.toFile(), folder + ".json");
|
||||
this.configWriter = new ConfigWriter();
|
||||
String file = ConfigWriter.scrubFileName(biomeId.toString());
|
||||
this.writer = new ConfigWriter("biomes/" + file);
|
||||
this.settings = writer.load();
|
||||
this.registerEntries();
|
||||
JsonObject config = configWriter.loadConfig(configFile);
|
||||
if (config.size() > 0) {
|
||||
this.configKeeper.fromJson(config);
|
||||
if (settings.size() > 0) {
|
||||
this.configKeeper.fromJson(settings);
|
||||
} else {
|
||||
this.configKeeper.toJson(config);
|
||||
this.configWriter.saveConfig();
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
}
|
||||
|
||||
private void registerEntries() {
|
||||
@Override
|
||||
protected void registerEntries() {
|
||||
//TODO: Need to register config params in the Keeper
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveChanges() {
|
||||
this.configKeeper.toJson(configWriter.getConfig());
|
||||
this.configWriter.saveConfig();
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.saveConfig();
|
||||
}
|
||||
|
||||
static {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue