I am trying to save information with the world. I have followed the docs to the best of my ability, along with reading similar posts on this forum, but I am still unable to get this to work. Currently, I have the Data class below which extends from WorldSavedData. It stores the data in one NBTTagCompound.
Data Class
public class ACSave extends WorldSavedData{
private static final String IDENTIFIER = "AC_DATA";
private NBTTagCompound data = new NBTTagCompound();
public ACSave(String identifier) {
super(identifier);
}
public ACSave() {
super(IDENTIFIER);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
data = nbt;
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
nbt = data;
return nbt;
}
public static ACSave get(World world) {
ACSave save = (ACSave) world.getMapStorage().getOrLoadData(ACSave.class, IDENTIFIER);
if(save == null) {
Minecraft.getMinecraft().player.sendChatMessage("Creating a new World Configuration File");
save = new ACSave();
world.getMapStorage().setData(IDENTIFIER, save);
}else {
Minecraft.getMinecraft().player.sendChatMessage("Using a pre-existing Configuration File!");
}
return save;
}
}
The follow event handler class calls upon this class. When the player loads into the world it creates a new ACSave object and calls the get() method. Whenever the player right clicks it increases the integer "Yeet" by one, displaying the new value in chat. It then marks the ACSave object as dirty, so it should be saving but it never does. Whether I wait or stop the world the data file is never created/saved. When I relaunch the world the data is all lost. On another NBT data related post, the user showed that the console ouptuted a line saying how the class was marked dirty when he exited the world, but that has never happened for me. When the world stops nothing special happens at all.
public class ACEventHandler {
ACSave save;
NBTTagCompound nbt = new NBTTagCompound();
@SubscribeEvent
public void RightClickBlock(PlayerInteractEvent.RightClickItem event) {
if(event.getEntity() == Minecraft.getMinecraft().player) {
nbt.setInteger("Yeets", nbt.getInteger("Yeets") + 1);
save.readFromNBT(nbt);
save.markDirty();
Minecraft.getMinecraft().player.sendChatMessage("Yeet Count: " + nbt.getInteger("Yeets"));
}
}
@SubscribeEvent
public void EntityJoinWorldEvent(EntityJoinWorldEvent event) {
if(event.getEntity() == Minecraft.getMinecraft().player) {
save = ACSave.get(Minecraft.getMinecraft().world);
nbt = save.writeToNBT(nbt);
}
}
}
I know there have been a lot of NTB related questions, and I am sorry if this is another stupid question.
Again, to reiterate the data is not being saved, even though I have marked it as dirty. The data file is not being created at any point, and every time I start a world the save is null so it creates a new one.