I've created a tile entity that (through the consumption of bottles-o-enchanting placed in its inventory) maintains an 'experience level' internal to itself. It seems to be working fine, until the game is closed and reloaded- then the level has been reset to zero.
My understanding was that overriding the ReadFromNBT and WriteToNBT methods would allow the level data to be saved, as indeed that seems to be all that is required to save the items:
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
NBTTagList list = new NBTTagList();
for (int i = 0; i < this.getSizeInventory(); ++i) {
if (this.getStackInSlot(i) != null) {
NBTTagCompound stackTag = new NBTTagCompound();
stackTag.setByte("Slot", (byte) i);
this.getStackInSlot(i).writeToNBT(stackTag);
list.appendTag(stackTag);
}
}
nbt.setTag("Items", list);
NBTTagInt lev = new NBTTagInt(level);
nbt.setTag("level", lev);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
NBTTagList list = nbt.getTagList("Items", 10);
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound stackTag = list.getCompoundTagAt(i);
int slot = stackTag.getByte("Slot") & 255;
this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
}
this.level = nbt.getInteger("level");
}
Is this really all that is required? More broadly, is any modification or implementation of a network manager required to keep something like this synchronized between clients and servers? There don't seem to be many good tutorials on this subject that don't point to dead source code, etc., and looking at other mods has left me more confused than enlightened by the variety of implementations used.