From 78e2d187e262f60c97bb7a893e12056f1bfe350a Mon Sep 17 00:00:00 2001 From: LemADEC Date: Thu, 18 May 2017 03:06:29 +0200 Subject: [PATCH] Improved jumpgates exception handling --- .../warpdrive/data/JumpgatesRegistry.java | 274 +++++++++--------- 1 file changed, 138 insertions(+), 136 deletions(-) diff --git a/src/main/java/cr0s/warpdrive/data/JumpgatesRegistry.java b/src/main/java/cr0s/warpdrive/data/JumpgatesRegistry.java index a00c960a..0f46d3a3 100644 --- a/src/main/java/cr0s/warpdrive/data/JumpgatesRegistry.java +++ b/src/main/java/cr0s/warpdrive/data/JumpgatesRegistry.java @@ -9,146 +9,148 @@ import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; -import java.util.logging.Level; -import java.util.logging.Logger; public final class JumpgatesRegistry { - private File file; - private ArrayList gates = new ArrayList<>(); - - public JumpgatesRegistry() { - file = new File("gates.txt"); - WarpDrive.logger.info("Opening gates file '" + file + "'"); - - if (file != null && !file.exists()) { - try { - file.createNewFile(); - } catch (IOException exception) { - exception.printStackTrace(); + + private File file; + private ArrayList gates = new ArrayList<>(); + + public JumpgatesRegistry() { + file = new File("gates.txt"); + + loadGates(); + } + + public void saveGates() { + try { + PrintWriter out = new PrintWriter(new FileWriter(file)); + + // Write each string in the array on a separate line + for (Jumpgate jg : gates) { + out.println(jg); } - } + + out.close(); + } catch (IOException exception) { + exception.printStackTrace(); + WarpDrive.logger.error("Exception while saving jumpgates to disk"); + } + } + + public void loadGates() { + WarpDrive.logger.info("Loading jump gates from gates.txt..."); + try { + if (file != null && !file.exists()) { + try { + file.createNewFile(); + } catch (IOException exception) { + exception.printStackTrace(); + } + } + + BufferedReader bufferedreader; + bufferedreader = new BufferedReader(new FileReader(file)); + String s1; + + while ((s1 = bufferedreader.readLine()) != null) { + gates.add(new Jumpgate(s1)); + } + + bufferedreader.close(); + WarpDrive.logger.info("Loaded " + gates.size() + " jump gates."); + } catch (IOException exception) { + exception.printStackTrace(); + WarpDrive.logger.error("Exception while loading jumpgates from disk"); + } + } + + public void addGate(final Jumpgate jg) { + gates.add(jg); + } + + public boolean addGate(final String name, final int x, final int y, final int z) { + // Gate already exists + if (findGateByName(name) != null) { + return false; + } - try { - loadGates(); - } catch (IOException exception) { - Logger.getLogger(JumpgatesRegistry.class.getName()).log(Level.SEVERE, null, exception); - } - } - - public void saveGates() throws IOException { - PrintWriter out = new PrintWriter(new FileWriter(file)); - - // Write each string in the array on a separate line - for (Jumpgate jg : gates) { - out.println(jg); - } - - out.close(); - } - - public void loadGates() throws IOException { - WarpDrive.logger.info("Loading jump gates from gates.txt..."); - BufferedReader bufferedreader; - bufferedreader = new BufferedReader(new FileReader(file)); - String s1; - - while ((s1 = bufferedreader.readLine()) != null) { - gates.add(new Jumpgate(s1)); - } - - bufferedreader.close(); - WarpDrive.logger.info("Loaded " + gates.size() + " jump gates."); - } - - public void addGate(Jumpgate jg) { - gates.add(jg); - } - - public boolean addGate(String name, int x, int y, int z) { - // Gate already exists - if (findGateByName(name) != null) { - return false; - } - - addGate(new Jumpgate(name, x, y, z)); - - try { - saveGates(); - } catch (IOException ex) { - Logger.getLogger(JumpgatesRegistry.class.getName()).log(Level.SEVERE, null, ex); - } - - return true; - } - - public void removeGate(String name) { - Jumpgate jg; - - for (int i = 0; i < gates.size(); i++) { - jg = gates.get(i); - - if (jg.name.equalsIgnoreCase(name)) - { - gates.remove(i); - return; - } - } - - try { - saveGates(); - } catch (IOException ex) { - Logger.getLogger(JumpgatesRegistry.class.getName()).log(Level.SEVERE, null, ex); - } - } - - public Jumpgate findGateByName(String name) { - for (Jumpgate jg : gates) { - if (jg.name.equalsIgnoreCase(name)) { - return jg; - } - } - - return null; - } - - public String JumpgatesList() { - String result = ""; - - for (Jumpgate jg : gates) { - result += jg.toNiceString() + "\n"; - } - - return result; - } - - public String commaList() { - String result = ""; - if (gates.isEmpty()) { - result += " (check /generate to create one)"; - } else { - for (Jumpgate jg : gates) { - result += jg.toNiceString() + ","; - } - } - return result; - } - - public Jumpgate findNearestGate(int x, int y, int z) { - double minDistance2 = -1; - Jumpgate res = null; + addGate(new Jumpgate(name, x, y, z)); - for (Jumpgate jg : gates) { - double dX = jg.xCoord - x; - double dY = jg.yCoord - y; - double dZ = jg.zCoord - z; - double distance2 = dX * dX + dY * dY + dZ * dZ; - - if ((minDistance2 == -1) || (distance2 < minDistance2)) { - minDistance2 = distance2; - res = jg; - } - } + saveGates(); + + return true; + } + + public void removeGate(final String name) { + Jumpgate jg; - return res; - } + for (int i = 0; i < gates.size(); i++) { + jg = gates.get(i); + + if (jg.name.equalsIgnoreCase(name)) + { + gates.remove(i); + return; + } + } + + saveGates(); + } + + public Jumpgate findGateByName(String name) { + for (Jumpgate jg : gates) { + if (jg.name.equalsIgnoreCase(name)) { + return jg; + } + } + + return null; + } + + public String JumpgatesList() { + String result = ""; + + for (Jumpgate jg : gates) { + result += jg.toNiceString() + "\n"; + } + + return result; + } + + public String commaList() { + if (gates.isEmpty()) { + return " (check /generate to create one)"; + } + + final StringBuilder result = new StringBuilder(); + boolean isFirst = true; + for (Jumpgate jg : gates) { + if (isFirst) { + isFirst = false; + } else { + result.append(", "); + } + result.append(jg.toNiceString()); + } + return result.toString(); + } + + public Jumpgate findNearestGate(int x, int y, int z) { + double minDistance2 = -1; + Jumpgate res = null; + + for (Jumpgate jg : gates) { + double dX = jg.xCoord - x; + double dY = jg.yCoord - y; + double dZ = jg.zCoord - z; + double distance2 = dX * dX + dY * dY + dZ * dZ; + + if ((minDistance2 == -1) || (distance2 < minDistance2)) { + minDistance2 = distance2; + res = jg; + } + } + + return res; + } }