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
+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)));
}
}
}