I have a custom mob which has a custom data type. This data type contains a set of 8 BlockPos and a boolean. The problem I have is that when I restart the game, the mob is recreated and does not load the previously set data type. How can I go about making the mob save and load the previous data on exit of the world and loading of the world, respectively?
Override readFromNBT(NBTTagCompound tag) and writeToNBT(NBTTagCompound tag) to include your information. Remember to make a super call first so superclass NBT info is kept.
private int whateverValue;
private final String KEY_INT = "myInt";
Here I set the key to a constant variable KEY_INT, which I reference in writeToNBT. In readFromNBT I would set whateverValue using tag.getInteger and passing it KEY_INT. The key has to be the same for each.
Rollback Post to RevisionRollBack
Click this banner for a list of illegal mod distributors -- only download from legal sites!
Yep. You could add readFromNBT and writeToNBT methods to your custom data class and let it handle itself, then just call 'myDataObject.writeToNBT(compound)' and 'this.myDataObject = new MyDataObject(); this.myDataObject.readFromNBT(compound)' to save and recreate it. Of course, those need to go in the eponymous methods of the Entity class.