Posted December 29, 201311 yr So I have a problem. Let me explain WHAT I want to do, what actually happens, and what I actually do (the code), in that order... What I want to do: I have a block called the Stopper. When a stopper block is placed, it sends its coordinates to a Tracker. The Tracker is a class extending WorldSavedData and it keeps track of the location of all placed stoppers and in which chunk they are stored. The Tracker saves its own data to the NBT. Note that the Tracker is "global" for all dimensions, so the "coordinates" fed to the Tracker is of the x, y, z, w format, where w is the dimension id. The Tracker exists so that I can quickly check whether or not there is a stopper within some particular chunk. What happens: It turns out the Tracker is too persistent. If I leave the game and go to the start menu and enter another save, the Tracker data from the last save remains when it should most definitely not do so. I tried to fix this using a method to clear the Tracker upon WorldEvent.Unload, but this only made it so that the Tracker doesn't even remember stuff at all. It seem to fail even writing and restoring data from the NBT so if I restart the game, it loses track of all stoppers. What I do: This static method in Tracker generates a new Tracker instance. This is called from a WorldEvent.Load. Note the null check, followed by a check to see if a tracker was already connected to the mapStorage. I suspect if there's anything wrong, that will be in this particular method. public static Tracker forWorld(World world) { if (instance == null){ MapStorage storage = world.mapStorage; Tracker result = (Tracker)storage.loadData(Tracker.class, key); if (result == null) { result = new Tracker(key); storage.setData(key, result); } instance = result; return result; } else return instance; } This is the event listener (registered from my main mod class). It calls the static method Tracker.forWorld(world) which generates a new Tracker instance if one does not exist, and links it to the world so that it will be saved correctly in nbt. The Tracker only needs to be available for some events, so I have made its creation server only. public class WorldEventListener { @ForgeSubscribe public void worldLoading(WorldEvent.Load event) { if (event.world.isRemote) { } else{ KGBlocks.tracker = Tracker.forWorld(event.world); } } } These are the save/load nbt methods in Tracker. @Override public void readFromNBT(NBTTagCompound nbt) { NBTTagList tagList = nbt.getTagList("tracker"); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound)tagList.tagAt(i); int[] arr = tag.getIntArray("position"); addMobstopper(arr[0], arr[1], arr[2], arr[3]); } } @Override public void writeToNBT(NBTTagCompound nbt) { NBTTagList itemList = new NBTTagList(); Set cl = chunk.keySet(); Iterator<ChunkLocation> it = cl.iterator(); while (it.hasNext()){ ChunkLocation temp = it.next(); for (Position p : chunk.get(temp)){ NBTTagCompound tag = new NBTTagCompound(); int[] arr = {p.getX(), p.getY(), p.getZ(), p.getW()}; tag.setIntArray("position", arr); itemList.appendTag(tag); } } nbt.setTag("tracker", itemList); }
December 30, 201311 yr Author Not entirely clear on what you mean. My guess is you mean that I should remove this: instance = result; (?)
December 31, 201311 yr Author I changed the forWorld method to the following and it worked public static Tracker forWorld(World world) { Tracker data = (Tracker)world.mapStorage.loadData(Tracker.class, key); if (data == null) { data = new Tracker(key); world.mapStorage.setData(key, data); } return data; }
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.