From 28dc3f6c9aba95ae86756f0756ff5d6757cd32e9 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Fri, 5 Jun 2015 20:45:13 +1000 Subject: [PATCH] Fixed plot rating --- .../plot/PlotSquared.java | 31 +--------- .../plot/commands/Database.java | 16 +---- .../plot/commands/DebugUUID.java | 2 +- .../plot/commands/Rate.java | 53 ++++++++--------- .../plot/database/AbstractDB.java | 9 ++- .../plot/database/DBFunc.java | 8 ++- .../plot/database/SQLManager.java | 59 +++++++++++++++++-- 7 files changed, 97 insertions(+), 81 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java index 34ce70754..817c0d8ed 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java @@ -645,13 +645,6 @@ public class PlotSquared { } public void setupDatabase() { - final String[] tables; - if (Settings.ENABLE_CLUSTERS) { - MainCommand.subCommands.add(new Cluster()); - tables = new String[] { "plot_trusted", "plot_ratings", "plot_comments", "cluster" }; - } else { - tables = new String[] { "plot_trusted", "plot_ratings", "plot_comments" }; - } if (Settings.DB.USE_MYSQL) { try { database = new MySQL(THIS, Settings.DB.HOST_NAME, Settings.DB.PORT, Settings.DB.DATABASE, Settings.DB.USER, Settings.DB.PASSWORD); @@ -660,18 +653,7 @@ public class PlotSquared { if (DBFunc.dbManager == null) { DBFunc.dbManager = new SQLManager(connection, Settings.DB.PREFIX); } - final DatabaseMetaData meta = connection.getMetaData(); - ResultSet res = meta.getTables(null, null, Settings.DB.PREFIX + "plot", null); - if (!res.next()) { - DBFunc.createTables("mysql", true); - } else { - for (final String table : tables) { - res = meta.getTables(null, null, Settings.DB.PREFIX + table, null); - if (!res.next()) { - DBFunc.createTables("mysql", false); - } - } - } + DBFunc.createTables("mysql"); } } catch (final Exception e) { log("&c[Plots] MySQL is not setup correctly. The plugin will disable itself."); @@ -699,16 +681,7 @@ public class PlotSquared { DBFunc.dbManager = new SQLManager(connection, Settings.DB.PREFIX); final DatabaseMetaData meta = connection.getMetaData(); ResultSet res = meta.getTables(null, null, Settings.DB.PREFIX + "plot", null); - if (!res.next()) { - DBFunc.createTables("sqlite", true); - } else { - for (final String table : tables) { - res = meta.getTables(null, null, Settings.DB.PREFIX + table, null); - if (!res.next()) { - DBFunc.createTables("sqlite", false); - } - } - } + DBFunc.createTables("sqlite"); } } catch (final Exception e) { log(C.PREFIX.s() + "&cFailed to open SQLite connection. The plugin will disable itself."); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Database.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Database.java index 8db789af1..c928eee19 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Database.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Database.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.UUID; import com.intellectualcrafters.plot.PlotSquared; +import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.database.MySQL; import com.intellectualcrafters.plot.database.SQLManager; import com.intellectualcrafters.plot.object.Plot; @@ -23,8 +24,6 @@ import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; * @author Citymonstret */ public class Database extends SubCommand { - final String[] tables = new String[] { "plot_trusted", "plot_ratings", "plot_comments" }; - public Database() { super(Command.DATABASE, "Convert/Backup Storage", "database [type] [...details]", CommandCategory.DEBUG, false); } @@ -106,18 +105,7 @@ public class Database extends SubCommand { } final SQLManager manager = new SQLManager(n, prefix); try { - final DatabaseMetaData meta = n.getMetaData(); - ResultSet set = meta.getTables(null, null, prefix + "plot", null); - if (!set.next()) { - manager.createTables("mysql", true); - } else { - for (final String s : this.tables) { - set = meta.getTables(null, null, prefix + s, null); - if (!set.next()) { - manager.createTables("mysql", false); - } - } - } + manager.createTables(Settings.DB.USE_MYSQL ? "mysql" : "sqlite"); } catch (final SQLException e) { e.printStackTrace(); return sendMessage(plr, "Could not create the required tables and/or load the database") && sendMessage(plr, "Please see the stacktrace for more information"); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java index 09d1cd52c..92c2f2351 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java @@ -233,7 +233,7 @@ public class DebugUUID extends SubCommand { MainUtil.sendConsoleMessage("&7 - Creating tables"); try { - database.createTables(Settings.DB.USE_MYSQL ? "mysql" : "sqlite", true); + database.createTables(Settings.DB.USE_MYSQL ? "mysql" : "sqlite"); if (!result) { MainUtil.sendConsoleMessage("&cConversion failed! Attempting recovery"); for (Plot plot : PlotSquared.getPlots()) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Rate.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Rate.java index 65c63f6cc..d46d18212 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Rate.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Rate.java @@ -20,12 +20,17 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.commands; +import java.util.UUID; + +import org.apache.commons.lang.StringUtils; + import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.TaskManager; public class Rate extends SubCommand { /* @@ -56,39 +61,31 @@ public class Rate extends SubCommand { return true; } final String arg = args[0]; - boolean o = false; - for (final char c : arg.toCharArray()) { - if (!Character.isDigit(c)) { - o = true; - break; + final int rating; + if (StringUtils.isNumeric(arg) && arg.length() < 3 && arg.length() > 0) { + rating = Integer.parseInt(arg); + if (rating > 10) { + sendMessage(plr, C.RATING_NOT_VALID); + return false; } } - int rating = 0; - if (!o) { - rating = Integer.parseInt(arg); - } - if (o || ((rating < 0) || (rating > 10))) { + else { sendMessage(plr, C.RATING_NOT_VALID); - return true; - } - // TODO implement check for already rated - boolean rated = true; - try { - DBFunc.getRatings(plot); - } catch (final Exception e) { - rated = false; - } - if (rated) { - sendMessage(plr, C.RATING_ALREADY_EXISTS, plot.getId().toString()); return false; } - final boolean success = true; - if (success) { - sendMessage(plr, C.RATING_APPLIED, plot.getId().toString()); - } else { - sendMessage(plr, C.COMMAND_WENT_WRONG); - } - DBFunc.setRating(plot, plr.getUUID(), rating); + final UUID uuid = plr.getUUID(); + // TODO implement check for already rated + TaskManager.runTaskAsync(new Runnable() { + @Override + public void run() { + if (DBFunc.hasRated(plot.world, plot.id, uuid)) { + sendMessage(plr, C.RATING_ALREADY_EXISTS, plot.getId().toString()); + return; + } + DBFunc.setRating(plot, uuid, rating); + sendMessage(plr, C.RATING_APPLIED, plot.getId().toString()); + } + }); return true; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java index a03a8f75a..5e3151d9d 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java @@ -76,7 +76,7 @@ public interface AbstractDB { * * @throws SQLException If the database manager is unable to create the tables */ - public void createTables(final String database, final boolean add_constraint) throws Exception; + public void createTables(final String database) throws Exception; /** * Delete a plot @@ -287,6 +287,13 @@ public interface AbstractDB { */ public double getRatings(final Plot plot); + /** + * if uuid has rated + * @param uuid + * @return + */ + public boolean hasRated(String world, PlotId id, UUID uuid); + /** * Set a rating for a plot * @param plot diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java index b8bb3c0b8..8cb353f42 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java @@ -121,8 +121,8 @@ public class DBFunc { * * @throws Exception */ - public static void createTables(final String database, final boolean add_constraint) throws Exception { - dbManager.createTables(database, add_constraint); + public static void createTables(final String database) throws Exception { + dbManager.createTables(database); } /** @@ -346,6 +346,10 @@ public class DBFunc { return dbManager.getRatings(plot); } + public static boolean hasRated(String world, PlotId id, final UUID uuid) { + return dbManager.hasRated(world, id, uuid); + } + public static void setRating(Plot plot, UUID rater, int value) { dbManager.setRating(plot, rater, value); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index d96b347d0..19747a873 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -673,7 +673,34 @@ public class SQLManager implements AbstractDB { * @throws SQLException */ @Override - public void createTables(final String database, final boolean add_constraint) throws SQLException { + public void createTables(final String database) throws SQLException { + final String[] tables; + if (Settings.ENABLE_CLUSTERS) { + tables = new String[] { "plot", "plot_denied", "plot_helpers", "plot_comments", "plot_trusted", "plot_rating", "plot_settings", "cluster" }; + } + else { + tables = new String[] { "plot", "plot_denied", "plot_helpers", "plot_comments", "plot_trusted", "plot_rating", "plot_settings" }; + } + final DatabaseMetaData meta = connection.getMetaData(); + int create = 0; + for (final String s : tables) { + ResultSet set = meta.getTables(null, null, prefix + s, null); + if (!set.next()) { + create++; + } + set.close(); + } + if (create == 0) { + return; + } + boolean add_constraint; + if (create == tables.length) { + add_constraint = true; + } + else { + add_constraint = false; + } + PlotSquared.log("Creating tables"); final boolean mysql = database.equals("mysql"); final Statement stmt = this.connection.createStatement(); if (mysql) { @@ -683,7 +710,7 @@ public class SQLManager implements AbstractDB { stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_comments` (" + "`world` VARCHAR(40) NOT NULL, `hashcode` INT(11) NOT NULL," + "`comment` VARCHAR(40) NOT NULL," + "`inbox` VARCHAR(40) NOT NULL," + "`timestamp` INT(11) NOT NULL," + "`sender` VARCHAR(40) NOT NULL" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_trusted` (" + "`plot_plot_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_settings` (" + " `plot_plot_id` INT(11) NOT NULL," + " `biome` VARCHAR(45) DEFAULT 'FOREST'," + " `rain` INT(1) DEFAULT 0," + " `custom_time` TINYINT(1) DEFAULT '0'," + " `time` INT(11) DEFAULT '8000'," + " `deny_entry` TINYINT(1) DEFAULT '0'," + " `alias` VARCHAR(50) DEFAULT NULL," + " `flags` VARCHAR(512) DEFAULT NULL," + " `merged` INT(11) DEFAULT NULL," + " `position` VARCHAR(50) NOT NULL DEFAULT 'DEFAULT'," + " PRIMARY KEY (`plot_plot_id`)," + " UNIQUE KEY `unique_alias` (`alias`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); - stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_ratings` ( `plot_plot_id` INT(11) NOT NULL, `rating` INT(2) NOT NULL, `player` VARCHAR(40) NOT NULL, PRIMARY KEY(`plot_plot_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_rating` ( `plot_plot_id` INT(11) NOT NULL, `rating` INT(2) NOT NULL, `player` VARCHAR(40) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8"); if (add_constraint) { stmt.addBatch("ALTER TABLE `" + this.prefix + "plot_settings` ADD CONSTRAINT `" + this.prefix + "plot_settings_ibfk_1` FOREIGN KEY (`plot_plot_id`) REFERENCES `" + this.prefix + "plot` (`id`) ON DELETE CASCADE"); } @@ -698,7 +725,7 @@ public class SQLManager implements AbstractDB { stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_trusted` (" + "`plot_plot_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_comments` (" + "`world` VARCHAR(40) NOT NULL, `hashcode` INT(11) NOT NULL," + "`comment` VARCHAR(40) NOT NULL," + "`inbox` VARCHAR(40) NOT NULL, `timestamp` INT(11) NOT NULL," + "`sender` VARCHAR(40) NOT NULL" + ")"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_settings` (" + " `plot_plot_id` INT(11) NOT NULL," + " `biome` VARCHAR(45) DEFAULT 'FOREST'," + " `rain` INT(1) DEFAULT 0," + " `custom_time` TINYINT(1) DEFAULT '0'," + " `time` INT(11) DEFAULT '8000'," + " `deny_entry` TINYINT(1) DEFAULT '0'," + " `alias` VARCHAR(50) DEFAULT NULL," + " `flags` VARCHAR(512) DEFAULT NULL," + " `merged` INT(11) DEFAULT NULL," + " `position` VARCHAR(50) NOT NULL DEFAULT 'DEFAULT'," + " PRIMARY KEY (`plot_plot_id`)" + ")"); - stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_ratings` (`plot_plot_id` INT(11) NOT NULL, `rating` INT(2) NOT NULL, `player` VARCHAR(40) NOT NULL, PRIMARY KEY(`plot_plot_id`))"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_rating` (`plot_plot_id` INT(11) NOT NULL, `rating` INT(2) NOT NULL, `player` VARCHAR(40) NOT NULL)"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "cluster` (" + "`id` INTEGER PRIMARY KEY AUTOINCREMENT," + "`pos1_x` INT(11) NOT NULL," + "`pos1_z` INT(11) NOT NULL," + "`pos2_x` INT(11) NOT NULL," + "`pos2_z` INT(11) NOT NULL," + "`owner` VARCHAR(40) NOT NULL," + "`world` VARCHAR(45) NOT NULL," + "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + ")"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "cluster_helpers` (" + "`cluster_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "cluster_invited` (" + "`cluster_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); @@ -1604,7 +1631,7 @@ public class SQLManager implements AbstractDB { @Override public double getRatings(final Plot plot) { try { - final PreparedStatement statement = this.connection.prepareStatement("SELECT AVG(`rating`) AS `rating` FROM `" + this.prefix + "plot_ratings` WHERE `plot_plot_id` = ? "); + final PreparedStatement statement = this.connection.prepareStatement("SELECT AVG(`rating`) AS `rating` FROM `" + this.prefix + "plot_rating` WHERE `plot_plot_id` = ? "); statement.setInt(1, getId(plot.world, plot.id)); final ResultSet set = statement.executeQuery(); double rating = 0; @@ -1628,7 +1655,7 @@ public class SQLManager implements AbstractDB { @Override public void run() { try { - final PreparedStatement statement = SQLManager.this.connection.prepareStatement("INSERT IGNORE INTO `" + SQLManager.this.prefix + "plot_ratings` (`plot_plot_id`, `rating`, `player`) VALUES(?,?,?)"); + final PreparedStatement statement = SQLManager.this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "plot_rating` (`plot_plot_id`, `rating`, `player`) VALUES(?,?,?)"); statement.setInt(1, getId(plot.world, plot.id)); statement.setInt(2, value); statement.setString(3, rater.toString()); @@ -2142,7 +2169,7 @@ public class SQLManager implements AbstractDB { stmt.addBatch("DROP TABLE `" + prefix + "cluster_invited`"); stmt.addBatch("DROP TABLE `" + prefix + "cluster_helpers`"); stmt.addBatch("DROP TABLE `" + prefix + "cluster`"); - stmt.addBatch("DROP TABLE `" + prefix + "plot_ratings`"); + stmt.addBatch("DROP TABLE `" + prefix + "plot_rating`"); stmt.addBatch("DROP TABLE `" + prefix + "plot_settings`"); stmt.addBatch("DROP TABLE `" + prefix + "plot_comments`"); stmt.addBatch("DROP TABLE `" + prefix + "plot_trusted`"); @@ -2162,4 +2189,24 @@ public class SQLManager implements AbstractDB { return false; } } + + @Override + public boolean hasRated(String world, PlotId id, UUID uuid) { + try { + PreparedStatement stmt = SQLManager.this.connection.prepareStatement("SELECT * FROM `" + this.prefix + "plot_rating` WHERE `plot_plot_id` = ? AND `player` = ?"); + stmt.setInt(1, getId(world, id)); + stmt.setString(2, uuid.toString()); + final ResultSet r = stmt.executeQuery(); + if (r.next()) { + stmt.close(); + return true; + } + stmt.close(); + return false; + } + catch (Exception e) { + e.printStackTrace(); + } + return false; + } }