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
@@ -61,6 +61,7 @@ import java.util.List;
import java.util.*;
import static appeng.client.render.BlockPosHighlighter.hilightBlock;
import static appeng.helpers.ItemStackHelper.stackFromNBT;
public class GuiInterfaceConfigurationTerminal extends AEBaseGui implements IJEIGhostIngredients {
@@ -299,11 +300,7 @@ public class GuiInterfaceConfigurationTerminal extends AEBaseGui implements IJEI
for (int x = 0; x < current.getInventory().getSlots(); x++) {
final String which = Integer.toString(x);
if (invData.hasKey(which)) {
NBTTagCompound tag = invData.getCompoundTag(which);
current.getInventory().setStackInSlot(x, new ItemStack(tag));
if (tag.hasKey("stackSize")) {
current.getInventory().getStackInSlot(x).setCount(tag.getInteger("stackSize"));
}
current.getInventory().setStackInSlot(x, stackFromNBT(invData.getCompoundTag(which)));
}
}
} catch (final NumberFormatException ignored) {
@@ -52,6 +52,7 @@ import java.io.IOException;
import java.util.*;
import static appeng.client.render.BlockPosHighlighter.hilightBlock;
import static appeng.helpers.ItemStackHelper.stackFromNBT;
public class GuiInterfaceTerminal extends AEBaseGui {
@@ -321,7 +322,7 @@ public class GuiInterfaceTerminal extends AEBaseGui {
for (int x = 0; x < current.getInventory().getSlots(); x++) {
final String which = Integer.toString(x);
if (invData.hasKey(which)) {
current.getInventory().setStackInSlot(x, new ItemStack(invData.getCompoundTag(which)));
current.getInventory().setStackInSlot(x, stackFromNBT(invData.getCompoundTag(which)));
}
}
} catch (final NumberFormatException ignored) {
@@ -50,6 +50,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import static appeng.helpers.ItemStackHelper.stackWriteToNBT;
public final class ContainerInterfaceConfigurationTerminal extends AEBaseContainer {
@@ -310,10 +312,7 @@ public final class ContainerInterfaceConfigurationTerminal extends AEBaseContain
ItemHandlerUtil.setStackInSlot(inv.client, x + offset, is.isEmpty() ? ItemStack.EMPTY : is.copy());
if (!is.isEmpty()) {
is.writeToNBT(itemNBT);
if (is.getCount() > Byte.MAX_VALUE) {
itemNBT.setInteger("stackSize", is.getCount());
}
stackWriteToNBT(is, itemNBT);
}
tag.setTag(Integer.toString(x + offset), itemNBT);
@@ -57,6 +57,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import static appeng.helpers.ItemStackHelper.stackWriteToNBT;
public final class ContainerInterfaceTerminal extends AEBaseContainer {
@@ -345,7 +347,7 @@ public final class ContainerInterfaceTerminal extends AEBaseContainer {
ItemHandlerUtil.setStackInSlot(inv.client, x + offset, is.isEmpty() ? ItemStack.EMPTY : is.copy());
if (!is.isEmpty()) {
is.writeToNBT(itemNBT);
stackWriteToNBT(is, itemNBT);
}
tag.setTag(Integer.toString(x + offset), itemNBT);
@@ -38,6 +38,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static appeng.helpers.ItemStackHelper.stackWriteToNBT;
public abstract class ContainerPatternEncoder extends ContainerMEMonitorable implements IAEAppEngInventory, IOptionalSlotHost, IContainerCraftingPacket {
private final AbstractPartEncoder patternTerminal;
@@ -526,10 +528,7 @@ public abstract class ContainerPatternEncoder extends ContainerMEMonitorable imp
final NBTTagCompound c = new NBTTagCompound();
if (!i.isEmpty()) {
i.writeToNBT(c);
if (i.getCount() > Byte.MAX_VALUE) {
c.setInteger("stackSize", i.getCount());
}
stackWriteToNBT(i, c);
}
return c;
@@ -63,6 +63,8 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static appeng.helpers.ItemStackHelper.stackFromNBT;
public class PacketJEIRecipe extends AppEngPacket {
@@ -85,7 +87,7 @@ public class PacketJEIRecipe extends AppEngPacket {
if (list.tagCount() > 0) {
this.recipe.add(new ItemStack[list.tagCount()]);
for (int y = 0; y < list.tagCount(); y++) {
this.recipe.get(x)[y] = new ItemStack(list.getCompoundTagAt(y));
this.recipe.get(x)[y] = stackFromNBT(list.getCompoundTagAt(y));
}
} else {
this.recipe.add(emptyArray);
@@ -97,7 +99,7 @@ public class PacketJEIRecipe extends AppEngPacket {
final NBTTagList outputList = comp.getTagList("outputs", 10);
this.output = new ArrayList<>();
for (int z = 0; z < outputList.tagCount(); z++) {
this.output.add(new ItemStack(outputList.getCompoundTagAt(z)));
this.output.add(stackFromNBT(outputList.getCompoundTagAt(z)));
}
}
}
@@ -101,6 +101,8 @@ import net.minecraftforge.items.wrapper.RangedWrapper;
import javax.annotation.Nullable;
import java.util.*;
import static appeng.helpers.ItemStackHelper.*;
public class DualityInterface implements IGridTickable, IStorageMonitorable, IInventoryDestination, IAEAppEngInventory, IConfigManagerHost, ICraftingProvider, IUpgradeableHost {
public static final int NUMBER_OF_STORAGE_SLOTS = 9;
@@ -208,12 +210,8 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
final NBTTagList waitingToSend = new NBTTagList();
if (this.waitingToSend != null) {
for (final ItemStack is : this.waitingToSend) {
final NBTTagCompound item = new NBTTagCompound();
is.writeToNBT(item);
if (is.getCount() > Byte.MAX_VALUE) {
item.setInteger("stackSize", is.getCount());
}
waitingToSend.appendTag(item);
final NBTTagCompound itemNBT = stackToNBT(is);
waitingToSend.appendTag(itemNBT);
}
}
data.setTag("waitingToSend", waitingToSend);
@@ -225,12 +223,8 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
NBTTagList waitingListSided = new NBTTagList();
if (this.waitingToSendFacing.containsKey(s)) {
for (final ItemStack is : this.waitingToSendFacing.get(s)) {
final NBTTagCompound item = new NBTTagCompound();
is.writeToNBT(item);
if (is.getCount() > Byte.MAX_VALUE) {
item.setInteger("stackSize", is.getCount());
}
waitingListSided.appendTag(item);
final NBTTagCompound itemNBT = stackToNBT(is);
waitingListSided.appendTag(itemNBT);
}
sidedWaitList.setTag(s.name(), waitingListSided);
}
@@ -246,10 +240,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
for (int x = 0; x < waitingList.tagCount(); x++) {
final NBTTagCompound c = waitingList.getCompoundTagAt(x);
if (c != null) {
final ItemStack is = new ItemStack(c);
if (c.hasKey("stackSize")) {
is.setCount(c.getInteger("stackSize"));
}
final ItemStack is = stackFromNBT(c);
this.addToSendList(is);
}
}
@@ -264,10 +255,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
for (int x = 0; x < w.tagCount(); x++) {
final NBTTagCompound c = w.getCompoundTagAt(x);
if (c != null) {
final ItemStack is = new ItemStack(c);
if (c.hasKey("stackSize")) {
is.setCount(c.getInteger("stackSize"));
}
final ItemStack is = stackFromNBT(c);
this.addToSendListFacing(is, EnumFacing.getFront(s.getIndex()));
}
}
@@ -28,6 +28,8 @@ import net.minecraft.util.text.TextFormatting;
import java.util.ArrayList;
import java.util.List;
import static appeng.helpers.ItemStackHelper.stackFromNBT;
public class InvalidPatternHelper {
@@ -89,11 +91,14 @@ public class InvalidPatternHelper {
private final ItemStack stack;
public PatternIngredient(NBTTagCompound tag) {
this.stack = new ItemStack(tag);
this.stack = stackFromNBT(tag);
if (this.stack.isEmpty()) {
this.id = tag.getString("id");
this.count = tag.getByte("Count");
if (tag.hasKey("stackSize")) {
this.count = tag.getInteger("stackSize");
}
this.damage = Math.max(0, tag.getShort("Damage"));
}
}
@@ -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;
}
}
@@ -40,6 +40,8 @@ import net.minecraftforge.common.crafting.IShapedRecipe;
import java.util.*;
import static appeng.helpers.ItemStackHelper.stackFromNBT;
public class PatternHelper implements ICraftingPatternDetails, Comparable<PatternHelper> {
@@ -91,11 +93,7 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable<Patter
for (int x = 0; x < inTag.tagCount(); x++) {
NBTTagCompound ingredient = inTag.getCompoundTagAt(x);
final ItemStack gs = new ItemStack(ingredient);
if (ingredient.hasKey("stackSize")) {
gs.setCount(ingredient.getInteger("stackSize"));
}
final ItemStack gs = stackFromNBT(ingredient);
if (!ingredient.hasNoTags() && gs.isEmpty()) {
throw new IllegalArgumentException("No pattern here!");
@@ -126,11 +124,7 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable<Patter
for (int x = 0; x < outTag.tagCount(); x++) {
NBTTagCompound resultItemTag = outTag.getCompoundTagAt(x);
final ItemStack gs = new ItemStack(resultItemTag);
if (resultItemTag.hasKey("stackSize")) {
gs.setCount(resultItemTag.getInteger("stackSize"));
}
final ItemStack gs = stackFromNBT(resultItemTag);
if (!resultItemTag.hasNoTags() && gs.isEmpty()) {
throw new IllegalArgumentException("No pattern here!");
@@ -48,6 +48,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static appeng.helpers.ItemStackHelper.stackToNBT;
class RecipeTransferHandler<T extends Container> implements IRecipeTransferHandler<T> {
@@ -108,8 +110,7 @@ class RecipeTransferHandler<T extends Container> implements IRecipeTransferHandl
if (!ingredient.isInput()) {
ItemStack output = ingredient.getDisplayedIngredient();
if (output != null) {
final NBTTagCompound tag = new NBTTagCompound();
output.writeToNBT(tag);
final NBTTagCompound tag = stackToNBT(output);
outputs.appendTag(tag);
}
continue;
@@ -140,8 +141,7 @@ class RecipeTransferHandler<T extends Container> implements IRecipeTransferHandl
}
for (final ItemStack is : list) {
final NBTTagCompound tag = new NBTTagCompound();
is.writeToNBT(tag);
final NBTTagCompound tag = stackToNBT(is);
tags.appendTag(tag);
}
+7 -4
View File
@@ -39,6 +39,9 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import static appeng.helpers.ItemStackHelper.stackFromNBT;
import static appeng.helpers.ItemStackHelper.stackWriteToNBT;
public abstract class AEBaseInvTile extends AEBaseTile implements IAEAppEngInventory {
@@ -50,7 +53,7 @@ public abstract class AEBaseInvTile extends AEBaseTile implements IAEAppEngInven
final NBTTagCompound opt = data.getCompoundTag("inv");
for (int x = 0; x < inv.getSlots(); x++) {
final NBTTagCompound item = opt.getCompoundTag("item" + x);
ItemHandlerUtil.setStackInSlot(inv, x, new ItemStack(item));
ItemHandlerUtil.setStackInSlot(inv, x, stackFromNBT(item));
}
}
}
@@ -65,12 +68,12 @@ public abstract class AEBaseInvTile extends AEBaseTile implements IAEAppEngInven
if (inv != EmptyHandler.INSTANCE) {
final NBTTagCompound opt = new NBTTagCompound();
for (int x = 0; x < inv.getSlots(); x++) {
final NBTTagCompound item = new NBTTagCompound();
final NBTTagCompound itemNBT = new NBTTagCompound();
final ItemStack is = inv.getStackInSlot(x);
if (!is.isEmpty()) {
is.writeToNBT(item);
stackWriteToNBT(is, itemNBT);
}
opt.setTag("item" + x, item);
opt.setTag("item" + x, itemNBT);
}
data.setTag("inv", opt);
}
@@ -80,6 +80,9 @@ import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import static appeng.helpers.ItemStackHelper.stackFromNBT;
import static appeng.helpers.ItemStackHelper.stackWriteToNBT;
public class TileMolecularAssembler extends AENetworkInvTile implements IUpgradeableHost, IConfigManagerHost, IGridTickable, ICraftingMachine, IPowerChannelState {
private final InventoryCrafting craftingInv;
@@ -288,7 +291,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade
final ItemStack pattern = this.myPlan.getPattern();
if (!pattern.isEmpty()) {
final NBTTagCompound compound = new NBTTagCompound();
pattern.writeToNBT(compound);
stackWriteToNBT(pattern, compound);
data.setTag("myPlan", compound);
data.setInteger("pushDirection", this.pushDirection.ordinal());
}
@@ -303,7 +306,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade
public void readFromNBT(final NBTTagCompound data) {
super.readFromNBT(data);
if (data.hasKey("myPlan")) {
final ItemStack myPat = new ItemStack(data.getCompoundTag("myPlan"));
final ItemStack myPat = stackFromNBT(data.getCompoundTag("myPlan"));
if (!myPat.isEmpty() && myPat.getItem() instanceof ItemEncodedPattern) {
final World w = this.getWorld();
@@ -35,6 +35,9 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import static appeng.helpers.ItemStackHelper.stackFromNBT;
import static appeng.helpers.ItemStackHelper.stackToNBT;
public class AppEngInternalInventory extends ItemStackHandler implements Iterable<ItemStack> {
protected boolean enableClientEvents = false;
@@ -162,12 +165,8 @@ public class AppEngInternalInventory extends ItemStackHandler implements Iterabl
for (int i = 0; i < stacks.size(); i++) {
ItemStack is = stacks.get(i);
if (!is.isEmpty()) {
NBTTagCompound itemTag = new NBTTagCompound();
NBTTagCompound itemTag = stackToNBT(is);
itemTag.setInteger("Slot", i);
if (is.getCount() > Byte.MAX_VALUE) {
itemTag.setInteger("stackSize", stacks.get(i).getCount());
}
stacks.get(i).writeToNBT(itemTag);
nbtTagList.appendTag(itemTag);
}
}
@@ -196,11 +195,7 @@ public class AppEngInternalInventory extends ItemStackHandler implements Iterabl
NBTTagCompound itemTags = tagList.getCompoundTagAt(i);
int slot = itemTags.getInteger("Slot");
if (slot >= 0 && slot < stacks.size()) {
stacks.set(slot, new ItemStack(itemTags));
if (itemTags.hasKey("stackSize")) {
int stackSize = itemTags.getInteger("stackSize");
stacks.get(slot).setCount(stackSize);
}
stacks.set(slot, stackFromNBT(itemTags));
}
}
onLoad();
@@ -71,6 +71,8 @@ import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import static appeng.helpers.ItemStackHelper.stackFromNBT;
public class TileSecurityStation extends AENetworkTile implements ITerminalHost, IAEAppEngInventory, ILocatable, IConfigManagerHost, ISecurityProvider, IColorableTile {
@@ -175,7 +177,7 @@ public class TileSecurityStation extends AENetworkTile implements ITerminalHost,
for (final Object key : storedItems.getKeySet()) {
final NBTBase obj = storedItems.getTag((String) key);
if (obj instanceof NBTTagCompound) {
this.inventory.getStoredItems().add(AEItemStack.fromItemStack(new ItemStack((NBTTagCompound) obj)));
this.inventory.getStoredItems().add(AEItemStack.fromItemStack(stackFromNBT((NBTTagCompound) obj)));
}
}
}