From 50dad818766298fb92040ac5e2eb7f0935112f4e Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Thu, 27 Dec 2012 15:44:21 +0100 Subject: [PATCH] Handle exceptions thrown by location deserialization. Move invalid gates (e.g. world does not exist anymore) into 'invalid_gates.yml' file. --- src/de/craftinc/gates/Gate.java | 28 ++++++++++----- src/de/craftinc/gates/Plugin.java | 58 ++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/de/craftinc/gates/Gate.java b/src/de/craftinc/gates/Gate.java index 496cc39..be1157d 100644 --- a/src/de/craftinc/gates/Gate.java +++ b/src/de/craftinc/gates/Gate.java @@ -25,8 +25,7 @@ public class Gate extends BaseGate implements ConfigurationSerializable protected String id; - protected static Map instances = new HashMap(); - + protected static Map instances = new HashMap(); /* * CONSTRUCTORS @@ -74,16 +73,27 @@ public class Gate extends BaseGate implements ConfigurationSerializable public Gate(Map map) { id = (String)map.get(idKey); - location = LocationSerializer.deserializeLocation((Map) map.get(locationKey)); - exit = LocationSerializer.deserializeLocation((Map) map.get(exitKey)); isHidden = (Boolean)map.get(isHiddenKey); isOpen = (Boolean)map.get(isOpenKey); - gateBlockLocations = new HashSet(); - List> serializedGateBlocks = (List>)map.get(gateBlocksKey); - - for (Map sgb : serializedGateBlocks) { - gateBlockLocations.add(LocationSerializer.deserializeLocation(sgb)); + try { + location = LocationSerializer.deserializeLocation((Map) map.get(locationKey)); + exit = LocationSerializer.deserializeLocation((Map) map.get(exitKey)); + + gateBlockLocations = new HashSet(); + List> serializedGateBlocks = (List>)map.get(gateBlocksKey); + + for (Map sgb : serializedGateBlocks) { + gateBlockLocations.add(LocationSerializer.deserializeLocation(sgb)); + } + } + catch (Exception e) { + Plugin.log("ERROR: Failed to load gate '" + id + "'! (" + e.getMessage() + ")"); + Plugin.log("NOTE: This gate will be removed from 'gates.yml' and added to 'invalid_gates.yml'!"); + + Plugin.instance.storeInvalidGate(map); + + return; } instances.put(id, this); diff --git a/src/de/craftinc/gates/Plugin.java b/src/de/craftinc/gates/Plugin.java index 5e85f4b..0030974 100644 --- a/src/de/craftinc/gates/Plugin.java +++ b/src/de/craftinc/gates/Plugin.java @@ -1,6 +1,7 @@ package de.craftinc.gates; import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -17,7 +18,6 @@ import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; - import de.craftinc.gates.commands.*; import de.craftinc.gates.listeners.PluginBlockListener; import de.craftinc.gates.listeners.PluginPlayerListener; @@ -190,4 +190,60 @@ public class Plugin extends JavaPlugin e.printStackTrace(); } } + + + public void storeInvalidGate(Map map) + { + File invalidGatesFile = new File(getDataFolder(), "invalid_gates.yml"); + Boolean invalidGatesFileExists = invalidGatesFile.exists(); + + try { + FileWriter fileWriter = new FileWriter(invalidGatesFile, true); + + if (!invalidGatesFileExists) { + fileWriter.write("gates:\n"); + } + + fileWriter.write("- ==: "); + fileWriter.write(map.get("==").toString() + "\n"); + map.remove("=="); + + fileWriter.write("\topen: false\n"); + map.remove("open"); + + fileWriter.write("\tgateBlocks: []\n"); + map.remove("gateBlocks"); + + + for (String key : map.keySet()) { + Object value = map.get(key); + + fileWriter.write("\t" + key + ": "); + + if (value instanceof Map) { + fileWriter.write("\n"); + + @SuppressWarnings("unchecked") + Map valueMap = (Map)value; + + for (String k : valueMap.keySet()) { + Object v = valueMap.get(k); + + fileWriter.write("\t\t" + k + ": " + v.toString() + "\n"); + } + + } + else { + fileWriter.write(value.toString() + "\n"); + } + + } + + fileWriter.close(); + } + catch (IOException e) { + log("ERROR: Could not save invalid gates to disk."); + e.printStackTrace(); + } + } }