Posted August 21, 201411 yr Hi all, How can I (with 1.7.10) synchronize changes to the client TileEntity to the server? I have a GUI where I want to modify some settings in my tileentity and I want it to be saved on the server side. I found some tutorials on the web but they are for older versions and I have trouble adapting them to latest forge. Any help here? Thanks
August 21, 201411 yr Author Don't modify the client TE from the GUI. Send a (custom) packet to the server to notify it that the user e.g. pressed a button and then let the server update the data. Yes I gathered that. But how? I can't find example code on this (at least example code that still works in this version of forge)
August 21, 201411 yr Author Hmm sorry but I'm probably looking for something else or it is named differently that I'm expecting but I can't find any tutorial there that seems to match what I'm looking at?
August 21, 201411 yr Author Ah sorry thanks. Didn't think of looking into networking code. This is a new programming area for me. Have to get used to it.
August 22, 201411 yr Author Ok, thanks to your help I managed to get a bit further but I'm still stuck with a problem. The problem I have now is that when my GUI updates a value in the tile entity (does this by sending a message) the server version of the tile entity is updated correctly (and saved when I exit the game) but the client tile entity does not reflect this change (even though I call markBlockForUpdate). I have to save and load the world again before the client tile entity is updated. Here is (in summary) what I have done: I made a simple message channel and registered my message with: INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("rftools"); INSTANCE.registerMessage(PacketRFMonitor.class, PacketRFMonitor.class, nextID(), Side.SERVER); PacketRFMonitor basically looks like this: public class PacketRFMonitor implements IMessage, IMessageHandler<PacketRFMonitor, IMessage> { @Override public void fromBytes(ByteBuf buf) { ... } @Override public void toBytes(ByteBuf buf) { ... } @Override public IMessage onMessage(PacketRFMonitor message, MessageContext ctx) { EntityPlayer player = ctx.getServerHandler().playerEntity; TileEntity te = player.worldObj.getTileEntity(message.x, message.y, message.z); if(!(te instanceof RFMonitorBlockTileEntity)) { System.out.println("createPowerMonitotPacket: TileEntity is not a RFMonitorBlockTileEntity!"); return null; } RFMonitorBlockTileEntity monitorBlockTileEntity = (RFMonitorBlockTileEntity) te; monitorBlockTileEntity.setMonitor(message.monitor); player.worldObj.markBlockForUpdate(x, y, z); return null; } } In my tile entity class I have the following: public class RFMonitorBlockTileEntity extends TileEntity { private int monitorX = -1; private int monitorY = -1; // Invalid y coordinate so we know it is not initialized yet private int monitorZ = -1; ... setters and getters .... @Override public Packet getDescriptionPacket() { NBTTagCompound nbtTag = new NBTTagCompound(); this.writeToNBT(nbtTag); return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag); } @Override public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) { readFromNBT(packet.func_148857_g()); } @Override public void readFromNBT(NBTTagCompound tagCompound) { super.readFromNBT(tagCompound); monitorX = tagCompound.getInteger("monitorX"); monitorY = tagCompound.getInteger("monitorY"); monitorZ = tagCompound.getInteger("monitorZ"); } @Override public void writeToNBT(NBTTagCompound tagCompound) { super.writeToNBT(tagCompound); tagCompound.setInteger("monitorX", monitorX); tagCompound.setInteger("monitorY", monitorY); tagCompound.setInteger("monitorZ", monitorZ); } } Then in my GUI code I have this: private void sendChangeToServer(Coordinate c) { PacketHandler.INSTANCE.sendToServer(new PacketRFMonitor(monitorBlockTileEntity.xCoord, monitorBlockTileEntity.yCoord, monitorBlockTileEntity.zCoord, c)); } My full source code can be seen on github: https://github.com/McJty/RFTools As far as I know I have done everything correctly but apparently not. I can of course manually change the client tile entity in the gui code but I have been told not to do that and I prefer to do things the proper way. Any ideas what could be wrong here? 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.