Skip to content

Commit

Permalink
Re-add config support for custom compacting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jaquadro committed Sep 17, 2019
1 parent 2d3059d commit 3406cea
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -11,14 +15,25 @@ public final class CommonConfig
public static final ForgeConfigSpec spec = BUILDER.build();

private static boolean loaded = false;
private static List<Runnable> loadActions = new ArrayList<>();

public static void setLoaded() {
if (!loaded)
loadActions.forEach(Runnable::run);
loaded = true;
}

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<Integer> baseStackStorage;
public final ForgeConfigSpec.ConfigValue<Boolean> enableUI;
Expand All @@ -28,9 +43,12 @@ public static class General {
public final ForgeConfigSpec.ConfigValue<Boolean> debugTrace;
public final ForgeConfigSpec.ConfigValue<Boolean> enableExtraCompactingRules;
public final ForgeConfigSpec.ConfigValue<Integer> controllerRange;
public final ForgeConfigSpec.ConfigValue<List<? extends String>> compRules;

public General(ForgeConfigSpec.Builder builder) {
builder.push("General");
List<String> 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.",
Expand All @@ -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;
Expand Down Expand Up @@ -84,7 +112,7 @@ public static class Upgrades {
public final ForgeConfigSpec.ConfigValue<Integer> 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.");

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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<String> rules) {
rules.forEach(this::register);
}

public boolean register (String rule) {
if (!initialized) {
pendingRules.add(rule);
Expand Down

0 comments on commit 3406cea

Please sign in to comment.