From 95e9c7c50ab8f1a20f6ff3f3b6df076f854a0985 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Fri, 21 Feb 2014 09:32:11 -0600 Subject: [PATCH] Reorganize how we save new cells, this way doesn't dupe cells any more. --- .../java/com/graywolf336/jail/JailIO.java | 125 +++++++++++++----- .../java/com/graywolf336/jail/beans/Jail.java | 2 +- 2 files changed, 90 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/graywolf336/jail/JailIO.java b/src/main/java/com/graywolf336/jail/JailIO.java index fe20af1..efa9bce 100644 --- a/src/main/java/com/graywolf336/jail/JailIO.java +++ b/src/main/java/com/graywolf336/jail/JailIO.java @@ -464,42 +464,27 @@ public class JailIO { if(con == null) this.prepareStorage(false); for(Cell c : j.getCells()) { - PreparedStatement cPS = con.prepareStatement("REPLACE INTO `" + prefix + "cells` (`name`, `jail`, `tp.x`, `tp.y`, `tp.z`, `tp.yaw`," - + "`tp.pitch`, `chest.x`, `chest.y`, `chest.z`, `signs`) VALUES (?,?,?,?,?,?,?,?,?,?,?)"); - - cPS.setString(1, c.getName()); - cPS.setString(2, j.getName()); - cPS.setDouble(3, c.getTeleport().getX()); - cPS.setDouble(4, c.getTeleport().getY()); - cPS.setDouble(5, c.getTeleport().getZ()); - cPS.setDouble(6, c.getTeleport().getYaw()); - cPS.setDouble(7, c.getTeleport().getPitch()); - cPS.setInt(8, c.getChestLocation().getBlockX()); - cPS.setInt(9, c.getChestLocation().getBlockY()); - cPS.setInt(10, c.getChestLocation().getBlockZ()); - cPS.setString(11, c.getSignString()); - - cPS.executeUpdate(); - cPS.close(); - - Prisoner p = c.getPrisoner(); - PreparedStatement pPS = con.prepareStatement("REPLACE INTO `" + prefix + "prisoners` (`name`, `jail`, `cell`, `muted`, `time`," - + "`offlinePending`, `toBeTransferred`, `jailer`, `reason`, `inventory`, `armor`, `previousLocation`, `previousGameMode`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"); - pPS.setString(1, p.getName()); - pPS.setString(2, j.getName()); - pPS.setString(3, c.getName()); - pPS.setBoolean(4, p.isMuted()); - pPS.setFloat(5, p.getRemainingTime()); - pPS.setBoolean(6, p.isOfflinePending()); - pPS.setBoolean(7, p.isToBeTransferred()); - pPS.setString(8, p.getReason()); - pPS.setBytes(9, p.getInventory().getBytes()); - pPS.setBytes(10, p.getArmor().getBytes()); - pPS.setString(11, p.getPreviousLocationString()); - pPS.setString(12, p.getPreviousGameMode().toString()); - - pPS.executeUpdate(); - pPS.close(); + if(c.hasPrisoner()) { + Prisoner p = c.getPrisoner(); + PreparedStatement pPS = con.prepareStatement("REPLACE INTO `" + prefix + "prisoners` (`name`, `jail`, `cell`, `muted`, `time`," + + "`offlinePending`, `toBeTransferred`, `jailer`, `reason`, `inventory`, `armor`, `previousLocation`, `previousGameMode`)" + + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"); + pPS.setString(1, p.getName()); + pPS.setString(2, j.getName()); + pPS.setString(3, c.getName()); + pPS.setBoolean(4, p.isMuted()); + pPS.setFloat(5, p.getRemainingTime()); + pPS.setBoolean(6, p.isOfflinePending()); + pPS.setBoolean(7, p.isToBeTransferred()); + pPS.setString(8, p.getReason()); + pPS.setBytes(9, p.getInventory().getBytes()); + pPS.setBytes(10, p.getArmor().getBytes()); + pPS.setString(11, p.getPreviousLocationString()); + pPS.setString(12, p.getPreviousGameMode().toString()); + + pPS.executeUpdate(); + pPS.close(); + } } con.commit(); @@ -746,6 +731,74 @@ public class JailIO { 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) { + switch(storage) { + case 1: + case 2: + try { + if(con == null) this.prepareStorage(false); + + PreparedStatement cPS = con.prepareStatement("INSERT INTO `" + prefix + "cells` (`name`, `jail`, `tp.x`, `tp.y`, `tp.z`, `tp.yaw`," + + "`tp.pitch`, `chest.x`, `chest.y`, `chest.z`, `signs`) VALUES (?,?,?,?,?,?,?,?,?,?,?)"); + + cPS.setString(1, c.getName()); + cPS.setString(2, j.getName()); + cPS.setDouble(3, c.getTeleport().getX()); + cPS.setDouble(4, c.getTeleport().getY()); + cPS.setDouble(5, c.getTeleport().getZ()); + cPS.setDouble(6, c.getTeleport().getYaw()); + cPS.setDouble(7, c.getTeleport().getPitch()); + + if(c.hasChest()) { + cPS.setInt(8, c.getChestLocation().getBlockX()); + cPS.setInt(9, c.getChestLocation().getBlockY()); + cPS.setInt(10, c.getChestLocation().getBlockZ()); + }else { + cPS.setNull(8, java.sql.Types.INTEGER); + cPS.setNull(9, java.sql.Types.INTEGER); + cPS.setNull(10, java.sql.Types.INTEGER); + } + + cPS.setString(11, c.getSignString()); + + cPS.executeUpdate(); + cPS.close(); + + if(c.hasPrisoner()) { + Prisoner p = c.getPrisoner(); + PreparedStatement pPS = con.prepareStatement("REPLACE INTO `" + prefix + "prisoners` (`name`, `jail`, `cell`, `muted`, `time`," + + "`offlinePending`, `toBeTransferred`, `jailer`, `reason`, `inventory`, `armor`, `previousLocation`, `previousGameMode`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"); + pPS.setString(1, p.getName()); + pPS.setString(2, j.getName()); + pPS.setString(3, c.getName()); + pPS.setBoolean(4, p.isMuted()); + pPS.setFloat(5, p.getRemainingTime()); + pPS.setBoolean(6, p.isOfflinePending()); + pPS.setBoolean(7, p.isToBeTransferred()); + pPS.setString(8, p.getReason()); + pPS.setBytes(9, p.getInventory().getBytes()); + pPS.setBytes(10, p.getArmor().getBytes()); + pPS.setString(11, p.getPreviousLocationString()); + pPS.setString(12, p.getPreviousGameMode().toString()); + + pPS.executeUpdate(); + pPS.close(); + } + + con.commit(); + } catch (SQLException e) { + e.printStackTrace(); + pl.getLogger().severe("---------- Jail Error!!! ----------"); + pl.getLogger().severe("Error while saving the cell '" + c.getName() + "' of the Jail '" + j.getName() + "', please check the error and fix what is wrong."); + } + + break; + default: + this.saveJail(j); + break; + } + } + /** * Removes the prisoner from the storage system. * diff --git a/src/main/java/com/graywolf336/jail/beans/Jail.java b/src/main/java/com/graywolf336/jail/beans/Jail.java index 9e86edc..544bec1 100644 --- a/src/main/java/com/graywolf336/jail/beans/Jail.java +++ b/src/main/java/com/graywolf336/jail/beans/Jail.java @@ -144,7 +144,7 @@ public class Jail { /** Adds a cell to the Jail. */ public void addCell(Cell cell, boolean save) { - if(save) plugin.getJailIO().saveJail(this); + if(save) plugin.getJailIO().saveCell(this, cell); this.cells.put(cell.getName(), cell); }