From 64f8a4899a9d55d2f9c03d184d77659b0cfd44c0 Mon Sep 17 00:00:00 2001 From: TreemanK Date: Fri, 21 Jun 2024 01:49:30 +1000 Subject: [PATCH] feat: make sure old data is not lost! Now, I was thinking if there is a way to directly convert it but there isn't because of the way it was structured. This *ISN'T* the best way around things but if someone can find a better way around it, be my guest. --- .../bentobox/warps/objects/WarpsData.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/world/bentobox/warps/objects/WarpsData.java b/src/main/java/world/bentobox/warps/objects/WarpsData.java index a1dd1ae..7743df6 100644 --- a/src/main/java/world/bentobox/warps/objects/WarpsData.java +++ b/src/main/java/world/bentobox/warps/objects/WarpsData.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; +import org.bukkit.Location; import org.bukkit.World; import com.google.gson.annotations.Expose; @@ -16,8 +17,12 @@ public class WarpsData implements DataObject { @Expose private String uniqueId = "warps"; + + @Deprecated @Expose + private Map warpSigns = new HashMap<>(); + @Expose - private Map warpSigns = new HashMap<>(); + private Map newWarpSigns = new HashMap<>(); public WarpsData() { // Required by YAML database @@ -34,13 +39,28 @@ public class WarpsData implements DataObject { } public Map getWarpSigns() { - if (warpSigns == null) + convertOldWarpSigns(); + if (newWarpSigns == null) return new HashMap<>(); - return warpSigns; + return newWarpSigns; + } + + /** + * Method for converting old warp signs to new warp signs + */ + public void convertOldWarpSigns() { + if (warpSigns == null) { + return; + } + + for (Map.Entry entry : warpSigns.entrySet()) { + PlayerWarp playerWarp = new PlayerWarp(entry.getKey(), true); + newWarpSigns.put(playerWarp, entry.getValue()); + } } public void setWarpSigns(Map warpSigns) { - this.warpSigns = warpSigns; + this.newWarpSigns = warpSigns; } /** @@ -50,7 +70,7 @@ public class WarpsData implements DataObject { */ public WarpsData save(Map> worldsWarpList) { getWarpSigns().clear(); - worldsWarpList.values().forEach(world -> world.forEach((uuid,playerWarp) -> warpSigns.put(playerWarp, uuid))); + worldsWarpList.values().forEach(world -> world.forEach((uuid,playerWarp) -> newWarpSigns.put(playerWarp, uuid))); return this; }