Some clean up work on the JailIO class.

This commit is contained in:
graywolf336 2014-03-06 14:04:11 -06:00
parent 02a4e206cb
commit ac6a5b23cb

View File

@ -126,7 +126,7 @@ public class JailIO {
Class.forName("org.sqlite.JDBC"); Class.forName("org.sqlite.JDBC");
pl.getLogger().info("Connecting to the sqlite database."); pl.getLogger().info("Connecting to the sqlite database.");
Connection sqliteConnection = DriverManager.getConnection("jdbc:sqlite:" + new File(pl.getDataFolder().getPath(), "jail.sqlite").getPath()); Connection sqliteConnection = DriverManager.getConnection("jdbc:sqlite:" + new File(pl.getDataFolder().getPath(), "jail.sqlite").getPath());
sqliteConnection.setAutoCommit(false); sqliteConnection.setAutoCommit(true);
this.con = sqliteConnection; this.con = sqliteConnection;
pl.debug("Connection created for sqlite."); pl.debug("Connection created for sqlite.");
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@ -185,6 +185,14 @@ public class JailIO {
case 1: case 1:
case 2: case 2:
if(con == null) this.prepareStorage(false); if(con == null) this.prepareStorage(false);
try {
if(!con.isValid(10)) this.prepareStorage(false);
} catch (SQLException e) {
e.printStackTrace();
pl.getLogger().severe("---------- Jail Error!!! ----------");
pl.getLogger().severe("Unable to get a Sql connection, please see the error above and fix the problem.");
return null;
}
return con; return con;
default: default:
return null; return null;
@ -308,8 +316,6 @@ public class JailIO {
//pl.debug(proCreateCmd); //pl.debug(proCreateCmd);
st.executeUpdate(proCreateCmd); st.executeUpdate(proCreateCmd);
con.commit();
st.close(); st.close();
break; break;
default: default:
@ -367,6 +373,7 @@ public class JailIO {
//that doesn't exist anymore //that doesn't exist anymore
List<Integer> cellsToRemove = new LinkedList<Integer>(); List<Integer> cellsToRemove = new LinkedList<Integer>();
int cs = 0;
try { try {
if(con == null) this.prepareStorage(false); if(con == null) this.prepareStorage(false);
PreparedStatement ps = con.prepareStatement("SELECT * FROM " + prefix + "cells"); PreparedStatement ps = con.prepareStatement("SELECT * FROM " + prefix + "cells");
@ -389,6 +396,7 @@ public class JailIO {
} }
j.addCell(c, false); j.addCell(c, false);
cs++;
}else { }else {
cellsToRemove.add(set.getInt("cellid")); cellsToRemove.add(set.getInt("cellid"));
} }
@ -427,11 +435,14 @@ public class JailIO {
} }
} }
pl.getLogger().info("Loaded " + cs + (cs == 1 ? " cell." : " cells."));
//This list contains a string which refers to the name of the prisoner in sql //This list contains a string which refers to the name of the prisoner in sql
//this list only gets populated if there are prisoners which reference a jail //this list only gets populated if there are prisoners which reference a jail
//that doesn't exist anymore //that doesn't exist anymore
List<String> prisonersToRemove = new LinkedList<String>(); List<String> prisonersToRemove = new LinkedList<String>();
int pc = 0;
try { try {
if(con == null) this.prepareStorage(false); if(con == null) this.prepareStorage(false);
PreparedStatement ps = con.prepareStatement("SELECT * FROM " + prefix + "prisoners"); PreparedStatement ps = con.prepareStatement("SELECT * FROM " + prefix + "prisoners");
@ -462,6 +473,8 @@ public class JailIO {
//the prisoner is assigned to a cell which doesn't exist, so just put them into the jail //the prisoner is assigned to a cell which doesn't exist, so just put them into the jail
j.addPrisoner(p); j.addPrisoner(p);
} }
pc++;
} else { } else {
//if the jail doesn't exist, do the same as the cells //if the jail doesn't exist, do the same as the cells
prisonersToRemove.add(set.getString("name")); prisonersToRemove.add(set.getString("name"));
@ -501,6 +514,8 @@ public class JailIO {
} }
} }
pl.getLogger().info("Loaded " + pc + (pc == 1 ? " prisoner." : " prisoners."));
pl.debug("Took " + (System.currentTimeMillis() - st) + " millis."); pl.debug("Took " + (System.currentTimeMillis() - st) + " millis.");
break; break;
default: default:
@ -516,8 +531,108 @@ public class JailIO {
break; break;
} }
int s = pl.getJailManager().getJails().size(); int js = pl.getJailManager().getJails().size();
pl.getLogger().info("Loaded " + s + (s == 1 ? " jail." : " jails.")); pl.getLogger().info("Loaded " + js + (js == 1 ? " jail." : " jails."));
}
private void loadJailFromFlatFile(String name) {
String node = "jails." + name + ".";
String cNode = node + "cells.";
Jail j = new Jail(pl, name);
j.setWorld(flat.getString(node + "world"));
j.setMaxPoint(new int[] {flat.getInt(node + "top.x"), flat.getInt(node + "top.y"), flat.getInt(node + "top.z")});
j.setMinPoint(new int[] {flat.getInt(node + "bottom.x"), flat.getInt(node + "bottom.y"), flat.getInt(node + "bottom.z")});
j.setTeleportIn(new SimpleLocation(
flat.getString(node + "world"),
flat.getDouble(node + "tps.in.x"),
flat.getDouble(node + "tps.in.y"),
flat.getDouble(node + "tps.in.z"),
(float) flat.getDouble(node + "tps.in.yaw"),
(float) flat.getDouble(node + "tps.in.pitch")));
j.setTeleportFree(new SimpleLocation(
flat.getString(node + "tps.free.world"),
flat.getDouble(node + "tps.free.x"),
flat.getDouble(node + "tps.free.y"),
flat.getDouble(node + "tps.free.z"),
(float) flat.getDouble(node + "tps.free.yaw"),
(float) flat.getDouble(node + "tps.free.pitch")));
if(flat.isConfigurationSection(node + "cells")) {
Set<String> cells = flat.getConfigurationSection(node + "cells").getKeys(false);
if(!cells.isEmpty()) {
for(String cell : cells) {
Cell c = new Cell(cell);
String cellNode = cNode + cell + ".";
c.setTeleport(new SimpleLocation(j.getTeleportIn().getWorld().getName(),
flat.getDouble(cellNode + "tp.x"),
flat.getDouble(cellNode + "tp.y"),
flat.getDouble(cellNode + "tp.z"),
(float) flat.getDouble(cellNode + "tp.yaw"),
(float) flat.getDouble(cellNode + "tp.pitch")));
c.setChestLocation(new Location(j.getTeleportIn().getWorld(),
flat.getInt(cellNode + "chest.x"),
flat.getInt(cellNode + "chest.y"),
flat.getInt(cellNode + "chest.z")));
for(String sign : flat.getStringList(cellNode + "signs")) {
String[] arr = sign.split(",");
c.addSign(new SimpleLocation(arr[0],
Double.valueOf(arr[1]),
Double.valueOf(arr[2]),
Double.valueOf(arr[3]),
Float.valueOf(arr[4]),
Float.valueOf(arr[5])));
}
if(flat.contains(cellNode + "prisoner")) {
Prisoner p = new Prisoner(flat.getString(cellNode + "prisoner.name"),
flat.getBoolean(cellNode + "prisoner.muted"),
flat.getLong(cellNode + "prisoner.time"),
flat.getString(cellNode + "prisoner.jailer"),
flat.getString(cellNode + "prisoner.reason"));
p.setOfflinePending(flat.getBoolean(cellNode + "prisoner.offlinePending"));
p.setToBeTransferred(flat.getBoolean(cellNode + "prisoner.toBeTransferred"));
p.setPreviousPosition(flat.getString(cellNode + "prisoner.previousLocation"));
p.setPreviousGameMode(flat.getString(cellNode + "prisoner.previousGameMode"));
p.setInventory(flat.getString(cellNode + "prisoner.inventory", ""));
p.setArmor(flat.getString(cellNode + "prisoner.armor", ""));
c.setPrisoner(p);
}
j.addCell(c, false);
}
}
}
if(flat.isConfigurationSection(node + "prisoners")) {
Set<String> prisoners = flat.getConfigurationSection(node + "prisoners").getKeys(false);
if(!prisoners.isEmpty()) {
for(String prisoner : prisoners) {
String pNode = node + "prisoners." + prisoner + ".";
Prisoner pris = new Prisoner(prisoner,
flat.getBoolean(pNode + "muted"),
flat.getLong(pNode + "time"),
flat.getString(pNode + "jailer"),
flat.getString(pNode + "reason"));
pris.setOfflinePending(flat.getBoolean(pNode + "offlinePending"));
pris.setToBeTransferred(flat.getBoolean(pNode + "toBeTransferred"));
pris.setPreviousPosition(flat.getString(pNode + "previousLocation"));
pris.setPreviousGameMode(flat.getString(pNode + "previousGameMode"));
pris.setInventory(flat.getString(pNode + "inventory", ""));
pris.setArmor(flat.getString(pNode + "armor", ""));
j.addPrisoner(pris);
}
}
}
if(pl.getServer().getWorld(j.getWorldName()) != null) {
pl.getJailManager().addJail(j, false);
pl.getLogger().info("Loaded jail " + j.getName() + " with " + j.getAllPrisoners().size() + " prisoners and " + j.getCellCount() + " cells.");
} else
pl.getLogger().severe("Failed to load the jail " + j.getName() + " as the world '" + j.getWorldName() + "' does not exist (is null). Did you remove this world?");
} }
/** /**
@ -528,6 +643,7 @@ public class JailIO {
public void saveJail(Jail j) { public void saveJail(Jail j) {
switch(storage) { switch(storage) {
case 1: case 1:
break;
case 2: case 2:
long st = System.currentTimeMillis(); long st = System.currentTimeMillis();
@ -561,7 +677,6 @@ public class JailIO {
ps.executeUpdate(); ps.executeUpdate();
ps.close(); ps.close();
con.commit();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
pl.getLogger().severe("---------- Jail Error!!! ----------"); pl.getLogger().severe("---------- Jail Error!!! ----------");
@ -595,8 +710,6 @@ public class JailIO {
pPS.close(); pPS.close();
} }
} }
con.commit();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
pl.getLogger().severe("---------- Jail Error!!! ----------"); pl.getLogger().severe("---------- Jail Error!!! ----------");
@ -626,8 +739,6 @@ public class JailIO {
pPS.executeUpdate(); pPS.executeUpdate();
pPS.close(); pPS.close();
} }
con.commit();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
pl.getLogger().severe("---------- Jail Error!!! ----------"); pl.getLogger().severe("---------- Jail Error!!! ----------");
@ -740,109 +851,10 @@ public class JailIO {
} }
} }
private void loadJailFromFlatFile(String name) {
String node = "jails." + name + ".";
String cNode = node + "cells.";
Jail j = new Jail(pl, name);
j.setWorld(flat.getString(node + "world"));
j.setMaxPoint(new int[] {flat.getInt(node + "top.x"), flat.getInt(node + "top.y"), flat.getInt(node + "top.z")});
j.setMinPoint(new int[] {flat.getInt(node + "bottom.x"), flat.getInt(node + "bottom.y"), flat.getInt(node + "bottom.z")});
j.setTeleportIn(new SimpleLocation(
flat.getString(node + "world"),
flat.getDouble(node + "tps.in.x"),
flat.getDouble(node + "tps.in.y"),
flat.getDouble(node + "tps.in.z"),
(float) flat.getDouble(node + "tps.in.yaw"),
(float) flat.getDouble(node + "tps.in.pitch")));
j.setTeleportFree(new SimpleLocation(
flat.getString(node + "tps.free.world"),
flat.getDouble(node + "tps.free.x"),
flat.getDouble(node + "tps.free.y"),
flat.getDouble(node + "tps.free.z"),
(float) flat.getDouble(node + "tps.free.yaw"),
(float) flat.getDouble(node + "tps.free.pitch")));
if(flat.isConfigurationSection(node + "cells")) {
Set<String> cells = flat.getConfigurationSection(node + "cells").getKeys(false);
if(!cells.isEmpty()) {
for(String cell : cells) {
Cell c = new Cell(cell);
String cellNode = cNode + cell + ".";
c.setTeleport(new SimpleLocation(j.getTeleportIn().getWorld().getName(),
flat.getDouble(cellNode + "tp.x"),
flat.getDouble(cellNode + "tp.y"),
flat.getDouble(cellNode + "tp.z"),
(float) flat.getDouble(cellNode + "tp.yaw"),
(float) flat.getDouble(cellNode + "tp.pitch")));
c.setChestLocation(new Location(j.getTeleportIn().getWorld(),
flat.getInt(cellNode + "chest.x"),
flat.getInt(cellNode + "chest.y"),
flat.getInt(cellNode + "chest.z")));
for(String sign : flat.getStringList(cellNode + "signs")) {
String[] arr = sign.split(",");
c.addSign(new SimpleLocation(arr[0],
Double.valueOf(arr[1]),
Double.valueOf(arr[2]),
Double.valueOf(arr[3]),
Float.valueOf(arr[4]),
Float.valueOf(arr[5])));
}
if(flat.contains(cellNode + "prisoner")) {
Prisoner p = new Prisoner(flat.getString(cellNode + "prisoner.name"),
flat.getBoolean(cellNode + "prisoner.muted"),
flat.getLong(cellNode + "prisoner.time"),
flat.getString(cellNode + "prisoner.jailer"),
flat.getString(cellNode + "prisoner.reason"));
p.setOfflinePending(flat.getBoolean(cellNode + "prisoner.offlinePending"));
p.setToBeTransferred(flat.getBoolean(cellNode + "prisoner.toBeTransferred"));
p.setPreviousPosition(flat.getString(cellNode + "prisoner.previousLocation"));
p.setPreviousGameMode(flat.getString(cellNode + "prisoner.previousGameMode"));
p.setInventory(flat.getString(cellNode + "prisoner.inventory", ""));
p.setArmor(flat.getString(cellNode + "prisoner.armor", ""));
c.setPrisoner(p);
}
j.addCell(c, false);
}
}
}
if(flat.isConfigurationSection(node + "prisoners")) {
Set<String> prisoners = flat.getConfigurationSection(node + "prisoners").getKeys(false);
if(!prisoners.isEmpty()) {
for(String prisoner : prisoners) {
String pNode = node + "prisoners." + prisoner + ".";
Prisoner pris = new Prisoner(prisoner,
flat.getBoolean(pNode + "muted"),
flat.getLong(pNode + "time"),
flat.getString(pNode + "jailer"),
flat.getString(pNode + "reason"));
pris.setOfflinePending(flat.getBoolean(pNode + "offlinePending"));
pris.setToBeTransferred(flat.getBoolean(pNode + "toBeTransferred"));
pris.setPreviousPosition(flat.getString(pNode + "previousLocation"));
pris.setPreviousGameMode(flat.getString(pNode + "previousGameMode"));
pris.setInventory(flat.getString(pNode + "inventory", ""));
pris.setArmor(flat.getString(pNode + "armor", ""));
j.addPrisoner(pris);
}
}
}
if(pl.getServer().getWorld(j.getWorldName()) != null) {
pl.getJailManager().addJail(j, false);
pl.getLogger().info("Loaded jail " + j.getName() + " with " + j.getAllPrisoners().size() + " prisoners and " + j.getCellCount() + " cells.");
} else
pl.getLogger().severe("Failed to load the jail " + j.getName() + " as the world '" + j.getWorldName() + "' does not exist (is null). Did you remove this world?");
}
public void saveCell(Jail j, Cell c) { public void saveCell(Jail j, Cell c) {
switch(storage) { switch(storage) {
case 1: case 1:
break;
case 2: case 2:
try { try {
if(con == null) this.prepareStorage(false); if(con == null) this.prepareStorage(false);
@ -893,8 +905,6 @@ public class JailIO {
pPS.executeUpdate(); pPS.executeUpdate();
pPS.close(); pPS.close();
} }
con.commit();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
pl.getLogger().severe("---------- Jail Error!!! ----------"); pl.getLogger().severe("---------- Jail Error!!! ----------");
@ -928,6 +938,7 @@ public class JailIO {
public void removePrisoner(Jail j, Cell c, Prisoner p) { public void removePrisoner(Jail j, Cell c, Prisoner p) {
switch(storage) { switch(storage) {
case 1: case 1:
break;
case 2: case 2:
try { try {
PreparedStatement pp = con.prepareStatement("delete from `" + prefix + "prisoners` where name = ? limit 1;"); PreparedStatement pp = con.prepareStatement("delete from `" + prefix + "prisoners` where name = ? limit 1;");