mirror of
https://github.com/AlmostReliable/almostunified.git
synced 2024-11-28 10:35:38 -05:00
Fix re-loadable configs & replacement lookup
This commit is contained in:
parent
e505e94953
commit
977273241a
3 changed files with 65 additions and 38 deletions
|
@ -44,8 +44,6 @@ public class AlmostUnifiedRuntime {
|
||||||
int transformedRecipes = 0;
|
int transformedRecipes = 0;
|
||||||
int transformedPropertiesInRecipes = 0;
|
int transformedPropertiesInRecipes = 0;
|
||||||
long start = System.nanoTime();
|
long start = System.nanoTime();
|
||||||
StopWatch stopWatch = new StopWatch();
|
|
||||||
stopWatch.start();
|
|
||||||
for (var entry : recipes.entrySet()) {
|
for (var entry : recipes.entrySet()) {
|
||||||
if (entry.getValue() instanceof JsonObject json) {
|
if (entry.getValue() instanceof JsonObject json) {
|
||||||
int changes = transformRecipe(json, helper);
|
int changes = transformRecipe(json, helper);
|
||||||
|
@ -57,7 +55,6 @@ public class AlmostUnifiedRuntime {
|
||||||
}
|
}
|
||||||
long finish = System.nanoTime();
|
long finish = System.nanoTime();
|
||||||
long timeElapsed = finish - start;
|
long timeElapsed = finish - start;
|
||||||
stopWatch.stop();
|
|
||||||
AlmostUnified.LOG.info("Transformed {}/{} recipes with {} changes in {}ms",
|
AlmostUnified.LOG.info("Transformed {}/{} recipes with {} changes in {}ms",
|
||||||
transformedRecipes,
|
transformedRecipes,
|
||||||
recipes.size(),
|
recipes.size(),
|
||||||
|
|
|
@ -6,9 +6,7 @@ import net.minecraft.core.Registry;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class LookupByModPriority implements ReplacementLookupHelper {
|
public class LookupByModPriority implements ReplacementLookupHelper {
|
||||||
private final Collection<String> modPriorities;
|
private final Collection<String> modPriorities;
|
||||||
|
@ -19,6 +17,7 @@ public class LookupByModPriority implements ReplacementLookupHelper {
|
||||||
* Cache for replacements. Key is the item to replace, value is the replacement.
|
* Cache for replacements. Key is the item to replace, value is the replacement.
|
||||||
*/
|
*/
|
||||||
private final Map<ResourceLocation, ResourceLocation> replacementCache = new HashMap<>();
|
private final Map<ResourceLocation, ResourceLocation> replacementCache = new HashMap<>();
|
||||||
|
private final Set<ResourceLocation> invalidCache = new HashSet<>();
|
||||||
|
|
||||||
public LookupByModPriority(Map<ResourceLocation, ResourceLocation> itemToTag, Multimap<ResourceLocation, ResourceLocation> tagToItem, Collection<String> modPriorities) {
|
public LookupByModPriority(Map<ResourceLocation, ResourceLocation> itemToTag, Multimap<ResourceLocation, ResourceLocation> tagToItem, Collection<String> modPriorities) {
|
||||||
this.itemToTag = itemToTag;
|
this.itemToTag = itemToTag;
|
||||||
|
@ -30,22 +29,32 @@ public class LookupByModPriority implements ReplacementLookupHelper {
|
||||||
@Override
|
@Override
|
||||||
public String findReplacement(String id) {
|
public String findReplacement(String id) {
|
||||||
ResourceLocation asLocation = new ResourceLocation(id);
|
ResourceLocation asLocation = new ResourceLocation(id);
|
||||||
if (!Registry.ITEM.containsKey(asLocation)) {
|
if(invalidCache.contains(asLocation)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResourceLocation tagRL = itemToTag.get(asLocation);
|
ResourceLocation replacement = replacementCache.get(asLocation);
|
||||||
if (tagRL == null) {
|
if(replacement != null) {
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
ResourceLocation replacement = replacementCache.computeIfAbsent(
|
|
||||||
asLocation,
|
|
||||||
key -> computeReplacement(key, tagToItem.get(tagRL))
|
|
||||||
);
|
|
||||||
return replacement.toString();
|
return replacement.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ResourceLocation tagRL = itemToTag.get(asLocation);
|
||||||
|
if (!Registry.ITEM.containsKey(asLocation) || tagRL == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
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<ResourceLocation> items) {
|
private ResourceLocation computeReplacement(ResourceLocation toReplace, Collection<ResourceLocation> items) {
|
||||||
for (String mod : modPriorities) {
|
for (String mod : modPriorities) {
|
||||||
for (ResourceLocation item : items) {
|
for (ResourceLocation item : items) {
|
||||||
|
@ -55,6 +64,6 @@ public class LookupByModPriority implements ReplacementLookupHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return toReplace;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,12 @@ package com.almostreliable.unified;
|
||||||
import com.electronwill.nightconfig.core.Config;
|
import com.electronwill.nightconfig.core.Config;
|
||||||
import com.electronwill.nightconfig.core.ConfigSpec;
|
import com.electronwill.nightconfig.core.ConfigSpec;
|
||||||
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
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.HashMultimap;
|
||||||
import com.google.common.collect.Multimap;
|
import com.google.common.collect.Multimap;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class ModConfig {
|
public class ModConfig {
|
||||||
|
@ -99,24 +101,15 @@ public class ModConfig {
|
||||||
"rods",
|
"rods",
|
||||||
"storage_blocks");
|
"storage_blocks");
|
||||||
private static final List<String> DEFAULT_UNIFIES = getDefaultPatterns();
|
private static final List<String> DEFAULT_UNIFIES = getDefaultPatterns();
|
||||||
private final CommentedFileConfig config;
|
private final String name;
|
||||||
|
|
||||||
|
@Nullable private FileConfig currentConfig;
|
||||||
private final ConfigSpec spec;
|
private final ConfigSpec spec;
|
||||||
|
|
||||||
public ModConfig(String name) {
|
public ModConfig(String name) {
|
||||||
config = CommentedFileConfig.ofConcurrent(AlmostUnifiedPlatform.INSTANCE.getConfigPath().resolve(name + ".toml"));
|
this.name = name;
|
||||||
|
|
||||||
spec = new ConfigSpec();
|
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_MOD_PRIORITIES, () -> DEFAULT_MOD_PRIORITIES, o -> o instanceof String);
|
||||||
spec.defineList(UNIFICATION_VARIABLES_MATERIALS, () -> DEFAULT_METALS, 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);
|
spec.defineList(UNIFICATION_VARIABLES_TYPES, () -> DEFAULT_TYPES, o -> o instanceof String);
|
||||||
|
@ -131,26 +124,50 @@ public class ModConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load() {
|
protected FileConfig createConfig() {
|
||||||
config.load();
|
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;
|
||||||
AlmostUnified.LOG.warn("Config has missing or invalid values - correcting now");
|
|
||||||
spec.correct(config);
|
|
||||||
config.save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config.close();
|
public void load() {
|
||||||
|
currentConfig = createConfig();
|
||||||
|
currentConfig.load();
|
||||||
|
|
||||||
|
if (!spec.isCorrect(currentConfig)) {
|
||||||
|
AlmostUnified.LOG.warn("Config has missing or invalid values - correcting now");
|
||||||
|
spec.correct(currentConfig);
|
||||||
|
currentConfig.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
currentConfig.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getModPriorities() {
|
public List<String> 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<ResourceLocation> getAllowedTags() {
|
public List<ResourceLocation> getAllowedTags() {
|
||||||
|
if(currentConfig == null) {
|
||||||
|
throw new IllegalStateException("Config is not loaded");
|
||||||
|
}
|
||||||
|
|
||||||
Multimap<String, String> variables = compileVariables();
|
Multimap<String, String> variables = compileVariables();
|
||||||
List<ResourceLocation> collectedPattern = new ArrayList<>();
|
List<ResourceLocation> collectedPattern = new ArrayList<>();
|
||||||
Collection<String> patterns = config.get(UNIFICATION_PATTERN);
|
Collection<String> patterns = currentConfig.get(UNIFICATION_PATTERN);
|
||||||
|
|
||||||
for (String pattern : patterns) {
|
for (String pattern : patterns) {
|
||||||
Collection<String> compiledPattern = compilePattern(pattern, variables);
|
Collection<String> compiledPattern = compilePattern(pattern, variables);
|
||||||
|
@ -208,13 +225,17 @@ public class ModConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Multimap<String, String> compileVariables() {
|
private Multimap<String, String> compileVariables() {
|
||||||
|
if(currentConfig == null) {
|
||||||
|
throw new IllegalStateException("Config is not loaded");
|
||||||
|
}
|
||||||
|
|
||||||
Multimap<String, String> computedVariables = HashMultimap.create();
|
Multimap<String, String> computedVariables = HashMultimap.create();
|
||||||
|
|
||||||
if (!config.contains(UNIFICATION_VARIABLES)) {
|
if (!currentConfig.contains(UNIFICATION_VARIABLES)) {
|
||||||
return computedVariables;
|
return computedVariables;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.get(UNIFICATION_VARIABLES) instanceof Config variables) {
|
if (currentConfig.get(UNIFICATION_VARIABLES) instanceof Config variables) {
|
||||||
Map<String, Object> values = variables.valueMap();
|
Map<String, Object> values = variables.valueMap();
|
||||||
values.forEach((k, v) -> {
|
values.forEach((k, v) -> {
|
||||||
if (v instanceof Collection<?> asCollection) {
|
if (v instanceof Collection<?> asCollection) {
|
||||||
|
|
Loading…
Reference in a new issue