Posted July 13, 201312 yr Hello! Some of you may know me for creating a furniture mod called "MrCrayfish's Furniture Mod" but that is besides the point. I am very new to packets and handling them. So far I wrote a code that you would think would seem to work. I am sending an itemstack through a packet. I have taken a look how the book does it, copied the code and just modified to suit mine. The problem is that it doesn't send the NBTTagCompound when I'm on a server or LAN. If anyone could help me out to solve this, I will thank you a lot! Here is all the code associated. GuiEnvelope Only the nesssary code is here @Override protected void actionPerformed(GuiButton guibutton) { if(!guibutton.enabled) { return; } if(guibutton.id == 0) { if(player.getCurrentEquippedItem() != null) { ItemStack mail = player.getCurrentEquippedItem(); mail.setTagInfo("author", new NBTTagString("author", this.player.getCommandSenderName())); mail.setTagInfo("signed", new NBTTagString("signed", "true")); mail.itemID = MrCrayfishFurnitureMod.itemEnvelopeSigned.itemID; this.sendItemStackToServer(mail); this.mc.displayGuiScreen(null); } } } protected void sendItemStackToServer(ItemStack mail) { //Sending packet ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(); DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream); NBTTagCompound nbt = NBTHelper.getCompoundTag(mail, "Envelope"); if(nbt != null) { if(nbt.getTagList("Items").tagCount() > 0) { System.out.println("[sENDING]Has contents"); } } try { PacketManager.writeItemStack(mail, dataoutputstream); } catch (Exception exception) { exception.printStackTrace(); } Packet250CustomPayload packet = new Packet250CustomPayload(); packet.channel = "envelope"; packet.data = bytearrayoutputstream.toByteArray(); packet.length = bytearrayoutputstream.size(); PacketDispatcher.sendPacketToServer(packet); } PacketManager package com.mrcrayfish.furniture; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInput; import java.io.DataInputStream; import java.io.DataOutput; import java.io.DataOutputStream; import java.io.IOException; import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteStreams; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.MathHelper; import net.minecraft.world.World; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.network.Player; import net.minecraft.entity.*; public class PacketManager implements IPacketHandler { @Override public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data)); if(packet.channel.equals("envelope")) { handlePacket(inputStream, (EntityPlayerMP) player, 0); } if(packet.channel.equals("package")) { handlePacket(inputStream, (EntityPlayerMP) player, 1); } } private void handlePacket(DataInputStream inputStream, EntityPlayerMP player, int id) { System.out.println(player.username); if(id == 0) { try { ItemStack itemstack = readItemStack(inputStream); NBTTagCompound nbt = NBTHelper.getCompoundTag(itemstack, "Envelope"); if(nbt != null) { if(nbt.getTagList("Items") != null) { System.out.println("Has contents"); } } player.setCurrentItemOrArmor(0, itemstack); } catch (IOException e) { e.printStackTrace(); } } /*if(id == 1) { String author = "ERROR"; try { author = inputStream.readUTF(); } catch (IOException e) { e.printStackTrace(); } if(player.getCurrentEquippedItem() != null) { ItemStack mail = player.getCurrentEquippedItem(); NBTTagCompound nbt = NBTHelper.getCompoundTag(mail, "Envelope"); if(nbt == null) { System.out.println("Doesn't have contents"); } } }*/ } public static ItemStack readItemStack(DataInput par0DataInput) throws IOException { ItemStack itemstack = null; short short1 = par0DataInput.readShort(); System.out.println("[RECIEVING]itemstack id: " + short1); if (short1 >= 0) { byte b0 = par0DataInput.readByte(); short short2 = par0DataInput.readShort(); itemstack = new ItemStack(short1, b0, short2); itemstack.stackTagCompound = readNBTTagCompound(par0DataInput); System.out.println("[RECIEVING]itemstack stacksize: " + b0); System.out.println("[RECIEVING]itemstack damage: " + short2); } return itemstack; } public static NBTTagCompound readNBTTagCompound(DataInput par0DataInput) throws IOException { short short1 = par0DataInput.readShort(); System.out.println("[RECIEVING]abyte length: " + short1); if (short1 < 0) { return null; } else { byte[] abyte = new byte[short1]; par0DataInput.readFully(abyte); return CompressedStreamTools.decompress(abyte); } } public static void writeItemStack(ItemStack par0ItemStack, DataOutput par1DataOutput) throws IOException { if (par0ItemStack == null) { par1DataOutput.writeShort(-1); } else { par1DataOutput.writeShort(par0ItemStack.itemID); par1DataOutput.writeByte(par0ItemStack.stackSize); par1DataOutput.writeShort(par0ItemStack.getItemDamage()); NBTTagCompound nbttagcompound = null; if (par0ItemStack.getItem().isDamageable() || par0ItemStack.getItem().getShareTag()) { nbttagcompound = par0ItemStack.stackTagCompound; } writeNBTTagCompound(nbttagcompound, par1DataOutput); System.out.println("[sENDING]itemstack id: " + par0ItemStack.itemID); System.out.println("[sENDING]itemstack stacksize: " + par0ItemStack.stackSize); System.out.println("[sENDING]itemstack damage: " + par0ItemStack.getItemDamage()); } } protected static void writeNBTTagCompound(NBTTagCompound par0NBTTagCompound, DataOutput par1DataOutput) throws IOException { if (par0NBTTagCompound == null) { par1DataOutput.writeShort(-1); } else { byte[] abyte = CompressedStreamTools.compress(par0NBTTagCompound); par1DataOutput.writeShort((short)abyte.length); par1DataOutput.write(abyte); System.out.println("[sENDING]abyte length: " + (short)abyte.length); System.out.println("[sENDING]abyte: " + abyte); } } } ItemEnvelope' package com.mrcrayfish.furniture; import java.util.List; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagString; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.StatCollector; import net.minecraft.world.World; public class ItemEnvelope extends Item implements IMail { public ItemEnvelope(int par1) { super(par1); this.maxStackSize = 1; } @Override public boolean getShareTag() { return false; } public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10) { TileEntity tile_entity = par3World.getBlockTileEntity(par4, par5, par6); if(!par3World.isRemote) { if(par2EntityPlayer.capabilities.isCreativeMode && par2EntityPlayer.isSneaking()) { par2EntityPlayer.addChatMessage("You cannot use this in creative."); } else if(tile_entity instanceof TileEntityMailBox) { TileEntityMailBox tileEntityMailBox = (TileEntityMailBox)tile_entity; if(par2EntityPlayer.isSneaking() && !par3World.isRemote) { par2EntityPlayer.addChatMessage("You must sign the envelope before depositing it."); } } } return true; } public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) { if(!par2World.isRemote) { par3EntityPlayer.openGui(MrCrayfishFurnitureMod.instance, 5, par2World, 0, 0, 0); } return par1ItemStack; } public static IInventory getInv(EntityPlayer par1EntityPlayer) { ItemStack mail = par1EntityPlayer.getCurrentEquippedItem(); InventoryEnvelope invMail = null; if(mail != null && mail.getItem() instanceof ItemEnvelope) { invMail = new InventoryEnvelope(par1EntityPlayer, mail); } return invMail; } public void registerIcons(IconRegister par1IconRegister) { this.itemIcon = par1IconRegister.registerIcon("cfm:itemenvelope"); } } ItemEnvelopeSigned' package com.mrcrayfish.furniture; import java.util.List; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagString; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.StatCollector; import net.minecraft.world.World; public class ItemEnvelopeSigned extends Item implements IMail { public static boolean canUse; public ItemEnvelopeSigned(int par1) { super(par1); this.maxStackSize = 1; } @Override public boolean getShareTag() { return true; } public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4) { if (par1ItemStack.hasTagCompound()) { NBTTagCompound nbttagcompound = par1ItemStack.getTagCompound(); NBTTagString nbttagstring = (NBTTagString)nbttagcompound.getTag("author"); if (nbttagstring != null) { par3List.add(EnumChatFormatting.GRAY + String.format("from " + nbttagstring.data)); } } } public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10) { TileEntity tile_entity = par3World.getBlockTileEntity(par4, par5, par6); if(!par3World.isRemote) { if(par2EntityPlayer.capabilities.isCreativeMode && par2EntityPlayer.isSneaking()) { par2EntityPlayer.addChatMessage("You cannot use this in creative."); } else if(tile_entity instanceof TileEntityMailBox) { TileEntityMailBox tileEntityMailBox = (TileEntityMailBox)tile_entity; if(tileEntityMailBox.isMailBoxFull() == false && par2EntityPlayer.isSneaking() && !par3World.isRemote) { ItemStack itemStack = par1ItemStack.copy(); tileEntityMailBox.addMail(itemStack); par2EntityPlayer.addChatMessage("Thank you! - " + tileEntityMailBox.ownerName); par1ItemStack.stackSize--; } else if(tileEntityMailBox.isMailBoxFull() == true && par2EntityPlayer.isSneaking()) { par2EntityPlayer.addChatMessage(tileEntityMailBox.ownerName + "'s mail box seems to be full. Try again later."); } } } return true; } public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) { if(!par2World.isRemote) { par3EntityPlayer.openGui(MrCrayfishFurnitureMod.instance, 6, par2World, 0, 0, 0); } return par1ItemStack; } public static IInventory getInv(EntityPlayer par1EntityPlayer) { ItemStack mail = par1EntityPlayer.getCurrentEquippedItem(); InventoryEnvelope invMail = null; if(mail != null && mail.getItem() instanceof ItemEnvelopeSigned) { invMail = new InventoryEnvelope(par1EntityPlayer, mail); } return invMail; } public void registerIcons(IconRegister par1IconRegister) { this.itemIcon = par1IconRegister.registerIcon("cfm:itemenvelope"); } @SideOnly(Side.CLIENT) public boolean hasEffect(ItemStack par1ItemStack) { return true; } } GuiHandler package com.mrcrayfish.furniture; import cpw.mods.fml.common.network.IGuiHandler; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.src.*; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class GuiHandler implements IGuiHandler { @Override public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) { TileEntity tile_entity = world.getBlockTileEntity(x, y, z); if(id == 5) { return new ContainerEnvelope(player.inventory, ItemEnvelope.getInv(player)); } if(id == 6) { return new ContainerEnvelope(player.inventory, ItemEnvelopeSigned.getInv(player)); } return null; } @Override public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) { TileEntity tile_entity = world.getBlockTileEntity(x, y, z); if(id == 5) { return new GuiEnvelope(player.inventory, ItemEnvelope.getInv(player), player); } if(id == 6) { return new GuiEnvelope(player.inventory, ItemEnvelopeSigned.getInv(player), player); } return null; } }
July 13, 201312 yr In your main mod class, at the NetworkMod annotation, did you set packetHandler=PacketManager.class and your channels {"enveloppe","package"} ?
July 14, 201312 yr Author In your main mod class, at the NetworkMod annotation, did you set packetHandler=PacketManager.class and your channels {"enveloppe","package"} ? Yes. This is the exact line in my main class. @NetworkMod(clientSideRequired = true, serverSideRequired = false, channels = {"envelope", "package"}, packetHandler = PacketManager.class)
July 14, 201312 yr not a solution, but i suggest making your channels more unique than "envelope" and "package"
July 14, 201312 yr Author not a solution, but i suggest making your channels more unique than "envelope" and "package" Thanks for that tip.
July 14, 201312 yr Author I found a solution. I had to cast "Player" to "EntityPlayerMP". Works like a charm now. Thank you anyway guys!
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.