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!