diff --git a/src/main/java/ru/betterend/config/CategoryConfig.java b/src/main/java/ru/betterend/config/CategoryConfig.java index b341b2eb..97833379 100644 --- a/src/main/java/ru/betterend/config/CategoryConfig.java +++ b/src/main/java/ru/betterend/config/CategoryConfig.java @@ -4,7 +4,7 @@ public class CategoryConfig extends IdConfig { public CategoryConfig(String group) { super(group, (id, category) -> { - return new ConfigKey(id.getNamespace(), category, id.getPath()); + return new ConfigKey(id.getPath(), id.getNamespace(), category); }); } } diff --git a/src/main/java/ru/betterend/config/ConfigKeeper.java b/src/main/java/ru/betterend/config/ConfigKeeper.java index f35babc1..6cfba622 100644 --- a/src/main/java/ru/betterend/config/ConfigKeeper.java +++ b/src/main/java/ru/betterend/config/ConfigKeeper.java @@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable; import com.google.common.collect.Maps; import com.google.common.reflect.TypeToken; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import net.minecraft.util.JsonHelper; @@ -38,25 +39,29 @@ public final class ConfigKeeper { if (configObject == null) { return; } - String group = key.getOwner(); - JsonObject jsonGroup; - if (configObject.has(group)) { - jsonGroup = JsonHelper.getObject(configObject, group); - } else { - jsonGroup = new JsonObject(); - configObject.add(group, jsonGroup); - } - String category = key.getCategory(); - JsonObject jsonCategory; - if (jsonGroup.has(category)) { - jsonCategory = JsonHelper.getObject(jsonGroup, category); - } else { - jsonCategory = new JsonObject(); - jsonGroup.add(category, jsonCategory); + String[] path = key.getPath(); + JsonObject obj = null; + + if (!key.isRoot()) { + for (String group: path) { + JsonElement element = configObject.get(group); + if (element == null || !element.isJsonObject()) { + element = new JsonObject(); + if (obj == null) { + obj = element.getAsJsonObject(); + configObject.add(group, obj); + } + else { + obj.add(group, element); + } + } + obj = element.getAsJsonObject(); + } } + String paramKey = key.getEntry(); paramKey += " [default: " + entry.getDefault() + "]"; - this.changed = entry.setLocation(jsonCategory, paramKey); + this.changed = entry.setLocation(obj == null ? configObject : obj, paramKey); } private > void storeValue(E entry, T value) { diff --git a/src/main/java/ru/betterend/config/ConfigKey.java b/src/main/java/ru/betterend/config/ConfigKey.java index 5c7ed7bc..02606ca4 100644 --- a/src/main/java/ru/betterend/config/ConfigKey.java +++ b/src/main/java/ru/betterend/config/ConfigKey.java @@ -1,38 +1,41 @@ package ru.betterend.config; -import org.jetbrains.annotations.NotNull; +import net.minecraft.util.Identifier; public class ConfigKey { - private final String owner; - private final String category; + private final String path[]; private final String entry; + private final boolean root; - public ConfigKey(@NotNull String owner, @NotNull String category, @NotNull String entry) { - this.validate(owner, category, entry); - this.owner = owner; - this.category = category; + public ConfigKey(String entry, String... path) { + validate(entry, path); + this.path = path; this.entry = entry; + this.root = path.length == 0 || (path.length == 1 && path[0].isEmpty()); + } + + public ConfigKey(String entry, Identifier path) { + this(entry, path.getNamespace(), path.getPath()); } - public String getOwner() { - return owner; - } - - public String getCategory() { - return category; + public String[] getPath() { + return path; } public String getEntry() { return entry; } + + public boolean isRoot() { + return root; + } @Override public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + category.hashCode(); + result = prime * result + path.hashCode(); result = prime * result + entry.hashCode(); - result = prime * result + owner.hashCode(); return result; } @@ -45,25 +48,15 @@ public class ConfigKey { return false; } ConfigKey other = (ConfigKey) obj; - if (category == null) { - if (other.category != null) { - return false; - } - } else if (!category.equals(other.category)) { + if (other.path.length != path.length) { return false; } - if (entry == null) { - if (other.entry != null) { + for (int i = 0; i < path.length; i++) { + if (!path[i].equals(other.path[i])) { return false; } - } else if (!entry.equals(other.entry)) { - return false; } - if (owner == null) { - if (other.owner != null) { - return false; - } - } else if (!owner.equals(other.owner)) { + if (!entry.equals(other.entry)) { return false; } return true; @@ -71,18 +64,22 @@ public class ConfigKey { @Override public String toString() { - return String.format("%s:%s:%s", owner, category, entry); + if (root) { + return String.format("ROOT:%s", entry); + } + String p = path[0]; + for (int i = 1; i < path.length; i++) { + p += "." + path[i]; + } + return String.format("%s:%s", p, entry); } - - private void validate(String owner, String category, String entry) { - if (owner == null) { - throw new NullPointerException("Failed to create ConfigKey: 'owner' can't be null."); - } - if (category == null) { - throw new NullPointerException("Failed to create ConfigKey: 'category' can't be null."); - } + + private void validate(String entry, String... path) { if (entry == null) { - throw new NullPointerException("Failed to create ConfigKey: 'entry' can't be null."); + throw new NullPointerException("Config key must be not null!"); + } + if (entry.isEmpty()) { + throw new IndexOutOfBoundsException("Config key must be not empty!"); } } } diff --git a/src/main/java/ru/betterend/config/Configs.java b/src/main/java/ru/betterend/config/Configs.java index ddee0536..20028f10 100644 --- a/src/main/java/ru/betterend/config/Configs.java +++ b/src/main/java/ru/betterend/config/Configs.java @@ -1,17 +1,17 @@ package ru.betterend.config; public class Configs { - public static final IdConfig ENTITY_CONFIG = new CategoryConfig("entities"); - public static final IdConfig BLOCK_CONFIG = new CategoryConfig("blocks"); - public static final SimpleConfig GENERAL = new SimpleConfig("settings"); - public static final IdConfig ITEM_CONFIG = new CategoryConfig("items"); + public static final PathConfig ENTITY_CONFIG = new PathConfig("entities"); + public static final PathConfig BLOCK_CONFIG = new PathConfig("blocks"); + public static final PathConfig ITEM_CONFIG = new PathConfig("items"); public static final IdConfig BIOME_CONFIG = new EntryConfig("biomes"); + public static final PathConfig GENERATOR_CONFIG = new PathConfig("generator"); public static void saveConfigs() { ENTITY_CONFIG.saveChanges(); BLOCK_CONFIG.saveChanges(); BIOME_CONFIG.saveChanges(); ITEM_CONFIG.saveChanges(); - GENERAL.saveChanges(); + GENERATOR_CONFIG.saveChanges(); } } diff --git a/src/main/java/ru/betterend/config/EntryConfig.java b/src/main/java/ru/betterend/config/EntryConfig.java index 88ef5f5a..44ed19a2 100644 --- a/src/main/java/ru/betterend/config/EntryConfig.java +++ b/src/main/java/ru/betterend/config/EntryConfig.java @@ -4,7 +4,7 @@ public class EntryConfig extends IdConfig { public EntryConfig(String group) { super(group, (id, entry) -> { - return new ConfigKey(id.getNamespace(), id.getPath(), entry); + return new ConfigKey(entry, id); }); } } diff --git a/src/main/java/ru/betterend/config/SimpleConfig.java b/src/main/java/ru/betterend/config/PathConfig.java similarity index 59% rename from src/main/java/ru/betterend/config/SimpleConfig.java rename to src/main/java/ru/betterend/config/PathConfig.java index 463857a6..85b99a72 100644 --- a/src/main/java/ru/betterend/config/SimpleConfig.java +++ b/src/main/java/ru/betterend/config/PathConfig.java @@ -2,14 +2,13 @@ package ru.betterend.config; import org.jetbrains.annotations.Nullable; -import ru.betterend.BetterEnd; import ru.betterend.config.ConfigKeeper.Entry; import ru.betterend.config.ConfigKeeper.FloatRange; import ru.betterend.config.ConfigKeeper.IntegerRange; -public class SimpleConfig extends Config { +public class PathConfig extends Config { - public SimpleConfig(String group) { + public PathConfig(String group) { super(group); } @@ -17,7 +16,7 @@ public class SimpleConfig extends Config { protected void registerEntries() {} protected ConfigKey createKey(String category, String key) { - return new ConfigKey(BetterEnd.MOD_ID, category, key); + return new ConfigKey(key, category.split("\\.")); } @Nullable @@ -85,4 +84,62 @@ public class SimpleConfig extends Config { public boolean setBoolean(String category, String key, boolean value) { return this.setBoolean(createKey(category, key), value); } + + // From Root + + public String getStringRoot(String key, String defaultValue) { + return this.getString(createKey("", key), defaultValue); + } + + public String getStringRoot(String key) { + return this.getString(createKey("", key)); + } + + public boolean setStringRoot(String key, String value) { + return this.setString(createKey("", key), value); + } + + public int getIntRoot(String key, int defaultValue) { + return this.getInt(createKey("", key), defaultValue); + } + + public int getIntRoot(String key) { + return this.getInt(createKey("", key)); + } + + public boolean setIntRoot(String key, int value) { + return this.setInt(createKey("", key), value); + } + + public boolean setRangedIntRoot(String key, int value) { + return this.setRanged(createKey("", key), value, IntegerRange.class); + } + + public boolean setRangedFloatRoot(String key, float value) { + return this.setRanged(createKey("", key), value, FloatRange.class); + } + + public float getFloatRoot(String key, float defaultValue) { + return this.getFloat(createKey("", key), defaultValue); + } + + public float getFloatRoot(String key) { + return this.getFloat(createKey("", key)); + } + + public boolean setFloatRoot(String key, float value) { + return this.setFloat(createKey("", key), value); + } + + public boolean getBooleanRoot(String key, boolean defaultValue) { + return this.getBoolean(createKey("", key), defaultValue); + } + + public boolean getBooleanRoot(String key) { + return this.getBoolean(createKey("", key)); + } + + public boolean setBooleanRoot(String key, boolean value) { + return this.setBoolean(createKey("", key), value); + } } diff --git a/src/main/java/ru/betterend/registry/EndBlocks.java b/src/main/java/ru/betterend/registry/EndBlocks.java index e2e2fcdd..68e0a8cb 100644 --- a/src/main/java/ru/betterend/registry/EndBlocks.java +++ b/src/main/java/ru/betterend/registry/EndBlocks.java @@ -302,7 +302,7 @@ public class EndBlocks { public static void register() {} public static Block registerBlock(Identifier id, Block block) { - if (!Configs.BLOCK_CONFIG.getBoolean(id, "blocks", true)) { + if (!Configs.BLOCK_CONFIG.getBooleanRoot(id.getPath(), true)) { return block; } Registry.register(Registry.BLOCK, id, block); diff --git a/src/main/java/ru/betterend/registry/EndEntities.java b/src/main/java/ru/betterend/registry/EndEntities.java index 1201df47..7e4b812e 100644 --- a/src/main/java/ru/betterend/registry/EndEntities.java +++ b/src/main/java/ru/betterend/registry/EndEntities.java @@ -39,7 +39,7 @@ public class EndEntities { protected static EntityType register(String name, SpawnGroup group, float width, float height, EntityFactory entity) { Identifier id = BetterEnd.makeID(name); EntityType type = FabricEntityTypeBuilder.create(group, entity).dimensions(EntityDimensions.fixed(width, height)).build(); - if (Configs.ENTITY_CONFIG.getBoolean(id, "entities", true)) { + if (Configs.ENTITY_CONFIG.getBooleanRoot(id.getPath(), true)) { return Registry.register(Registry.ENTITY_TYPE, id, type); } return type; @@ -48,7 +48,7 @@ public class EndEntities { private static EntityType register(String name, SpawnGroup group, float width, float height, EntityFactory entity, Builder attributes, boolean fixedSize, int eggColor, int dotsColor) { Identifier id = BetterEnd.makeID(name); EntityType type = FabricEntityTypeBuilder.create(group, entity).dimensions(fixedSize ? EntityDimensions.fixed(width, height) : EntityDimensions.changing(width, height)).build(); - if (Configs.ENTITY_CONFIG.getBoolean(id, "entities", true)) { + if (Configs.ENTITY_CONFIG.getBooleanRoot(id.getPath(), true)) { FabricDefaultAttributeRegistry.register(type, attributes); EndItems.registerEgg("spawn_egg_" + name, type, eggColor, dotsColor); return Registry.register(Registry.ENTITY_TYPE, BetterEnd.makeID(name), type); diff --git a/src/main/java/ru/betterend/registry/EndItems.java b/src/main/java/ru/betterend/registry/EndItems.java index 7e51f182..03076eb2 100644 --- a/src/main/java/ru/betterend/registry/EndItems.java +++ b/src/main/java/ru/betterend/registry/EndItems.java @@ -124,7 +124,7 @@ public class EndItems { if (item instanceof ArmorItem) { return registerArmor(id, item); } - if (!Configs.ITEM_CONFIG.getBoolean(id, "items", true)) { + if (!Configs.ITEM_CONFIG.getBoolean("items", id.getPath(), true)) { return item; } registerItem(id, item, MOD_ITEMS); @@ -144,7 +144,7 @@ public class EndItems { } private static Item registerArmor(Identifier id, Item item) { - if (!Configs.ITEM_CONFIG.getBoolean(id, "armor", true)) { + if (!Configs.ITEM_CONFIG.getBoolean("armor", id.getPath(), true)) { return item; } registerItem(id, item, MOD_ITEMS); @@ -153,7 +153,7 @@ public class EndItems { private static ToolItem registerTool(String name, ToolItem item) { Identifier id = BetterEnd.makeID(name); - if (!Configs.ITEM_CONFIG.getBoolean(id, "tools", true)) { + if (!Configs.ITEM_CONFIG.getBoolean("tools", id.getPath(), true)) { return item; } registerItem(id, item, MOD_ITEMS); diff --git a/src/main/java/ru/betterend/world/generator/TerrainGenerator.java b/src/main/java/ru/betterend/world/generator/TerrainGenerator.java index 6ce5b425..abe22eca 100644 --- a/src/main/java/ru/betterend/world/generator/TerrainGenerator.java +++ b/src/main/java/ru/betterend/world/generator/TerrainGenerator.java @@ -22,8 +22,8 @@ public class TerrainGenerator { private static boolean noRingVoid; public static void init() { - newGenerator = Configs.GENERAL.getBoolean("generator", "useNewGenerator", false); - noRingVoid = Configs.GENERAL.getBoolean("generator", "noRingVoid", false); + newGenerator = Configs.GENERATOR_CONFIG.getBoolean("generator", "useNewGenerator", false); + noRingVoid = Configs.GENERATOR_CONFIG.getBoolean("generator", "noRingVoid", false); } public static void initNoise(long seed) {