Jump to content

1.7.10 How would I save an arraylist of tile entities to NBT?


TheRealMcrafter

Recommended Posts

Am I on the right track here?

 

@Override
public void writeToNBT(NBTTagCompound nbt){
    	super.writeToNBT(nbt);  
    	nbt.setByte("renderValue", this.renderValue);
    	nbt.setString("state", this.state);
    	
    	NBTTagCompound siren = new NBTTagCompound();

    	for (int i = 0; i < sirens.size(); i++){
        	siren.setInteger("x" + i, sirens.get(i).xCoord);
        	siren.setInteger("y" + i, sirens.get(i).yCoord);
        	siren.setInteger("z" + i, sirens.get(i).zCoord);
        	siren.setInteger("listSize", i);
    	}
    	

    	NBTTagList list = new NBTTagList();
    	
    	list.appendTag(siren);
    	
    	nbt.setTag("sirens", list);
}

@Override
public void readFromNBT(NBTTagCompound nbt){
	super.readFromNBT(nbt);
	this.renderValue = nbt.getByte("renderValue");
	this.state = nbt.getString("state");

	NBTTagList list = (NBTTagList) nbt.getTag("sirens"); 
	NBTTagCompound siren = list.getCompoundTagAt(0);

	for (int i = 0; i < siren.getInteger("listSize"); i ++){
		TileEntity tile = new TileEntity();
		//tile.xCoord is (siren.getInteger("x" + i);
		this.sirens.add(tile);
	}

}

Link to comment
Share on other sites

Yes and No, you redesigned wheel.

 

Example:

NBTTagCompound mainNBT = new NBTTagCompound(); //pareent nbt
NBTTagList history = new NBTTagList(); // the list
NBTTagCompound level; //tag in list on "i" index
for (int lvl = 0; lvl <= this.getLevel(); ++lvl)
{
level = new NBTTagCompound(); // each "i" index has one NBT

level.setString("A", a); // write whatever you want in given NBT index

history.appendTag(level); // apend index to list
}
mainNBT.setTag("History", history); // write list to main NBT

 

The NBTTagList ships its size.

 

Reading:

NBTTagList history = stats.getTagList("History", 10);
NBTTagCompound level;
for (int lvl = 0; lvl < history.tagCount(); ++lvl)
{
level = (NBTTagCompound) history.getCompoundTagAt(lvl);
...

 

EDIT

Personal note:

If your whole array will look like this: x1, y1, z1, x2, y2, z2, x3, y3, z3, ....    then you know that i*1 is x, i*2 is y, i*3 is z (for i >= 1).

You can use "NBTTagIntArray" - will be a bit smaller and faster, but whatever, reading/writing is done once in a while so you might aswell not bother.

1.7.10 is no longer supported by forge, you are on your own.

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.