Arbitrary hierarchy
This commit is contained in:
parent
640671bfce
commit
fc71ea4eba
10 changed files with 133 additions and 74 deletions
|
@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 <T, E extends Entry<T>> void storeValue(E entry, T value) {
|
||||
|
|
|
@ -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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue