From 74e9adf42ebb0b2c807cedb166453474ebb6dcb9 Mon Sep 17 00:00:00 2001 From: Aleksey Date: Sat, 12 Dec 2020 19:14:25 +0300 Subject: [PATCH] More config refactor --- .../ru/betterend/config/ConfigKeeper.java | 17 ++-- .../java/ru/betterend/config/ConfigKey.java | 53 +++++++++--- .../java/ru/betterend/config/Configs.java | 12 ++- .../{IdentifierConfig.java => IdConfig.java} | 14 ++-- .../java/ru/betterend/config/ItemConfig.java | 84 ------------------- .../java/ru/betterend/registry/EndBlocks.java | 4 +- .../java/ru/betterend/registry/EndItems.java | 45 ++++++---- 7 files changed, 96 insertions(+), 133 deletions(-) rename src/main/java/ru/betterend/config/{IdentifierConfig.java => IdConfig.java} (87%) delete mode 100644 src/main/java/ru/betterend/config/ItemConfig.java diff --git a/src/main/java/ru/betterend/config/ConfigKeeper.java b/src/main/java/ru/betterend/config/ConfigKeeper.java index 8a6ad88f..a2f4286d 100644 --- a/src/main/java/ru/betterend/config/ConfigKeeper.java +++ b/src/main/java/ru/betterend/config/ConfigKeeper.java @@ -12,7 +12,6 @@ import com.google.common.reflect.TypeToken; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import net.minecraft.util.Identifier; import net.minecraft.util.JsonHelper; import ru.betterend.util.JsonFactory; @@ -28,9 +27,7 @@ public final class ConfigKeeper { private > void storeValue(ConfigKey key, E entry, T value) { if (configObject == null) return; - Identifier categoryId = key.getCategory(); - Identifier paramId = key.getParameter(); - String group = categoryId.getPath(); + String group = key.getOwner(); JsonObject jsonGroup; if (configObject.has(group)) { jsonGroup = JsonHelper.getObject(configObject, group); @@ -38,7 +35,7 @@ public final class ConfigKeeper { jsonGroup = new JsonObject(); configObject.add(group, jsonGroup); } - String category = paramId.getNamespace(); + String category = key.getCategory(); JsonObject jsonCategory; if (jsonGroup.has(category)) { jsonCategory = JsonHelper.getObject(jsonGroup, category); @@ -46,7 +43,7 @@ public final class ConfigKeeper { jsonCategory = new JsonObject(); jsonGroup.add(category, jsonCategory); } - String paramKey = paramId.getPath(); + String paramKey = key.getEntry(); paramKey += " [default: " + entry.getDefault() + "]"; entry.toJson(jsonCategory, paramKey, value); } @@ -56,21 +53,19 @@ public final class ConfigKeeper { return entry.getDefault(); } - Identifier categoryId = key.getCategory(); - Identifier paramId = key.getParameter(); - String group = categoryId.getPath(); + String group = key.getOwner(); if (!configObject.has(group)) { return entry.getDefault(); } JsonObject jsonGroup = JsonHelper.getObject(configObject, group); - String category = paramId.getNamespace(); + String category = key.getCategory(); if (!jsonGroup.has(category)) { return entry.getDefault(); } JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category); - String paramKey = paramId.getPath(); + String paramKey = key.getEntry(); paramKey += " [default: " + entry.getDefault() + "]"; if (!jsonCategory.has(paramKey)) { return entry.getDefault(); diff --git a/src/main/java/ru/betterend/config/ConfigKey.java b/src/main/java/ru/betterend/config/ConfigKey.java index 110ce186..d037ddfa 100644 --- a/src/main/java/ru/betterend/config/ConfigKey.java +++ b/src/main/java/ru/betterend/config/ConfigKey.java @@ -1,30 +1,38 @@ package ru.betterend.config; -import net.minecraft.util.Identifier; +import org.jetbrains.annotations.NotNull; public class ConfigKey { - private final Identifier category; - private final Identifier parameter; + private final String owner; + 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.parameter = parameter; + this.entry = entry; } - public Identifier getCategory() { + public String getOwner() { + return owner; + } + + public String getCategory() { return category; } - public Identifier getParameter() { - return parameter; + public String getEntry() { + return entry; } @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()); + result = prime * result + category.hashCode(); + result = prime * result + entry.hashCode(); + result = prime * result + owner.hashCode(); return result; } @@ -44,13 +52,32 @@ public class ConfigKey { } else if (!category.equals(other.category)) { return false; } - if (parameter == null) { - if (other.parameter != null) { + if (entry == null) { + if (other.entry != null) { 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 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."); + } + } } diff --git a/src/main/java/ru/betterend/config/Configs.java b/src/main/java/ru/betterend/config/Configs.java index 2e00f4a6..09fcca40 100644 --- a/src/main/java/ru/betterend/config/Configs.java +++ b/src/main/java/ru/betterend/config/Configs.java @@ -1,9 +1,15 @@ package ru.betterend.config; public class Configs { - public static final IdentifierConfig ITEM_CONFIG = new IdentifierConfig("items"); - public static final IdentifierConfig BLOCK_CONFIG = new IdentifierConfig("blocks"); - public static final IdentifierConfig BIOME_CONFIG = new IdentifierConfig("biomes"); + public static final IdConfig ITEM_CONFIG = new IdConfig("items", (item, category) -> { + return new ConfigKey(item.getNamespace(), category, item.getPath()); + }); + 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() { ITEM_CONFIG.saveChanges(); diff --git a/src/main/java/ru/betterend/config/IdentifierConfig.java b/src/main/java/ru/betterend/config/IdConfig.java similarity index 87% rename from src/main/java/ru/betterend/config/IdentifierConfig.java rename to src/main/java/ru/betterend/config/IdConfig.java index 4e44b370..9555e4e1 100644 --- a/src/main/java/ru/betterend/config/IdentifierConfig.java +++ b/src/main/java/ru/betterend/config/IdConfig.java @@ -1,22 +1,24 @@ package ru.betterend.config; +import java.util.function.BiFunction; import org.jetbrains.annotations.Nullable; - import net.minecraft.util.Identifier; import ru.betterend.config.ConfigKeeper.Entry; -public class IdentifierConfig extends Config { - public IdentifierConfig(String group) { +public class IdConfig extends Config { + + private final BiFunction keyFactory; + + public IdConfig(String group, BiFunction keyFactory) { super(group); + this.keyFactory = keyFactory; } @Override protected void registerEntries() {} private ConfigKey createKey(Identifier id, String key) { - Identifier groupId = new Identifier(group, id.getNamespace()); - Identifier categoryId = new Identifier(id.getPath(), key); - return new ConfigKey(groupId, categoryId); + return this.keyFactory.apply(id, key); } @Nullable diff --git a/src/main/java/ru/betterend/config/ItemConfig.java b/src/main/java/ru/betterend/config/ItemConfig.java deleted file mode 100644 index 6e39d54b..00000000 --- a/src/main/java/ru/betterend/config/ItemConfig.java +++ /dev/null @@ -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 getEntry(Identifier item, String category) { - return this.getEntry(createKey(item, category)); - } - - @Nullable - public 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 > 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); - } -} diff --git a/src/main/java/ru/betterend/registry/EndBlocks.java b/src/main/java/ru/betterend/registry/EndBlocks.java index 6c12fc69..990bc5f7 100644 --- a/src/main/java/ru/betterend/registry/EndBlocks.java +++ b/src/main/java/ru/betterend/registry/EndBlocks.java @@ -265,11 +265,11 @@ public class EndBlocks { public static void register() {} 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; } Registry.register(Registry.BLOCK, id, block); - EndItems.registerItem(id, new BlockItem(block, EndItems.makeBlockItemSettings())); + EndItems.registerBlockItem(id, new BlockItem(block, EndItems.makeBlockItemSettings())); return block; } diff --git a/src/main/java/ru/betterend/registry/EndItems.java b/src/main/java/ru/betterend/registry/EndItems.java index 63873dfe..9ca0569a 100644 --- a/src/main/java/ru/betterend/registry/EndItems.java +++ b/src/main/java/ru/betterend/registry/EndItems.java @@ -5,6 +5,7 @@ import java.util.List; import com.google.common.collect.Lists; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; + import net.minecraft.block.DispenserBlock; import net.minecraft.block.dispenser.ItemDispenserBehavior; import net.minecraft.entity.EntityType; @@ -14,7 +15,6 @@ import net.minecraft.entity.effect.StatusEffectInstance; import net.minecraft.entity.effect.StatusEffects; import net.minecraft.fluid.Fluids; import net.minecraft.item.ArmorItem; -import net.minecraft.item.BlockItem; import net.minecraft.item.FishBucketItem; import net.minecraft.item.FoodComponent; 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.Direction; import net.minecraft.util.registry.Registry; + import ru.betterend.BetterEnd; import ru.betterend.config.Configs; import ru.betterend.item.EndArmorMaterial; @@ -118,26 +119,42 @@ public class EndItems { } 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; } - if (item != Items.AIR) { - Registry.register(Registry.ITEM, id, item); - if (item instanceof BlockItem) - MOD_BLOCKS.add(item); - else - MOD_ITEMS.add(item); - } + registerItem(id, item, MOD_ITEMS); return item; } - protected static ToolItem registerTool(String name, ToolItem item) { - Identifier id = BetterEnd.makeID(name); - if (!Configs.ITEM_CONFIG.getBoolean(id, "enabled", true)) { + public static Item registerBlockItem(Identifier id, Item item) { + registerItem(id, item, MOD_BLOCKS); + return item; + } + + private static void registerItem(Identifier id, Item item, List 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; } - Registry.register(Registry.ITEM, id, item); - MOD_ITEMS.add(item); + registerItem(id, item, MOD_ITEMS); + 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) { TagHelper.addTag((Tag.Identified) FabricToolTags.SHOVELS, item);