diff --git a/src/main/java/org/maxgamer/quickshop/Command/QS.java b/src/main/java/org/maxgamer/quickshop/Command/QS.java index d98056a84..4cf8a316e 100644 --- a/src/main/java/org/maxgamer/quickshop/Command/QS.java +++ b/src/main/java/org/maxgamer/quickshop/Command/QS.java @@ -725,6 +725,8 @@ public class QS implements CommandExecutor { if (s.hasPermission("quickshop.empty")) s.sendMessage(ChatColor.GREEN + "/qs empty" + ChatColor.YELLOW + " - " + MsgUtil.getMessage("command.description.empty")); if (s.hasPermission("quickshop.export")) - s.sendMessage(ChatColor.GREEN + "/qs export mysql|sqlite" + ChatColor.YELLOW + " - Exports the database to SQLite or MySQL"); + s.sendMessage(ChatColor.GREEN + "/qs export mysql|sqlite" + ChatColor.YELLOW + " - " + MsgUtil.getMessage("command.description.export")); + if (s.hasPermission("quickshop.debug")) + s.sendMessage(ChatColor.GREEN + "/qs debug" + ChatColor.YELLOW + " - " + MsgUtil.getMessage("command.description.debug")); } } \ 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 d8e1dc633..749df80bb 100644 --- a/src/main/java/org/maxgamer/quickshop/Database/DatabaseHelper.java +++ b/src/main/java/org/maxgamer/quickshop/Database/DatabaseHelper.java @@ -33,7 +33,12 @@ public class DatabaseHelper { connectCheck(); 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 schedule (owner TEXT(32) NOT NULL, world VARCHAR(32) NOT NULL, x INT NOT NULL, y INT NOT NULL, z INT NOT NULL, timestamp INT NOT NULL, PRIMARY KEY (owner, world, x, y, z, timestamp) );"; + String createTable = null; + if(QuickShop.instance.getConfig().getBoolean("database.use-varchar")) { + createTable ="CREATE TABLE "+QuickShop.instance.dbPrefix+"schedule (owner TEXT(32) NOT NULL, world VARCHAR(32) NOT NULL, x INT NOT NULL, y INT NOT NULL, z INT NOT NULL, timestamp INT NOT NULL, PRIMARY KEY (owner, world, x, y, z, timestamp) );"; + }else { + createTable ="CREATE TABLE "+QuickShop.instance.dbPrefix+"schedule (owner VARCHAR(32) NOT NULL, world VARCHAR(32) NOT NULL, x INT NOT NULL, y INT NOT NULL, z INT NOT NULL, timestamp INT NOT NULL, PRIMARY KEY (owner, world, x, y, z, timestamp) );"; + } st.execute(createTable); } @@ -45,14 +50,14 @@ public class DatabaseHelper { PreparedStatement ps = null; try { // V3.4.2 - ps = db.getConnection().prepareStatement("ALTER TABLE shops MODIFY COLUMN price double(32,2) NOT NULL AFTER owner"); + ps = db.getConnection().prepareStatement("ALTER TABLE "+QuickShop.instance.dbPrefix+"shops MODIFY COLUMN price double(32,2) NOT NULL AFTER owner"); ps.execute(); ps.close(); } catch (SQLException e) { } try { // V3.4.3 - ps = db.getConnection().prepareStatement("ALTER TABLE messages MODIFY COLUMN time BIGINT(32) NOT NULL AFTER message"); + ps = db.getConnection().prepareStatement("ALTER TABLE "+QuickShop.instance.dbPrefix+"messages MODIFY COLUMN time BIGINT(32) NOT NULL AFTER message"); ps.execute(); ps.close(); } catch (SQLException e) { @@ -68,7 +73,12 @@ public class DatabaseHelper { public static void createShopsTable(Database db) throws SQLException { connectCheck(); Statement st = db.getConnection().createStatement(); - String createTable = "CREATE TABLE shops (owner VARCHAR(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 = null; + if(QuickShop.instance.getConfig().getBoolean("database.use-varchar")) { + createTable = "CREATE TABLE "+QuickShop.instance.dbPrefix+"shops (owner VARCHAR(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) );"; + }else { + createTable = "CREATE TABLE "+QuickShop.instance.dbPrefix+"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); } @@ -81,37 +91,43 @@ public class DatabaseHelper { public static void createMessagesTable(Database db) throws SQLException { connectCheck(); Statement st = db.getConnection().createStatement(); - String createTable = "CREATE TABLE messages (owner VARCHAR(32) NOT NULL, message TEXT(25) NOT NULL, time BIGINT(32) NOT NULL );"; + String createTable = null; + if(QuickShop.instance.getConfig().getBoolean("database.use-varchar")) { + createTable = "CREATE TABLE "+QuickShop.instance.dbPrefix+"messages (owner VARCHAR(32) NOT NULL, message TEXT(25) NOT NULL, time BIGINT(32) NOT NULL );"; + }else { + createTable = "CREATE TABLE "+QuickShop.instance.dbPrefix+"messages (owner TEXT(32) NOT NULL, message TEXT(25) NOT NULL, time BIGINT(32) NOT NULL );"; + } + st.execute(createTable); } public static ResultSet selectAllShops(Database db) throws SQLException { connectCheck(); - PreparedStatement ps = db.getConnection().prepareStatement("SELECT * FROM shops"); + PreparedStatement ps = db.getConnection().prepareStatement("SELECT * FROM "+QuickShop.instance.dbPrefix+"shops"); return ps.executeQuery(); } public static void removeShop(Database db, int x, int y, int z, String worldName) throws SQLException { connectCheck(); db.getConnection().createStatement() - .executeUpdate("DELETE FROM shops WHERE x = " + x + " AND y = " + y + " AND z = " + z + .executeUpdate("DELETE FROM "+QuickShop.instance.dbPrefix+"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 + .executeUpdate("DELETE FROM "+QuickShop.instance.dbPrefix+"schedule WHERE x = " + x + " AND y = " + y + " AND z = " + z + " AND world = \"" + worldName + "\"" + (db.getCore() instanceof MySQLCore ? " LIMIT 1" : "")); } public static void insertSchedule(String argUUID, String world, int x, int y, int z, long l) { connectCheck(); - String scheduleq = "INSERT INTO schedule (owner, world, x, y, z, timestamp) VALUES (?, ?, ?, ?, ?, ?)"; + String scheduleq = "INSERT INTO "+QuickShop.instance.dbPrefix+"schedule (owner, world, x, y, z, timestamp) VALUES (?, ?, ?, ?, ?, ?)"; QuickShop.instance.getDB().execute(scheduleq , argUUID, world, x, y, z, System.currentTimeMillis()); } public static void updateOwner2UUID(String ownerUUID, int x, int y, int z, String worldName) throws SQLException { connectCheck(); QuickShop.instance.getDB().getConnection().createStatement() - .executeUpdate("UPDATE shops SET owner = \"" + ownerUUID.toString() + .executeUpdate("UPDATE "+QuickShop.instance.dbPrefix+"shops SET owner = \"" + ownerUUID.toString() + "\" WHERE x = " + x + " AND y = " + y + " AND z = " + z + " AND world = \"" + worldName + "\" LIMIT 1"); @@ -120,16 +136,16 @@ public class DatabaseHelper { public static void updateShop(Database db, String owner, ItemStack item, int unlimited, int shopType, double price, int x, int y, int z, String world) { connectCheck(); - String q = "UPDATE shops SET owner = ?, itemConfig = ?, unlimited = ?, type = ?, price = ? WHERE x = ? AND y = ? and z = ? and world = ?"; + String q = "UPDATE "+QuickShop.instance.dbPrefix+"shops SET owner = ?, itemConfig = ?, unlimited = ?, type = ?, price = ? WHERE x = ? AND y = ? and z = ? and world = ?"; db.execute(q, owner, Util.serialize(item), unlimited, shopType, price, x, y, z, world); } public static void createShop(String owner, double price, ItemStack item, int unlimited, int shopType, String world, int x, int y, int z) { connectCheck(); - String q = "INSERT INTO shops (owner, price, itemConfig, x, y, z, world, unlimited, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; + String q = "INSERT INTO "+QuickShop.instance.dbPrefix+"shops (owner, price, itemConfig, x, y, z, world, unlimited, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; QuickShop.instance.getDB().execute(q, owner, price, Util.serialize(item), x, y, z, world, unlimited, shopType); // Reremake write in schedule data - String scheduleq = "INSERT INTO schedule (owner, world, x, y, z, timestamp) VALUES (?, ?, ?, ?, ?, ?)"; + String scheduleq = "INSERT INTO "+QuickShop.instance.dbPrefix+"schedule (owner, world, x, y, z, timestamp) VALUES (?, ?, ?, ?, ?, ?)"; QuickShop.instance.getDB().execute(scheduleq , owner, world,x,y,z, System.currentTimeMillis()); } @@ -145,7 +161,7 @@ public class DatabaseHelper { } try { if(QuickShop.instance.getDB().getConnection().isReadOnly()) { - QuickShop.instance.getLogger().severe("Database is read-only, QSRR can't write in data!"); + QuickShop.instance.getLogger().severe("Database is read-only, QuickShop can't write in data!"); return; } } catch (SQLException e1) { diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 536a2db20..a7ed9c3bc 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -95,6 +95,7 @@ public class QuickShop extends JavaPlugin { private int displayItemCheckTicks; private boolean noopDisable; private boolean setupDBonEnableding = false; + public String dbPrefix; /** The plugin metrics from Hidendra */ // public Metrics getMetrics(){ return metrics; } public int getShopLimit(Player p) { @@ -338,7 +339,7 @@ public class QuickShop extends JavaPlugin { if(getConfig().getBoolean("ongoingfee.reset-on-startup")) { step = "Reset schedule data"; getDB().getConnection().createStatement() - .executeUpdate("DELETE FROM schedule WHERE x = " + x + " AND y = " + y + " AND z = " + z + .executeUpdate("DELETE FROM "+QuickShop.instance.dbPrefix+"schedule WHERE x = " + x + " AND y = " + y + " AND z = " + z + " AND world = \"" + worldName + "\"" + (getDB().getCore() instanceof MySQLCore ? " LIMIT 1" : "")); } @@ -346,7 +347,7 @@ public class QuickShop extends JavaPlugin { step = "Checking shop schedule data"; //Check shop is or not exist in schedule table Statement st = getDB().getConnection().createStatement(); - String checkq = "SELECT * FROM schedule WHERE owner ='{owner}' and world ='{world}' and x ='{x}' and y ='{y}' and z='{z}' and timestamp ='%'"; + String checkq = "SELECT * FROM "+QuickShop.instance.dbPrefix+"schedule WHERE owner ='{owner}' and world ='{world}' and x ='{x}' and y ='{y}' and z='{z}' and timestamp ='%'"; checkq.replace("{x}", String.valueOf(loc.getBlockX())); checkq.replace("{y}", String.valueOf(loc.getBlockY())); checkq.replace("{z}", String.valueOf(loc.getBlockZ())); @@ -356,9 +357,8 @@ public class QuickShop extends JavaPlugin { if(!resultSet.next()) { //Not exist, write in step = "Writeing shop schedule data"; - String scheduleq = "INSERT INTO schedule (owner, world, x, y, z, timestamp) VALUES (?, ?, ?, ?, ?, ?)"; + String scheduleq = "INSERT INTO "+QuickShop.instance.dbPrefix+"schedule (owner, world, x, y, z, timestamp) VALUES (?, ?, ?, ?, ?, ?)"; getDB().execute(scheduleq , shop.getOwner().toString(), loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), System.currentTimeMillis()); - getLogger().info("Adding shop at world:"+loc.getWorld().getName()+" x:"+loc.getBlockX()+" y:"+loc.getBlockY()+" z:"+loc.getBlockZ()+" into schedule table"); } if (loc.getWorld() != null && loc.getChunk().isLoaded()) { @@ -686,6 +686,9 @@ public class QuickShop extends JavaPlugin { String port = dbCfg.getString("port"); String database = dbCfg.getString("database"); dbCore = new MySQLCore(host, user, pass, database, port); + dbPrefix = dbCfg.getString("prefix"); + if (dbPrefix.equals("none")) + dbPrefix = ""; } else { // SQLite database - Doing this handles file creation dbCore = new SQLiteCore(new File(this.getDataFolder(), "shops.db")); @@ -695,20 +698,20 @@ public class QuickShop extends JavaPlugin { DatabaseHelper.setup(getDB()); } catch (ConnectionException e) { e.printStackTrace(); - if(setupDBonEnableding) { + if (setupDBonEnableding) { getLogger().severe("Error connecting to database. Aborting plugin load."); getServer().getPluginManager().disablePlugin(this); - }else { + } else { getLogger().severe("Error connecting to database."); } return false; } catch (SQLException e) { e.printStackTrace(); getServer().getPluginManager().disablePlugin(this); - if(setupDBonEnableding) { + if (setupDBonEnableding) { getLogger().severe("Error setting up database. Aborting plugin load."); getServer().getPluginManager().disablePlugin(this); - }else { + } else { getLogger().severe("Error setting up database."); } return false; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index dec6ed2ed..1e1da7478 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -37,8 +37,13 @@ database: database: quickshop user: root password: passwd + # Set prefix to "none" to remove prefix + prefix: none # Beta! Use your own risk! Auto-Reconnect reconnect: false + # Beta! Use VARCHAR for database to solve exception: "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: BLOB/TEXT column 'owner' used in key specification without a key length" + # If you are using SQLite or all right, Please do not turn on this. + use-varchar: false #Limits the number of shops a person can create and own at a single time. limits: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e3653a9c5..6a4e956b3 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -63,4 +63,7 @@ permissions: default: op quickshop.about: description: Show QuickShop about message - default: true \ No newline at end of file + default: true + quickshop.debug: + description: Print debug infomation + default: op \ No newline at end of file