Posted May 30, 201510 yr Title says it all. How would I go about saving ArrayList<TileEntity> to NBT in my tile entity class?
May 31, 201510 yr Author 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); } }
May 31, 201510 yr 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.
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.