From 56a268bcda1be9e7dd6b873b84505aa54c6aeea6 Mon Sep 17 00:00:00 2001 From: Mykhailo Balaker <83900885+Angry3vilbot@users.noreply.github.com> Date: Sun, 25 Oct 2026 05:39:49 +0000 Subject: [PATCH] Make Altar packet write the ItemStack to NBT (#155) --- .../common/NewPacketHandler.java | 21 ++++------- .../common/tileEntity/TEAltar.java | 35 ++----------------- 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/NewPacketHandler.java b/src/main/java/WayofTime/alchemicalWizardry/common/NewPacketHandler.java index 83d14bb5..65c082c9 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/NewPacketHandler.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/NewPacketHandler.java @@ -11,6 +11,7 @@ import java.util.stream.IntStream; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.ItemStack; import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; @@ -34,6 +35,7 @@ import WayofTime.alchemicalWizardry.common.tileEntity.TESocket; import WayofTime.alchemicalWizardry.common.tileEntity.TETeleposer; import WayofTime.alchemicalWizardry.common.tileEntity.TEWritingTable; import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.network.ByteBufUtils; import cpw.mods.fml.common.network.FMLEmbeddedChannel; import cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec; import cpw.mods.fml.common.network.FMLOutboundHandler; @@ -106,7 +108,7 @@ public enum NewPacketHandler { World world = AlchemicalWizardry.proxy.getClientWorld(); TileEntity te = world.getTileEntity(msg.x, msg.y, msg.z); if (te instanceof TEAltar altar) { - altar.handlePacketData(msg.items, msg.fluids, msg.capacity); + altar.handlePacketData(msg.itemStack, msg.fluids, msg.capacity); } } } @@ -317,7 +319,7 @@ public enum NewPacketHandler { int y; int z; - int[] items; + ItemStack itemStack; int[] fluids; int capacity; } @@ -482,11 +484,7 @@ public enum NewPacketHandler { target.writeInt(m.y); target.writeInt(m.z); - target.writeBoolean(m.items != null); - if (m.items != null) { - int[] items = m.items; - Arrays.stream(items).forEach(target::writeInt); - } + ByteBufUtils.writeItemStack(target, m.itemStack); target.writeBoolean(m.fluids != null); if (m.fluids != null) { @@ -658,13 +656,8 @@ public enum NewPacketHandler { m.x = dat.readInt(); m.y = dat.readInt(); m.z = dat.readInt(); - boolean hasStacks = dat.readBoolean(); - m.items = new int[TEAltar.sizeInv * 3]; - if (hasStacks) { - m.items = new int[TEAltar.sizeInv * 3]; - IntStream.range(0, m.items.length).forEach(i -> m.items[i] = dat.readInt()); - } + m.itemStack = ByteBufUtils.readItemStack(dat); boolean hasFluids = dat.readBoolean(); m.fluids = new int[6]; @@ -841,7 +834,7 @@ public enum NewPacketHandler { msg.x = tileAltar.xCoord; msg.y = tileAltar.yCoord; msg.z = tileAltar.zCoord; - msg.items = tileAltar.buildIntDataList(); + msg.itemStack = tileAltar.getStackInSlot(0); msg.fluids = tileAltar.buildFluidList(); msg.capacity = tileAltar.getCapacity(); diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/tileEntity/TEAltar.java b/src/main/java/WayofTime/alchemicalWizardry/common/tileEntity/TEAltar.java index 06a3cab9..79158cfd 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/tileEntity/TEAltar.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/tileEntity/TEAltar.java @@ -5,7 +5,6 @@ import java.util.List; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.Packet; @@ -718,19 +717,8 @@ public class TEAltar extends TEInventory implements IFluidTank, IFluidHandler, I return NewPacketHandler.getPacket(this); } - public void handlePacketData(int[] intData, int[] fluidData, int capacity) { - if (intData == null) { - return; - } - - if (intData.length == 3) { - if (intData[2] != 0) { - ItemStack is = new ItemStack(Item.getItemById(intData[0]), intData[2], intData[1]); - inv[0] = is; - } else { - inv[0] = null; - } - } + public void handlePacketData(ItemStack itemStack, int[] fluidData, int capacity) { + inv[0] = itemStack == null ? null : itemStack.copy(); FluidStack flMain = new FluidStack(fluidData[0], fluidData[1]); FluidStack flIn = new FluidStack(fluidData[2], fluidData[3]); @@ -743,25 +731,6 @@ public class TEAltar extends TEInventory implements IFluidTank, IFluidHandler, I this.capacity = capacity; } - public int[] buildIntDataList() { - int[] sortList = new int[3]; // 1 * 3 - int pos = 0; - - for (ItemStack is : inv) { - if (is != null) { - sortList[pos++] = Item.getIdFromItem(is.getItem()); - sortList[pos++] = is.getItemDamage(); - sortList[pos++] = is.stackSize; - } else { - sortList[pos++] = 0; - sortList[pos++] = 0; - sortList[pos++] = 0; - } - } - - return sortList; - } - @Override public void startCycle() { if (worldObj != null) {