add ItemStack to/from NBT helpers (#202)

to simplify the "stackSize" NBT tag workaround
This commit is contained in:
MycroftJr
2023-01-08 20:10:35 -08:00
committed by GitHub
parent c46eadec08
commit 662a7805dd
15 changed files with 89 additions and 69 deletions
@@ -0,0 +1,30 @@
package appeng.helpers;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
/**
* Methods to help with the added "stackSize" NBT tag to get around "Count" being written and read as a byte.
*/
public class ItemStackHelper {
public static ItemStack stackFromNBT(NBTTagCompound itemNBT) {
ItemStack is = new ItemStack(itemNBT);
if (itemNBT.hasKey("stackSize")) {
is.setCount(itemNBT.getInteger("stackSize"));
}
return is;
}
public static void stackWriteToNBT(ItemStack is, NBTTagCompound itemNBT) {
is.writeToNBT(itemNBT);
if (is.getCount() > Byte.MAX_VALUE) {
itemNBT.setInteger("stackSize", is.getCount());
}
}
public static NBTTagCompound stackToNBT(ItemStack is) {
NBTTagCompound itemNBT = new NBTTagCompound();
stackWriteToNBT(is, itemNBT);
return itemNBT;
}
}