From 0f4bd4881748a40c6ed0d7114bf6901849780034 Mon Sep 17 00:00:00 2001 From: gravitylow Date: Tue, 15 Apr 2014 21:29:45 -0400 Subject: [PATCH] Create player backups before converting and save data straight away --- .../zcore/persist/EntityCollection.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/EntityCollection.java b/src/main/java/com/massivecraft/factions/zcore/persist/EntityCollection.java index 39c141cf..77acc8e5 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/EntityCollection.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/EntityCollection.java @@ -9,6 +9,7 @@ import org.bukkit.Bukkit; import org.bukkit.craftbukkit.libs.com.google.gson.Gson; import java.io.File; +import java.io.IOException; import java.lang.reflect.Type; import java.util.*; import java.util.Map.Entry; @@ -193,11 +194,11 @@ public abstract class EntityCollection { } saveIsRunning = false; - return this.saveCore(entitiesThatShouldBeSaved); + return this.saveCore(this.file, entitiesThatShouldBeSaved); } - private boolean saveCore(Map entities) { - return DiscUtil.writeCatch(this.file, this.gson.toJson(entities)); + private boolean saveCore(File target, Map entities) { + return DiscUtil.writeCatch(target, this.gson.toJson(entities)); } public boolean loadFromDisc() { @@ -240,7 +241,20 @@ public abstract class EntityCollection { } } if (list.size() > 0) { + // We've got some converting to do! Bukkit.getLogger().log(Level.INFO, "Please wait while Factions converts " + list.size() + " old player names to UUID. This may take a while."); + + // First we'll make a backup, because god forbid anybody heed a warning + File file = new File(this.file.getParentFile(), "players.json.old"); + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + saveCore(file, (Map) data); + Bukkit.getLogger().log(Level.INFO, "Backed up your old data at " + file.getAbsolutePath()); + + // Start fetching those UUIDs UUIDFetcher fetcher = new UUIDFetcher(list); try { Map response = fetcher.call(); @@ -278,6 +292,7 @@ public abstract class EntityCollection { Bukkit.getLogger().log(Level.INFO, "While converting we found names that either don't have a UUID or aren't players and removed them from storage."); Bukkit.getLogger().log(Level.INFO, "The following names were detected as being invalid: " + StringUtils.join(invalidNames, ", ")); } + saveToDisc(); // Update the flatfile Bukkit.getLogger().log(Level.INFO, "Done converting to UUID."); } return (Map) data;