From f1f8a3121d4a0b6a83dc14eeb9fd3c1819af48be Mon Sep 17 00:00:00 2001 From: LLytho <29131229+LLytho@users.noreply.github.com> Date: Tue, 12 Jul 2022 12:59:28 +0200 Subject: [PATCH] Fix tag replacement --- .../unified/AlmostUnifiedRuntime.java | 11 +++-- .../unified/utils/ReplacementMap.java | 43 +++++++------------ .../almostreliable/unified/utils/TagMap.java | 8 +++- .../com/almostreliable/unified/TestUtils.java | 2 - .../unified/recipe/RecipeContextImplTest.java | 6 --- .../unified/utils/ReplacementMapTests.java | 4 +- 6 files changed, 30 insertions(+), 44 deletions(-) diff --git a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java index f84d720..58e1b2c 100644 --- a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java +++ b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java @@ -6,9 +6,11 @@ import com.almostreliable.unified.recipe.RecipeTransformer; import com.almostreliable.unified.recipe.handler.RecipeHandlerFactory; import com.almostreliable.unified.utils.ReplacementMap; import com.almostreliable.unified.utils.TagMap; +import com.almostreliable.unified.utils.UnifyTag; import com.google.gson.JsonElement; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagManager; +import net.minecraft.world.item.Item; import javax.annotation.Nullable; import java.util.ArrayList; @@ -31,8 +33,9 @@ public abstract class AlmostUnifiedRuntime { config.load(); modPriorities = config.getModPriorities(); onRun(); - TagMap tagMap = createTagMap(); - ReplacementMap replacementMap = new ReplacementMap(tagMap, config.getAllowedTags(), modPriorities); + List> allowedTags = config.getAllowedTags(); + TagMap tagMap = createTagMap(allowedTags); + ReplacementMap replacementMap = new ReplacementMap(tagMap, modPriorities); RecipeTransformer transformer = new RecipeTransformer(recipeHandlerFactory, replacementMap); RecipeTransformationResult result = transformer.transformRecipes(recipes); new RecipeDumper(result).dump(); @@ -42,12 +45,12 @@ public abstract class AlmostUnifiedRuntime { this.tagManager = tagManager; } - protected TagMap createTagMap() { + protected TagMap createTagMap(List> allowedTags) { if (tagManager == null) { throw new IllegalStateException("Internal error. TagManager was not updated correctly"); } - return TagMap.create(tagManager); + return TagMap.create(tagManager, allowedTags::contains); } protected abstract void onRun(); diff --git a/Common/src/main/java/com/almostreliable/unified/utils/ReplacementMap.java b/Common/src/main/java/com/almostreliable/unified/utils/ReplacementMap.java index 7cdbc47..6d2af1e 100644 --- a/Common/src/main/java/com/almostreliable/unified/utils/ReplacementMap.java +++ b/Common/src/main/java/com/almostreliable/unified/utils/ReplacementMap.java @@ -4,51 +4,40 @@ import com.almostreliable.unified.AlmostUnified; import com.almostreliable.unified.api.recipe.ReplacementFallbackStrategy; import com.almostreliable.unified.recipe.fallbacks.StoneStrataFallbackStrategy; import net.minecraft.resources.ResourceLocation; -import net.minecraft.tags.TagKey; import net.minecraft.world.item.Item; import javax.annotation.Nullable; import java.util.Collection; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class ReplacementMap { private final Collection modPriorities; private final TagMap tagMap; - private final Map> itemToTagMapping; // TODO - In the future this may be a list of multiple fallbacks. private final ReplacementFallbackStrategy fallbackStrategy = new StoneStrataFallbackStrategy(); - public ReplacementMap(TagMap tagMap, List> allowedTags, List modPriorities) { + public ReplacementMap(TagMap tagMap, List modPriorities) { this.tagMap = tagMap; this.modPriorities = modPriorities; - this.itemToTagMapping = createItemMapping(allowedTags); - } - - protected Map> createItemMapping(List> allowedTags) { - Map> itemToTagMapping = new HashMap<>(allowedTags.size()); - for (UnifyTag tag : allowedTags) { - Collection items = tagMap.getItems(tag); - for (ResourceLocation item : items) { - if (itemToTagMapping.containsKey(item)) { - AlmostUnified.LOG.warn("Item '{}' already has a tag '{}' for recipe replacement. Skipping this tag", - item, - tag); - continue; - } - - itemToTagMapping.put(item, tag); - } - } - - return itemToTagMapping; } @Nullable public UnifyTag getPreferredTag(ResourceLocation item) { - return itemToTagMapping.get(item); + Collection> tags = tagMap.getTags(item); + + if (tags.isEmpty()) { + return null; + } + + if (tags.size() > 1) { + AlmostUnified.LOG.warn( + "Item '{}' has multiple preferred tags '{}' for recipe replacement. This needs to be manually fixed by the user.", + item, + tags.stream().map(UnifyTag::location).toList()); + } + + return tags.iterator().next(); } @Nullable @@ -74,7 +63,7 @@ public class ReplacementMap { @Nullable public ResourceLocation getPreferredItemByTag(UnifyTag tag, @Nullable String ignoredNamespace) { for (String mod : modPriorities) { - if(mod.equals(ignoredNamespace)) { + if (mod.equals(ignoredNamespace)) { return null; } diff --git a/Common/src/main/java/com/almostreliable/unified/utils/TagMap.java b/Common/src/main/java/com/almostreliable/unified/utils/TagMap.java index 0ba2095..eeb3549 100644 --- a/Common/src/main/java/com/almostreliable/unified/utils/TagMap.java +++ b/Common/src/main/java/com/almostreliable/unified/utils/TagMap.java @@ -5,11 +5,11 @@ import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.Tag; -import net.minecraft.tags.TagKey; import net.minecraft.tags.TagManager; import net.minecraft.world.item.Item; import java.util.*; +import java.util.function.Predicate; public class TagMap { private final Map, Set> tagsToItems = new HashMap<>(); @@ -17,7 +17,7 @@ public class TagMap { protected TagMap() {} - public static TagMap create(TagManager tagManager) { + public static TagMap create(TagManager tagManager, Predicate> filter) { Objects.requireNonNull(tagManager, "Requires a non-null tag manager"); var tags = tagManager @@ -32,6 +32,10 @@ public class TagMap { for (var entry : tags.entrySet()) { UnifyTag tag = UnifyTag.item(entry.getKey()); + if (!filter.test(tag)) { + continue; + } + Tag> holderTag = entry.getValue(); for (Holder holder : holderTag.getValues()) { diff --git a/Common/src/test/java/com/almostreliable/unified/TestUtils.java b/Common/src/test/java/com/almostreliable/unified/TestUtils.java index eeb22fc..b55be58 100644 --- a/Common/src/test/java/com/almostreliable/unified/TestUtils.java +++ b/Common/src/test/java/com/almostreliable/unified/TestUtils.java @@ -5,7 +5,6 @@ import com.almostreliable.unified.recipe.handler.RecipeHandlerFactory; import com.almostreliable.unified.utils.ReplacementMap; import com.almostreliable.unified.utils.TagMapTests; import com.almostreliable.unified.utils.UnifyTag; -import com.google.gson.Gson; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; @@ -67,7 +66,6 @@ public class TestUtils { public static RecipeTransformer basicTransformer(Consumer consumer) { ReplacementMap map = new ReplacementMap(TagMapTests.testTagMap(), - TestUtils.TEST_ALLOWED_TAGS, TestUtils.TEST_MOD_PRIORITIES); RecipeHandlerFactory factory = new RecipeHandlerFactory(); consumer.accept(factory); diff --git a/Common/src/test/java/com/almostreliable/unified/recipe/RecipeContextImplTest.java b/Common/src/test/java/com/almostreliable/unified/recipe/RecipeContextImplTest.java index 230d118..d9667b1 100644 --- a/Common/src/test/java/com/almostreliable/unified/recipe/RecipeContextImplTest.java +++ b/Common/src/test/java/com/almostreliable/unified/recipe/RecipeContextImplTest.java @@ -1,17 +1,12 @@ package com.almostreliable.unified.recipe; import com.almostreliable.unified.TestUtils; -import com.almostreliable.unified.utils.JsonQuery; import com.almostreliable.unified.utils.ReplacementMap; import com.almostreliable.unified.utils.TagMapTests; import com.google.gson.Gson; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import net.minecraft.resources.ResourceLocation; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.assertNotEquals; public class RecipeContextImplTest { @@ -28,7 +23,6 @@ public class RecipeContextImplTest { public void depthReplace_MekaTest() { JsonObject json = new Gson().fromJson(mekaTest, JsonObject.class); ReplacementMap map = new ReplacementMap(TagMapTests.testTagMap(), - TestUtils.TEST_ALLOWED_TAGS, TestUtils.TEST_MOD_PRIORITIES); // RecipeContextImpl context = new RecipeContextImpl(new ResourceLocation("test"), json, map); // JsonElement result = context.createResultReplacement(json.getAsJsonObject("output")); diff --git a/Common/src/test/java/com/almostreliable/unified/utils/ReplacementMapTests.java b/Common/src/test/java/com/almostreliable/unified/utils/ReplacementMapTests.java index cd4c704..676d417 100644 --- a/Common/src/test/java/com/almostreliable/unified/utils/ReplacementMapTests.java +++ b/Common/src/test/java/com/almostreliable/unified/utils/ReplacementMapTests.java @@ -14,7 +14,6 @@ public class ReplacementMapTests { @Test public void getPreferredItemByTag() { ReplacementMap map = new ReplacementMap(TagMapTests.testTagMap(), - TestUtils.TEST_ALLOWED_TAGS, TestUtils.TEST_MOD_PRIORITIES); assertEquals(map.getPreferredItemByTag(TestUtils.BRONZE_ORES_TAG), TestUtils.mod1RL("bronze_ore")); assertNotEquals(map.getPreferredItemByTag(TestUtils.BRONZE_ORES_TAG), TestUtils.mod2RL("bronze_ore")); @@ -30,7 +29,7 @@ public class ReplacementMapTests { public void getPreferredItemByTag_ReversePriority() { // We reverse the order. See `testTagMap` for the mapping. List reverse = Lists.reverse(TestUtils.TEST_MOD_PRIORITIES); - ReplacementMap reverseMap = new ReplacementMap(TagMapTests.testTagMap(), TestUtils.TEST_ALLOWED_TAGS, reverse); + ReplacementMap reverseMap = new ReplacementMap(TagMapTests.testTagMap(), reverse); assertEquals(reverseMap.getPreferredItemByTag(TestUtils.BRONZE_ORES_TAG), TestUtils.mod3RL("bronze_ore")); assertEquals(reverseMap.getPreferredItemByTag(TestUtils.INVAR_ORES_TAG), TestUtils.mod4RL("invar_ore")); assertEquals(reverseMap.getPreferredItemByTag(TestUtils.TIN_ORES_TAG), TestUtils.mod4RL("tin_ore")); @@ -40,7 +39,6 @@ public class ReplacementMapTests { @Test public void getPreferredTag() { ReplacementMap map = new ReplacementMap(TagMapTests.testTagMap(), - TestUtils.TEST_ALLOWED_TAGS, TestUtils.TEST_MOD_PRIORITIES); assertEquals(map.getPreferredTag(TestUtils.mod1RL("bronze_ore")), TestUtils.BRONZE_ORES_TAG);