• 0

    posted a message on NBT Data seems to be persisting between world saves if I don't close the entire application between loading

    I've been working on a mod for 1.15.2 that gives the player teleportation gates. I store the data from those gates (id, destination, whitelists, etc.) in a list of objects, which i then in turn store as an NBT in the world save.

    For some reason, it seems that when i play in a save, exit to the menu, then load or create a new save, the NBT values for the gates from the previous save are present.

     

    Is this a known occurrence? or has anyone else encountered this?

     

    If anyone has any experience or insight with this issue, I would appreciate any advice.

     

    EDIT: I do have logging in place when the directory is read from NBT, and when creating the second save, nothing shows up in logs to say that it read the values again.

     

    EDIT 2: This actually appears to only happen when playing in a save, then exiting to menu, and then creating a new save.

     

    below is my data handler class, for reference as to what i am doing.

     

    public class GateInfoHandler extends WorldSavedData
    {
    public static List<GateInfo> GATE_DIRECTORY;
    private static final String DATA_NAME = travelgates.MOD_ID + "_GateInfoHandler";
    private static final Logger LOGGER = LogManager.getLogger();

    public GateInfoHandler() {
    super(DATA_NAME);
    }

    @Override
    public void read(CompoundNBT nbt) {

    List<GateInfo> gateList = new ArrayList<GateInfo>();
    ListNBT nbtList = nbt.getList("DIRECTORY", 10); //this might need changing
    for(int i = 0; i < nbtList.size(); i++)
    {
    GateInfo tempInfo = new GateInfo(nbtList.getCompound(i));
    gateList.add(tempInfo);
    LOGGER.info("TravelGates:Found gate with ID:" + tempInfo.GATE_ID + " in nbt");
    }
    this.GATE_DIRECTORY = gateList;

    LOGGER.info("TravelGates:Loaded Directory from nbt with a size of:"+this.GATE_DIRECTORY.size()+" entries");
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {

    ListNBT nbtList = new ListNBT();
    if(!GATE_DIRECTORY.isEmpty())
    {
    for(GateInfo info : GATE_DIRECTORY)
    {
    nbtList.add(info.WriteNBT(new CompoundNBT()));
    }
    }

    compound.put("DIRECTORY",nbtList);

    LOGGER.info("TravelGates:Wrote Directory to nbt with a size of:" + nbtList.size() + " entries");
    return compound;
    }

    public static GateInfoHandler get(World world) {

    ServerWorld overworld = ((ServerWorld) world).getServer().getWorld(DimensionType.OVERWORLD);
    return overworld.getSavedData().getOrCreate(GateInfoHandler::new, DATA_NAME);
    }

    @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE, modid = travelgates.MOD_ID)
    public static class WorldDataHandlerSaveEvent {

    @SubscribeEvent
    public static void onWorldSave(WorldEvent.Save event) {

    if (event.getWorld() instanceof ServerWorld) {
    ServerWorld server = (ServerWorld) event.getWorld();
    ServerWorld overworld = server.getServer().getWorld(DimensionType.OVERWORLD);
    GateInfoHandler.get(overworld).markDirty();

    }
    }

    @SubscribeEvent
    public static void onWorldLoad(WorldEvent.Load event) {

    if (event.getWorld() instanceof ServerWorld) {
    ServerWorld server = (ServerWorld) event.getWorld();
    ServerWorld overworld = server.getServer().getWorld(DimensionType.OVERWORLD);
    GateInfoHandler.get(overworld);

    }
    }
    }
    }
    Posted in: Mod Development
  • To post a comment, please or register a new account.