mirror of
https://github.com/AlmostReliable/almostunified.git
synced 2024-11-14 19:25:13 -05:00
Fix tag replacement
This commit is contained in:
parent
be8a58f5d3
commit
f1f8a3121d
6 changed files with 30 additions and 44 deletions
|
@ -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<UnifyTag<Item>> 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<UnifyTag<Item>> 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();
|
||||
|
|
|
@ -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<String> modPriorities;
|
||||
private final TagMap tagMap;
|
||||
private final Map<ResourceLocation, UnifyTag<Item>> itemToTagMapping;
|
||||
// TODO - In the future this may be a list of multiple fallbacks.
|
||||
private final ReplacementFallbackStrategy fallbackStrategy = new StoneStrataFallbackStrategy();
|
||||
|
||||
public ReplacementMap(TagMap tagMap, List<UnifyTag<Item>> allowedTags, List<String> modPriorities) {
|
||||
public ReplacementMap(TagMap tagMap, List<String> modPriorities) {
|
||||
this.tagMap = tagMap;
|
||||
this.modPriorities = modPriorities;
|
||||
this.itemToTagMapping = createItemMapping(allowedTags);
|
||||
}
|
||||
|
||||
protected Map<ResourceLocation, UnifyTag<Item>> createItemMapping(List<UnifyTag<Item>> allowedTags) {
|
||||
Map<ResourceLocation, UnifyTag<Item>> itemToTagMapping = new HashMap<>(allowedTags.size());
|
||||
for (UnifyTag<Item> tag : allowedTags) {
|
||||
Collection<ResourceLocation> 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<Item> getPreferredTag(ResourceLocation item) {
|
||||
return itemToTagMapping.get(item);
|
||||
Collection<UnifyTag<Item>> 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<Item> tag, @Nullable String ignoredNamespace) {
|
||||
for (String mod : modPriorities) {
|
||||
if(mod.equals(ignoredNamespace)) {
|
||||
if (mod.equals(ignoredNamespace)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<UnifyTag<Item>, Set<ResourceLocation>> 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<UnifyTag<Item>> 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<Item> tag = UnifyTag.item(entry.getKey());
|
||||
if (!filter.test(tag)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Tag<? extends Holder<?>> holderTag = entry.getValue();
|
||||
|
||||
for (Holder<?> holder : holderTag.getValues()) {
|
||||
|
|
|
@ -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<RecipeHandlerFactory> consumer) {
|
||||
ReplacementMap map = new ReplacementMap(TagMapTests.testTagMap(),
|
||||
TestUtils.TEST_ALLOWED_TAGS,
|
||||
TestUtils.TEST_MOD_PRIORITIES);
|
||||
RecipeHandlerFactory factory = new RecipeHandlerFactory();
|
||||
consumer.accept(factory);
|
||||
|
|
|
@ -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"));
|
||||
|
|
|
@ -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<String> 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);
|
||||
|
|
Loading…
Reference in a new issue