Jump to content

Synchronizing client TileEntity to server?


McJty

Recommended Posts

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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!

 

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.



×
×
  • Create New...

Important Information

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