From c7b4184e1d167d6ca7b68bfd9c0b194487c58774 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Fri, 19 Oct 2012 01:50:36 -0500 Subject: [PATCH] Backups are now created of bad factions.json and players.json files if they fail to load due to a JSON parse error; same as we already do for bad conf.json --- .../zcore/persist/EntityCollection.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/com/massivecraft/factions/zcore/persist/EntityCollection.java b/src/com/massivecraft/factions/zcore/persist/EntityCollection.java index 20d046ab..265118e0 100644 --- a/src/com/massivecraft/factions/zcore/persist/EntityCollection.java +++ b/src/com/massivecraft/factions/zcore/persist/EntityCollection.java @@ -3,9 +3,12 @@ package com.massivecraft.factions.zcore.persist; import java.io.File; import java.lang.reflect.Type; import java.util.*; +import java.util.logging.Level; import java.util.Map.Entry; +import org.bukkit.Bukkit; import org.bukkit.craftbukkit.libs.com.google.gson.Gson; +import org.bukkit.craftbukkit.libs.com.google.gson.JsonSyntaxException; import com.massivecraft.factions.zcore.util.DiscUtil; import com.massivecraft.factions.zcore.util.TextUtil; @@ -216,7 +219,22 @@ public abstract class EntityCollection } Type type = this.getMapType(); - return this.gson.fromJson(content, type); + try + { + return this.gson.fromJson(content, type); + } + catch(JsonSyntaxException ex) + { + Bukkit.getLogger().log(Level.WARNING, "JSON error encountered loading \"" + file + "\": " + ex.getLocalizedMessage()); + + // backup bad file, so user can attempt to recover something from it + File backup = new File(file.getPath()+"_bad"); + if (backup.exists()) backup.delete(); + Bukkit.getLogger().log(Level.WARNING, "Backing up copy of bad file to: "+backup); + file.renameTo(backup); + + return null; + } } // -------------------------------------------- //