Config refactor
This commit is contained in:
parent
7f2bad86ca
commit
0f12a477e0
10 changed files with 408 additions and 265 deletions
|
@ -1,9 +1,10 @@
|
|||
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;
|
||||
import ru.betterend.config.MainConfig;
|
||||
import ru.betterend.effects.EndEnchantments;
|
||||
|
@ -28,8 +29,6 @@ import ru.betterend.world.generator.BetterEndBiomeSource;
|
|||
public class BetterEnd implements ModInitializer {
|
||||
public static final String MOD_ID = "betterend";
|
||||
public static final Logger LOGGER = Logger.get();
|
||||
public static final MainConfig CONFIG = MainConfig.getInstance();
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
EndSounds.register();
|
||||
|
@ -51,9 +50,7 @@ public class BetterEnd implements ModInitializer {
|
|||
EndStructures.register();
|
||||
|
||||
FabricLoader.getInstance().getEntrypoints("betterend", BetterEndPlugin.class).forEach(BetterEndPlugin::register);
|
||||
ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
|
||||
CONFIG.saveChanges();
|
||||
});
|
||||
MainConfig.saveConfig();
|
||||
}
|
||||
|
||||
public static Identifier makeID(String path) {
|
||||
|
|
|
@ -1,101 +1,102 @@
|
|||
package ru.betterend.world.biome;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.minecraft.util.Identifier;
|
||||
import ru.betterend.config.Config;
|
||||
import ru.betterend.config.ConfigKeeper.Entry;
|
||||
import ru.betterend.config.ConfigWriter;
|
||||
|
||||
public class BiomeConfig extends Config {
|
||||
|
||||
private final ConfigWriter writer;
|
||||
|
||||
public BiomeConfig() {
|
||||
this.writer = new ConfigWriter("biomes");
|
||||
this.settings = writer.load();
|
||||
this.registerEntries();
|
||||
if (settings.size() > 0) {
|
||||
this.configKeeper.fromJson(settings);
|
||||
} else {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerEntries() {}
|
||||
|
||||
@Override
|
||||
public void saveChanges() {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.saveConfig();
|
||||
}
|
||||
|
||||
public String getCategory(EndBiome biome) {
|
||||
Identifier biomeId = biome.getID();
|
||||
return biomeId.getPath();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <E extends Entry<?>> E getEntry(EndBiome biome, String key) {
|
||||
return this.getEntry(getCategory(biome), key);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T getDefault(EndBiome biome, String key) {
|
||||
return this.getDefault(getCategory(biome), key);
|
||||
}
|
||||
|
||||
public String getString(EndBiome biome, String key, String defaultValue) {
|
||||
return this.getString(getCategory(biome), key, defaultValue);
|
||||
}
|
||||
|
||||
public String getString(EndBiome biome, String key) {
|
||||
return this.getString(getCategory(biome), key);
|
||||
}
|
||||
|
||||
public boolean setString(EndBiome biome, String key, String value) {
|
||||
return this.setString(getCategory(biome), key, value);
|
||||
}
|
||||
|
||||
public int getInt(EndBiome biome, String key, int defaultValue) {
|
||||
return this.getInt(getCategory(biome), key, defaultValue);
|
||||
}
|
||||
|
||||
public int getInt(EndBiome biome, String key) {
|
||||
return this.getInt(getCategory(biome), key);
|
||||
}
|
||||
|
||||
public boolean setInt(EndBiome biome, String key, int value) {
|
||||
return this.setInt(getCategory(biome), key, value);
|
||||
}
|
||||
|
||||
public <T extends Comparable<T>> boolean setRanged(EndBiome biome, String key, T value) {
|
||||
return this.setRanged(getCategory(biome), key, value);
|
||||
}
|
||||
|
||||
public float getFloat(EndBiome biome, String key, float defaultValue) {
|
||||
return this.getFloat(getCategory(biome), key, defaultValue);
|
||||
}
|
||||
|
||||
public float getFloat(EndBiome biome, String key) {
|
||||
return this.getFloat(getCategory(biome), key);
|
||||
}
|
||||
|
||||
public boolean setFloat(EndBiome biome, String key, float value) {
|
||||
return this.setFloat(getCategory(biome), key, value);
|
||||
}
|
||||
|
||||
public boolean getBoolean(EndBiome biome, String key, boolean defaultValue) {
|
||||
return this.getBoolean(getCategory(biome), key, defaultValue);
|
||||
}
|
||||
|
||||
public boolean getBoolean(EndBiome biome, String key) {
|
||||
return this.getBoolean(getCategory(biome), key);
|
||||
}
|
||||
|
||||
public boolean setBoolean(EndBiome biome, String key, boolean value) {
|
||||
return this.setBoolean(getCategory(biome), key, value);
|
||||
}
|
||||
}
|
||||
package ru.betterend.config;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.minecraft.util.Identifier;
|
||||
import ru.betterend.config.ConfigKeeper.Entry;
|
||||
import ru.betterend.world.biome.EndBiome;
|
||||
|
||||
public class BiomeConfig extends Config {
|
||||
|
||||
private final ConfigWriter writer;
|
||||
private final String group = "biomes";
|
||||
|
||||
protected BiomeConfig() {
|
||||
this.writer = new ConfigWriter(group);
|
||||
this.settings = writer.load();
|
||||
this.registerEntries();
|
||||
if (settings.size() > 0) {
|
||||
this.configKeeper.fromJson(settings);
|
||||
} else {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerEntries() {}
|
||||
|
||||
@Override
|
||||
public void saveChanges() {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.saveConfig();
|
||||
}
|
||||
|
||||
private ConfigKey createKey(Identifier biome, String key) {
|
||||
Identifier groupId = new Identifier(group, biome.getNamespace());
|
||||
Identifier categoryId = new Identifier(biome.getPath(), key);
|
||||
return new ConfigKey(groupId, categoryId);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <E extends Entry<?>> E getEntry(EndBiome biome, String key) {
|
||||
return this.getEntry(createKey(biome.getID(), key));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T getDefault(EndBiome biome, String key) {
|
||||
return this.getDefault(createKey(biome.getID(), key));
|
||||
}
|
||||
|
||||
public String getString(EndBiome biome, String key, String defaultValue) {
|
||||
return this.getString(createKey(biome.getID(), key), defaultValue);
|
||||
}
|
||||
|
||||
public String getString(EndBiome biome, String key) {
|
||||
return this.getString(createKey(biome.getID(), key));
|
||||
}
|
||||
|
||||
public boolean setString(EndBiome biome, String key, String value) {
|
||||
return this.setString(createKey(biome.getID(), key), value);
|
||||
}
|
||||
|
||||
public int getInt(EndBiome biome, String key, int defaultValue) {
|
||||
return this.getInt(createKey(biome.getID(), key), defaultValue);
|
||||
}
|
||||
|
||||
public int getInt(EndBiome biome, String key) {
|
||||
return this.getInt(createKey(biome.getID(), key));
|
||||
}
|
||||
|
||||
public boolean setInt(EndBiome biome, String key, int value) {
|
||||
return this.setInt(createKey(biome.getID(), key), value);
|
||||
}
|
||||
|
||||
public <T extends Comparable<T>> boolean setRanged(EndBiome biome, String key, T value) {
|
||||
return this.setRanged(createKey(biome.getID(), key), value);
|
||||
}
|
||||
|
||||
public float getFloat(EndBiome biome, String key, float defaultValue) {
|
||||
return this.getFloat(createKey(biome.getID(), key), defaultValue);
|
||||
}
|
||||
|
||||
public float getFloat(EndBiome biome, String key) {
|
||||
return this.getFloat(createKey(biome.getID(), key));
|
||||
}
|
||||
|
||||
public boolean setFloat(EndBiome biome, String key, float value) {
|
||||
return this.setFloat(createKey(biome.getID(), key), value);
|
||||
}
|
||||
|
||||
public boolean getBoolean(EndBiome biome, String key, boolean defaultValue) {
|
||||
return this.getBoolean(createKey(biome.getID(), key), defaultValue);
|
||||
}
|
||||
|
||||
public boolean getBoolean(EndBiome biome, String key) {
|
||||
return this.getBoolean(createKey(biome.getID(), key));
|
||||
}
|
||||
|
||||
public boolean setBoolean(EndBiome biome, String key, boolean value) {
|
||||
return this.setBoolean(createKey(biome.getID(), key), value);
|
||||
}
|
||||
}
|
|
@ -4,7 +4,6 @@ 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;
|
||||
|
@ -22,25 +21,23 @@ public abstract class Config {
|
|||
protected abstract void registerEntries();
|
||||
|
||||
@Nullable
|
||||
public <E extends Entry<?>> E getEntry(String category, String key) {
|
||||
return this.configKeeper.getEntry(category, key);
|
||||
public <E extends Entry<?>> E getEntry(ConfigKey key) {
|
||||
return this.configKeeper.getEntry(key);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T getDefault(String category, String key) {
|
||||
Entry<T> entry = configKeeper.getEntry(category, key);
|
||||
public <T> T getDefault(ConfigKey key) {
|
||||
Entry<T> entry = configKeeper.getEntry(key);
|
||||
return entry != null ? entry.getDefault() : null;
|
||||
}
|
||||
|
||||
public String getString(String category, String key, String defaultValue) {
|
||||
String str = configKeeper.getValue(category, key);
|
||||
public String getString(ConfigKey key, String defaultValue) {
|
||||
String str = configKeeper.getValue(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));
|
||||
StringEntry entry = this.configKeeper.registerEntry(key, new StringEntry(defaultValue));
|
||||
if (settings != null) {
|
||||
if (settings != null) {
|
||||
this.configKeeper.loadFromJson(settings, key, entry);
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
|
@ -48,35 +45,30 @@ public abstract class Config {
|
|||
return str != null ? str : defaultValue;
|
||||
}
|
||||
|
||||
public String getString(String category, String key) {
|
||||
String str = configKeeper.getValue(category, key);
|
||||
public String getString(ConfigKey key) {
|
||||
String str = configKeeper.getValue(key);
|
||||
return str != null ? str : "";
|
||||
}
|
||||
|
||||
public boolean setString(String category, String key, String value) {
|
||||
public boolean setString(ConfigKey key, String value) {
|
||||
try {
|
||||
StringEntry entry = configKeeper.getEntry(category, key);
|
||||
StringEntry entry = configKeeper.getEntry(key);
|
||||
if (entry == null) return false;
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(category, key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getInt(String category, String key, int defaultValue) {
|
||||
Integer val = configKeeper.getValue(category, key);
|
||||
public int getInt(ConfigKey key, int defaultValue) {
|
||||
Integer val = configKeeper.getValue(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));
|
||||
IntegerEntry entry = this.configKeeper.registerEntry(key, new IntegerEntry(defaultValue));
|
||||
if (settings != null) {
|
||||
if (settings != null) {
|
||||
this.configKeeper.loadFromJson(settings, key, entry);
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
|
@ -84,50 +76,42 @@ public abstract class Config {
|
|||
return val != null ? val : defaultValue;
|
||||
}
|
||||
|
||||
public int getInt(String category, String key) {
|
||||
Integer val = configKeeper.getValue(category, key);
|
||||
public int getInt(ConfigKey key) {
|
||||
Integer val = configKeeper.getValue(key);
|
||||
return val != null ? val : 0;
|
||||
}
|
||||
|
||||
public boolean setInt(String category, String key, int value) {
|
||||
public boolean setInt(ConfigKey key, int value) {
|
||||
try {
|
||||
IntegerEntry entry = configKeeper.getEntry(category, key);
|
||||
IntegerEntry entry = configKeeper.getEntry(key);
|
||||
if (entry == null) return false;
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(category, key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public <T extends Comparable<T>> boolean setRanged(String category, String key, T value) {
|
||||
public <T extends Comparable<T>> boolean setRanged(ConfigKey key, T value) {
|
||||
try {
|
||||
RangeEntry<T> entry = configKeeper.getEntry(category, key);
|
||||
RangeEntry<T> entry = configKeeper.getEntry(key);
|
||||
if (entry == null) return false;
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(category, key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException | ClassCastException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public float getFloat(String category, String key, float defaultValue) {
|
||||
Float val = configKeeper.getValue(category, key);
|
||||
public float getFloat(ConfigKey key, float defaultValue) {
|
||||
Float val = configKeeper.getValue(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));
|
||||
FloatEntry entry = this.configKeeper.registerEntry(key, new FloatEntry(defaultValue));
|
||||
if (settings != null) {
|
||||
if (settings != null) {
|
||||
this.configKeeper.loadFromJson(settings, key, entry);
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
|
@ -135,59 +119,49 @@ public abstract class Config {
|
|||
return val != null ? val : defaultValue;
|
||||
}
|
||||
|
||||
public float getFloat(String category, String key) {
|
||||
Float val = configKeeper.getValue(category, key);
|
||||
public float getFloat(ConfigKey key) {
|
||||
Float val = configKeeper.getValue(key);
|
||||
return val != null ? val : 0.0F;
|
||||
}
|
||||
|
||||
public boolean setFloat(String category, String key, float value) {
|
||||
public boolean setFloat(ConfigKey key, float value) {
|
||||
try {
|
||||
FloatEntry entry = configKeeper.getEntry(category, key);
|
||||
FloatEntry entry = configKeeper.getEntry(key);
|
||||
if (entry == null) return false;
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(category, key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean getBoolean(String category, String key, boolean defaultValue) {
|
||||
Boolean val = configKeeper.getValue(category, key);
|
||||
public boolean getBoolean(ConfigKey key, boolean defaultValue) {
|
||||
Boolean val = configKeeper.getValue(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();
|
||||
}
|
||||
BooleanEntry entry = this.configKeeper.registerEntry(key, new BooleanEntry(defaultValue));
|
||||
if (settings != null) {
|
||||
this.configKeeper.loadFromJson(settings, key, entry);
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
return val != null ? val : defaultValue;
|
||||
}
|
||||
|
||||
public boolean getBoolean(String category, String key) {
|
||||
Boolean val = configKeeper.getValue(category, key);
|
||||
public boolean getBoolean(ConfigKey key) {
|
||||
Boolean val = configKeeper.getValue(key);
|
||||
return val != null ? val : false;
|
||||
}
|
||||
|
||||
public boolean setBoolean(String category, String key, boolean value) {
|
||||
public boolean setBoolean(ConfigKey key, boolean value) {
|
||||
try {
|
||||
BooleanEntry entry = configKeeper.getEntry(category, key);
|
||||
BooleanEntry entry = configKeeper.getEntry(key);
|
||||
if (entry == null) return false;
|
||||
entry.setValue(value);
|
||||
this.configKeeper.set(category, key, entry);
|
||||
|
||||
return true;
|
||||
} catch (NullPointerException ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,74 +8,83 @@ import com.google.common.collect.Maps;
|
|||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.JsonHelper;
|
||||
|
||||
import ru.betterend.BetterEnd;
|
||||
|
||||
public final class ConfigKeeper {
|
||||
|
||||
private Map<String, Map<String, Entry<?>>> configEntries = Maps.newHashMap();
|
||||
private Map<ConfigKey, Entry<?>> configEntries = Maps.newHashMap();
|
||||
|
||||
public JsonElement toJson(JsonObject jsonObject) {
|
||||
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);
|
||||
}
|
||||
this.configEntries.forEach((key, entry) -> {
|
||||
Identifier categoryId = key.getCategory();
|
||||
Identifier paramId = key.getParameter();
|
||||
String group = categoryId.getPath();
|
||||
JsonObject jsonGroup;
|
||||
if (jsonObject.has(group)) {
|
||||
jsonGroup = JsonHelper.getObject(jsonObject, group);
|
||||
} else {
|
||||
jsonGroup = new JsonObject();
|
||||
jsonObject.add(group, jsonGroup);
|
||||
}
|
||||
String category = paramId.getNamespace();
|
||||
JsonObject jsonCategory;
|
||||
if (jsonGroup.has(category)) {
|
||||
jsonCategory = JsonHelper.getObject(jsonGroup, category);
|
||||
} else {
|
||||
jsonCategory = new JsonObject();
|
||||
jsonGroup.add(category, jsonCategory);
|
||||
}
|
||||
String paramKey = paramId.getPath();
|
||||
paramKey += " [default: " + entry.getDefault() + "]";
|
||||
jsonCategory.addProperty(paramKey, entry.asString());
|
||||
});
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
public void fromJson(JsonObject jsonObject) {
|
||||
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));
|
||||
});
|
||||
}
|
||||
this.configEntries.forEach((key, entry) -> {
|
||||
this.loadFromJson(jsonObject, key, entry);
|
||||
});
|
||||
}
|
||||
|
||||
public <E extends Entry<?>> void loadFromJson(JsonObject jsonObject, ConfigKey key, E entry) {
|
||||
Identifier categoryId = key.getCategory();
|
||||
Identifier paramId = key.getParameter();
|
||||
String group = categoryId.getPath();
|
||||
if (!jsonObject.has(group)) return;
|
||||
|
||||
JsonObject jsonGroup = JsonHelper.getObject(jsonObject, group);
|
||||
String category = paramId.getNamespace();
|
||||
if (jsonGroup.has(category)) return;
|
||||
|
||||
JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category);
|
||||
String paramKey = paramId.getPath();
|
||||
paramKey += " [default: " + entry.getDefault() + "]";
|
||||
entry.fromString(JsonHelper.getString(jsonCategory, paramKey));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@SuppressWarnings("unchecked")
|
||||
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) entryCategory.get(key);
|
||||
public <E extends Entry<?>> E getEntry(ConfigKey key) {
|
||||
return (E) this.configEntries.get(key);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T getValue(String category, String key) {
|
||||
Entry<T> entry = this.getEntry(category, key);
|
||||
public <T> T getValue(ConfigKey key) {
|
||||
Entry<T> entry = this.getEntry(key);
|
||||
if (entry == null) {
|
||||
return null;
|
||||
}
|
||||
return entry.getValue();
|
||||
}
|
||||
|
||||
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<?>> 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);
|
||||
|
||||
public <T extends Entry<?>> T registerEntry(ConfigKey key, T entry) {
|
||||
this.configEntries.put(key, entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
|
|
56
src/main/java/ru/betterend/config/ConfigKey.java
Normal file
56
src/main/java/ru/betterend/config/ConfigKey.java
Normal file
|
@ -0,0 +1,56 @@
|
|||
package ru.betterend.config;
|
||||
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
public class ConfigKey {
|
||||
private final Identifier category;
|
||||
private final Identifier parameter;
|
||||
|
||||
public ConfigKey(Identifier category, Identifier parameter) {
|
||||
this.category = category;
|
||||
this.parameter = parameter;
|
||||
}
|
||||
|
||||
public Identifier getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public Identifier getParameter() {
|
||||
return parameter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((category == null) ? 0 : category.hashCode());
|
||||
result = prime * result + ((parameter == null) ? 0 : parameter.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof ConfigKey)) {
|
||||
return false;
|
||||
}
|
||||
ConfigKey other = (ConfigKey) obj;
|
||||
if (category == null) {
|
||||
if (other.category != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!category.equals(other.category)) {
|
||||
return false;
|
||||
}
|
||||
if (parameter == null) {
|
||||
if (other.parameter != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!parameter.equals(other.parameter)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
100
src/main/java/ru/betterend/config/ItemConfig.java
Normal file
100
src/main/java/ru/betterend/config/ItemConfig.java
Normal file
|
@ -0,0 +1,100 @@
|
|||
package ru.betterend.config;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import net.minecraft.util.Identifier;
|
||||
import ru.betterend.config.ConfigKeeper.Entry;
|
||||
|
||||
public class ItemConfig extends Config {
|
||||
|
||||
private final ConfigWriter writer;
|
||||
private final String group = "settings";
|
||||
|
||||
protected ItemConfig() {
|
||||
this.writer = new ConfigWriter(group);
|
||||
this.settings = this.writer.load();
|
||||
this.registerEntries();
|
||||
if (settings.size() > 0) {
|
||||
this.configKeeper.fromJson(settings);
|
||||
} else {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
}
|
||||
|
||||
private ConfigKey createKey(Identifier item, String category) {
|
||||
Identifier groupId = new Identifier(group, item.getNamespace());
|
||||
Identifier categoryId = new Identifier(category, item.getPath());
|
||||
return new ConfigKey(groupId, categoryId);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerEntries() {}
|
||||
|
||||
@Override
|
||||
public void saveChanges() {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <E extends Entry<?>> E getEntry(Identifier item, String category) {
|
||||
return this.getEntry(createKey(item, category));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T getDefault(Identifier item, String category) {
|
||||
return this.getDefault(createKey(item, category));
|
||||
}
|
||||
|
||||
public String getString(Identifier item, String category, String defaultValue) {
|
||||
return this.getString(createKey(item, category), defaultValue);
|
||||
}
|
||||
|
||||
public String getString(Identifier item, String category) {
|
||||
return this.getString(createKey(item, category));
|
||||
}
|
||||
|
||||
public boolean setString(Identifier item, String category, String value) {
|
||||
return this.setString(createKey(item, category), value);
|
||||
}
|
||||
|
||||
public int getInt(Identifier item, String category, int defaultValue) {
|
||||
return this.getInt(createKey(item, category), defaultValue);
|
||||
}
|
||||
|
||||
public int getInt(Identifier item, String category) {
|
||||
return this.getInt(createKey(item, category));
|
||||
}
|
||||
|
||||
public boolean setInt(Identifier item, String category, int value) {
|
||||
return this.setInt(createKey(item, category), value);
|
||||
}
|
||||
|
||||
public <T extends Comparable<T>> boolean setRanged(Identifier item, String category, T value) {
|
||||
return this.setRanged(createKey(item, category), value);
|
||||
}
|
||||
|
||||
public float getFloat(Identifier item, String category, float defaultValue) {
|
||||
return this.getFloat(createKey(item, category), defaultValue);
|
||||
}
|
||||
|
||||
public float getFloat(Identifier item, String category) {
|
||||
return this.getFloat(createKey(item, category));
|
||||
}
|
||||
|
||||
public boolean setFloat(Identifier item, String category, float value) {
|
||||
return this.setFloat(createKey(item, category), value);
|
||||
}
|
||||
|
||||
public boolean getBoolean(Identifier item, String category, boolean defaultValue) {
|
||||
return this.getBoolean(createKey(item, category), defaultValue);
|
||||
}
|
||||
|
||||
public boolean getBoolean(Identifier item, String category) {
|
||||
return this.getBoolean(createKey(item, category));
|
||||
}
|
||||
|
||||
public boolean setBoolean(Identifier item, String category, boolean value) {
|
||||
return this.setBoolean(createKey(item, category), value);
|
||||
}
|
||||
}
|
|
@ -1,37 +1,28 @@
|
|||
package ru.betterend.config;
|
||||
|
||||
public class MainConfig extends Config {
|
||||
|
||||
private static MainConfig instance;
|
||||
|
||||
public static MainConfig getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new MainConfig();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
private final ConfigWriter writer;
|
||||
|
||||
private MainConfig() {
|
||||
this.writer = new ConfigWriter("settings");
|
||||
this.settings = this.writer.load();
|
||||
this.registerEntries();
|
||||
if (settings.size() > 0) {
|
||||
this.configKeeper.fromJson(settings);
|
||||
} else {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerEntries() {}
|
||||
|
||||
@Override
|
||||
public void saveChanges() {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
}
|
||||
package ru.betterend.config;
|
||||
|
||||
public class MainConfig {
|
||||
public static final ItemConfig ITEM_CONFIG = getItemConfig();
|
||||
public static final BiomeConfig BIOME_CONFIG = getBiomeConfig();
|
||||
|
||||
private static ItemConfig itemConfig;
|
||||
private static BiomeConfig biomeConfig;
|
||||
|
||||
public static ItemConfig getItemConfig() {
|
||||
if (itemConfig == null) {
|
||||
itemConfig = new ItemConfig();
|
||||
}
|
||||
return itemConfig;
|
||||
}
|
||||
|
||||
public static BiomeConfig getBiomeConfig() {
|
||||
if (biomeConfig == null) {
|
||||
biomeConfig = new BiomeConfig();
|
||||
}
|
||||
return biomeConfig;
|
||||
}
|
||||
|
||||
public static void saveConfig() {
|
||||
itemConfig.saveChanges();
|
||||
biomeConfig.saveChanges();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package ru.betterend.item.model;
|
||||
|
||||
import net.minecraft.client.render.entity.model.BipedEntityModel;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
|
||||
public class CrystaliteArmorModel extends BipedEntityModel<LivingEntity> {
|
||||
|
||||
public CrystaliteArmorModel(float scale) {
|
||||
super(scale);
|
||||
}
|
||||
|
||||
}
|
|
@ -65,11 +65,12 @@ import ru.betterend.blocks.basis.BlockWallPlant;
|
|||
import ru.betterend.blocks.complex.ColoredMaterial;
|
||||
import ru.betterend.blocks.complex.StoneMaterial;
|
||||
import ru.betterend.blocks.complex.WoodenMaterial;
|
||||
import ru.betterend.config.ItemConfig;
|
||||
import ru.betterend.config.MainConfig;
|
||||
import ru.betterend.tab.CreativeTab;
|
||||
|
||||
public class EndBlocks {
|
||||
private static final MainConfig CONFIG = MainConfig.getInstance();
|
||||
private static final ItemConfig CONFIG = MainConfig.ITEM_CONFIG;
|
||||
|
||||
// Terrain //
|
||||
public static final Block ENDSTONE_DUST = registerBlock("endstone_dust", new BlockEndstoneDust());
|
||||
|
@ -207,7 +208,7 @@ public class EndBlocks {
|
|||
public static void register() {}
|
||||
|
||||
public static Block registerBlock(Identifier id, Block block) {
|
||||
if (!CONFIG.getBoolean("blocks", id.getPath(), true)) {
|
||||
if (!CONFIG.getBoolean(id, "blocks", true)) {
|
||||
return block;
|
||||
}
|
||||
Registry.register(Registry.BLOCK, id, 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.ItemConfig;
|
||||
import ru.betterend.config.MainConfig;
|
||||
import ru.betterend.item.EndArmorMaterial;
|
||||
import ru.betterend.item.EndAxe;
|
||||
|
@ -44,7 +45,7 @@ import ru.betterend.util.TagHelper;
|
|||
|
||||
public class EndItems {
|
||||
|
||||
private static final MainConfig CONFIG = MainConfig.getInstance();
|
||||
private static final ItemConfig CONFIG = MainConfig.ITEM_CONFIG;
|
||||
private static final List<Item> MOD_BLOCKS = Lists.newArrayList();
|
||||
private static final List<Item> MOD_ITEMS = Lists.newArrayList();
|
||||
|
||||
|
@ -106,7 +107,7 @@ public class EndItems {
|
|||
}
|
||||
|
||||
public static Item registerItem(Identifier id, Item item) {
|
||||
if (!(item instanceof BlockItem) && !CONFIG.getBoolean("items", id.getPath(), true)) {
|
||||
if (!(item instanceof BlockItem) && !CONFIG.getBoolean(id, "items", true)) {
|
||||
return item;
|
||||
}
|
||||
if (item != Items.AIR) {
|
||||
|
@ -120,10 +121,11 @@ public class EndItems {
|
|||
}
|
||||
|
||||
protected static ToolItem registerTool(String name, ToolItem item) {
|
||||
if (!CONFIG.getBoolean("items", name, true)) {
|
||||
Identifier id = BetterEnd.makeID(name);
|
||||
if (!CONFIG.getBoolean(id, "items", true)) {
|
||||
return item;
|
||||
}
|
||||
Registry.register(Registry.ITEM, BetterEnd.makeID(name), item);
|
||||
Registry.register(Registry.ITEM, id, item);
|
||||
MOD_ITEMS.add(item);
|
||||
|
||||
if (item instanceof ShovelItem) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue