Jump to content

[SOLVED] NBT read and write question


Recommended Posts

I have a class, Tracker, which contains a hashtable and some other data. Tracker extends WorldSavedData and reads and writes correctly from and to the NBT.


The tracker stores locations of some mod related blocks so that I can quickly find out if one of these  blocks exist in some particular chunk.


There's a mysteriou issue, however: The read and write NBT methods both output a message along the lines of "Tracker saved to NBT" and "Tracker loaded from NBT" (System.out.println())... But the weird thing is that this does not always happen. This indicates either that the code does not always run or that something is preventing it from writing to stdout.


This is one such scenario:

*I start a new world and place a block tracked by Tracker. My print functions indicate the block is registered correctly by the Tracker. There is no message from the NBT write that it is passively saving.

*I exit to main menu. There is no message now either from the write method.

*I return to the world. There is no message from the load method.

*I use another print test feature to see that yes, the block positions are correctly stored in the Tracker.


This behavior indicates to me that SOMEHOW the Tracker both loaded and saved data from the NBT, but my print calls from those two methods did not successfully print to stdout. What could cause this?


All I can think of is that this might have something to do with the server/client side divide. It would be nice if someone could explain why this is happening and how I can fix it.

Link to comment
Share on other sites

Figured code would make little sense in this scenario given that I know the code executes, it just does not show up in stdout.


Here you go. This code is from the Tracker. The code correctly reads and writes to the NBT:


public void readFromNBT(NBTTagCompound nbt) {
	System.out.println("----------------------------------------------------------Reading Tracker from NBT"); // this does not show up
	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]);

public void writeToNBT(NBTTagCompound nbt) {
	System.out.println("----------------------------------------------------------Writing Tracker to NBT"); // this does not show up
	NBTTagList itemList = new NBTTagList();
	int counter = 0;
	Set cl = chunk.keySet();
	Iterator<ChunkLocation> it = cl.iterator();
	while (it.hasNext()){
		ChunkLocation temp = it.next();
		for (Position p : chunk.get(temp)){
			counter ++;
			NBTTagCompound tag = new NBTTagCompound();
			int[] arr = {p.getX(), p.getY(), p.getZ(), p.getW()};
			tag.setIntArray("position", arr);

	nbt.setTag("tracker", itemList);		

	System.out.println("Blocks saved to NBT: " + counter); // this does not show up

Link to comment
Share on other sites

Huh... For some reason it works as it should when I test it today...


But... It seems that the Tracker stays in memory when you exit the world to the main menu. So when I start a new game in a new world, the old Tracker remains. I probably need to clear the Tracker somehow.


This might actually explain why I didn't see the NBT read/write behavior yesterday. For some reason those methods were not called and it looked as if things were working correctly because the Tracker retains data between worlds...


Edit: Will restart this topic in a new thread with better focus. Consider this current one solved.

Link to comment
Share on other sites

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.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

By using this site, you agree to our Terms of Use.