Handle exceptions thrown by location deserialization. Move invalid gates (e.g. world does not exist anymore) into 'invalid_gates.yml' file.

This commit is contained in:
Tobias Ottenweller 2012-12-27 15:44:21 +01:00
parent 0cd67f3488
commit 50dad81876
2 changed files with 76 additions and 10 deletions

View File

@ -25,8 +25,7 @@ public class Gate extends BaseGate implements ConfigurationSerializable
protected String id;
protected static Map<String, Gate> instances = new HashMap<String, Gate>();
protected static Map<String, Gate> instances = new HashMap<String, Gate>();
/*
* CONSTRUCTORS
@ -74,16 +73,27 @@ public class Gate extends BaseGate implements ConfigurationSerializable
public Gate(Map<String, Object> map)
{
id = (String)map.get(idKey);
location = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(locationKey));
exit = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(exitKey));
isHidden = (Boolean)map.get(isHiddenKey);
isOpen = (Boolean)map.get(isOpenKey);
gateBlockLocations = new HashSet<Location>();
List<Map<String, Object>> serializedGateBlocks = (List<Map<String, Object>>)map.get(gateBlocksKey);
for (Map<String, Object> sgb : serializedGateBlocks) {
gateBlockLocations.add(LocationSerializer.deserializeLocation(sgb));
try {
location = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(locationKey));
exit = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(exitKey));
gateBlockLocations = new HashSet<Location>();
List<Map<String, Object>> serializedGateBlocks = (List<Map<String, Object>>)map.get(gateBlocksKey);
for (Map<String, Object> 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);

View File

@ -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<String, Object> 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<String, Object> valueMap = (Map<String, Object>)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();
}
}
}