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.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
public final class JumpgatesRegistry {
|
public final class JumpgatesRegistry {
|
||||||
|
|
||||||
private File file;
|
private File file;
|
||||||
private ArrayList<Jumpgate> gates = new ArrayList<>();
|
private ArrayList<Jumpgate> gates = new ArrayList<>();
|
||||||
|
|
||||||
public JumpgatesRegistry() {
|
public JumpgatesRegistry() {
|
||||||
file = new File("gates.txt");
|
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();
|
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));
|
PrintWriter out = new PrintWriter(new FileWriter(file));
|
||||||
|
|
||||||
// Write each string in the array on a separate line
|
// Write each string in the array on a separate line
|
||||||
|
@ -44,10 +31,23 @@ public final class JumpgatesRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
out.close();
|
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...");
|
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 bufferedreader;
|
||||||
bufferedreader = new BufferedReader(new FileReader(file));
|
bufferedreader = new BufferedReader(new FileReader(file));
|
||||||
String s1;
|
String s1;
|
||||||
|
@ -58,13 +58,17 @@ public final class JumpgatesRegistry {
|
||||||
|
|
||||||
bufferedreader.close();
|
bufferedreader.close();
|
||||||
WarpDrive.logger.info("Loaded " + gates.size() + " jump gates.");
|
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);
|
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
|
// Gate already exists
|
||||||
if (findGateByName(name) != null) {
|
if (findGateByName(name) != null) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -72,16 +76,12 @@ public final class JumpgatesRegistry {
|
||||||
|
|
||||||
addGate(new Jumpgate(name, x, y, z));
|
addGate(new Jumpgate(name, x, y, z));
|
||||||
|
|
||||||
try {
|
|
||||||
saveGates();
|
saveGates();
|
||||||
} catch (IOException ex) {
|
|
||||||
Logger.getLogger(JumpgatesRegistry.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeGate(String name) {
|
public void removeGate(final String name) {
|
||||||
Jumpgate jg;
|
Jumpgate jg;
|
||||||
|
|
||||||
for (int i = 0; i < gates.size(); i++) {
|
for (int i = 0; i < gates.size(); i++) {
|
||||||
|
@ -94,11 +94,7 @@ public final class JumpgatesRegistry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
saveGates();
|
saveGates();
|
||||||
} catch (IOException ex) {
|
|
||||||
Logger.getLogger(JumpgatesRegistry.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Jumpgate findGateByName(String name) {
|
public Jumpgate findGateByName(String name) {
|
||||||
|
@ -122,15 +118,21 @@ public final class JumpgatesRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String commaList() {
|
public String commaList() {
|
||||||
String result = "";
|
|
||||||
if (gates.isEmpty()) {
|
if (gates.isEmpty()) {
|
||||||
result += "<none> (check /generate to create one)";
|
return "<none> (check /generate to create one)";
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
final StringBuilder result = new StringBuilder();
|
||||||
|
boolean isFirst = true;
|
||||||
for (Jumpgate jg : gates) {
|
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) {
|
public Jumpgate findNearestGate(int x, int y, int z) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue