Enum names instead of Enum indexes for saving

-Made the PocketRegistry use the toString() values of the
EnumPocketTypes, to save its maps in an NBTTagCompound instead of using
the indexes of those Enums to store the maps in NBTTagLists
This commit is contained in:
Mathijs Riezebos 2017-02-03 13:56:37 +01:00
parent ff62ca29ec
commit 21a008c11f
3 changed files with 25 additions and 26 deletions

View file

@ -85,19 +85,25 @@ public class PocketRegistry {
privatePocketSize = nbt.getInteger("privatePocketSize"); privatePocketSize = nbt.getInteger("privatePocketSize");
publicPocketSize = nbt.getInteger("publicPocketSize"); publicPocketSize = nbt.getInteger("publicPocketSize");
if (nbt.hasKey("nextUnusedIDs")) { //@todo should not be doing this, since all pockets re-register on world-load if (nbt.hasKey("nextUnusedIDs")) { //@todo should not be doing this, since all pockets re-register on world-load
NBTTagList nextUnusedIDTagList = (NBTTagList) nbt.getTag("nextUnusedIDs"); NBTTagCompound nextUnusedIDTagCompound = nbt.getCompoundTag("nextUnusedIDs");
for (int i = 0; i < nextUnusedIDTagList.tagCount(); i++) { for (EnumPocketType pocketType : EnumPocketType.values()) {
int nextUnusedID = nextUnusedIDTagList.getIntAt(i); String tagListName = pocketType.toString();
nextUnusedIDs.put(EnumPocketType.getFromInt(i), nextUnusedID); if (nextUnusedIDTagCompound.hasKey(tagListName)) {
int nextUnusedID = nextUnusedIDTagCompound.getInteger(tagListName);
nextUnusedIDs.put(pocketType, nextUnusedID);
}
} }
} }
if (nbt.hasKey("pocketData")) { if (nbt.hasKey("pocketData")) {
NBTTagList pocketsTagList = (NBTTagList) nbt.getTag("pocketData"); NBTTagCompound pocketsTagCompound = nbt.getCompoundTag("pocketData");
for (int i = 0; i < pocketsTagList.tagCount(); i++) { for (EnumPocketType pocketType : EnumPocketType.values()) {
NBTTagList pocketTagList = (NBTTagList) pocketsTagList.get(i); String tagListName = pocketType.toString();
for (int j = 0; j < pocketTagList.tagCount(); j++) { if (pocketsTagCompound.hasKey(tagListName)) {
NBTTagCompound pocketTag = pocketTagList.getCompoundTagAt(j); NBTTagList pocketTagList = (NBTTagList) pocketsTagCompound.getTag(tagListName);
Pocket.readFromNBT(pocketTag); //this also re-registers the pocket for (int j = 0; j < pocketTagList.tagCount(); j++) {
NBTTagCompound pocketTag = pocketTagList.getCompoundTagAt(j);
Pocket.readFromNBT(pocketTag); //this also re-registers the pocket @todo, should it?
}
} }
} }
} }
@ -112,29 +118,22 @@ public class PocketRegistry {
nbt.setInteger("privatePocketSize", privatePocketSize); nbt.setInteger("privatePocketSize", privatePocketSize);
nbt.setInteger("publicPocketSize", publicPocketSize); nbt.setInteger("publicPocketSize", publicPocketSize);
int[] nextUnusedIDArray = new int[nextUnusedIDs.size()]; //@todo do not have to do this, since all pockets re-register on world-load NBTTagCompound nextUnusedIDTagCompound = new NBTTagCompound(); //@todo do not have to do this, since all pockets re-register on world-load
for (EnumPocketType pocketType : nextUnusedIDs.keySet()) { for (EnumPocketType pocketType : nextUnusedIDs.keySet()) {
nextUnusedIDArray[pocketType.getIntValue()] = nextUnusedIDs.get(pocketType); //this is an extra ensurance that all the IDs end up at the right index in the TagList nextUnusedIDTagCompound.setInteger(pocketType.toString(), nextUnusedIDs.get(pocketType));
} }
NBTTagList nextUnusedIDTagList = new NBTTagList(); nbt.setTag("nextUnusedIDs", nextUnusedIDTagCompound);
for (int nextUnusedID : nextUnusedIDArray) {
nextUnusedIDTagList.appendTag(new NBTTagInt(nextUnusedID));
}
nbt.setTag("nextUnusedIDs", nextUnusedIDTagList);
Map<Integer, Pocket>[] pocketListsArray = new Map[pocketLists.size()]; NBTTagCompound pocketsTagCompound = new NBTTagCompound();
for (EnumPocketType pocketType : pocketLists.keySet()) { for (EnumPocketType pocketType : pocketLists.keySet()) {
pocketListsArray[pocketType.getIntValue()] = pocketLists.get(pocketType); Map<Integer, Pocket> pocketList = pocketLists.get(pocketType);
}
NBTTagList pocketsTagList = new NBTTagList();
for (Map<Integer, Pocket> pocketList : pocketListsArray) { //this is an extra ensurance that all the IDs end up at the right index in the TagList
NBTTagList pocketTagList = new NBTTagList(); NBTTagList pocketTagList = new NBTTagList();
for (Map.Entry<Integer, Pocket> entry : pocketList.entrySet()) { for (int i : pocketList.keySet()) {
pocketTagList.appendTag(Pocket.writeToNBT(entry.getValue())); pocketTagList.appendTag(Pocket.writeToNBT(pocketList.get(i)));
} }
pocketsTagList.appendTag(pocketTagList); pocketsTagCompound.setTag(pocketType.toString(), pocketTagList);
} }
nbt.setTag("pocketData", pocketsTagList); nbt.setTag("pocketData", pocketsTagCompound);
} }
public int registerNewPocket(Pocket pocket, EnumPocketType pocketType) { public int registerNewPocket(Pocket pocket, EnumPocketType pocketType) {