Jump to content

[1.7.10][Solved] Sending information from GUI to TileEntity


Recommended Posts

Posted

Hi! Im trying to send information (Clicking a button to change the value of a integer in the tileEntity) from the GUI to the TileEntity. I know I have to do some packets, and there's where the problem is.

I have a IMessage and an IMessageHandler, but when I try pressing the button I get "A fatal error has occured, this connection is terminated".

 

Here's the code for the IMessage and the IMessageHandler.

public class PacketDrill implements IMessage{

private int x;
private int y;
private int z;
private int mode;

public PacketDrill(TileEntityDrillModifier te){
	x = te.xCoord;
	y = te.yCoord;
	z = te.zCoord;
	mode = te.getMode();
}

@Override
public void fromBytes(ByteBuf buf) {
	x = buf.readInt();
	y = buf.readInt();
	z = buf.readInt();
	mode = buf.readInt();
}

@Override
public void toBytes(ByteBuf buf) {
	buf.writeInt(x);
	buf.writeInt(y);
	buf.writeInt(z);		
	buf.writeInt(mode);
}

public static class Handler implements IMessageHandler<PacketDrill, IMessage>{

	@Override
	public IMessage onMessage(PacketDrill message, MessageContext ctx) {
		TileEntity tile = ctx.getServerHandler().playerEntity.worldObj.getTileEntity(message.x, message.y, message.z);
		if(tile instanceof TileEntityDrillModifier){
			TileEntityDrillModifier modifier =  (TileEntityDrillModifier)tile;
			modifier.setMode(message.mode);
			ctx.getServerHandler().playerEntity.worldObj.markBlockForUpdate(message.x, message.y, message.z);
		}
		return null;
	}
}
}

 

After that I register it in the preInit:

Miscellany.network.registerMessage(PacketDrill.Handler.class, PacketDrill.class, 0, Side.SERVER);

 

And finally here's the code of me using it in the GUI:

@Override
protected void actionPerformed(GuiButton button) {
	super.actionPerformed(button);
	switch(button.id){
	case 0:
		if(tile.getMode()==0){
			tile.setMode(1);
		}else{
			tile.setMode(0); 
		}
		Miscellany.network.sendToServer(new PacketDrill(this.tile));
	}
}

 

What am I doing wrong? First time trying to do packets in GUI so this may be a small error, thanks!

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.