I have a Block that needs to store the position of another block, to interact with that block. Everything works fine, as long as you are not logging out of the world. I want to save the coordinates of the other block in the tile entities nbt data. The saving of the data works fine, but when logging back in to the world and therefore loading the nbt data, there is some kind of problem. I now have figured out that the problem only exists on client side. When loading the world, the correct coordinates are loaded from nbt by the server, but the client loads only 0 for each coordinate and that causes some problems when trying to interact with the block at these coordinates. I dont know how to fix this problem and I'm confused as to why the data is loaded correctly on server- but not correctly on client-side.
Here are the methods for writing and reading the nbt in the TileEntity:
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
int[] cont = {0, 0, 0};
if(this.controller != null) {
LogHelper.info("Writing " + this.controller);
cont[0] = this.controller.getX();
cont[1] = this.controller.getY();
cont[2] = this.controller.getZ();
}
compound.setInteger("controllerX", cont[0]);
compound.setInteger("controllerY", cont[1]);
compound.setInteger("controllerZ", cont[2]);
super.writeToNBT(compound);
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound){
super.readFromNBT(compound);
int[] coords = {0, 0, 0};
coords[0] = compound.getInteger("controllerX");
coords[1] = compound.getInteger("controllerY");
coords[2] = compound.getInteger("controllerZ");
LogHelper.info("Loading " + Arrays.toString(coords));
this.controller = new BlockPos(coords[0], coords[1], coords[2]);
LogHelper.info("Loading " + this.controller);
}
I also tried using an int array for the nbt, but that doesn't work at all. It would just return an empty array even if the correct data was stored in the nbt tag. I hope you can help me! :-)
By default Minecraft does not sync NBT Data between the Client and the Server.
For the NBT Data to be synced you need to Override the onDataPacket and the getUpdatePacket functions.
Mine typically look like this:
@Nullable
@Override
public SPacketUpdateTileEntity getUpdatePacket() {
return new SPacketUpdateTileEntity(getPos(), getBlockMetadata(), writeToNBT(new NBTTagCompound()));
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
readFromNBT(pkt.getNbtCompound());
}
Also did you make sure to properly register your TileEntity so it can be created on world load?
For this your Block should Override hasTileEntity to return true if it has a TileEntity in the given IBlockState and createTileEntity to return a fresh TileEntity.
Last but not least you have to register your TileEntity with the GameRegistry
GameRegistry.registerTileEntity(YourTileEntity.class, "YourModid:ResourceString");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With