From 542ad4a6a0374ada7b5c09abb795bc4d4f34db3f Mon Sep 17 00:00:00 2001 From: Brettflan Date: Wed, 6 Apr 2011 04:08:08 -0500 Subject: [PATCH] Widened the exception catching nets for loading and saving all data (IOException -> Exception, etc.), so loading and saving problems don't wipe out unrelated plugin data --- src/com/bukkit/mcteam/factions/Board.java | 8 +++--- src/com/bukkit/mcteam/factions/Conf.java | 5 ++-- src/com/bukkit/mcteam/factions/FPlayer.java | 7 ++--- src/com/bukkit/mcteam/factions/Faction.java | 9 ++++--- .../factions/MyLocationTypeAdapter.java | 27 ++++++++++--------- 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/com/bukkit/mcteam/factions/Board.java b/src/com/bukkit/mcteam/factions/Board.java index 90ef9574..41e239ec 100644 --- a/src/com/bukkit/mcteam/factions/Board.java +++ b/src/com/bukkit/mcteam/factions/Board.java @@ -195,9 +195,9 @@ public class Board { try { DiscUtil.write(file, Factions.gson.toJson(dumpAsSaveFormat())); - } catch (IOException e) { - Factions.log("Failed to save the board to disk."); + } catch (Exception e) { e.printStackTrace(); + Factions.log("Failed to save the board to disk."); return false; } @@ -218,9 +218,9 @@ public class Board { Type type = new TypeToken>>(){}.getType(); Map> worldCoordIds = Factions.gson.fromJson(DiscUtil.read(file), type); loadFromSaveFormat(worldCoordIds); - } catch (IOException e) { - Factions.log("Failed to load the board from disk."); + } catch (Exception e) { e.printStackTrace(); + Factions.log("Failed to load the board from disk."); return false; } diff --git a/src/com/bukkit/mcteam/factions/Conf.java b/src/com/bukkit/mcteam/factions/Conf.java index 4397a7eb..30fc0c05 100644 --- a/src/com/bukkit/mcteam/factions/Conf.java +++ b/src/com/bukkit/mcteam/factions/Conf.java @@ -7,6 +7,7 @@ import org.bukkit.*; import org.bukkit.entity.CreatureType; import com.bukkit.mcteam.util.DiscUtil; +import com.bukkit.mcteam.gson.JsonParseException; public class Conf { public static transient File file = new File(Factions.instance.getDataFolder(), "conf.json"); @@ -96,7 +97,7 @@ public class Conf { try { DiscUtil.write(file, Factions.gson.toJson(new Conf())); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); Factions.log("Failed to save the config to disk."); return false; @@ -115,7 +116,7 @@ public class Conf { try { Factions.gson.fromJson(DiscUtil.read(file), Conf.class); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); Factions.log("Failed to load the config from disk."); return false; diff --git a/src/com/bukkit/mcteam/factions/FPlayer.java b/src/com/bukkit/mcteam/factions/FPlayer.java index 1be671f6..120a01b1 100644 --- a/src/com/bukkit/mcteam/factions/FPlayer.java +++ b/src/com/bukkit/mcteam/factions/FPlayer.java @@ -460,9 +460,9 @@ public class FPlayer { try { DiscUtil.write(file, Factions.gson.toJson(playersToSave)); - } catch (IOException e) { - Factions.log("Failed to save the players to disk."); + } catch (Exception e) { e.printStackTrace(); + Factions.log("Failed to save the players to disk."); return false; } return true; @@ -482,8 +482,9 @@ public class FPlayer { Map instancesFromFile = Factions.gson.fromJson(DiscUtil.read(file), type); instances.clear(); instances.putAll(instancesFromFile); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); + Factions.log("Failed to load the players from disk."); return false; } diff --git a/src/com/bukkit/mcteam/factions/Faction.java b/src/com/bukkit/mcteam/factions/Faction.java index ae0c4f54..599f0155 100644 --- a/src/com/bukkit/mcteam/factions/Faction.java +++ b/src/com/bukkit/mcteam/factions/Faction.java @@ -332,12 +332,12 @@ public class Faction { try { DiscUtil.write(file, Factions.gson.toJson(instances)); } catch (IOException e) { + e.printStackTrace(); Factions.log("Failed to save the factions to disk due to I/O exception."); - e.printStackTrace(); return false; - } catch (NullPointerException e) { - Factions.log("Failed to save the factions to disk due to NPE."); + } catch (Exception e) { e.printStackTrace(); + Factions.log("Failed to save the factions to disk."); return false; } @@ -358,8 +358,9 @@ public class Faction { Map instancesFromFile = Factions.gson.fromJson(DiscUtil.read(file), type); instances.clear(); instances.putAll(instancesFromFile); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); + Factions.log("Failed to load the factions from disk."); return false; } diff --git a/src/com/bukkit/mcteam/factions/MyLocationTypeAdapter.java b/src/com/bukkit/mcteam/factions/MyLocationTypeAdapter.java index d3b40ba3..ebcc3f17 100644 --- a/src/com/bukkit/mcteam/factions/MyLocationTypeAdapter.java +++ b/src/com/bukkit/mcteam/factions/MyLocationTypeAdapter.java @@ -27,7 +27,13 @@ public class MyLocationTypeAdapter implements JsonDeserializer, JsonSe try { JsonObject obj = json.getAsJsonObject(); - World world = Factions.instance.getServer().getWorld(obj.get(WORLD).getAsString()); + String worldname = obj.get(WORLD).getAsString(); + World world = Factions.instance.getServer().getWorld(worldname); + if (world == null) { + Factions.log(Level.WARNING, "Stored location's world \"" + worldname + "\" not found on server; dropping the location."); + return null; + } + double x = obj.get(X).getAsDouble(); double y = obj.get(Y).getAsDouble(); double z = obj.get(Z).getAsDouble(); @@ -36,8 +42,9 @@ public class MyLocationTypeAdapter implements JsonDeserializer, JsonSe return new Location(world, x, y, z, yaw, pitch); - } catch (NullPointerException ex) { - Factions.log(Level.SEVERE, "NPE encountered while deserializing a location"); + } catch (Exception ex) { + ex.printStackTrace(); + Factions.log(Level.WARNING, "Error encountered while deserializing a location."); return null; } } @@ -47,14 +54,9 @@ public class MyLocationTypeAdapter implements JsonDeserializer, JsonSe JsonObject obj = new JsonObject(); try { - if (src == null) + if (src.getWorld() == null) { - Factions.log("Passed location is null in MyLocationTypeAdapter."); - return obj; - } - else if (src.getWorld() == null) - { - Factions.log("Passed location's world is null in MyLocationTypeAdapter."); + Factions.log(Level.WARNING, "Passed location's world was not found on the server. Dropping the location."); return obj; } @@ -67,8 +69,9 @@ public class MyLocationTypeAdapter implements JsonDeserializer, JsonSe return obj; - } catch (NullPointerException ex) { - Factions.log(Level.SEVERE, "NPE encountered while serializing a location"); + } catch (Exception ex) { + ex.printStackTrace(); + Factions.log(Level.WARNING, "Error encountered while serializing a location."); return obj; } }