diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/config/CommonConfig.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/config/CommonConfig.java index 0c3d333e2..2e8893a27 100644 --- a/src/main/java/com/jaquadro/minecraft/storagedrawers/config/CommonConfig.java +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/config/CommonConfig.java @@ -2,6 +2,10 @@ import net.minecraftforge.common.ForgeConfigSpec; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + public final class CommonConfig { private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder(); @@ -11,7 +15,11 @@ public final class CommonConfig public static final ForgeConfigSpec spec = BUILDER.build(); private static boolean loaded = false; + private static List loadActions = new ArrayList<>(); + public static void setLoaded() { + if (!loaded) + loadActions.forEach(Runnable::run); loaded = true; } @@ -19,6 +27,13 @@ public static boolean isLoaded() { return loaded; } + public static void onLoad(Runnable action) { + if (loaded) + action.run(); + else + loadActions.add(action); + } + public static class General { public final ForgeConfigSpec.ConfigValue baseStackStorage; public final ForgeConfigSpec.ConfigValue enableUI; @@ -28,9 +43,12 @@ public static class General { public final ForgeConfigSpec.ConfigValue debugTrace; public final ForgeConfigSpec.ConfigValue enableExtraCompactingRules; public final ForgeConfigSpec.ConfigValue controllerRange; + public final ForgeConfigSpec.ConfigValue> compRules; public General(ForgeConfigSpec.Builder builder) { builder.push("General"); + List test = new ArrayList<>(); + test.add("minecraft:clay, minecraft:clay_ball, 4"); baseStackStorage = builder .comment("The number of item stacks held in a basic unit of storage.", @@ -51,10 +69,20 @@ public General(ForgeConfigSpec.Builder builder) { .define("enableExtraCompactingRules", true); debugTrace = builder .define("debugTrace", false); + compRules = builder + .comment("List of rules in format \"domain:item1, domain:item2, n\".", + "Causes a compacting drawer convert n of item1 into 1 of item2.") + .defineList("compactingRules", test, obj -> CompTierRegistry.validateRuleSyntax((String)obj)); builder.pop(); } + /*cache.compRules = config.getStringList("compactingRules", sectionRegistries.getQualifiedName(), new String[] { "minecraft:clay, minecraft:clay_ball, 4" }, "Items should be in form domain:item or domain:item:meta.", null, LANG_PREFIX + "registries.compRules"); + if (StorageDrawers.compRegistry != null) { + for (String rule : cache.compRules) + StorageDrawers.compRegistry.register(rule); + }*/ + public int getBaseStackStorage() { if (!isLoaded()) return 1; @@ -84,7 +112,7 @@ public static class Upgrades { public final ForgeConfigSpec.ConfigValue level5Mult; public Upgrades (ForgeConfigSpec.Builder builder) { - builder.push("Storage Upgrades"); + builder.push("StorageUpgrades"); builder.comment("Storage upgrades multiply storage capacity by the given amount.", "When multiple storage upgrades are used together, their multipliers are added before being applied."); diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/config/CompTierRegistry.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/config/CompTierRegistry.java index 3cd93f590..068aa84e9 100644 --- a/src/main/java/com/jaquadro/minecraft/storagedrawers/config/CompTierRegistry.java +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/config/CompTierRegistry.java @@ -1,5 +1,6 @@ package com.jaquadro.minecraft.storagedrawers.config; +import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import net.minecraft.block.Blocks; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -53,11 +54,7 @@ public void initialize () { } } - // TODO: Configurable compacting rules - //if (StorageDrawers.config.cache.compRules != null) { - // for (String rule : StorageDrawers.config.cache.compRules) - // register(rule); - //} + CommonConfig.onLoad(() -> CommonConfig.GENERAL.compRules.get().forEach(this::register)); for (String rule : pendingRules) { register(rule); @@ -79,9 +76,34 @@ public boolean register (@Nonnull ItemStack upper, @Nonnull ItemStack lower, int records.add(r); + StorageDrawers.log.info("New compacting rule " + convRate + " " + lower.getItem().toString() + " = 1 " + upper.getItem().toString()); + return true; } + public static boolean validateRuleSyntax (String rule) { + String[] parts = rule.split("\\s*,\\s*"); + if (parts.length != 3) + return false; + + ResourceLocation upperResource = ResourceLocation.tryCreate(parts[0]); + ResourceLocation lowerResource = ResourceLocation.tryCreate(parts[1]); + if (upperResource == null || lowerResource == null) + return false; + + try { + int conv = Integer.parseInt(parts[2]); + return conv >= 1; + } + catch (NumberFormatException e) { + return false; + } + } + + public void register (List rules) { + rules.forEach(this::register); + } + public boolean register (String rule) { if (!initialized) { pendingRules.add(rule);