More config refactor

This commit is contained in:
Aleksey 2020-12-12 19:14:25 +03:00
parent c630e6d22a
commit 74e9adf42e
7 changed files with 96 additions and 133 deletions

View file

@ -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 <T, E extends Entry<T>> 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();

View file

@ -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.");
}
}
}

View file

@ -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();

View file

@ -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<Identifier, String, ConfigKey> keyFactory;
public IdConfig(String group, BiFunction<Identifier, String, ConfigKey> 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

View file

@ -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);
}
}

View file

@ -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;
}

View file

@ -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<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;
}
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<Item>) FabricToolTags.SHOVELS, item);