From 70f06f89461d9a0c084ea1fca6731832fd67fee9 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Sat, 27 Oct 2018 02:20:34 +0800 Subject: [PATCH] Cleanup Code and ready for OngoingFee Former-commit-id: ac07ff44c4d9fc4a366e3a4f7e3d645a74b74e0c --- ...3.2-R0.1-SNAPSHOT-b1967.jar.REMOVED.git-id | 1 + .../quickshop/Database/DatabaseHelper.java | 21 +++- .../org/maxgamer/quickshop/QuickShop.java | 106 +++++++++--------- .../quickshop/Shop/ContainerShop.java | 10 +- .../maxgamer/quickshop/Shop/ShopManager.java | 2 +- 5 files changed, 83 insertions(+), 57 deletions(-) create mode 100644 lib/spigot-1.13.2-R0.1-SNAPSHOT-b1967.jar.REMOVED.git-id diff --git a/lib/spigot-1.13.2-R0.1-SNAPSHOT-b1967.jar.REMOVED.git-id b/lib/spigot-1.13.2-R0.1-SNAPSHOT-b1967.jar.REMOVED.git-id new file mode 100644 index 000000000..8d5e26a89 --- /dev/null +++ b/lib/spigot-1.13.2-R0.1-SNAPSHOT-b1967.jar.REMOVED.git-id @@ -0,0 +1 @@ +3a56a0dabe54eaf7e3b57f165e065d7afb27180c \ No newline at end of file diff --git a/src/main/java/org/maxgamer/quickshop/Database/DatabaseHelper.java b/src/main/java/org/maxgamer/quickshop/Database/DatabaseHelper.java index 9b2acbfca..e6a66c537 100644 --- a/src/main/java/org/maxgamer/quickshop/Database/DatabaseHelper.java +++ b/src/main/java/org/maxgamer/quickshop/Database/DatabaseHelper.java @@ -1,6 +1,7 @@ package org.maxgamer.quickshop.Database; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -60,7 +61,7 @@ public class DatabaseHelper { */ public static void createShopsTable(Database db) throws SQLException { Statement st = db.getConnection().createStatement(); - String createTable = "CREATE TABLE shops (" + "owner TEXT(32) NOT NULL, " + "price double(32, 2) NOT NULL, " + "itemConfig TEXT CHARSET utf8 NOT NULL, " + "x INTEGER(32) NOT NULL, " + "y INTEGER(32) NOT NULL, " + "z INTEGER(32) NOT NULL, " + "world VARCHAR(32) NOT NULL, " + "unlimited boolean, " + "type boolean, " + "PRIMARY KEY (x, y, z, world) " + ");"; + String createTable = "CREATE TABLE shops (owner TEXT(32) NOT NULL, price double(32, 2) NOT NULL, itemConfig TEXT CHARSET utf8 NOT NULL, x INTEGER(32) NOT NULL, y INTEGER(32) NOT NULL, z INTEGER(32) NOT NULL, world VARCHAR(32) NOT NULL, unlimited boolean, type boolean, PRIMARY KEY (x, y, z, world) );"; st.execute(createTable); } @@ -72,8 +73,24 @@ public class DatabaseHelper { */ public static void createMessagesTable(Database db) throws SQLException { Statement st = db.getConnection().createStatement(); - String createTable = "CREATE TABLE messages (" + "owner TEXT(32) NOT NULL, " + "message TEXT(200) NOT NULL, " + "time BIGINT(32) NOT NULL " + ");"; + String createTable = "CREATE TABLE messages (owner TEXT(32) NOT NULL, message TEXT(200) NOT NULL, time BIGINT(32) NOT NULL );"; st.execute(createTable); } + public static ResultSet selectAllShops(Database db) throws SQLException { + PreparedStatement ps = db.getConnection().prepareStatement("SELECT * FROM shops"); + return ps.executeQuery(); + + } + public static void removeShop(Database db, int x, int y, int z, String worldName) throws SQLException { + db.getConnection().createStatement() + .executeUpdate("DELETE FROM shops WHERE x = " + x + " AND y = " + y + " AND z = " + z + + " AND world = \"" + worldName + "\"" + + (db.getCore() instanceof MySQLCore ? " LIMIT 1" : "")); + db.getConnection().createStatement() + .executeUpdate("DELETE FROM schedule WHERE x = " + x + " AND y = " + y + " AND z = " + z + + " AND world = \"" + worldName + "\"" + + (db.getCore() instanceof MySQLCore ? " LIMIT 1" : "")); + } + } \ No newline at end of file diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 195327395..ae37a57a3 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -5,8 +5,6 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; -import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -283,7 +281,6 @@ public class QuickShop extends JavaPlugin { } /* Load shops from database to memory */ int count = 0; // Shops count - Connection con; try { getLogger().info("Loading shops from database..."); /* @@ -292,9 +289,9 @@ public class QuickShop extends JavaPlugin { * return; } if (res > 0) { plugin.getLogger().log(Level.WARNING, * "Conversion success. Continuing..."); } */ - con = database.getConnection(); - PreparedStatement ps = con.prepareStatement("SELECT * FROM shops"); - ResultSet rs = ps.executeQuery(); +// con = database.getConnection(); +// PreparedStatement ps = con.prepareStatement("SELECT * FROM shops"); + ResultSet rs = DatabaseHelper.selectAllShops(database); int errors = 0; // ======================== MsgUtil.loadItemi18n(); @@ -312,7 +309,7 @@ public class QuickShop extends JavaPlugin { ItemStack item = null; String owner = null; UUID ownerUUID = null; - String step = "Crycle"; + String step = "while init"; // ========================================================================================== try { x = rs.getInt("x"); @@ -345,14 +342,15 @@ public class QuickShop extends JavaPlugin { + " AND world = \"" + worldName + "\" LIMIT 1"); } else { // Invalid shop owner - getDB().getConnection().createStatement() - .executeUpdate("DELETE FROM shops WHERE x = " + x + " AND y = " + y + " AND z = " - + z + " AND world = \"" + worldName + "\"" - + (getDB().getCore() instanceof MySQLCore ? " LIMIT 1" : "")); - getDB().getConnection().createStatement() - .executeUpdate("DELETE FROM schedule WHERE x = " + x + " AND y = " + y + " AND z = " - + z + " AND world = \"" + worldName + "\"" - + (getDB().getCore() instanceof MySQLCore ? " LIMIT 1" : "")); +// getDB().getConnection().createStatement() +// .executeUpdate("DELETE FROM shops WHERE x = " + x + " AND y = " + y + " AND z = " +// + z + " AND world = \"" + worldName + "\"" +// + (getDB().getCore() instanceof MySQLCore ? " LIMIT 1" : "")); +// getDB().getConnection().createStatement() +// .executeUpdate("DELETE FROM schedule WHERE x = " + x + " AND y = " + y + " AND z = " +// + z + " AND world = \"" + worldName + "\"" +// + (getDB().getCore() instanceof MySQLCore ? " LIMIT 1" : "")); + DatabaseHelper.removeShop(database, x, y, z, worldName); continue; } } @@ -366,14 +364,15 @@ public class QuickShop extends JavaPlugin { step = "Removeing shop in world: Because it not a correct InventoryHolder"; getLogger().info("Shop is not an InventoryHolder in " + rs.getString("world") + " at: " + x + ", " + y + ", " + z + ". Deleting."); - getDB().getConnection().createStatement() - .executeUpdate("DELETE FROM shops WHERE x = " + x + " AND y = " + y + " AND z = " + z - + " AND world = \"" + worldName + "\"" - + (getDB().getCore() instanceof MySQLCore ? " LIMIT 1" : "")); - getDB().getConnection().createStatement() - .executeUpdate("DELETE FROM schedule WHERE x = " + x + " AND y = " + y + " AND z = " + z - + " AND world = \"" + worldName + "\"" - + (getDB().getCore() instanceof MySQLCore ? " LIMIT 1" : "")); +// getDB().getConnection().createStatement() +// .executeUpdate("DELETE FROM shops WHERE x = " + x + " AND y = " + y + " AND z = " + z +// + " AND world = \"" + worldName + "\"" +// + (getDB().getCore() instanceof MySQLCore ? " LIMIT 1" : "")); +// getDB().getConnection().createStatement() +// .executeUpdate("DELETE FROM schedule WHERE x = " + x + " AND y = " + y + " AND z = " + z +// + " AND world = \"" + worldName + "\"" +// + (getDB().getCore() instanceof MySQLCore ? " LIMIT 1" : "")); + DatabaseHelper.removeShop(database, x, y, z, worldName); continue; } step = "Loading shop type"; @@ -505,41 +504,44 @@ public class QuickShop extends JavaPlugin { if (errors < 3) { getLogger().info("Removeing shop from database..."); // return; - PreparedStatement delps = getDB().getConnection() - .prepareStatement("DELETE FROM shops WHERE x = ? AND y = ? and z = ? and world = ?"); - delps.setInt(1, x); - delps.setInt(2, y); - delps.setInt(3, z); - delps.setString(4, worldName); - delps.execute(); +// PreparedStatement delps = getDB().getConnection() +// .prepareStatement("DELETE FROM shops WHERE x = ? AND y = ? and z = ? and world = ?"); +// delps.setInt(1, x); +// delps.setInt(2, y); +// delps.setInt(3, z); +// delps.setString(4, worldName); +// delps.execute(); - PreparedStatement scdelps = getDB().getConnection() - .prepareStatement("DELETE FROM schedule WHERE x = ? AND y = ? and z = ? and world = ?"); - scdelps.setInt(1, x); - scdelps.setInt(2, y); - scdelps.setInt(3, z); - scdelps.setString(4, worldName); - scdelps.execute(); + DatabaseHelper.removeShop(database, x, y, z, worldName); +// +// PreparedStatement scdelps = getDB().getConnection() +// .prepareStatement("DELETE FROM schedule WHERE x = ? AND y = ? and z = ? and world = ?"); +// scdelps.setInt(1, x); +// scdelps.setInt(2, y); +// scdelps.setInt(3, z); +// scdelps.setString(4, worldName); +// scdelps.execute(); getLogger().info("Trying keep loading..."); } else { getLogger().severe( "Multiple errors in shops - Something seems to be wrong with your shops database! Please check it out immediately!"); getLogger().info("Removeing shop from database..."); - PreparedStatement delps = getDB().getConnection() - .prepareStatement("DELETE FROM shops WHERE x = ? AND y = ? and z = ? and world = ?"); - delps.setInt(1, x); - delps.setInt(2, y); - delps.setInt(3, z); - delps.setString(4, worldName); - delps.execute(); - - PreparedStatement scdelps = getDB().getConnection() - .prepareStatement("DELETE FROM schedule WHERE x = ? AND y = ? and z = ? and world = ?"); - scdelps.setInt(1, x); - scdelps.setInt(2, y); - scdelps.setInt(3, z); - scdelps.setString(4, worldName); - scdelps.execute(); +// PreparedStatement delps = getDB().getConnection() +// .prepareStatement("DELETE FROM shops WHERE x = ? AND y = ? and z = ? and world = ?"); +// delps.setInt(1, x); +// delps.setInt(2, y); +// delps.setInt(3, z); +// delps.setString(4, worldName); +// delps.execute(); +// +// PreparedStatement scdelps = getDB().getConnection() +// .prepareStatement("DELETE FROM schedule WHERE x = ? AND y = ? and z = ? and world = ?"); +// scdelps.setInt(1, x); +// scdelps.setInt(2, y); +// scdelps.setInt(3, z); +// scdelps.setString(4, worldName); +// scdelps.execute(); + DatabaseHelper.removeShop(database, x, y, z, worldName); e.printStackTrace(); } } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index b6a81f3b2..f7e55b6a6 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -1,5 +1,6 @@ package org.maxgamer.quickshop.Shop; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -20,6 +21,7 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.maxgamer.quickshop.QuickShop; +import org.maxgamer.quickshop.Database.DatabaseHelper; import org.maxgamer.quickshop.Util.MsgUtil; import org.maxgamer.quickshop.Util.Util; @@ -577,8 +579,12 @@ public class ContainerShop implements Shop { int y = this.getLocation().getBlockY(); int z = this.getLocation().getBlockZ(); String world = this.getLocation().getWorld().getName(); - plugin.getDB().execute("DELETE FROM shops WHERE x = '" + x + "' AND y = '" + y + "' AND z = '" + z + "' AND world = '" + world + "'"); - plugin.getDB().execute("DELETE FROM schedule WHERE x = '" + x + "' AND y = '" + y + "' AND z = '" + z + "' AND world = '" + world + "'"); + try { + DatabaseHelper.removeShop(plugin.getDB(), x, y, z, world); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } // Refund if necessary if (plugin.getConfig().getBoolean("shop.refund")) { plugin.getEcon().deposit(this.getOwner(), plugin.getConfig().getDouble("shop.cost")); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java index 45a98ce51..cf220d863 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java @@ -384,7 +384,7 @@ public class ShopManager { } } } catch (Exception e2) { - e2.getMessage(); + e2.printStackTrace(); plugin.getLogger().log(Level.WARNING,"QuickShop can't pay tax to account in config.yml,Please set tax account name to a exist player!"); } }