More config refactor
This commit is contained in:
parent
c630e6d22a
commit
74e9adf42e
7 changed files with 96 additions and 133 deletions
|
@ -12,7 +12,6 @@ import com.google.common.reflect.TypeToken;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import net.minecraft.util.Identifier;
|
|
||||||
import net.minecraft.util.JsonHelper;
|
import net.minecraft.util.JsonHelper;
|
||||||
import ru.betterend.util.JsonFactory;
|
import ru.betterend.util.JsonFactory;
|
||||||
|
|
||||||
|
@ -28,9 +27,7 @@ public final class ConfigKeeper {
|
||||||
private <T, E extends Entry<T>> void storeValue(ConfigKey key, E entry, T value) {
|
private <T, E extends Entry<T>> void storeValue(ConfigKey key, E entry, T value) {
|
||||||
if (configObject == null) return;
|
if (configObject == null) return;
|
||||||
|
|
||||||
Identifier categoryId = key.getCategory();
|
String group = key.getOwner();
|
||||||
Identifier paramId = key.getParameter();
|
|
||||||
String group = categoryId.getPath();
|
|
||||||
JsonObject jsonGroup;
|
JsonObject jsonGroup;
|
||||||
if (configObject.has(group)) {
|
if (configObject.has(group)) {
|
||||||
jsonGroup = JsonHelper.getObject(configObject, group);
|
jsonGroup = JsonHelper.getObject(configObject, group);
|
||||||
|
@ -38,7 +35,7 @@ public final class ConfigKeeper {
|
||||||
jsonGroup = new JsonObject();
|
jsonGroup = new JsonObject();
|
||||||
configObject.add(group, jsonGroup);
|
configObject.add(group, jsonGroup);
|
||||||
}
|
}
|
||||||
String category = paramId.getNamespace();
|
String category = key.getCategory();
|
||||||
JsonObject jsonCategory;
|
JsonObject jsonCategory;
|
||||||
if (jsonGroup.has(category)) {
|
if (jsonGroup.has(category)) {
|
||||||
jsonCategory = JsonHelper.getObject(jsonGroup, category);
|
jsonCategory = JsonHelper.getObject(jsonGroup, category);
|
||||||
|
@ -46,7 +43,7 @@ public final class ConfigKeeper {
|
||||||
jsonCategory = new JsonObject();
|
jsonCategory = new JsonObject();
|
||||||
jsonGroup.add(category, jsonCategory);
|
jsonGroup.add(category, jsonCategory);
|
||||||
}
|
}
|
||||||
String paramKey = paramId.getPath();
|
String paramKey = key.getEntry();
|
||||||
paramKey += " [default: " + entry.getDefault() + "]";
|
paramKey += " [default: " + entry.getDefault() + "]";
|
||||||
entry.toJson(jsonCategory, paramKey, value);
|
entry.toJson(jsonCategory, paramKey, value);
|
||||||
}
|
}
|
||||||
|
@ -56,21 +53,19 @@ public final class ConfigKeeper {
|
||||||
return entry.getDefault();
|
return entry.getDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
Identifier categoryId = key.getCategory();
|
String group = key.getOwner();
|
||||||
Identifier paramId = key.getParameter();
|
|
||||||
String group = categoryId.getPath();
|
|
||||||
if (!configObject.has(group)) {
|
if (!configObject.has(group)) {
|
||||||
return entry.getDefault();
|
return entry.getDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject jsonGroup = JsonHelper.getObject(configObject, group);
|
JsonObject jsonGroup = JsonHelper.getObject(configObject, group);
|
||||||
String category = paramId.getNamespace();
|
String category = key.getCategory();
|
||||||
if (!jsonGroup.has(category)) {
|
if (!jsonGroup.has(category)) {
|
||||||
return entry.getDefault();
|
return entry.getDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category);
|
JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category);
|
||||||
String paramKey = paramId.getPath();
|
String paramKey = key.getEntry();
|
||||||
paramKey += " [default: " + entry.getDefault() + "]";
|
paramKey += " [default: " + entry.getDefault() + "]";
|
||||||
if (!jsonCategory.has(paramKey)) {
|
if (!jsonCategory.has(paramKey)) {
|
||||||
return entry.getDefault();
|
return entry.getDefault();
|
||||||
|
|
|
@ -1,30 +1,38 @@
|
||||||
package ru.betterend.config;
|
package ru.betterend.config;
|
||||||
|
|
||||||
import net.minecraft.util.Identifier;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class ConfigKey {
|
public class ConfigKey {
|
||||||
private final Identifier category;
|
private final String owner;
|
||||||
private final Identifier parameter;
|
private final String category;
|
||||||
|
private final String entry;
|
||||||
|
|
||||||
public ConfigKey(Identifier category, Identifier parameter) {
|
public ConfigKey(@NotNull String owner, @NotNull String category, @NotNull String entry) {
|
||||||
|
this.validate(owner, category, entry);
|
||||||
|
this.owner = owner;
|
||||||
this.category = category;
|
this.category = category;
|
||||||
this.parameter = parameter;
|
this.entry = entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Identifier getCategory() {
|
public String getOwner() {
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCategory() {
|
||||||
return category;
|
return category;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Identifier getParameter() {
|
public String getEntry() {
|
||||||
return parameter;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 == null) ? 0 : category.hashCode());
|
result = prime * result + category.hashCode();
|
||||||
result = prime * result + ((parameter == null) ? 0 : parameter.hashCode());
|
result = prime * result + entry.hashCode();
|
||||||
|
result = prime * result + owner.hashCode();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,13 +52,32 @@ public class ConfigKey {
|
||||||
} else if (!category.equals(other.category)) {
|
} else if (!category.equals(other.category)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (parameter == null) {
|
if (entry == null) {
|
||||||
if (other.parameter != null) {
|
if (other.entry != null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (!parameter.equals(other.parameter)) {
|
} else if (!entry.equals(other.entry)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (owner == null) {
|
||||||
|
if (other.owner != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if (!owner.equals(other.owner)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
if (entry == null) {
|
||||||
|
throw new NullPointerException("Failed to create ConfigKey: 'entry' can't be null.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
package ru.betterend.config;
|
package ru.betterend.config;
|
||||||
|
|
||||||
public class Configs {
|
public class Configs {
|
||||||
public static final IdentifierConfig ITEM_CONFIG = new IdentifierConfig("items");
|
public static final IdConfig ITEM_CONFIG = new IdConfig("items", (item, category) -> {
|
||||||
public static final IdentifierConfig BLOCK_CONFIG = new IdentifierConfig("blocks");
|
return new ConfigKey(item.getNamespace(), category, item.getPath());
|
||||||
public static final IdentifierConfig BIOME_CONFIG = new IdentifierConfig("biomes");
|
});
|
||||||
|
public static final IdConfig BLOCK_CONFIG = new IdConfig("blocks", (block, category) -> {
|
||||||
|
return new ConfigKey(block.getNamespace(), category, block.getPath());
|
||||||
|
});
|
||||||
|
public static final IdConfig BIOME_CONFIG = new IdConfig("biomes", (biome, entry) -> {
|
||||||
|
return new ConfigKey(biome.getNamespace(), biome.getPath(), entry);
|
||||||
|
});
|
||||||
|
|
||||||
public static void saveConfigs() {
|
public static void saveConfigs() {
|
||||||
ITEM_CONFIG.saveChanges();
|
ITEM_CONFIG.saveChanges();
|
||||||
|
|
|
@ -1,22 +1,24 @@
|
||||||
package ru.betterend.config;
|
package ru.betterend.config;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
import ru.betterend.config.ConfigKeeper.Entry;
|
import ru.betterend.config.ConfigKeeper.Entry;
|
||||||
|
|
||||||
public class IdentifierConfig extends Config {
|
public class IdConfig extends Config {
|
||||||
public IdentifierConfig(String group) {
|
|
||||||
|
private final BiFunction<Identifier, String, ConfigKey> keyFactory;
|
||||||
|
|
||||||
|
public IdConfig(String group, BiFunction<Identifier, String, ConfigKey> keyFactory) {
|
||||||
super(group);
|
super(group);
|
||||||
|
this.keyFactory = keyFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void registerEntries() {}
|
protected void registerEntries() {}
|
||||||
|
|
||||||
private ConfigKey createKey(Identifier id, String key) {
|
private ConfigKey createKey(Identifier id, String key) {
|
||||||
Identifier groupId = new Identifier(group, id.getNamespace());
|
return this.keyFactory.apply(id, key);
|
||||||
Identifier categoryId = new Identifier(id.getPath(), key);
|
|
||||||
return new ConfigKey(groupId, categoryId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
|
@ -1,84 +0,0 @@
|
||||||
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 {
|
|
||||||
|
|
||||||
protected ItemConfig() {
|
|
||||||
super("settings");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void registerEntries() {}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -265,11 +265,11 @@ 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, "enabled", true)) {
|
if (!Configs.BLOCK_CONFIG.getBoolean(id, "blocks", true)) {
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
Registry.register(Registry.BLOCK, id, block);
|
Registry.register(Registry.BLOCK, id, block);
|
||||||
EndItems.registerItem(id, new BlockItem(block, EndItems.makeBlockItemSettings()));
|
EndItems.registerBlockItem(id, new BlockItem(block, EndItems.makeBlockItemSettings()));
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import java.util.List;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
|
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
|
||||||
|
|
||||||
import net.minecraft.block.DispenserBlock;
|
import net.minecraft.block.DispenserBlock;
|
||||||
import net.minecraft.block.dispenser.ItemDispenserBehavior;
|
import net.minecraft.block.dispenser.ItemDispenserBehavior;
|
||||||
import net.minecraft.entity.EntityType;
|
import net.minecraft.entity.EntityType;
|
||||||
|
@ -14,7 +15,6 @@ import net.minecraft.entity.effect.StatusEffectInstance;
|
||||||
import net.minecraft.entity.effect.StatusEffects;
|
import net.minecraft.entity.effect.StatusEffects;
|
||||||
import net.minecraft.fluid.Fluids;
|
import net.minecraft.fluid.Fluids;
|
||||||
import net.minecraft.item.ArmorItem;
|
import net.minecraft.item.ArmorItem;
|
||||||
import net.minecraft.item.BlockItem;
|
|
||||||
import net.minecraft.item.FishBucketItem;
|
import net.minecraft.item.FishBucketItem;
|
||||||
import net.minecraft.item.FoodComponent;
|
import net.minecraft.item.FoodComponent;
|
||||||
import net.minecraft.item.FoodComponents;
|
import net.minecraft.item.FoodComponents;
|
||||||
|
@ -32,6 +32,7 @@ import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.math.BlockPointer;
|
import net.minecraft.util.math.BlockPointer;
|
||||||
import net.minecraft.util.math.Direction;
|
import net.minecraft.util.math.Direction;
|
||||||
import net.minecraft.util.registry.Registry;
|
import net.minecraft.util.registry.Registry;
|
||||||
|
|
||||||
import ru.betterend.BetterEnd;
|
import ru.betterend.BetterEnd;
|
||||||
import ru.betterend.config.Configs;
|
import ru.betterend.config.Configs;
|
||||||
import ru.betterend.item.EndArmorMaterial;
|
import ru.betterend.item.EndArmorMaterial;
|
||||||
|
@ -118,26 +119,42 @@ public class EndItems {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Item registerItem(Identifier id, Item item) {
|
public static Item registerItem(Identifier id, Item item) {
|
||||||
if (!Configs.ITEM_CONFIG.getBoolean(id, "enabled", true)) {
|
if (item instanceof ArmorItem) {
|
||||||
|
return registerArmor(id, item);
|
||||||
|
}
|
||||||
|
if (!Configs.ITEM_CONFIG.getBoolean(id, "items", true)) {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
if (item != Items.AIR) {
|
registerItem(id, item, MOD_ITEMS);
|
||||||
Registry.register(Registry.ITEM, id, item);
|
|
||||||
if (item instanceof BlockItem)
|
|
||||||
MOD_BLOCKS.add(item);
|
|
||||||
else
|
|
||||||
MOD_ITEMS.add(item);
|
|
||||||
}
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static ToolItem registerTool(String name, ToolItem item) {
|
public static Item registerBlockItem(Identifier id, Item item) {
|
||||||
Identifier id = BetterEnd.makeID(name);
|
registerItem(id, item, MOD_BLOCKS);
|
||||||
if (!Configs.ITEM_CONFIG.getBoolean(id, "enabled", true)) {
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void registerItem(Identifier id, Item item, List<Item> registry) {
|
||||||
|
if (item != Items.AIR) {
|
||||||
|
Registry.register(Registry.ITEM, id, item);
|
||||||
|
registry.add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Item registerArmor(Identifier id, Item item) {
|
||||||
|
if (!Configs.ITEM_CONFIG.getBoolean(id, "armor", true)) {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
Registry.register(Registry.ITEM, id, item);
|
registerItem(id, item, MOD_ITEMS);
|
||||||
MOD_ITEMS.add(item);
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ToolItem registerTool(String name, ToolItem item) {
|
||||||
|
Identifier id = BetterEnd.makeID(name);
|
||||||
|
if (!Configs.ITEM_CONFIG.getBoolean(id, "tools", true)) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
registerItem(id, item, MOD_ITEMS);
|
||||||
|
|
||||||
if (item instanceof ShovelItem) {
|
if (item instanceof ShovelItem) {
|
||||||
TagHelper.addTag((Tag.Identified<Item>) FabricToolTags.SHOVELS, item);
|
TagHelper.addTag((Tag.Identified<Item>) FabricToolTags.SHOVELS, item);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue