Cleanup Code and ready for OngoingFee

Former-commit-id: ac07ff44c4d9fc4a366e3a4f7e3d645a74b74e0c
This commit is contained in:
Ghost_chu committed 2018-10-27 02:20:34 +08:00
1 parent 07fa83c6c1
commit 70f06f8946
5 files changed
+83 -57

No files matched your search

@@ -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" : ""));
}
}
@@ -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();
}
}
@@ -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"));
@@ -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!");
}
}