From 1844df81f38f020ad68bf5c9aed9d84103003c6a Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Thu, 17 May 2012 16:43:51 +0200 Subject: [PATCH] Gate will throw exceptions when something goes wrong --- src/org/mcteam/ancientgates/Gate.java | 42 +++++++++++++++------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/org/mcteam/ancientgates/Gate.java b/src/org/mcteam/ancientgates/Gate.java index d3904b6..e276e3b 100644 --- a/src/org/mcteam/ancientgates/Gate.java +++ b/src/org/mcteam/ancientgates/Gate.java @@ -31,8 +31,9 @@ public class Gate extends BaseGate implements ConfigurationSerializable * CONSTRUCTORS */ - public Gate() + public Gate(String id) throws Exception { + setId(id); } @@ -46,17 +47,14 @@ public class Gate extends BaseGate implements ConfigurationSerializable } - /** - * @return true if the id has been changed; false otherwise - */ - public boolean setId(String id) + + public void setId(String id) throws Exception { - if (!exists(id)) { - this.id = id; - return true; + if (exists(id)) { + throw new Exception("A gate with '" + id + "' already exists"); } - - return false; + + this.id = id; } @@ -93,7 +91,11 @@ public class Gate extends BaseGate implements ConfigurationSerializable public Map serialize() { - validate(); // make sure to not write invalid stuff to disk + try { + validate(); // make sure to not write invalid stuff to disk + } + catch (Exception e) { + } Map retVal = new HashMap(); @@ -131,20 +133,18 @@ public class Gate extends BaseGate implements ConfigurationSerializable } - public static Gate create(String id) + public static Gate create(String id) throws Exception { - Gate gate = new Gate(); - gate.id = id; - instances.put(gate.id, gate); - Plugin.log("created new gate " + gate.id); + Gate gate = new Gate(id); + instances.put(gate.id, gate); return gate; } - public static void rename(Gate gate, String newId) throws Exception + public static void rename(String oldId, String newId) throws Exception { - String oldId = gate.id; + Gate gate = get(oldId); gate.setId(newId); @@ -155,6 +155,12 @@ public class Gate extends BaseGate implements ConfigurationSerializable public static void delete(String id) { + Gate g = get(id); + + if (g != null) { + g.emptyGate(); + } + instances.remove(id); }