Improved jumpgates exception handling

This commit is contained in:
LemADEC 2017-05-18 03:06:29 +02:00
parent 72dafe9665
commit 78e2d187e2

View file

@ -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<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();
private File file;
private ArrayList<Jumpgate> 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 += "<none> (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 "<none> (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;
}
}