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.
This commit is contained in:
David Vierra authored and Vincent Lee committed 2019-04-11 20:38:49 -05:00
1 parent 4f6f712c67
commit fdc957c87f
2 files changed
+31 -27

No files matched your search

@@ -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<Object> getInputs() {
@@ -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;
}
}