Jump to content

GUI Button Packets (Change variable)


ss7

Recommended Posts

Hello,

 

I'm trying to figure out how to change a variable in my TileEntity, when a button is pressed and updating all the players about that change. I have 1 number and 2 buttons one for increment the other for decrement. I think, i need packets. So i'm sending a packet when a button is pressed like this:

 

 

@Override
protected void actionPerformed(GuiButton par1GuiButton)
{
	switch (par1GuiButton.id)
	{

	case 0:
		PacketDispatcher.sendPacketToServer(PacketHelper.SendPacket(1, Arrays.asList(1, tileentity.xCoord, tileentity.yCoord, tileentity.zCoord)));
	break;

	case 1:
		PacketDispatcher.sendPacketToServer(PacketHelper.SendPacket(1, Arrays.asList(2, tileentity.xCoord, tileentity.yCoord, tileentity.zCoord)));
	break;

	}
}

 

 

My PacketHelper class is like this:

 

 

public static Packet SendPacket(int id, List<Integer> data)
{
	ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(;		
	DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);

	try 
	{
	    dataoutputstream.writeInt(id);
	    
	    for (int element : data)
	    {
	    	dataoutputstream.writeInt(element);
	    }
	}

	catch (Exception ex) 
	{
	    ex.printStackTrace();
	}

	Packet250CustomPayload packet = new Packet250CustomPayload();

	packet.channel = "brickcraft";
	packet.data = bytearrayoutputstream.toByteArray();
	packet.length = bytearrayoutputstream.size();

	return packet;
}

 

 

It just creates a packet with a integer list for data storing.

 

Here is my PacketHandler class:

 

 

@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) 
{
	DataInputStream datainputstream = new DataInputStream(new ByteArrayInputStream(packet.data));
        
	int id;
	ArrayList<Integer> data = new ArrayList<Integer>();

        try 
        {
            id = datainputstream.readInt();
            
            while (datainputstream.available() > 0)
            {
            	data.add(datainputstream.readInt());
            }
        }
        
        catch (IOException ex)
        {	
            ex.printStackTrace();
            
            return;
        }
        
        HandlePacket(id, data, (EntityPlayer)player);
}

void HandlePacket(int id, ArrayList<Integer> data, EntityPlayer player)
{		
	if (id == 1)
	{
		if (player.worldObj.getBlockTileEntity(data.get(1), data.get(2), data.get(3)) != null)
		{
			PoseblockTileEntity tileentity = (PoseblockTileEntity)player.worldObj.getBlockTileEntity(data.get(1), data.get(2), data.get(3));

			if (data.get(0) == 1)
			{
				tileentity.SetScale(tileentity.scale - 1);
			}

			if (data.get(0) == 2)
			{
				tileentity.SetScale(tileentity.scale + 1);
			}
		}
	}
}

 

 

I checked if a packet is incoming and it is, but the var in the GUI is not updating. I've tried it with world.markBlockForUpdate(x, y, z) but that doesn't worked.

 

 

Thank you in advance!

 

ss7

You sir are a god damn hero.

Link to comment
Share on other sites

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.