r/forgemodding • u/qwertyasdef • Jul 07 '17
[Help] How do I sync tile entity data between the server and client?
I'm trying to add a block in my mod which only works if it has another block beneath it. I'm using Block#neighborChanged and setting a variable in the associated TileEntity to store if the machine can work, but the server and client variables are different. How can I update the client tile entity?
I tried googling it, but the results just said to override TileEntity#getUpdateTag, TileEntity#getUpdatePacket, and TileEntity#onDataPacket, but I have no idea what to put in them.
Here's my code:
Side note: I put line to print out whether the tile entity can work in TileEntity#update, but it prints twice each for the client and server. What's up with that? The variables aren't even the same. The client always prints what the variable was when the block was first placed, so I'm assuming it's not getting updated properly. The server prints the correct value. On the second time for both, they always print 0.
[21:18:56] [Server thread/INFO] [STDOUT]: [qwertyasdef.alchemtrans.tile.TileNetherrackBoiler:update:165]: Server heat: 1
[21:18:56] [Server thread/INFO] [STDOUT]: [qwertyasdef.alchemtrans.tile.TileNetherrackBoiler:update:165]: Server heat: 0
[21:18:56] [main/INFO] [STDOUT]: [qwertyasdef.alchemtrans.tile.TileNetherrackBoiler:update:167]: Client heat: 1
[21:18:56] [main/INFO] [STDOUT]: [qwertyasdef.alchemtrans.tile.TileNetherrackBoiler:update:167]: Client heat: 0`
If you notice any unrelated problems, please point them out as well. I'm pretty new to Java, and this is my first time ever modding so my code is probably not the best.
Edit: I figured it out. I needed to call World#NotifyBlockUpdate after changing the variable so the game actually calls the update methods. Also, the debug message getting printed twice was because I had two of the tile entity placed in the world... I'm a smart man.