From 55a8719748de7078d05e7fc0b8ec0184e1e0cb35 Mon Sep 17 00:00:00 2001 From: StevenRS11 Date: Tue, 28 Jan 2014 14:05:43 -0500 Subject: [PATCH] Final savedata fixes --- .../mod_pocketDim/core/PocketManager.java | 7 +- .../mod_pocketDim/saving/DDSaveHandler.java | 64 +++++++++++++------ .../ticking/RiftRegenerator.java | 1 - 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/main/java/StevenDimDoors/mod_pocketDim/core/PocketManager.java b/src/main/java/StevenDimDoors/mod_pocketDim/core/PocketManager.java index 4c4b4f02..758a4953 100644 --- a/src/main/java/StevenDimDoors/mod_pocketDim/core/PocketManager.java +++ b/src/main/java/StevenDimDoors/mod_pocketDim/core/PocketManager.java @@ -271,25 +271,26 @@ public class PocketManager public static boolean registerPackedDimData(PackedDimData packedData) { - InnerDimData dimData; + InnerDimData dimData; + //register roots if(packedData.ID==packedData.ParentID) { dimData = new InnerDimData(packedData.ID, null, false, false, linkWatcher); dimData.root=dimData; dimData.parent=dimData; + dimData.depth=packedData.Depth; dimData.isFilled=packedData.IsFilled; dimData.origin = new Point4D(packedData.Origin.getX(),packedData.Origin.getY(),packedData.Origin.getZ(),packedData.ID); PocketManager.rootDimensions.add(dimData); } - else + else //register children { InnerDimData test = PocketManager.dimensionData.get(packedData.ParentID); dimData = new InnerDimData(packedData.ID, test,true, packedData.IsDungeon, linkWatcher); dimData.isFilled=packedData.IsFilled; dimData.origin = new Point4D(packedData.Origin.getX(),packedData.Origin.getY(),packedData.Origin.getZ(),packedData.ID); - dimData.root=PocketManager.getDimensionData(packedData.RootID); if(packedData.DungeonData!=null) diff --git a/src/main/java/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java b/src/main/java/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java index cf6124ce..fb39f5b5 100644 --- a/src/main/java/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java +++ b/src/main/java/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java @@ -89,38 +89,34 @@ public class DDSaveHandler */ public static boolean unpackDimData(HashMap packedDims) { - LinkedList children = new LinkedList(); - ArrayList tempChildren = new ArrayList(); - int registeredDims=packedDims.keySet().size(); + LinkedList dimsToRegister = new LinkedList(); for(PackedDimData packedDim : packedDims.values()) - { - //Load roots + { + //fix pockets without parents + verifyParents(packedDim, packedDims); + + //Load roots first by inserting them in the LinkedList first. if(packedDim.RootID==packedDim.ID) { - children.add(packedDim.ID); - PocketManager.registerPackedDimData(packedDim); - } - //fix pockets without parents - if(!packedDims.containsKey(packedDim.ParentID)) - { - packedDim=(new PackedDimData(packedDim.ID, 1, packedDim.PackDepth, packedDim.RootID, packedDim.RootID, packedDim.Orientation, packedDim.IsDungeon, packedDim.IsFilled, packedDim.DungeonData, packedDim.Origin, packedDim.ChildIDs, packedDim.Links, packedDim.Tails)); - packedDims.put(packedDim.ID, packedDim); - children.addLast(packedDim.ID); + dimsToRegister.addFirst(packedDim.ID); } } + //load the children for each root - while(!children.isEmpty()) + while(!dimsToRegister.isEmpty()) { - Integer childID = children.pop(); + Integer childID = dimsToRegister.pop(); PackedDimData data = packedDims.get(childID); - children.addAll(verifyChildren(data, packedDims)); + dimsToRegister.addAll(verifyChildren(data, packedDims)); PocketManager.registerPackedDimData(data); } return true; } /** - * ensures that a pocket's children havent been deleted + * Fixes the case where a child of a parent has been deleted. + * -removes the child from parent + * * @param packedDim * @param packedDims * @return @@ -146,6 +142,38 @@ public class DDSaveHandler return children; } + /** + * Fixes the case where a child had its parent deleted OR where a parent forgot about its child + * -Changes the missing parent to the dims root if its original parent is gone. + * -Finds the new parent and adds it to its list of children or reminds the old parent if it forgot its child + * + * @param packedDim + * @param packedDims + */ + public static void verifyParents(PackedDimData packedDim,HashMap packedDims) + { + ArrayList fosterChildren = new ArrayList(); + fosterChildren.add(packedDim.ID); + + //fix pockets without parents + if(!packedDims.containsKey(packedDim.ParentID)) + { + //Fix the orphan by changing its root to its parent, re-connecting it to the list + packedDim=(new PackedDimData(packedDim.ID, 1, packedDim.PackDepth, packedDim.RootID, packedDim.RootID, packedDim.Orientation, packedDim.IsDungeon, packedDim.IsFilled, packedDim.DungeonData, packedDim.Origin, packedDim.ChildIDs, packedDim.Links, packedDim.Tails)); + packedDims.put(packedDim.ID, packedDim); + } + //fix pockets whose parents have forgotten about them + PackedDimData fosterParent = packedDims.get(packedDim.ParentID); + if(!fosterParent.ChildIDs.contains(packedDim.ID)&&packedDim.ID!=packedDim.RootID) + { + //find the root, and fix it by adding the orphan's ID to its children + fosterChildren.addAll(fosterParent.ChildIDs); + fosterParent=(new PackedDimData(fosterParent.ID, fosterParent.Depth, fosterParent.PackDepth, fosterParent.ParentID, fosterParent.RootID, fosterParent.Orientation, fosterParent.IsDungeon, fosterParent.IsFilled, fosterParent.DungeonData, fosterParent.Origin, fosterChildren, fosterParent.Links, fosterParent.Tails)); + packedDims.put(fosterParent.ID, fosterParent); + } + + } + public static boolean unpackLinkData(List linksToUnpack) { Point3D fakePoint = new Point3D(-1,-1,-1); diff --git a/src/main/java/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java b/src/main/java/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java index 2d2d368f..7187c9d7 100644 --- a/src/main/java/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java +++ b/src/main/java/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java @@ -25,7 +25,6 @@ public class RiftRegenerator implements IRegularTickReceiver { regenerateRiftsInAllWorlds(); } - @SuppressWarnings("deprecation") public static void regenerateRiftsInAllWorlds() { //Regenerate rifts that have been replaced (not permanently removed) by players