From fdc957c87f49a669e63d432cb997f920bbb1c8fc Mon Sep 17 00:00:00 2001 From: David Vierra Date: Thu, 11 Apr 2019 15:38:49 -1000 Subject: [PATCH] When matching recipe tags, also match the first items of nested lists (#2881) Mostly useful when adding CraftTweaker recipes which have Forestry bees as inputs, since the bee's chromosomes are in a list and the recipe only needs to match some of the chromosomes. --- .../botania/api/recipe/RecipePetals.java | 3 +- .../common/core/helper/ItemNBTHelper.java | 55 ++++++++++--------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/main/java/vazkii/botania/api/recipe/RecipePetals.java b/src/main/java/vazkii/botania/api/recipe/RecipePetals.java index ce1b26735..7082d4f75 100644 --- a/src/main/java/vazkii/botania/api/recipe/RecipePetals.java +++ b/src/main/java/vazkii/botania/api/recipe/RecipePetals.java @@ -12,7 +12,6 @@ package vazkii.botania.api.recipe; import com.google.common.collect.ImmutableList; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.oredict.OreDictionary; import vazkii.botania.common.core.helper.ItemNBTHelper; @@ -80,7 +79,7 @@ public class RecipePetals { } private boolean compareStacks(ItemStack recipe, ItemStack supplied) { - return recipe.getItem() == supplied.getItem() && recipe.getItemDamage() == supplied.getItemDamage() && ItemNBTHelper.isTagSubset(recipe.getTagCompound(), supplied.getTagCompound()); + return recipe.getItem() == supplied.getItem() && recipe.getItemDamage() == supplied.getItemDamage() && ItemNBTHelper.matchTag(recipe.getTagCompound(), supplied.getTagCompound()); } public List getInputs() { diff --git a/src/main/java/vazkii/botania/common/core/helper/ItemNBTHelper.java b/src/main/java/vazkii/botania/common/core/helper/ItemNBTHelper.java index 569a65e87..420891892 100644 --- a/src/main/java/vazkii/botania/common/core/helper/ItemNBTHelper.java +++ b/src/main/java/vazkii/botania/common/core/helper/ItemNBTHelper.java @@ -136,35 +136,40 @@ public final class ItemNBTHelper { } /** - * Checks that one tag is a subset of another - that is, it's ok if the superset has more NBT keys than the subset, but the subset has to have at least all the same keys as the superset, and the values of the keys that *are* shared between the two must match. - * - * This is useful when, e.g. matching NBT tags in recipes. + * Returns true if the `target` tag contains all of the tags and values present in the `template` tag. Recurses into + * compound tags and matches all template keys and values; recurses into list tags and matches the template against + * the first elements of target. Empty lists and compounds in the template will match target lists and compounds of + * any size. */ - public static boolean isTagSubset(@Nullable NBTTagCompound subset, @Nullable NBTTagCompound superset) { - //an empty set is a subset of everything - if(subset == null || subset.isEmpty()) return true; - //an empty set is a superset of only another empty set (which was already checked above) - if(superset == null || superset.isEmpty()) return false; - //a subset can't be bigger than its superset - if(subset.getKeySet().size() > superset.getKeySet().size()) return false; + + public static boolean matchTag(@Nullable NBTBase template, @Nullable NBTBase target) { + if(template instanceof NBTTagCompound && target instanceof NBTTagCompound) { + return matchTagCompound((NBTTagCompound) template, (NBTTagCompound) target); + } else if(template instanceof NBTTagList && target instanceof NBTTagList) { + return matchTagList((NBTTagList) template, (NBTTagList) target); + } else { + return template == null || (target != null && target.equals(template)); + } + } + + private static boolean matchTagCompound(NBTTagCompound template, NBTTagCompound target) { + if(template.getSize() > target.getSize()) return false; - //it's not an easy case, so we actually have to check the contents of each tag - for(String key : superset.getKeySet()) { - //it's ok if the subset is missing a key from the superset - if(!subset.hasKey(key)) continue; - - NBTBase supersetEntry = superset.getTag(key); - NBTBase subsetEntry = subset.getTag(key); - - //if a value is present on both tags, but they do not match, fail - if(supersetEntry instanceof NBTTagCompound && subsetEntry instanceof NBTTagCompound) { - //recurse into tag compounds (this properly compares nested tag compounds) - if(!isTagSubset((NBTTagCompound) subsetEntry, (NBTTagCompound) supersetEntry)) return false; - } else { - if(!supersetEntry.equals(subsetEntry)) return false; - } + for(String key : template.getKeySet()) { + if (!matchTag(template.getTag(key), target.getTag(key))) return false; } return true; } + + private static boolean matchTagList(NBTTagList template, NBTTagList target) { + if (template.tagCount() > target.tagCount()) return false; + + for (int i = 0; i < template.tagCount(); i++) { + if (!matchTag(template.get(i), target.get(i))) return false; + } + + return true; + } + }