This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Selim's profile picture

Suggestion for Storing HashMap in NBT

Started by Selim, 08 April 2016 - 05:48 PM
Selim #1
Posted 08 April 2016 - 07:48 PM
I was wondering if someone has done this before that they would be able to let me know how. I want to take a Lua table (HashMap) and store it in the NBT of the peripheral's TileEntity.
Lignum #2
Posted 08 April 2016 - 09:46 PM
I haven't done modding in a while, but if I recall correctly, you would do something like this (pseudocode):


// Our map in NBT form
NBTTagCompound nbtMap = new NBTTagCompound();

// Copy every element from the HashMap to the NBTTagCompound
for (every key value pair in the map) {
   nbtMap.setString(key, value); // This might not be setString in your case!
}

// Save it in the TileEntity's compound
tileEntityNBT.setTag("MyMap", nbtMap);

Of course, you can only store types in the map that are supported by NBT. Since your map represents a Lua table, this shouldn't be a problem.
Selim #3
Posted 09 April 2016 - 11:10 PM
Thanks!