From 7f79f826366e3e6635df31e1c3846fb46deabb94 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Thu, 20 Feb 2014 21:29:29 -0600 Subject: [PATCH] Let's try to load a jail from MySQL, #18. --- .../java/com/graywolf336/jail/JailIO.java | 227 ++++++++++-------- 1 file changed, 125 insertions(+), 102 deletions(-) diff --git a/src/main/java/com/graywolf336/jail/JailIO.java b/src/main/java/com/graywolf336/jail/JailIO.java index 545b32f..4b3d669 100644 --- a/src/main/java/com/graywolf336/jail/JailIO.java +++ b/src/main/java/com/graywolf336/jail/JailIO.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Set; @@ -327,6 +328,35 @@ public class JailIO { break; case 2: //load the jails from mysql + pl.debug("Time Now (str): " + System.currentTimeMillis()); + + try { + if(con == null) this.prepareStorage(false); + PreparedStatement ps = con.prepareStatement("SELECT * FROM " + prefix + "jails"); + ResultSet set = ps.executeQuery(); + + while(set.next()) { + Jail j = new Jail(pl, set.getString("name")); + + j.setWorld(set.getString("world")); + j.setMaxPoint(new int[] { set.getInt("top.x"), set.getInt("top.y"), set.getInt("top.z") }); + j.setMinPoint(new int[] { set.getInt("bottom.x"), set.getInt("bottom.y"), set.getInt("bottom.z") }); + j.setTeleportIn(new SimpleLocation(j.getWorldName(), set.getDouble("tps.in.x"), + set.getDouble("tps.in.y"), set.getDouble("tps.in.z"), + set.getFloat("tps.in.yaw"), set.getFloat("tps.in.pitch"))); + j.setTeleportFree(new SimpleLocation(set.getString("tps.free.world"), set.getDouble("tps.free.x"), + set.getDouble("tps.free.y"), set.getDouble("tps.free.z"), + set.getFloat("tps.free.yaw"), set.getFloat("tps.free.pitch"))); + pl.getJailManager().addJail(j, false); + } + + set.close(); + ps.close(); + }catch (SQLException e) { + + } + + pl.debug("Time Now (end): " + System.currentTimeMillis()); break; default: //load the jails from flatfile @@ -334,7 +364,7 @@ public class JailIO { Set jails = flat.getConfigurationSection("jails").getKeys(false); if(!jails.isEmpty()) { for(String name : jails) { - loadJail(name); + loadJailFromFlatFile(name); } } } @@ -579,111 +609,104 @@ public class JailIO { } } - private void loadJail(String name) { - switch(storage) { - case 1: - case 2: - break; - default: - 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 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); - } + 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 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.isConfigurationSection(node + "prisoners")) { - Set 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(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(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?"); - break; + } } + + if(flat.isConfigurationSection(node + "prisoners")) { + Set 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?"); } /**