Arbitrary hierarchy

This commit is contained in:
paulevsGitch 2020-12-31 07:15:46 +03:00
parent 640671bfce
commit fc71ea4eba
10 changed files with 133 additions and 74 deletions

View file

@ -4,7 +4,7 @@ public class CategoryConfig extends IdConfig {
public CategoryConfig(String group) { public CategoryConfig(String group) {
super(group, (id, category) -> { super(group, (id, category) -> {
return new ConfigKey(id.getNamespace(), category, id.getPath()); return new ConfigKey(id.getPath(), id.getNamespace(), category);
}); });
} }
} }

View file

@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.minecraft.util.JsonHelper; import net.minecraft.util.JsonHelper;
@ -38,25 +39,29 @@ public final class ConfigKeeper {
if (configObject == null) { if (configObject == null) {
return; return;
} }
String group = key.getOwner(); String[] path = key.getPath();
JsonObject jsonGroup; JsonObject obj = null;
if (configObject.has(group)) {
jsonGroup = JsonHelper.getObject(configObject, group); if (!key.isRoot()) {
} else { for (String group: path) {
jsonGroup = new JsonObject(); JsonElement element = configObject.get(group);
configObject.add(group, jsonGroup); if (element == null || !element.isJsonObject()) {
} element = new JsonObject();
String category = key.getCategory(); if (obj == null) {
JsonObject jsonCategory; obj = element.getAsJsonObject();
if (jsonGroup.has(category)) { configObject.add(group, obj);
jsonCategory = JsonHelper.getObject(jsonGroup, category); }
} else { else {
jsonCategory = new JsonObject(); obj.add(group, element);
jsonGroup.add(category, jsonCategory); }
}
obj = element.getAsJsonObject();
}
} }
String paramKey = key.getEntry(); String paramKey = key.getEntry();
paramKey += " [default: " + entry.getDefault() + "]"; paramKey += " [default: " + entry.getDefault() + "]";
this.changed = entry.setLocation(jsonCategory, paramKey); this.changed = entry.setLocation(obj == null ? configObject : obj, paramKey);
} }
private <T, E extends Entry<T>> void storeValue(E entry, T value) { private <T, E extends Entry<T>> void storeValue(E entry, T value) {

View file

@ -1,38 +1,41 @@
package ru.betterend.config; package ru.betterend.config;
import org.jetbrains.annotations.NotNull; import net.minecraft.util.Identifier;
public class ConfigKey { public class ConfigKey {
private final String owner; private final String path[];
private final String category;
private final String entry; private final String entry;
private final boolean root;
public ConfigKey(@NotNull String owner, @NotNull String category, @NotNull String entry) { public ConfigKey(String entry, String... path) {
this.validate(owner, category, entry); validate(entry, path);
this.owner = owner; this.path = path;
this.category = category;
this.entry = entry; 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() { public String[] getPath() {
return owner; return path;
}
public String getCategory() {
return category;
} }
public String getEntry() { public String getEntry() {
return entry; return entry;
} }
public boolean isRoot() {
return root;
}
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + category.hashCode(); result = prime * result + path.hashCode();
result = prime * result + entry.hashCode(); result = prime * result + entry.hashCode();
result = prime * result + owner.hashCode();
return result; return result;
} }
@ -45,25 +48,15 @@ public class ConfigKey {
return false; return false;
} }
ConfigKey other = (ConfigKey) obj; ConfigKey other = (ConfigKey) obj;
if (category == null) { if (other.path.length != path.length) {
if (other.category != null) {
return false;
}
} else if (!category.equals(other.category)) {
return false; return false;
} }
if (entry == null) { for (int i = 0; i < path.length; i++) {
if (other.entry != null) { if (!path[i].equals(other.path[i])) {
return false; return false;
} }
} else if (!entry.equals(other.entry)) {
return false;
} }
if (owner == null) { if (!entry.equals(other.entry)) {
if (other.owner != null) {
return false;
}
} else if (!owner.equals(other.owner)) {
return false; return false;
} }
return true; return true;
@ -71,18 +64,22 @@ public class ConfigKey {
@Override @Override
public String toString() { 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) { private void validate(String entry, String... path) {
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.");
}
if (entry == null) { 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!");
} }
} }
} }

View file

@ -1,17 +1,17 @@
package ru.betterend.config; package ru.betterend.config;
public class Configs { public class Configs {
public static final IdConfig ENTITY_CONFIG = new CategoryConfig("entities"); public static final PathConfig ENTITY_CONFIG = new PathConfig("entities");
public static final IdConfig BLOCK_CONFIG = new CategoryConfig("blocks"); public static final PathConfig BLOCK_CONFIG = new PathConfig("blocks");
public static final SimpleConfig GENERAL = new SimpleConfig("settings"); public static final PathConfig ITEM_CONFIG = new PathConfig("items");
public static final IdConfig ITEM_CONFIG = new CategoryConfig("items");
public static final IdConfig BIOME_CONFIG = new EntryConfig("biomes"); public static final IdConfig BIOME_CONFIG = new EntryConfig("biomes");
public static final PathConfig GENERATOR_CONFIG = new PathConfig("generator");
public static void saveConfigs() { public static void saveConfigs() {
ENTITY_CONFIG.saveChanges(); ENTITY_CONFIG.saveChanges();
BLOCK_CONFIG.saveChanges(); BLOCK_CONFIG.saveChanges();
BIOME_CONFIG.saveChanges(); BIOME_CONFIG.saveChanges();
ITEM_CONFIG.saveChanges(); ITEM_CONFIG.saveChanges();
GENERAL.saveChanges(); GENERATOR_CONFIG.saveChanges();
} }
} }

View file

