Jump to content

[1.7.10] [SOLVED] TileEntity not saving to disk properly


Pancake

Recommended Posts

Greetings!

 

I've been working with tile entities for quite some time now, and I had no troubles updating entity data from the server to client, and saving to disk.

However, I've come across something I really can't find the mistake in.

 

@Override
public void writeToNBT(NBTTagCompound tag){
super.writeToNBT(tag);
tag.setInteger("numberOfProcesses",craftingProcesses.size());
System.out.println("write:"+craftingProcesses.size());
if(worldObj!=null){
	System.out.println("world: "+worldObj.isRemote);
}
for(int i=0;i<craftingProcesses.size();i++){
	NBTTagCompound tag2=new NBTTagCompound();
	((ItemStack)craftingProcesses.get(i)[0]).writeToNBT(tag2);
	tag.setTag("process0"+i,tag2);
	tag.setInteger("process1"+i,(Integer)craftingProcesses.get(i)[1]);
	tag.setInteger("process2"+i,(Integer)craftingProcesses.get(i)[2]);
	tag.setInteger("process3"+i,(Integer)craftingProcesses.get(i)[3]);
}
}

@Override
public void readFromNBT(NBTTagCompound tag){
super.readFromNBT(tag);
int numberOfProcesses=tag.getInteger("numberOfProcesses");
System.out.println("read: "+numberOfProcesses);
if(worldObj!=null){
	System.out.println("world: "+worldObj.isRemote);
}
craftingProcesses.clear();
for(int i=0;i<numberOfProcesses;i++){
	craftingProcesses.add(new Object[]{ItemStack.loadItemStackFromNBT(tag.getCompoundTag("process0"+i)),
	tag.getInteger("process1+i"),
	tag.getInteger("process2"+i),
	tag.getInteger("process3"+i)});
}
}

 

 

I have an arraylist of objects to be saved. I put an integer in the tag, saying how many elements there are in the arraylist, and use a for-loop to put them all the objects (ItemStack and 3 integers per element) in the arraylist, nothing I haven't successfully done before.

 

When I read I do the reverse. I get the number, and do a for loop getting an itemstack and 3 integers and put them in an Object array, and add that to the (now cleared) arraylist.

 

I added prints to the writeToNBT and readFromNBT saying howmany elements they're saving. When using the world normally all is fine. If I edit my arraylist i do the following.

 

worldObj.markBlockForUpdate(xCoord,yCoord,zCoord);
markDirty();

 

This might be a bit overkill, but I wanted to be sure.

 

So, as I said, everything is okay, untill i press escape (the game checks for the dirty entities and saves them to disk)

 

I then get the following output...

 

[server thread/INFO]: Saving and pausing game...
[server thread/INFO]: Saving chunks for level 'New World'/Overworld
[server thread/INFO] [sTDOUT]: [net.pancake.singularity.tileentities.TileEntityItemCrafter:writeToNBT:44]: write:1
[server thread/INFO] [sTDOUT]: [net.pancake.singularity.tileentities.TileEntityItemCrafter:writeToNBT:46]: world: false
[server thread/INFO] [sTDOUT]: [net.pancake.singularity.tileentities.TileEntityItemCrafter:writeToNBT:44]: write:0
[server thread/INFO] [sTDOUT]: [net.pancake.singularity.tileentities.TileEntityItemCrafter:writeToNBT:46]: world: false
[server thread/INFO]: Saving chunks for level 'New World'/Nether
[server thread/INFO]: Saving chunks for level 'New World'/The End

 

As you can see, it writes two times, both from the server. But suddenly the size of my arraylist is 0. My writeToNBT method doesn't edit the arraylist in any way. What happened? If I exit the world, and log back in, the arraylist size is 0., instead of the 1 it was.

 

Maybe I just need a pair of fresh eyes... or fresh brains.

 

Thanks in advance,

Pancake.

Link to comment
Share on other sites

Maybe your craftingProcesses has been cleared by something else?  Or an extra TileEntity copy has been created with no craftingProcesses?

 

I suggest you add a small bit of code, put a breakpoint on it, and have a look at the tileEntity and craftingProcesses to see what's wrong with them, perhaps also add some other System.out.println() to other places which initialise or otherwise edit craftingProcesses

 

	super.writeToNBT(tag);
tag.setInteger("numberOfProcesses",craftingProcesses.size());
System.out.println("write:"+craftingProcesses.size());
   if (craftingProcesses.size() == 0) {
      int i = 1;  // breakpoint here
   }

 

-TGG

Link to comment
Share on other sites

One error I can see is that in readFromNBT you never do anything with the 3 integers, you just request them from the NBT and the immediately discard them.

 

He's not actually.  I thought so too, but they're actually just inside the

new Object[]{...}

chunk.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Oh jesus christ, that's some stupid formatting! Give this forum syntax-highlight!

 

None the less, it's still crappy code.  I always new-line and indent stuff like that unless it's simple crap.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

First of all, why are you not using a NBTTagList? It would be much more efficient (and more clear!) than using the "tag<n>" keys in an NBTTagCompound.

One error I can see is that in readFromNBT you never do anything with the 3 integers, you just request them from the NBT and the immediately discard them.

 

Your data structure is also inefficient and confusing at best. The elements in your craftingProcesses list should not be generic Object arrays, make a class that holds the ItemStack and the 3 numbers.

 

I do use those 3 integers, look at the way the brackets are around them. I know, It's hard to miss ;)

And efficiency wasn't the point of this setup, but I suppose putting it in a class would allow me to monitor access more. Thanks for the advice!

 

I've messed around a bit with the debugger, and figured out the tile entity that had arraylist size 0 was another entity, somewhere in my world. So now I'm testing it in a new world.

 

I did some more checking, and discovered a typo. It's also in my original post. instead of asking "process1"+i , I asked "process1+i". Wow, that was one of the hardest errors I ever had to find. Why don't Tags throw exceptions when they can't find a thing associated with the string? -.-' That's just silly!

#endrant

 

Thanks for the help anyways guys. :D

Link to comment
Share on other sites

Oh jesus christ, that's some stupid formatting! Give this forum syntax-highlight!

 

None the less, it's still crappy code.  I always new-line and indent stuff like that unless it's simple crap.

 

I actually had it one line, but decided to make it a bit more readable for you guys.

Yes, I should be ashamed of myself.

Link to comment
Share on other sites

I did some more checking, and discovered a typo. It's also in my original post. instead of asking "process1"+i , I asked "process1+i". Wow, that was one of the hardest errors I ever had to find. Why don't Tags throw exceptions when they can't find a thing associated with the string? -.-' That's just silly!

 

Not really.  I make use of the "tag doesn't exist, so it returns 0" functionality all the time.  Mainly because I'm taking that value (0 is a legitimate value) and increment it (or whatever), and shoving it back into the NBT.

 

If you really care, use NBTCompoundTag#hasTag

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.

Guest
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.

Announcements



×
×
  • Create New...

Important Information

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