Jump to content

Recommended Posts

Posted

I need to be able to sync an item selected in the clients GUI to an ItemStack inside my TileEntity.

 

I have created a packet sending method inside my Container:

 

public void sendSelectedItemPacket(){
	ByteArrayOutputStream bos = new ByteArrayOutputStream(;
        DataOutputStream outputStream = new DataOutputStream(bos);      
        
        try {
        	outputStream.writeUTF("ItemSelect");
        	
        	outputStream.write(tile_entity.xCoord);
        	outputStream.write(tile_entity.yCoord);
        	outputStream.write(tile_entity.zCoord);
        	
        	NBTTagCompound par1NBTTagCompound = new NBTTagCompound();
        	par1NBTTagCompound = selectedItem.writeToNBT(par1NBTTagCompound);
        	String temp = par1NBTTagCompound.getName();
        	byte[] itemStackByte =  par1NBTTagCompound.getByteArray(temp);
        	
        	outputStream.writeUTF(par1NBTTagCompound.getName());
        	outputStream.writeInt(itemStackByte.length);
        	outputStream.write(itemStackByte);
        	   	    
        } catch (Exception ex) {
                ex.printStackTrace();
        }
        
	Packet250CustomPayload packet = new Packet250CustomPayload();
	packet.channel = (ModInfo.CHANNEL + "GuiSync");
	packet.data = bos.toByteArray();
	packet.length = bos.size();

	PacketDispatcher.sendPacketToServer(packet);
}

 

This should (i hope) encode my selected items data to an NBTTag then byte array and send it through.

 

I'm then re-making the item on the server using this method:

 

private void handleItemSelectPacket(Packet250CustomPayload payload) {
	DataInputStream data = new DataInputStream(new ByteArrayInputStream(payload.data));

	try {
		String temp = data.readUTF();
		int byteLength = data.readInt();
		byte[] itemStackByte =  new byte[byteLength];

		int x = data.readInt();
		int y = data.readInt();
		int z = data.readInt();

		String compundName = data.readUTF();
		data.read(itemStackByte);

		NBTTagCompound par1NBTTagCompound = new NBTTagCompound();
		par1NBTTagCompound.setByteArray(compundName, itemStackByte);

		ItemStack iStack = ItemStack.loadItemStackFromNBT(par1NBTTagCompound);

//			MinecraftServer.getServer().get

	} catch (IOException e) {
            e.printStackTrace();
            return;
	}
}

 

As you can probably see i'm not doing anything with the data i receive, as i cannot work out how to get a reference to the world > tileEntity from the data i receive in the packet.

 

How would i go about getting that reference, or if there is an entirely better way of doing this then please let me know :)

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.