@ -4,7 +4,7 @@ public class EntryConfig extends IdConfig {
public EntryConfig(String group) { public EntryConfig(String group) {
super(group, (id, entry) -> { super(group, (id, entry) -> {
return new ConfigKey(id.getNamespace(), id.getPath(), entry); return new ConfigKey(entry, id);
}); });
} }
} }

View file

@ -2,14 +2,13 @@ package ru.betterend.config;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import ru.betterend.BetterEnd;
import ru.betterend.config.ConfigKeeper.Entry; import ru.betterend.config.ConfigKeeper.Entry;
import ru.betterend.config.ConfigKeeper.FloatRange; import ru.betterend.config.ConfigKeeper.FloatRange;
import ru.betterend.config.ConfigKeeper.IntegerRange; 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); super(group);
} }
@ -17,7 +16,7 @@ public class SimpleConfig extends Config {
protected void registerEntries() {} protected void registerEntries() {}
protected ConfigKey createKey(String category, String key) { protected ConfigKey createKey(String category, String key) {
return new ConfigKey(BetterEnd.MOD_ID, category, key); return new ConfigKey(key, category.split("\\."));
} }
@Nullable @Nullable
@ -85,4 +84,62 @@ public class SimpleConfig extends Config {
public boolean setBoolean(String category, String key, boolean value) { public boolean setBoolean(String category, String key, boolean value) {
return this.setBoolean(createKey(category, key), 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);
}
} }

View file

@ -302,7 +302,7 @@ 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 (!Configs.BLOCK_CONFIG.getBoolean(id, "blocks", true)) { if (!Configs.BLOCK_CONFIG.getBooleanRoot(id.getPath(), true)) {
return block; return block;
} }
Registry.register(Registry.BLOCK, id, block); Registry.register(Registry.BLOCK, id, block);

View file

@ -39,7 +39,7 @@ public class EndEntities {
protected static <T extends Entity> EntityType<T> register(String name, SpawnGroup group, float width, float height, EntityFactory<T> entity) { protected static <T extends Entity> EntityType<T> register(String name, SpawnGroup group, float width, float height, EntityFactory<T> entity) {
Identifier id = BetterEnd.makeID(name); Identifier id = BetterEnd.makeID(name);
EntityType<T> type = FabricEntityTypeBuilder.<T>create(group, entity).dimensions(EntityDimensions.fixed(width, height)).build(); EntityType<T> type = FabricEntityTypeBuilder.<T>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 Registry.register(Registry.ENTITY_TYPE, id, type);
} }
return type; return type;
@ -48,7 +48,7 @@ public class EndEntities {
private static <T extends LivingEntity> EntityType<T> register(String name, SpawnGroup group, float width, float height, EntityFactory<T> entity, Builder attributes, boolean fixedSize, int eggColor, int dotsColor) { private static <T extends LivingEntity> EntityType<T> register(String name, SpawnGroup group, float width, float height, EntityFactory<T> entity, Builder attributes, boolean fixedSize, int eggColor, int dotsColor) {
Identifier id = BetterEnd.makeID(name); Identifier id = BetterEnd.makeID(name);
EntityType<T> type = FabricEntityTypeBuilder.<T>create(group, entity).dimensions(fixedSize ? EntityDimensions.fixed(width, height) : EntityDimensions.changing(width, height)).build(); EntityType<T> type = FabricEntityTypeBuilder.<T>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); FabricDefaultAttributeRegistry.register(type, attributes);
EndItems.registerEgg("spawn_egg_" + name, type, eggColor, dotsColor); EndItems.registerEgg("spawn_egg_" + name, type, eggColor, dotsColor);
return Registry.register(Registry.ENTITY_TYPE, BetterEnd.makeID(name), type); return Registry.register(Registry.ENTITY_TYPE, BetterEnd.makeID(name), type);

View file

@ -124,7 +124,7 @@ public class EndItems {
if (item instanceof ArmorItem) { if (item instanceof ArmorItem) {
return registerArmor(id, item); return registerArmor(id, item);
} }
if (!Configs.ITEM_CONFIG.getBoolean(id, "items", true)) { if (!Configs.ITEM_CONFIG.getBoolean("items", id.getPath(), true)) {
return item; return item;
} }
registerItem(id, item, MOD_ITEMS); registerItem(id, item, MOD_ITEMS);
@ -144,7 +144,7 @@ public class EndItems {
} }
private static Item registerArmor(Identifier id, Item item) { 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; return item;
} }
registerItem(id, item, MOD_ITEMS); registerItem(id, item, MOD_ITEMS);
@ -153,7 +153,7 @@ public class EndItems {
private static ToolItem registerTool(String name, ToolItem item) { private static ToolItem registerTool(String name, ToolItem item) {
Identifier id = BetterEnd.makeID(name); Identifier id = BetterEnd.makeID(name);
if (!Configs.ITEM_CONFIG.getBoolean(id, "tools", true)) { if (!Configs.ITEM_CONFIG.getBoolean("tools", id.getPath(), true)) {
return item; return item;
} }
registerItem(id, item, MOD_ITEMS); registerItem(id, item, MOD_ITEMS);

View file

@ -22,8 +22,8 @@ public class TerrainGenerator {
private static boolean noRingVoid; private static boolean noRingVoid;
public static void init() { public static void init() {
newGenerator = Configs.GENERAL.getBoolean("generator", "useNewGenerator", false); newGenerator = Configs.GENERATOR_CONFIG.getBoolean("generator", "useNewGenerator", false);
noRingVoid = Configs.GENERAL.getBoolean("generator", "noRingVoid", false); noRingVoid = Configs.GENERATOR_CONFIG.getBoolean("generator", "noRingVoid", false);
} }
public static void initNoise(long seed) { public static void initNoise(long seed) {