Merge pull request #2845 from quat1024/nbt_fix_uwu

Change how NBT matching works, now it's faster and works better with nested tags
This commit is contained in:
Vincent Lee authored and GitHub committed 2019-02-12 00:12:37 -06:00
commit d357279ca4
2 files changed
+37 -13

No files matched your search

@@ -15,6 +15,7 @@ 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;
import java.util.ArrayList;
import java.util.List;
@@ -79,19 +80,7 @@ public class RecipePetals {
}
private boolean compareStacks(ItemStack recipe, ItemStack supplied) {
if(recipe.getItem() == supplied.getItem() && recipe.getItemDamage() == supplied.getItemDamage()) {
//check that the user supplied nbt tag is a superset of the recipe item nbt tag
//if the recipe doesn't have an NBT tag, the user supplied one doesn't matter, it is a superset
if(!recipe.hasTagCompound()) return true;
//if the recipe does have an NBT tag but the user supplied doesn't, also no way it's a superset
if(!supplied.hasTagCompound()) return false;
NBTTagCompound mergedNBT = supplied.getTagCompound().copy();
mergedNBT.merge(recipe.getTagCompound());
return supplied.getTagCompound().equals(mergedNBT);
}
return false;
return recipe.getItem() == supplied.getItem() && recipe.getItemDamage() == supplied.getItemDamage() && ItemNBTHelper.isTagSubset(recipe.getTagCompound(), supplied.getTagCompound());
}
public List<Object> getInputs() {
@@ -14,9 +14,12 @@
package vazkii.botania.common.core.helper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import javax.annotation.Nullable;
public final class ItemNBTHelper {
private static final int[] EMPTY_INT_ARRAY = new int[0];
@@ -132,4 +135,36 @@ public final class ItemNBTHelper {
return verifyExistance(stack, tag) ? getNBT(stack).getTagList(tag, objtype) : nullifyOnFail ? null : new NBTTagList();
}
/**
* 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.
*/
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;
//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;
}
}
return true;
}
}