diff --git a/src/org/mcteam/ancientgates/Gate.java b/src/org/mcteam/ancientgates/Gate.java index 1100bd5..face817 100644 --- a/src/org/mcteam/ancientgates/Gate.java +++ b/src/org/mcteam/ancientgates/Gate.java @@ -16,12 +16,12 @@ import org.bukkit.Material; import org.bukkit.block.Block; import org.mcteam.ancientgates.gson.reflect.TypeToken; - import org.mcteam.ancientgates.util.DiscUtil; import org.mcteam.ancientgates.util.FloodUtil; -public class Gate { +public class Gate +{ private static transient TreeMap instances = new TreeMap(String.CASE_INSENSITIVE_ORDER); private static transient File file = new File(Plugin.instance.getDataFolder(), "gates.json"); @@ -29,87 +29,105 @@ public class Gate { private Location from; private Location to; - private Integer[][] gateBlocks; + private Integer[][] gateBlocks; - public Gate() { - + + public Gate() + { } + // -------------------------------------------- // // Getters And Setters // -------------------------------------------- // - public void setId(String id) { + public void setId(String id) + { this.id = id; } - public String getId() { + + public String getId() + { return id; } - public void setFrom(Location from) { + + public void setFrom(Location from) + { this.from = from; } - public Location getFrom() { + + public Location getFrom() + { return from; } - public void setTo(Location to) { + public void setTo(Location to) + { this.to = to; } - public Location getTo() { + + public Location getTo() + { return to; } - - public Integer[][] getGateBlocks() { - return gateBlocks; - } - - public void setGateBlocks(Set gateBlocks) { - if (gateBlocks == null) - return; - - this.gateBlocks = new Integer[gateBlocks.size()][3]; - - int blockcount = 0; - for (Block b: gateBlocks) { - if (b != null) { - this.gateBlocks[blockcount][0] = b.getX(); - this.gateBlocks[blockcount][1] = b.getY(); - this.gateBlocks[blockcount][2] = b.getZ(); - } - blockcount++; - } - } + + public Integer[][] getGateBlocks() + { + return gateBlocks; + } + + + public void setGateBlocks(Set gateBlocks) + { + if (gateBlocks == null) + return; + + this.gateBlocks = new Integer[gateBlocks.size()][3]; + + int blockcount = 0; + for (Block b : gateBlocks) + { + if (b != null) + { + this.gateBlocks[blockcount][0] = b.getX(); + this.gateBlocks[blockcount][1] = b.getY(); + this.gateBlocks[blockcount][2] = b.getZ(); + } + blockcount++; + } + } + + //----------------------------------------------// // The Open And Close Methods //----------------------------------------------// - public boolean open() { + public boolean open() + { Set blocks = FloodUtil.getGateFrameBlocks(from.getBlock()); - if (blocks == null) { + if (blocks == null) return false; - } // Uncomment lines below to have the old Portal open functionality back. // This is not to do an effect - // It is to stop portalblocks from destroyingthemself as they cant rely on non created blocks :P - for (Block block : blocks) { + // It is to stop portal blocks from destroying themself as they cant rely on non created blocks :P + for (Block block : blocks) block.setType(Material.GLOWSTONE); - } - for (Block block : blocks) { + for (Block block : blocks) block.setType(Material.PORTAL); - } return true; } + public void close() { if (from != null) @@ -121,40 +139,64 @@ public class Gate { } } + //----------------------------------------------// // Persistance and entity management //----------------------------------------------// - public static Gate get(String id) { + public static Gate get(String id) + { return instances.get(id); } - public static boolean exists(String id) { + + public static boolean exists(String id) + { return instances.containsKey(id); } - public static Gate create(String id) { + + public static Gate create(String id) + { Gate gate = new Gate(); gate.id = id; instances.put(gate.id, gate); - Plugin.log("created new gate "+gate.id); + Plugin.log("created new gate " + gate.id); //faction.save(); return gate; } - public static void delete(String id) { + + public static void rename(Gate gate, String newId) + { + delete(gate.id); + + gate.setId(newId); + instances.put(gate.id, gate); + } + + + public static void delete(String id) + { // Remove the faction instances.remove(id); } - public static boolean save() { - try { + + public static boolean save() + { + try + { DiscUtil.write(file, Plugin.gson.toJson(instances)); - } catch (IOException e) { + } + catch (IOException e) + { Plugin.log("Failed to save the gates to disk due to I/O exception."); e.printStackTrace(); return false; - } catch (NullPointerException e) { + } + catch (NullPointerException e) + { Plugin.log("Failed to save the gates to disk due to NPE."); e.printStackTrace(); return false; @@ -163,7 +205,9 @@ public class Gate { return true; } - public static boolean load() { + + public static boolean load() + { Plugin.log("Loading gates from disk"); if ( ! file.exists()) { Plugin.log("No gates to load from disk. Creating new file."); @@ -171,12 +215,15 @@ public class Gate { return true; } - try { + try + { Type type = new TypeToken>(){}.getType(); Map instancesFromFile = Plugin.gson.fromJson(DiscUtil.read(file), type); instances.clear(); instances.putAll(instancesFromFile); - } catch (IOException e) { + } + catch (IOException e) + { e.printStackTrace(); return false; } @@ -186,13 +233,16 @@ public class Gate { return true; } - public static Collection getAll() { + + public static Collection getAll() + { return instances.values(); } - public static void fillIds() { - for(Entry entry : instances.entrySet()) { + + public static void fillIds() + { + for(Entry entry : instances.entrySet()) entry.getValue().setId(entry.getKey()); - } } }