Improved jumpgates exception handling
This commit is contained in:
parent
72dafe9665
commit
78e2d187e2
1 changed files with 138 additions and 136 deletions
|
@ -9,33 +9,20 @@ 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<Jumpgate> 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();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
loadGates();
|
||||
} catch (IOException exception) {
|
||||
Logger.getLogger(JumpgatesRegistry.class.getName()).log(Level.SEVERE, null, exception);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveGates() throws IOException {
|
||||
public void saveGates() {
|
||||
try {
|
||||
PrintWriter out = new PrintWriter(new FileWriter(file));
|
||||
|
||||
// Write each string in the array on a separate line
|
||||
|
@ -44,10 +31,23 @@ public final class JumpgatesRegistry {
|
|||
}
|
||||
|
||||
out.close();
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
WarpDrive.logger.error("Exception while saving jumpgates to disk");
|
||||
}
|
||||
}
|
||||
|
||||
public void loadGates() throws IOException {
|
||||
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;
|
||||
|
@ -58,13 +58,17 @@ public final class JumpgatesRegistry {
|
|||
|
||||
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(Jumpgate jg) {
|
||||
public void addGate(final Jumpgate jg) {
|
||||
gates.add(jg);
|
||||
}
|
||||
|
||||
public boolean addGate(String name, int x, int y, int z) {
|
||||
public boolean addGate(final String name, final int x, final int y, final int z) {
|
||||
// Gate already exists
|
||||
if (findGateByName(name) != null) {
|
||||
return false;
|
||||
|
@ -72,16 +76,12 @@ public final class JumpgatesRegistry {
|
|||
|
||||
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) {
|
||||
public void removeGate(final String name) {
|
||||
Jumpgate jg;
|
||||
|
||||
for (int i = 0; i < gates.size(); i++) {
|
||||
|
@ -94,11 +94,7 @@ public final class JumpgatesRegistry {
|
|||
}
|
||||
}
|
||||
|
||||
try {
|
||||
saveGates();
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(JumpgatesRegistry.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Jumpgate findGateByName(String name) {
|
||||
|
@ -122,15 +118,21 @@ public final class JumpgatesRegistry {
|
|||
}
|
||||
|
||||
public String commaList() {
|
||||
String result = "";
|
||||
if (gates.isEmpty()) {
|
||||
result += "<none> (check /generate to create one)";
|
||||
} else {
|
||||
return "<none> (check /generate to create one)";
|
||||
}
|
||||
|
||||
final StringBuilder result = new StringBuilder();
|
||||
boolean isFirst = true;
|
||||
for (Jumpgate jg : gates) {
|
||||
result += jg.toNiceString() + ",";
|
||||
if (isFirst) {
|
||||
isFirst = false;
|
||||
} else {
|
||||
result.append(", ");
|
||||
}
|
||||
result.append(jg.toNiceString());
|
||||
}
|
||||
return result;
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public Jumpgate findNearestGate(int x, int y, int z) {
|
||||
|
|
Loading…
Reference in a new issue