Last active
February 12, 2019 03:38
-
-
Save quat1024/9760226bf2f9efa93f18469f2dc415a7 to your computer and use it in GitHub Desktop.
matching nbt tags w/ recipe items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static boolean isTagSubset(@Nullable NBTTagCompound recipeTag, @Nullable NBTTagCompound suppliedTag) { | |
//check that the user supplied item has, at least, the same nbt tags as the recipe item | |
//it is OK if the supplied item has *more* tags - however all the tags that *are* specified | |
//in the recipe item must match | |
//check easy cases | |
//if there's no specified recipe nbt tag, anything goes | |
if(recipeTag == null || recipeTag.isEmpty()) return true; | |
//if the recipe has nbt but the supplied item doesn't, there's no way it can match | |
if(suppliedTag == null || suppliedTag.isEmpty()) return false; | |
//if the recipe simply has more nbt tags than the supplied item, there's no way it can match | |
if(recipeTag.getKeySet().size() > suppliedTag.getKeySet().size()) return false; | |
//it's not an easy case, so we actually have to check the contents of each tag | |
for(String key : suppliedTag.getKeySet()) { | |
//it's ok if the recipe is missing a key from the supplied item | |
if(!recipeTag.hasKey(key)) continue; | |
NBTBase suppliedEntry = suppliedTag.getTag(key); | |
NBTBase recipeEntry = recipeTag.getTag(key); | |
//if a value is present on both tags but they do not match, fail | |
if(suppliedEntry instanceof NBTTagCompound && recipeEntry instanceof NBTTagCompound) { | |
//recurse into tag compounds (see issue #42) | |
if(!isTagSubset((NBTTagCompound) suppliedEntry, (NBTTagCompound) recipeEntry)) return false; | |
} else { | |
if(!suppliedEntry.equals(recipeEntry)) return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment