Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.