From 977273241a93b550d41670060a6312f35d89877a Mon Sep 17 00:00:00 2001 From: LLytho <29131229+LLytho@users.noreply.github.com> Date: Fri, 17 Jun 2022 16:03:36 +0200 Subject: [PATCH] Fix re-loadable configs & replacement lookup --- .../unified/AlmostUnifiedRuntime.java | 3 - .../unified/LookupByModPriority.java | 31 ++++++--- .../com/almostreliable/unified/ModConfig.java | 69 ++++++++++++------- 3 files changed, 65 insertions(+), 38 deletions(-) diff --git a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java index c36f687..26e17ef 100644 --- a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java +++ b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java @@ -44,8 +44,6 @@ public class AlmostUnifiedRuntime { int transformedRecipes = 0; int transformedPropertiesInRecipes = 0; long start = System.nanoTime(); - StopWatch stopWatch = new StopWatch(); - stopWatch.start(); for (var entry : recipes.entrySet()) { if (entry.getValue() instanceof JsonObject json) { int changes = transformRecipe(json, helper); @@ -57,7 +55,6 @@ public class AlmostUnifiedRuntime { } long finish = System.nanoTime(); long timeElapsed = finish - start; - stopWatch.stop(); AlmostUnified.LOG.info("Transformed {}/{} recipes with {} changes in {}ms", transformedRecipes, recipes.size(), diff --git a/Common/src/main/java/com/almostreliable/unified/LookupByModPriority.java b/Common/src/main/java/com/almostreliable/unified/LookupByModPriority.java index 92d9dc3..ffbd46a 100644 --- a/Common/src/main/java/com/almostreliable/unified/LookupByModPriority.java +++ b/Common/src/main/java/com/almostreliable/unified/LookupByModPriority.java @@ -6,9 +6,7 @@ import net.minecraft.core.Registry; import net.minecraft.resources.ResourceLocation; import javax.annotation.Nullable; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.util.*; public class LookupByModPriority implements ReplacementLookupHelper { private final Collection modPriorities; @@ -19,6 +17,7 @@ public class LookupByModPriority implements ReplacementLookupHelper { * Cache for replacements. Key is the item to replace, value is the replacement. */ private final Map replacementCache = new HashMap<>(); + private final Set invalidCache = new HashSet<>(); public LookupByModPriority(Map itemToTag, Multimap tagToItem, Collection modPriorities) { this.itemToTag = itemToTag; @@ -30,22 +29,32 @@ public class LookupByModPriority implements ReplacementLookupHelper { @Override public String findReplacement(String id) { ResourceLocation asLocation = new ResourceLocation(id); - if (!Registry.ITEM.containsKey(asLocation)) { + if(invalidCache.contains(asLocation)) { return null; } + ResourceLocation replacement = replacementCache.get(asLocation); + if(replacement != null) { + return replacement.toString(); + } + ResourceLocation tagRL = itemToTag.get(asLocation); - if (tagRL == null) { + if (!Registry.ITEM.containsKey(asLocation) || tagRL == null) { return null; } - ResourceLocation replacement = replacementCache.computeIfAbsent( - asLocation, - key -> computeReplacement(key, tagToItem.get(tagRL)) - ); - return replacement.toString(); + ResourceLocation modReplacement = computeReplacement(asLocation, tagToItem.get(tagRL)); + if(modReplacement == null || modReplacement.equals(asLocation)) { + invalidCache.add(asLocation); + return null; + } + + replacementCache.put(asLocation, modReplacement); + AlmostUnified.LOG.info("########### {} -> {}", id, modReplacement.toString()); + return modReplacement.toString(); } + @Nullable private ResourceLocation computeReplacement(ResourceLocation toReplace, Collection items) { for (String mod : modPriorities) { for (ResourceLocation item : items) { @@ -55,6 +64,6 @@ public class LookupByModPriority implements ReplacementLookupHelper { } } - return toReplace; + return null; } } diff --git a/Common/src/main/java/com/almostreliable/unified/ModConfig.java b/Common/src/main/java/com/almostreliable/unified/ModConfig.java index caa6f56..9aa7ee2 100644 --- a/Common/src/main/java/com/almostreliable/unified/ModConfig.java +++ b/Common/src/main/java/com/almostreliable/unified/ModConfig.java @@ -3,10 +3,12 @@ package com.almostreliable.unified; import com.electronwill.nightconfig.core.Config; import com.electronwill.nightconfig.core.ConfigSpec; import com.electronwill.nightconfig.core.file.CommentedFileConfig; +import com.electronwill.nightconfig.core.file.FileConfig; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import net.minecraft.resources.ResourceLocation; +import javax.annotation.Nullable; import java.util.*; public class ModConfig { @@ -99,24 +101,15 @@ public class ModConfig { "rods", "storage_blocks"); private static final List DEFAULT_UNIFIES = getDefaultPatterns(); - private final CommentedFileConfig config; + private final String name; + + @Nullable private FileConfig currentConfig; private final ConfigSpec spec; public ModConfig(String name) { - config = CommentedFileConfig.ofConcurrent(AlmostUnifiedPlatform.INSTANCE.getConfigPath().resolve(name + ".toml")); + this.name = name; + spec = new ConfigSpec(); - - config.setComment(UNIFICATION_MOD_PRIORITIES, "Mod priorities for unification's"); - config.setComment(UNIFICATION_VARIABLES, - "Custom variables can be defined here. Look at the example for more info.\n" + - "They will be used in `resources.unify` to determine which tags or items should be unified."); - config.setComment(UNIFICATION_VARIABLES_MATERIALS, "List of materials to unify"); - config.setComment(UNIFICATION_VARIABLES_TYPES, "List of types to unify"); - config.setComment(UNIFICATION_PATTERN, """ - Define how the pattern for unification's should work. - - Using `{variable_names}`, will replace the values from `resources.variable_names` into the pattern. - - If the pattern starts with `!`, it will be ignored."""); - spec.defineList(UNIFICATION_MOD_PRIORITIES, () -> DEFAULT_MOD_PRIORITIES, o -> o instanceof String); spec.defineList(UNIFICATION_VARIABLES_MATERIALS, () -> DEFAULT_METALS, o -> o instanceof String); spec.defineList(UNIFICATION_VARIABLES_TYPES, () -> DEFAULT_TYPES, o -> o instanceof String); @@ -131,26 +124,50 @@ public class ModConfig { } } - public void load() { - config.load(); + protected FileConfig createConfig() { + CommentedFileConfig config = CommentedFileConfig.ofConcurrent(AlmostUnifiedPlatform.INSTANCE.getConfigPath().resolve(name + ".toml")); + config.setComment(UNIFICATION_MOD_PRIORITIES, "Mod priorities for unification's"); + config.setComment(UNIFICATION_VARIABLES, + "Custom variables can be defined here. Look at the example for more info.\n" + + "They will be used in `resources.unify` to determine which tags or items should be unified."); + config.setComment(UNIFICATION_VARIABLES_MATERIALS, "List of materials to unify"); + config.setComment(UNIFICATION_VARIABLES_TYPES, "List of types to unify"); + config.setComment(UNIFICATION_PATTERN, """ + Define how the pattern for unification's should work. + - Using `{variable_names}`, will replace the values from `resources.variable_names` into the pattern. + - If the pattern starts with `!`, it will be ignored."""); - if (!spec.isCorrect(config)) { + return config; + } + + public void load() { + currentConfig = createConfig(); + currentConfig.load(); + + if (!spec.isCorrect(currentConfig)) { AlmostUnified.LOG.warn("Config has missing or invalid values - correcting now"); - spec.correct(config); - config.save(); + spec.correct(currentConfig); + currentConfig.save(); } - config.close(); + currentConfig.close(); } public List getModPriorities() { - return config.get(UNIFICATION_MOD_PRIORITIES); + if(currentConfig == null) { + throw new IllegalStateException("Config is not loaded"); + } + return currentConfig.get(UNIFICATION_MOD_PRIORITIES); } public List getAllowedTags() { + if(currentConfig == null) { + throw new IllegalStateException("Config is not loaded"); + } + Multimap variables = compileVariables(); List collectedPattern = new ArrayList<>(); - Collection patterns = config.get(UNIFICATION_PATTERN); + Collection patterns = currentConfig.get(UNIFICATION_PATTERN); for (String pattern : patterns) { Collection compiledPattern = compilePattern(pattern, variables); @@ -208,13 +225,17 @@ public class ModConfig { } private Multimap compileVariables() { + if(currentConfig == null) { + throw new IllegalStateException("Config is not loaded"); + } + Multimap computedVariables = HashMultimap.create(); - if (!config.contains(UNIFICATION_VARIABLES)) { + if (!currentConfig.contains(UNIFICATION_VARIABLES)) { return computedVariables; } - if (config.get(UNIFICATION_VARIABLES) instanceof Config variables) { + if (currentConfig.get(UNIFICATION_VARIABLES) instanceof Config variables) { Map values = variables.valueMap(); values.forEach((k, v) -> { if (v instanceof Collection asCollection) {