From e548cd8158cce9a08c60ed495fb076b798fe4d13 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 22 Oct 2014 17:08:55 +1100 Subject: [PATCH] Making the database stuff abstract: This will make it easier to add support for MongoDB --- .../intellectualcrafters/plot/PlotMain.java | 9 +- .../plot/database/AbstractDB.java | 152 +++ .../plot/database/DBFunc.java | 878 +------------- .../plot/database/SQLManager.java | 1049 +++++++++++++++++ 4 files changed, 1237 insertions(+), 851 deletions(-) create mode 100644 PlotSquared/src/com/intellectualcrafters/plot/database/AbstractDB.java create mode 100644 PlotSquared/src/com/intellectualcrafters/plot/database/SQLManager.java diff --git a/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java index 8385ee672..18636934c 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java @@ -17,6 +17,7 @@ import com.intellectualcrafters.plot.commands.MainCommand; import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.database.MySQL; import com.intellectualcrafters.plot.database.PlotMeConverter; +import com.intellectualcrafters.plot.database.SQLManager; import com.intellectualcrafters.plot.database.SQLite; import com.intellectualcrafters.plot.events.PlayerTeleportToPlotEvent; import com.intellectualcrafters.plot.events.PlotDeleteEvent; @@ -572,6 +573,7 @@ public class PlotMain extends JavaPlugin { // Use mysql? if (Settings.DB.USE_MYSQL) { + DBFunc.dbManager = new SQLManager(); try { mySQL = new MySQL(this, Settings.DB.HOST_NAME, Settings.DB.PORT, Settings.DB.DATABASE, Settings.DB.USER, Settings.DB.PASSWORD); @@ -596,7 +598,7 @@ public class PlotMain extends JavaPlugin { } } } - catch (ClassNotFoundException | SQLException e) { + catch (Exception e) { Logger.add(LogLevel.DANGER, "MySQL connection failed."); sendConsoleSenderMessage("&c[Plots] MySQL is not setup correctly. The plugin will disable itself."); if (config==null || config.getBoolean("debug")) { @@ -614,11 +616,12 @@ public class PlotMain extends JavaPlugin { // TODO: Implement mongo else if (Settings.DB.USE_MONGO) { +// DBFunc.dbManager = new MongoManager(); sendConsoleSenderMessage(C.PREFIX.s() + "MongoDB is not yet implemented"); } - // Use Sqlite :D<3 else if (Settings.DB.USE_SQLITE) { + DBFunc.dbManager = new SQLManager(); try { connection = new SQLite(this, Settings.DB.SQLITE_DB + ".db").openConnection(); { @@ -641,7 +644,7 @@ public class PlotMain extends JavaPlugin { } } } - catch (ClassNotFoundException | SQLException e) { + catch (Exception e) { Logger.add(LogLevel.DANGER, "SQLite connection failed"); sendConsoleSenderMessage(C.PREFIX.s() + "&cFailed to open SQLite connection. The plugin will disable itself."); diff --git a/PlotSquared/src/com/intellectualcrafters/plot/database/AbstractDB.java b/PlotSquared/src/com/intellectualcrafters/plot/database/AbstractDB.java new file mode 100644 index 000000000..7df99b374 --- /dev/null +++ b/PlotSquared/src/com/intellectualcrafters/plot/database/AbstractDB.java @@ -0,0 +1,152 @@ +/* + * Copyright (c) IntellectualCrafters - 2014. You are not allowed to distribute + * and/or monetize any of our intellectual property. IntellectualCrafters is not + * affiliated with Mojang AB. Minecraft is a trademark of Mojang AB. + * + * >> File = DBFunc.java >> Generated by: Citymonstret at 2014-08-09 01:43 + */ + +package com.intellectualcrafters.plot.database; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.UUID; + +import org.bukkit.OfflinePlayer; + +import com.intellectualcrafters.plot.Flag; +import com.intellectualcrafters.plot.Plot; +import com.intellectualcrafters.plot.PlotId; + +/** + * @author Citymonstret + */ +public abstract class AbstractDB { + + // TODO MongoDB @Brandon + + /** + * Set Plot owner + * + * @param plot + * @param uuid + */ + public abstract void setOwner(final Plot plot, final UUID uuid); + + public abstract void createAllSettingsAndHelpers(ArrayList plots); + + /** + * Create a plot + * + * @param plots + */ + public abstract void createPlots(ArrayList plots); + + /** + * Create a plot + * + * @param plot + */ + public abstract void createPlot(Plot plot); + + /** + * Create tables + * + * @throws SQLException + */ + public abstract void createTables(String database, boolean add_constraint) throws Exception; + + /** + * Delete a plot + * + * @param plot + */ + public abstract void delete(final String world, final Plot plot); + + /** + * Create plot settings + * + * @param id + * @param plot + */ + public abstract void createPlotSettings(final int id, final Plot plot); + + public abstract int getId(String world, PlotId id2); + + /** + * @return + */ + public abstract HashMap> getPlots(); + + + public abstract void setMerged(final String world, final Plot plot, final boolean[] merged); + + public abstract void setFlags(final String world, final Plot plot, final Flag[] flags); + + /** + * @param plot + * @param alias + */ + public abstract void setAlias(final String world, final Plot plot, final String alias); + + public abstract void purge(final String world, final PlotId id); + + public abstract void purge(final String world); + + /** + * @param plot + * @param position + */ + public abstract void setPosition(final String world, final Plot plot, final String position); + + /** + * @param id + * @return + */ + public abstract HashMap getSettings(int id); + + /** + * + */ + public UUID everyone = UUID.fromString("1-1-3-3-7"); + + + /** + * @param plot + * @param player + */ + public abstract void removeHelper(final String world, final Plot plot, final OfflinePlayer player); + + /** + * @param plot + * @param player + */ + public abstract void removeTrusted(final String world, final Plot plot, final OfflinePlayer player); + + /** + * @param plot + * @param player + */ + public abstract void setHelper(final String world, final Plot plot, final OfflinePlayer player); + + /** + * @param plot + * @param player + */ + public abstract void setTrusted(final String world, final Plot plot, final OfflinePlayer player); + + /** + * @param plot + * @param player + */ + public abstract void removeDenied(final String world, final Plot plot, final OfflinePlayer player); + + /** + * @param plot + * @param player + */ + public abstract void setDenied(final String world, final Plot plot, final OfflinePlayer player); + + public abstract double getRatings(final Plot plot); +} diff --git a/PlotSquared/src/com/intellectualcrafters/plot/database/DBFunc.java b/PlotSquared/src/com/intellectualcrafters/plot/database/DBFunc.java index 26fd33811..478d7676e 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/database/DBFunc.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/database/DBFunc.java @@ -25,6 +25,8 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.block.Biome; +import sun.security.pkcs11.Secmod.DbMode; + import com.intellectualcrafters.plot.Flag; import com.intellectualcrafters.plot.FlagManager; import com.intellectualcrafters.plot.Logger; @@ -38,7 +40,9 @@ import com.intellectualcrafters.plot.PlotMain; * @author Citymonstret */ public class DBFunc { - + + public static AbstractDB dbManager; + // TODO MongoDB @Brandon /** @@ -48,113 +52,11 @@ public class DBFunc { * @param uuid */ public static void setOwner(final Plot plot, final UUID uuid) { - runTask(new Runnable() { - @Override - public void run() { - try { - PreparedStatement statement = - connection.prepareStatement("UPDATE `plot` SET `owner` = ? WHERE `plot_id_x` = ? AND `plot_id_z` = ? "); - statement.setString(1, uuid.toString()); - statement.setInt(2, plot.id.x); - statement.setInt(3, plot.id.y); - statement.executeUpdate(); - statement.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.DANGER, "Could not set owner for plot " + plot.id); - } - } - }); + dbManager.setOwner(plot, uuid); } public static void createAllSettingsAndHelpers(ArrayList plots) { - HashMap> stored = new HashMap>(); - HashMap> helpers = new HashMap>(); - try { - PreparedStatement stmt = - connection.prepareStatement("SELECT `id`, `plot_id_x`, `plot_id_z`, `world` FROM `plot`"); - ResultSet result = stmt.executeQuery(); - while (result.next()) { - int id = result.getInt("id"); - int idx = result.getInt("plot_id_x"); - int idz = result.getInt("plot_id_z"); - String world = result.getString("world"); - - if (!stored.containsKey(world)) { - stored.put(world, new HashMap()); - } - stored.get(world).put(new PlotId(idx, idz), id); - } - } - catch (SQLException e) { - e.printStackTrace(); - } - - for (Plot plot : plots) { - String world = Bukkit.getWorld(plot.world).getName(); - if (stored.containsKey(world)) { - Integer id = stored.get(world).get(plot.id); - if (id != null) { - helpers.put(id, plot.helpers); - } - } - } - - if (helpers.size() == 0) { - return; - } - - // add plot settings - Integer[] ids = helpers.keySet().toArray(new Integer[0]); - StringBuilder statement = new StringBuilder("INSERT INTO `plot_settings` (`plot_plot_id`) values "); - for (int i = 0; i < (ids.length - 1); i++) { - statement.append("(?),"); - } - statement.append("(?)"); - PreparedStatement stmt = null; - try { - stmt = connection.prepareStatement(statement.toString()); - for (int i = 0; i < ids.length; i++) { - stmt.setInt(i + 1, ids[i]); - } - stmt.executeUpdate(); - stmt.close(); - } - catch (SQLException e) { - e.printStackTrace(); - } - - // add plot helpers - String prefix = ""; - statement = new StringBuilder("INSERT INTO `plot_helpers` (`plot_plot_id`, `user_uuid`) values "); - for (Integer id : helpers.keySet()) { - for (UUID helper : helpers.get(id)) { - statement.append(prefix + "(?, ?)"); - prefix = ","; - } - } - if (prefix.equals("")) { - return; - } - try { - stmt = connection.prepareStatement(statement.toString()); - int counter = 0; - for (Integer id : helpers.keySet()) { - for (UUID helper : helpers.get(id)) { - - stmt.setInt((counter * 2) + 1, id); - stmt.setString((counter * 2) + 2, helper.toString()); - counter++; - } - } - stmt.executeUpdate(); - stmt.close(); - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to set helper for plots"); - e.printStackTrace(); - } + dbManager.createAllSettingsAndHelpers(plots); } /** @@ -163,37 +65,7 @@ public class DBFunc { * @param plots */ public static void createPlots(ArrayList plots) { - - // TODO collect list of plots to check if plot exists. - - if (plots.size() == 0) { - return; - } - StringBuilder statement = - new StringBuilder("INSERT INTO `plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) values "); - - for (int i = 0; i < (plots.size() - 1); i++) { - statement.append("(?, ?, ?, ?),"); - } - statement.append("(?, ?, ?, ?)"); - - PreparedStatement stmt = null; - try { - stmt = connection.prepareStatement(statement.toString()); - for (int i = 0; i < plots.size(); i++) { - Plot plot = plots.get(i); - stmt.setInt((i * 4) + 1, plot.id.x); - stmt.setInt((i * 4) + 2, plot.id.y); - stmt.setString((i * 4) + 3, plot.owner.toString()); - stmt.setString((i * 4) + 4, plot.world); - } - stmt.executeUpdate(); - stmt.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.DANGER, "Failed to save plots!"); - } + dbManager.createPlots(plots); } /** @@ -202,79 +74,15 @@ public class DBFunc { * @param plot */ public static void createPlot(Plot plot) { - PreparedStatement stmt = null; - try { - stmt = - connection.prepareStatement("INSERT INTO `plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) VALUES(?, ?, ?, ?)"); - stmt.setInt(1, plot.id.x); - stmt.setInt(2, plot.id.y); - stmt.setString(3, plot.owner.toString()); - stmt.setString(4, plot.world); - stmt.executeUpdate(); - stmt.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.DANGER, "Failed to save plot " + plot.id); - } + dbManager.createPlot(plot); } /** * Create tables - * - * @throws SQLException + * @throws Exception */ - public static void createTables(String database, boolean add_constraint) throws SQLException { - boolean mysql = database.equals("mysql"); - Statement stmt = connection.createStatement(); - - if (mysql) { - stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot` (" + "`id` INT(11) NOT NULL AUTO_INCREMENT," - + "`plot_id_x` INT(11) NOT NULL," + "`plot_id_z` INT(11) NOT NULL," - + "`owner` VARCHAR(45) NOT NULL," + "`world` VARCHAR(45) NOT NULL," - + "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + "PRIMARY KEY (`id`)" - + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0"); - stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_denied` (" + "`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 `plot_helpers` (" + "`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 `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 `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 `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"); - if (add_constraint) { - stmt.addBatch("ALTER TABLE `plot_settings` ADD CONSTRAINT `plot_settings_ibfk_1` FOREIGN KEY (`plot_plot_id`) REFERENCES `plot` (`id`) ON DELETE CASCADE"); - } - - } - else { - stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot` (" + "`id` INTEGER PRIMARY KEY AUTOINCREMENT," - + "`plot_id_x` INT(11) NOT NULL," + "`plot_id_z` INT(11) NOT NULL," - + "`owner` VARCHAR(45) NOT NULL," + "`world` VARCHAR(45) NOT NULL," - + "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP)"); - stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_denied` (" + "`plot_plot_id` INT(11) NOT NULL," - + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); - stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_helpers` (" + "`plot_plot_id` INT(11) NOT NULL," - + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); - stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_trusted` (" + "`plot_plot_id` INT(11) NOT NULL," - + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); - stmt.addBatch("CREATE TABLE IF NOT EXISTS `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 `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.executeBatch(); - stmt.clearBatch(); - stmt.close(); + public static void createTables(String database, boolean add_constraint) throws Exception { + dbManager.createTables(database, add_constraint); } /** @@ -283,36 +91,7 @@ public class DBFunc { * @param plot */ public static void delete(final String world, final Plot plot) { - PlotMain.removePlot(world, plot.id, false); - runTask(new Runnable() { - @Override - public void run() { - PreparedStatement stmt = null; - int id = getId(world, plot.id); - try { - stmt = connection.prepareStatement("DELETE FROM `plot_settings` WHERE `plot_plot_id` = ?"); - stmt.setInt(1, id); - stmt.executeUpdate(); - stmt.close(); - stmt = connection.prepareStatement("DELETE FROM `plot_helpers` WHERE `plot_plot_id` = ?"); - stmt.setInt(1, id); - stmt.executeUpdate(); - stmt.close(); - stmt = connection.prepareStatement("DELETE FROM `plot_trusted` WHERE `plot_plot_id` = ?"); - stmt.setInt(1, id); - stmt.executeUpdate(); - stmt.close(); - stmt = connection.prepareStatement("DELETE FROM `plot` WHERE `id` = ?"); - stmt.setInt(1, id); - stmt.executeUpdate(); - stmt.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.DANGER, "Failed to delete plot " + plot.id); - } - } - }); + dbManager.delete(world, plot); } /** @@ -322,43 +101,11 @@ public class DBFunc { * @param plot */ public static void createPlotSettings(final int id, final Plot plot) { - runTask(new Runnable() { - @Override - public void run() { - PreparedStatement stmt = null; - try { - stmt = connection.prepareStatement("INSERT INTO `plot_settings`(`plot_plot_id`) VALUES(" + "?)"); - stmt.setInt(1, id); - stmt.executeUpdate(); - stmt.close(); - } - catch (SQLException e) { - e.printStackTrace(); - } - - } - }); + dbManager.createPlotSettings(id, plot); } public static int getId(String world, PlotId id2) { - PreparedStatement stmt = null; - try { - stmt = connection.prepareStatement("SELECT `id` FROM `plot` WHERE `plot_id_x` = ? AND `plot_id_z` = ? AND world = ? ORDER BY `timestamp` ASC"); - stmt.setInt(1, id2.x); - stmt.setInt(2, id2.y); - stmt.setString(3, world); - ResultSet r = stmt.executeQuery(); - int id = Integer.MAX_VALUE; - while (r.next()) { - id = r.getInt("id"); - } - stmt.close(); - return id; - } - catch (SQLException e) { - e.printStackTrace(); - } - return Integer.MAX_VALUE; + return dbManager.getId(world, id2); } /** @@ -382,186 +129,16 @@ public class DBFunc { * @return */ public static HashMap> getPlots() { - try { - DatabaseMetaData data = connection.getMetaData(); - ResultSet rs = data.getColumns(null, null, "plot", "plot_id"); - boolean execute = rs.next(); - if (execute) { - Statement statement = connection.createStatement(); - statement.addBatch("ALTER IGNORE TABLE `plot` ADD `plot_id_x` int(11) DEFAULT 0"); - statement.addBatch("ALTER IGNORE TABLE `plot` ADD `plot_id_z` int(11) DEFAULT 0"); - statement.addBatch("UPDATE `plot` SET\n" + " `plot_id_x` = IF(" - + " LOCATE(';', `plot_id`) > 0," - + " SUBSTRING(`plot_id`, 1, LOCATE(';', `plot_id`) - 1)," + " `plot_id`" - + " )," + " `plot_id_z` = IF(" + " LOCATE(';', `plot_id`) > 0," - + " SUBSTRING(`plot_id`, LOCATE(';', `plot_id`) + 1)," + " NULL" + " )"); - statement.addBatch("ALTER TABLE `plot` DROP `plot_id`"); - statement.addBatch("ALTER IGNORE TABLE `plot_settings` ADD `flags` VARCHAR(512) DEFAULT NULL"); - statement.executeBatch(); - statement.close(); - } - rs = data.getColumns(null, null, "plot_settings", "merged"); - if (!rs.next()) { - Statement statement = connection.createStatement(); - statement.addBatch("ALTER TABLE `plot_settings` ADD `merged` int(11) DEFAULT NULL"); - statement.executeBatch(); - statement.close(); - } - } - catch (Exception e) { - e.printStackTrace(); - } - HashMap> plots = new HashMap>(); - Statement stmt = null; - try { - - Set worlds = new HashSet(); - if (PlotMain.config.contains("worlds")) { - worlds = PlotMain.config.getConfigurationSection("worlds").getKeys(false); - } - - stmt = connection.createStatement(); - ResultSet r = stmt.executeQuery("SELECT `id`, `plot_id_x`, `plot_id_z`, `owner`, `world` FROM `plot`"); - PlotId plot_id; - int id; - Plot p; - HashMap noExist = new HashMap(); - while (r.next()) { - plot_id = new PlotId(r.getInt("plot_id_x"), r.getInt("plot_id_z")); - id = r.getInt("id"); - String worldname = r.getString("world"); - HashMap settings = getSettings(id); - UUID owner = UUID.fromString(r.getString("owner")); - Biome plotBiome = Biome.FOREST; - String[] flags_string; - if (settings.get("flags") == null) { - flags_string = new String[] {}; - } - else { - flags_string = ((String) settings.get("flags")).split(","); - } - Flag[] flags = new Flag[flags_string.length]; - for (int i = 0; i < flags.length; i++) { - if (flags_string[i].contains(":")) { - String[] split = flags_string[i].split(":"); - flags[i] = new Flag(FlagManager.getFlag(split[0], true), split[1]); - } - else { - flags[i] = new Flag(FlagManager.getFlag(flags_string[i], true), ""); - } - } - ArrayList helpers = plotHelpers(id); - ArrayList trusted = plotTrusted(id); - ArrayList denied = plotDenied(id); - - String alias = (String) settings.get("alias"); - if ((alias == null) || alias.equalsIgnoreCase("NEW")) { - alias = ""; - } - PlotHomePosition position = null; - for (PlotHomePosition plotHomePosition : PlotHomePosition.values()) { - if (settings.get("position") == null) { - position = PlotHomePosition.DEFAULT; - break; - } - if (plotHomePosition.isMatching((String) settings.get("position"))) { - position = plotHomePosition; - } - } - if (position == null) { - position = PlotHomePosition.DEFAULT; - } - int merged_int = settings.get("merged") == null ? 0 : (int) settings.get("merged"); - - boolean[] merged = new boolean[4]; - for (int i = 0; i < 4; i++) { - merged[3 - i] = (merged_int & (1 << i)) != 0; - } - p = - new Plot(plot_id, owner, plotBiome, helpers, trusted, denied, alias, position, flags, worldname, merged); - if (plots.containsKey(worldname)) { - plots.get(worldname).put((plot_id), p); - } - else { - HashMap map = new HashMap(); - map.put((plot_id), p); - plots.put(worldname, map); - if (!worlds.contains(p.world)) { - if (noExist.containsKey(worldname)) { - noExist.put(worldname,noExist.get(worldname)+1); - } - else { - noExist.put(worldname,1); - } - } - } - } - for (String worldname: noExist.keySet()) { - PlotMain.sendConsoleSenderMessage("&c[WARNING] Found "+noExist.get(worldname)+" plots in DB for non existant world; '"+worldname+"'!!!\n&c - Please create this world, or remove the plots from the DB using the purge command!"); - } - stmt.close(); - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to load plots."); - e.printStackTrace(); - } - return plots; + return dbManager.getPlots(); } public static void setMerged(final String world, final Plot plot, final boolean[] merged) { - plot.settings.setMerged(merged); - runTask(new Runnable() { - @Override - public void run() { - try { - int n = 0; - for (int i = 0; i < 4; ++i) { - n = (n << 1) + (merged[i] ? 1 : 0); - } - PreparedStatement stmt = - connection.prepareStatement("UPDATE `plot_settings` SET `merged` = ? WHERE `plot_plot_id` = ?"); - stmt.setInt(1, n); - stmt.setInt(2, getId(world, plot.id)); - stmt.execute(); - stmt.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.WARNING, "Could not set merged for plot " + plot.id); - } - } - }); + dbManager.setMerged(world, plot, merged); } public static void setFlags(final String world, final Plot plot, final Flag[] flags) { - plot.settings.setFlags(flags); - final StringBuilder flag_string = new StringBuilder(); - int i = 0; - for (Flag flag : flags) { - if (i != 0) { - flag_string.append(","); - } - flag_string.append(flag.getKey() + ":" + flag.getValue()); - i++; - } - runTask(new Runnable() { - @Override - public void run() { - try { - PreparedStatement stmt = - connection.prepareStatement("UPDATE `plot_settings` SET `flags` = ? WHERE `plot_plot_id` = ?"); - stmt.setString(1, flag_string.toString()); - stmt.setInt(2, getId(world, plot.id)); - stmt.execute(); - stmt.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.WARNING, "Could not set flag for plot " + plot.id); - } - } - }); + dbManager.setFlags(world, plot, flags); } /** @@ -569,163 +146,15 @@ public class DBFunc { * @param alias */ public static void setAlias(final String world, final Plot plot, final String alias) { - plot.settings.setAlias(alias); - runTask(new Runnable() { - @Override - public void run() { - PreparedStatement stmt = null; - try { - stmt = - connection.prepareStatement("UPDATE `plot_settings` SET `alias` = ? WHERE `plot_plot_id` = ?"); - stmt.setString(1, alias); - stmt.setInt(2, getId(world, plot.id)); - stmt.executeUpdate(); - stmt.close(); - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to set alias for plot " + plot.id); - e.printStackTrace(); - } - - } - }); + dbManager.setAlias(world, plot, alias); } - /** - * @param r - */ - private static void runTask(Runnable r) { - PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), r); - } - public static void purge(final String world, final PlotId id) { - runTask(new Runnable() { - @Override - public void run() { - ArrayList ids = new ArrayList(); - - // Fetching a list of plot IDs for a world - try { - PreparedStatement stmt = connection.prepareStatement("SELECT `id` FROM `plot` WHERE `world` = ? AND `plot_id_x` = ? AND `plot_id_z` = ?"); - stmt.setString(1, world); - stmt.setInt(2, id.x); - stmt.setInt(3, id.y); - ResultSet result = stmt.executeQuery(); - while (result.next()) { - int id = result.getInt("id"); - ids.add(id); - } - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.WARNING, "FAILED TO PURGE WORLD '"+world+"'!"); - return; - } - if (ids.size() > 0) { - try { - - String prefix = ""; - StringBuilder idstr = new StringBuilder(""); - - for (Integer id:ids) { - idstr.append(prefix + id); - prefix = " OR `plot_plot_id` = "; - } - - PreparedStatement stmt = connection.prepareStatement("DELETE FROM `plot_helpers` WHERE `plot_plot_id` = "+idstr+""); - stmt.executeUpdate(); - stmt.close(); - - stmt = connection.prepareStatement("DELETE FROM `plot_denied` WHERE `plot_plot_id` = "+idstr+""); - stmt.executeUpdate(); - stmt.close(); - - stmt = connection.prepareStatement("DELETE FROM `plot_settings` WHERE `plot_plot_id` = "+idstr+""); - stmt.executeUpdate(); - stmt.close(); - - stmt = connection.prepareStatement("DELETE FROM `plot_trusted` WHERE `plot_plot_id` = "+idstr+""); - stmt.executeUpdate(); - stmt.close(); - - stmt = connection.prepareStatement("DELETE FROM `plot` WHERE `plot_plot_id` = "+idstr+""); - stmt.setString(1, world); - stmt.executeUpdate(); - stmt.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.DANGER, "FAILED TO PURGE PLOT FROM DB '"+world+"' , '"+id+"' !"); - return; - } - } - Logger.add(LogLevel.GENERAL, "SUCCESSFULLY PURGED PLOT FROM DB '"+world+"' , '"+id+"'!"); - } - }); + dbManager.purge(world, id); } public static void purge(final String world) { - runTask(new Runnable() { - @Override - public void run() { - ArrayList ids = new ArrayList(); - - // Fetching a list of plot IDs for a world - try { - PreparedStatement stmt = connection.prepareStatement("SELECT `id` FROM `plot` WHERE `world` = ?"); - stmt.setString(1, world); - ResultSet result = stmt.executeQuery(); - while (result.next()) { - int id = result.getInt("id"); - ids.add(id); - } - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.WARNING, "FAILED TO PURGE WORLD '"+world+"'!"); - return; - } - if (ids.size() > 0) { - try { - - String prefix = ""; - StringBuilder idstr = new StringBuilder(""); - - for (Integer id:ids) { - idstr.append(prefix + id); - prefix = " OR `plot_plot_id` = "; - } - - PreparedStatement stmt = connection.prepareStatement("DELETE FROM `plot_helpers` WHERE `plot_plot_id` = "+idstr+""); - stmt.executeUpdate(); - stmt.close(); - - stmt = connection.prepareStatement("DELETE FROM `plot_denied` WHERE `plot_plot_id` = "+idstr+""); - stmt.executeUpdate(); - stmt.close(); - - stmt = connection.prepareStatement("DELETE FROM `plot_settings` WHERE `plot_plot_id` = "+idstr+""); - stmt.executeUpdate(); - stmt.close(); - - stmt = connection.prepareStatement("DELETE FROM `plot_trusted` WHERE `plot_plot_id` = "+idstr+""); - stmt.executeUpdate(); - stmt.close(); - - stmt = connection.prepareStatement("DELETE FROM `plot` WHERE `world` = ?"); - stmt.setString(1, world); - stmt.executeUpdate(); - stmt.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.DANGER, "FAILED TO PURGE WORLD '"+world+"'!"); - return; - } - } - Logger.add(LogLevel.GENERAL, "SUCCESSFULLY PURGED WORLD '"+world+"'!"); - } - }); + dbManager.purge(world); } /** @@ -733,25 +162,7 @@ public class DBFunc { * @param position */ public static void setPosition(final String world, final Plot plot, final String position) { - plot.settings.setPosition(PlotHomePosition.valueOf(position)); - runTask(new Runnable() { - @Override - public void run() { - PreparedStatement stmt = null; - try { - stmt = - connection.prepareStatement("UPDATE `plot_settings` SET `position` = ? WHERE `plot_plot_id` = ?"); - stmt.setString(1, position); - stmt.setInt(2, getId(world, plot.id)); - stmt.executeUpdate(); - stmt.close(); - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to set position for plot " + plot.id); - e.printStackTrace(); - } - } - }); + dbManager.setPosition(world, plot, position); } /** @@ -759,51 +170,7 @@ public class DBFunc { * @return */ public static HashMap getSettings(int id) { - HashMap h = new HashMap(); - PreparedStatement stmt = null; - try { - stmt = connection.prepareStatement("SELECT * FROM `plot_settings` WHERE `plot_plot_id` = ?"); - stmt.setInt(1, id); - ResultSet r = stmt.executeQuery(); - String var; - Object val; - while (r.next()) { - var = "biome"; - val = r.getObject(var); - h.put(var, val); - var = "rain"; - val = r.getObject(var); - h.put(var, val); - var = "custom_time"; - val = r.getObject(var); - h.put(var, val); - var = "time"; - val = r.getObject(var); - h.put(var, val); - var = "deny_entry"; - val = r.getObject(var); - h.put(var, (short) 0); - var = "alias"; - val = r.getObject(var); - h.put(var, val); - var = "position"; - val = r.getObject(var); - h.put(var, val); - var = "flags"; - val = r.getObject(var); - h.put(var, val); - var = "merged"; - val = r.getObject(var); - h.put(var, val); - } - stmt.close(); - ; - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to load settings for plot: " + id); - e.printStackTrace(); - } - return h; + return dbManager.getSettings(id); } /** @@ -811,101 +178,12 @@ public class DBFunc { */ public static UUID everyone = UUID.fromString("1-1-3-3-7"); - /** - * @param id - * @return - */ - private static ArrayList plotDenied(int id) { - ArrayList l = new ArrayList(); - PreparedStatement stmt = null; - try { - stmt = connection.prepareStatement("SELECT `user_uuid` FROM `plot_denied` WHERE `plot_plot_id` = ?"); - stmt.setInt(1, id); - ResultSet r = stmt.executeQuery(); - UUID u; - while (r.next()) { - u = UUID.fromString(r.getString("user_uuid")); - l.add(u); - } - stmt.close(); - } - catch (Exception e) { - Logger.add(LogLevel.DANGER, "Failed to load denied for plot: " + id); - e.printStackTrace(); - } - return l; - } - - /** - * @param id - * @return - */ - private static ArrayList plotHelpers(int id) { - ArrayList l = new ArrayList(); - Statement stmt = null; - try { - stmt = connection.createStatement(); - ResultSet r = stmt.executeQuery("SELECT `user_uuid` FROM `plot_helpers` WHERE `plot_plot_id` = " + id); - UUID u; - while (r.next()) { - u = UUID.fromString(r.getString("user_uuid")); - l.add(u); - } - stmt.close(); - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to load helpers for plot: " + id); - e.printStackTrace(); - } - return l; - } - - /** - * @param id - * @return - */ - private static ArrayList plotTrusted(int id) { - ArrayList l = new ArrayList(); - Statement stmt = null; - try { - stmt = connection.createStatement(); - ResultSet r = stmt.executeQuery("SELECT `user_uuid` FROM `plot_trusted` WHERE `plot_plot_id` = " + id); - UUID u; - while (r.next()) { - u = UUID.fromString(r.getString("user_uuid")); - l.add(u); - } - stmt.close(); - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to load trusted users for plot: " + id); - e.printStackTrace(); - } - return l; - } - /** * @param plot * @param player */ public static void removeHelper(final String world, final Plot plot, final OfflinePlayer player) { - runTask(new Runnable() { - @Override - public void run() { - try { - PreparedStatement statement = - connection.prepareStatement("DELETE FROM `plot_helpers` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); - statement.setInt(1, getId(world, plot.id)); - statement.setString(2, player.getUniqueId().toString()); - statement.executeUpdate(); - statement.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.WARNING, "Failed to remove helper for plot " + plot.id); - } - } - }); + dbManager.removeHelper(world, plot, player); } /** @@ -913,23 +191,7 @@ public class DBFunc { * @param player */ public static void removeTrusted(final String world, final Plot plot, final OfflinePlayer player) { - runTask(new Runnable() { - @Override - public void run() { - try { - PreparedStatement statement = - connection.prepareStatement("DELETE FROM `plot_trusted` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); - statement.setInt(1, getId(world, plot.id)); - statement.setString(2, player.getUniqueId().toString()); - statement.executeUpdate(); - statement.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.WARNING, "Failed to remove trusted user for plot " + plot.id); - } - } - }); + dbManager.removeTrusted(world, plot, player); } /** @@ -937,23 +199,7 @@ public class DBFunc { * @param player */ public static void setHelper(final String world, final Plot plot, final OfflinePlayer player) { - runTask(new Runnable() { - @Override - public void run() { - try { - PreparedStatement statement = - connection.prepareStatement("INSERT INTO `plot_helpers` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); - statement.setInt(1, getId(world, plot.id)); - statement.setString(2, player.getUniqueId().toString()); - statement.executeUpdate(); - statement.close(); - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to set helper for plot " + plot.id); - e.printStackTrace(); - } - } - }); + dbManager.setHelper(world, plot, player); } /** @@ -961,23 +207,7 @@ public class DBFunc { * @param player */ public static void setTrusted(final String world, final Plot plot, final OfflinePlayer player) { - runTask(new Runnable() { - @Override - public void run() { - try { - PreparedStatement statement = - connection.prepareStatement("INSERT INTO `plot_trusted` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); - statement.setInt(1, getId(world, plot.id)); - statement.setString(2, player.getUniqueId().toString()); - statement.executeUpdate(); - statement.close(); - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to set plot trusted for plot " + plot.id); - e.printStackTrace(); - } - } - }); + dbManager.setTrusted(world, plot, player); } /** @@ -985,23 +215,7 @@ public class DBFunc { * @param player */ public static void removeDenied(final String world, final Plot plot, final OfflinePlayer player) { - runTask(new Runnable() { - @Override - public void run() { - try { - PreparedStatement statement = - connection.prepareStatement("DELETE FROM `plot_denied` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); - statement.setInt(1, getId(world, plot.id)); - statement.setString(2, player.getUniqueId().toString()); - statement.executeUpdate(); - statement.close(); - } - catch (SQLException e) { - e.printStackTrace(); - Logger.add(LogLevel.WARNING, "Failed to remove denied for plot " + plot.id); - } - } - }); + dbManager.removeDenied(world, plot, player); } /** @@ -1009,42 +223,10 @@ public class DBFunc { * @param player */ public static void setDenied(final String world, final Plot plot, final OfflinePlayer player) { - runTask(new Runnable() { - @Override - public void run() { - try { - PreparedStatement statement = - connection.prepareStatement("INSERT INTO `plot_denied` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); - statement.setInt(1, getId(world, plot.id)); - statement.setString(2, player.getUniqueId().toString()); - statement.executeUpdate(); - statement.close(); - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to set denied for plot " + plot.id); - e.printStackTrace(); - } - } - }); + dbManager.setDenied(world, plot, player); } public static double getRatings(final Plot plot) { - try { - PreparedStatement statement = - connection.prepareStatement("SELECT AVG(`rating`) AS `rating` FROM `plot_ratings` WHERE `plot_plot_id` = ? "); - statement.setInt(1, getId(plot.getWorld().getName(), plot.id)); - ResultSet set = statement.executeQuery(); - double rating = 0; - while (set.next()) { - rating = set.getDouble("rating"); - } - statement.close(); - return rating; - } - catch (SQLException e) { - Logger.add(LogLevel.WARNING, "Failed to fetch rating for plot " + plot.getId().toString()); - e.printStackTrace(); - } - return 0.0d; + return dbManager.getRatings(plot); } } diff --git a/PlotSquared/src/com/intellectualcrafters/plot/database/SQLManager.java b/PlotSquared/src/com/intellectualcrafters/plot/database/SQLManager.java new file mode 100644 index 000000000..b813adf8e --- /dev/null +++ b/PlotSquared/src/com/intellectualcrafters/plot/database/SQLManager.java @@ -0,0 +1,1049 @@ +/* + * Copyright (c) IntellectualCrafters - 2014. You are not allowed to distribute + * and/or monetize any of our intellectual property. IntellectualCrafters is not + * affiliated with Mojang AB. Minecraft is a trademark of Mojang AB. + * + * >> File = DBFunc.java >> Generated by: Citymonstret at 2014-08-09 01:43 + */ + +package com.intellectualcrafters.plot.database; + +import static com.intellectualcrafters.plot.PlotMain.connection; + +import java.nio.channels.AsynchronousByteChannel; +import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.block.Biome; + +import com.intellectualcrafters.plot.Flag; +import com.intellectualcrafters.plot.FlagManager; +import com.intellectualcrafters.plot.Logger; +import com.intellectualcrafters.plot.Logger.LogLevel; +import com.intellectualcrafters.plot.Plot; +import com.intellectualcrafters.plot.PlotHomePosition; +import com.intellectualcrafters.plot.PlotId; +import com.intellectualcrafters.plot.PlotMain; + +/** + * @author Citymonstret + */ +public class SQLManager extends AbstractDB { + + // TODO MongoDB @Brandon + + /** + * Set Plot owner + * + * @param plot + * @param uuid + */ + public void setOwner(final Plot plot, final UUID uuid) { + runTask(new Runnable() { + @Override + public void run() { + try { + PreparedStatement statement = + connection.prepareStatement("UPDATE `plot` SET `owner` = ? WHERE `plot_id_x` = ? AND `plot_id_z` = ? "); + statement.setString(1, uuid.toString()); + statement.setInt(2, plot.id.x); + statement.setInt(3, plot.id.y); + statement.executeUpdate(); + statement.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.DANGER, "Could not set owner for plot " + plot.id); + } + } + }); + } + + @Override + public void createAllSettingsAndHelpers(ArrayList plots) { + HashMap> stored = new HashMap>(); + HashMap> helpers = new HashMap>(); + try { + PreparedStatement stmt = + connection.prepareStatement("SELECT `id`, `plot_id_x`, `plot_id_z`, `world` FROM `plot`"); + ResultSet result = stmt.executeQuery(); + while (result.next()) { + int id = result.getInt("id"); + int idx = result.getInt("plot_id_x"); + int idz = result.getInt("plot_id_z"); + String world = result.getString("world"); + + if (!stored.containsKey(world)) { + stored.put(world, new HashMap()); + } + stored.get(world).put(new PlotId(idx, idz), id); + } + } + catch (SQLException e) { + e.printStackTrace(); + } + + for (Plot plot : plots) { + String world = Bukkit.getWorld(plot.world).getName(); + if (stored.containsKey(world)) { + Integer id = stored.get(world).get(plot.id); + if (id != null) { + helpers.put(id, plot.helpers); + } + } + } + + if (helpers.size() == 0) { + return; + } + + // add plot settings + Integer[] ids = helpers.keySet().toArray(new Integer[0]); + StringBuilder statement = new StringBuilder("INSERT INTO `plot_settings` (`plot_plot_id`) values "); + for (int i = 0; i < (ids.length - 1); i++) { + statement.append("(?),"); + } + statement.append("(?)"); + PreparedStatement stmt = null; + try { + stmt = connection.prepareStatement(statement.toString()); + for (int i = 0; i < ids.length; i++) { + stmt.setInt(i + 1, ids[i]); + } + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + e.printStackTrace(); + } + + // add plot helpers + String prefix = ""; + statement = new StringBuilder("INSERT INTO `plot_helpers` (`plot_plot_id`, `user_uuid`) values "); + for (Integer id : helpers.keySet()) { + for (UUID helper : helpers.get(id)) { + statement.append(prefix + "(?, ?)"); + prefix = ","; + } + } + if (prefix.equals("")) { + return; + } + try { + stmt = connection.prepareStatement(statement.toString()); + int counter = 0; + for (Integer id : helpers.keySet()) { + for (UUID helper : helpers.get(id)) { + + stmt.setInt((counter * 2) + 1, id); + stmt.setString((counter * 2) + 2, helper.toString()); + counter++; + } + } + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to set helper for plots"); + e.printStackTrace(); + } + } + + /** + * Create a plot + * + * @param plots + */ + @Override + public void createPlots(ArrayList plots) { + + // TODO collect list of plots to check if plot exists. + + if (plots.size() == 0) { + return; + } + StringBuilder statement = + new StringBuilder("INSERT INTO `plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) values "); + + for (int i = 0; i < (plots.size() - 1); i++) { + statement.append("(?, ?, ?, ?),"); + } + statement.append("(?, ?, ?, ?)"); + + PreparedStatement stmt = null; + try { + stmt = connection.prepareStatement(statement.toString()); + for (int i = 0; i < plots.size(); i++) { + Plot plot = plots.get(i); + stmt.setInt((i * 4) + 1, plot.id.x); + stmt.setInt((i * 4) + 2, plot.id.y); + stmt.setString((i * 4) + 3, plot.owner.toString()); + stmt.setString((i * 4) + 4, plot.world); + } + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.DANGER, "Failed to save plots!"); + } + } + + /** + * Create a plot + * + * @param plot + */ + @Override + public void createPlot(Plot plot) { + PreparedStatement stmt = null; + try { + stmt = + connection.prepareStatement("INSERT INTO `plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) VALUES(?, ?, ?, ?)"); + stmt.setInt(1, plot.id.x); + stmt.setInt(2, plot.id.y); + stmt.setString(3, plot.owner.toString()); + stmt.setString(4, plot.world); + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.DANGER, "Failed to save plot " + plot.id); + } + } + + /** + * Create tables + * + * @throws SQLException + */ + @Override + public void createTables(String database, boolean add_constraint) throws SQLException { + boolean mysql = database.equals("mysql"); + Statement stmt = connection.createStatement(); + if (mysql) { + stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot` (" + "`id` INT(11) NOT NULL AUTO_INCREMENT," + + "`plot_id_x` INT(11) NOT NULL," + "`plot_id_z` INT(11) NOT NULL," + + "`owner` VARCHAR(45) NOT NULL," + "`world` VARCHAR(45) NOT NULL," + + "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + "PRIMARY KEY (`id`)" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_denied` (" + "`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 `plot_helpers` (" + "`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 `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 `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 `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"); + if (add_constraint) { + stmt.addBatch("ALTER TABLE `plot_settings` ADD CONSTRAINT `plot_settings_ibfk_1` FOREIGN KEY (`plot_plot_id`) REFERENCES `plot` (`id`) ON DELETE CASCADE"); + } + + } + else { + stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot` (" + "`id` INTEGER PRIMARY KEY AUTOINCREMENT," + + "`plot_id_x` INT(11) NOT NULL," + "`plot_id_z` INT(11) NOT NULL," + + "`owner` VARCHAR(45) NOT NULL," + "`world` VARCHAR(45) NOT NULL," + + "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP)"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_denied` (" + "`plot_plot_id` INT(11) NOT NULL," + + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_helpers` (" + "`plot_plot_id` INT(11) NOT NULL," + + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_trusted` (" + "`plot_plot_id` INT(11) NOT NULL," + + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `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 `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.executeBatch(); + stmt.clearBatch(); + stmt.close(); + } + + /** + * Delete a plot + * + * @param plot + */ + @Override + public void delete(final String world, final Plot plot) { + PlotMain.removePlot(world, plot.id, false); + runTask(new Runnable() { + @Override + public void run() { + PreparedStatement stmt = null; + int id = getId(world, plot.id); + try { + stmt = connection.prepareStatement("DELETE FROM `plot_settings` WHERE `plot_plot_id` = ?"); + stmt.setInt(1, id); + stmt.executeUpdate(); + stmt.close(); + stmt = connection.prepareStatement("DELETE FROM `plot_helpers` WHERE `plot_plot_id` = ?"); + stmt.setInt(1, id); + stmt.executeUpdate(); + stmt.close(); + stmt = connection.prepareStatement("DELETE FROM `plot_trusted` WHERE `plot_plot_id` = ?"); + stmt.setInt(1, id); + stmt.executeUpdate(); + stmt.close(); + stmt = connection.prepareStatement("DELETE FROM `plot` WHERE `id` = ?"); + stmt.setInt(1, id); + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.DANGER, "Failed to delete plot " + plot.id); + } + } + }); + } + + /** + * Create plot settings + * + * @param id + * @param plot + */ + @Override + public void createPlotSettings(final int id, final Plot plot) { + runTask(new Runnable() { + @Override + public void run() { + PreparedStatement stmt = null; + try { + stmt = connection.prepareStatement("INSERT INTO `plot_settings`(`plot_plot_id`) VALUES(" + "?)"); + stmt.setInt(1, id); + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + e.printStackTrace(); + } + + } + }); + } + + @Override + public int getId(String world, PlotId id2) { + PreparedStatement stmt = null; + try { + stmt = connection.prepareStatement("SELECT `id` FROM `plot` WHERE `plot_id_x` = ? AND `plot_id_z` = ? AND world = ? ORDER BY `timestamp` ASC"); + stmt.setInt(1, id2.x); + stmt.setInt(2, id2.y); + stmt.setString(3, world); + ResultSet r = stmt.executeQuery(); + int id = Integer.MAX_VALUE; + while (r.next()) { + id = r.getInt("id"); + } + stmt.close(); + return id; + } + catch (SQLException e) { + e.printStackTrace(); + } + return Integer.MAX_VALUE; + } + + /** + * @return + */ + @Override + public HashMap> getPlots() { + try { + DatabaseMetaData data = connection.getMetaData(); + ResultSet rs = data.getColumns(null, null, "plot", "plot_id"); + boolean execute = rs.next(); + if (execute) { + Statement statement = connection.createStatement(); + statement.addBatch("ALTER IGNORE TABLE `plot` ADD `plot_id_x` int(11) DEFAULT 0"); + statement.addBatch("ALTER IGNORE TABLE `plot` ADD `plot_id_z` int(11) DEFAULT 0"); + statement.addBatch("UPDATE `plot` SET\n" + " `plot_id_x` = IF(" + + " LOCATE(';', `plot_id`) > 0," + + " SUBSTRING(`plot_id`, 1, LOCATE(';', `plot_id`) - 1)," + " `plot_id`" + + " )," + " `plot_id_z` = IF(" + " LOCATE(';', `plot_id`) > 0," + + " SUBSTRING(`plot_id`, LOCATE(';', `plot_id`) + 1)," + " NULL" + " )"); + statement.addBatch("ALTER TABLE `plot` DROP `plot_id`"); + statement.addBatch("ALTER IGNORE TABLE `plot_settings` ADD `flags` VARCHAR(512) DEFAULT NULL"); + statement.executeBatch(); + statement.close(); + } + rs = data.getColumns(null, null, "plot_settings", "merged"); + if (!rs.next()) { + Statement statement = connection.createStatement(); + statement.addBatch("ALTER TABLE `plot_settings` ADD `merged` int(11) DEFAULT NULL"); + statement.executeBatch(); + statement.close(); + } + } + catch (Exception e) { + e.printStackTrace(); + } + HashMap> plots = new HashMap>(); + Statement stmt = null; + try { + + Set worlds = new HashSet(); + if (PlotMain.config.contains("worlds")) { + worlds = PlotMain.config.getConfigurationSection("worlds").getKeys(false); + } + + stmt = connection.createStatement(); + ResultSet r = stmt.executeQuery("SELECT `id`, `plot_id_x`, `plot_id_z`, `owner`, `world` FROM `plot`"); + PlotId plot_id; + int id; + Plot p; + HashMap noExist = new HashMap(); + while (r.next()) { + plot_id = new PlotId(r.getInt("plot_id_x"), r.getInt("plot_id_z")); + id = r.getInt("id"); + String worldname = r.getString("world"); + HashMap settings = getSettings(id); + UUID owner = UUID.fromString(r.getString("owner")); + Biome plotBiome = Biome.FOREST; + String[] flags_string; + if (settings.get("flags") == null) { + flags_string = new String[] {}; + } + else { + flags_string = ((String) settings.get("flags")).split(","); + } + Flag[] flags = new Flag[flags_string.length]; + for (int i = 0; i < flags.length; i++) { + if (flags_string[i].contains(":")) { + String[] split = flags_string[i].split(":"); + flags[i] = new Flag(FlagManager.getFlag(split[0], true), split[1]); + } + else { + flags[i] = new Flag(FlagManager.getFlag(flags_string[i], true), ""); + } + } + ArrayList helpers = plotHelpers(id); + ArrayList trusted = plotTrusted(id); + ArrayList denied = plotDenied(id); + + String alias = (String) settings.get("alias"); + if ((alias == null) || alias.equalsIgnoreCase("NEW")) { + alias = ""; + } + PlotHomePosition position = null; + for (PlotHomePosition plotHomePosition : PlotHomePosition.values()) { + if (settings.get("position") == null) { + position = PlotHomePosition.DEFAULT; + break; + } + if (plotHomePosition.isMatching((String) settings.get("position"))) { + position = plotHomePosition; + } + } + if (position == null) { + position = PlotHomePosition.DEFAULT; + } + int merged_int = settings.get("merged") == null ? 0 : (int) settings.get("merged"); + + boolean[] merged = new boolean[4]; + for (int i = 0; i < 4; i++) { + merged[3 - i] = (merged_int & (1 << i)) != 0; + } + p = + new Plot(plot_id, owner, plotBiome, helpers, trusted, denied, alias, position, flags, worldname, merged); + if (plots.containsKey(worldname)) { + plots.get(worldname).put((plot_id), p); + } + else { + HashMap map = new HashMap(); + map.put((plot_id), p); + plots.put(worldname, map); + if (!worlds.contains(p.world)) { + if (noExist.containsKey(worldname)) { + noExist.put(worldname,noExist.get(worldname)+1); + } + else { + noExist.put(worldname,1); + } + } + } + } + for (String worldname: noExist.keySet()) { + PlotMain.sendConsoleSenderMessage("&c[WARNING] Found "+noExist.get(worldname)+" plots in DB for non existant world; '"+worldname+"'!!!\n&c - Please create this world, or remove the plots from the DB using the purge command!"); + } + stmt.close(); + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to load plots."); + e.printStackTrace(); + } + return plots; + } + + + @Override + public void setMerged(final String world, final Plot plot, final boolean[] merged) { + plot.settings.setMerged(merged); + runTask(new Runnable() { + @Override + public void run() { + try { + int n = 0; + for (int i = 0; i < 4; ++i) { + n = (n << 1) + (merged[i] ? 1 : 0); + } + PreparedStatement stmt = + connection.prepareStatement("UPDATE `plot_settings` SET `merged` = ? WHERE `plot_plot_id` = ?"); + stmt.setInt(1, n); + stmt.setInt(2, getId(world, plot.id)); + stmt.execute(); + stmt.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.WARNING, "Could not set merged for plot " + plot.id); + } + } + }); + } + + @Override + public void setFlags(final String world, final Plot plot, final Flag[] flags) { + plot.settings.setFlags(flags); + final StringBuilder flag_string = new StringBuilder(); + int i = 0; + for (Flag flag : flags) { + if (i != 0) { + flag_string.append(","); + } + flag_string.append(flag.getKey() + ":" + flag.getValue()); + i++; + } + runTask(new Runnable() { + @Override + public void run() { + try { + PreparedStatement stmt = + connection.prepareStatement("UPDATE `plot_settings` SET `flags` = ? WHERE `plot_plot_id` = ?"); + stmt.setString(1, flag_string.toString()); + stmt.setInt(2, getId(world, plot.id)); + stmt.execute(); + stmt.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.WARNING, "Could not set flag for plot " + plot.id); + } + } + }); + } + + /** + * @param plot + * @param alias + */ + @Override + public void setAlias(final String world, final Plot plot, final String alias) { + plot.settings.setAlias(alias); + runTask(new Runnable() { + @Override + public void run() { + PreparedStatement stmt = null; + try { + stmt = + connection.prepareStatement("UPDATE `plot_settings` SET `alias` = ? WHERE `plot_plot_id` = ?"); + stmt.setString(1, alias); + stmt.setInt(2, getId(world, plot.id)); + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to set alias for plot " + plot.id); + e.printStackTrace(); + } + + } + }); + } + + /** + * @param r + */ + private void runTask(Runnable r) { + PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), r); + } + + @Override + public void purge(final String world, final PlotId id) { + runTask(new Runnable() { + @Override + public void run() { + ArrayList ids = new ArrayList(); + + // Fetching a list of plot IDs for a world + try { + PreparedStatement stmt = connection.prepareStatement("SELECT `id` FROM `plot` WHERE `world` = ? AND `plot_id_x` = ? AND `plot_id_z` = ?"); + stmt.setString(1, world); + stmt.setInt(2, id.x); + stmt.setInt(3, id.y); + ResultSet result = stmt.executeQuery(); + while (result.next()) { + int id = result.getInt("id"); + ids.add(id); + } + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.WARNING, "FAILED TO PURGE WORLD '"+world+"'!"); + return; + } + if (ids.size() > 0) { + try { + + String prefix = ""; + StringBuilder idstr = new StringBuilder(""); + + for (Integer id:ids) { + idstr.append(prefix + id); + prefix = " OR `plot_plot_id` = "; + } + + PreparedStatement stmt = connection.prepareStatement("DELETE FROM `plot_helpers` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot_denied` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot_settings` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot_trusted` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot` WHERE `plot_plot_id` = "+idstr+""); + stmt.setString(1, world); + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.DANGER, "FAILED TO PURGE PLOT FROM DB '"+world+"' , '"+id+"' !"); + return; + } + } + Logger.add(LogLevel.GENERAL, "SUCCESSFULLY PURGED PLOT FROM DB '"+world+"' , '"+id+"'!"); + } + }); + } + + @Override + public void purge(final String world) { + runTask(new Runnable() { + @Override + public void run() { + ArrayList ids = new ArrayList(); + + // Fetching a list of plot IDs for a world + try { + PreparedStatement stmt = connection.prepareStatement("SELECT `id` FROM `plot` WHERE `world` = ?"); + stmt.setString(1, world); + ResultSet result = stmt.executeQuery(); + while (result.next()) { + int id = result.getInt("id"); + ids.add(id); + } + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.WARNING, "FAILED TO PURGE WORLD '"+world+"'!"); + return; + } + if (ids.size() > 0) { + try { + + String prefix = ""; + StringBuilder idstr = new StringBuilder(""); + + for (Integer id:ids) { + idstr.append(prefix + id); + prefix = " OR `plot_plot_id` = "; + } + + PreparedStatement stmt = connection.prepareStatement("DELETE FROM `plot_helpers` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot_denied` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot_settings` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot_trusted` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot` WHERE `world` = ?"); + stmt.setString(1, world); + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.DANGER, "FAILED TO PURGE WORLD '"+world+"'!"); + return; + } + } + Logger.add(LogLevel.GENERAL, "SUCCESSFULLY PURGED WORLD '"+world+"'!"); + } + }); + } + + /** + * @param plot + * @param position + */ + @Override + public void setPosition(final String world, final Plot plot, final String position) { + plot.settings.setPosition(PlotHomePosition.valueOf(position)); + runTask(new Runnable() { + @Override + public void run() { + PreparedStatement stmt = null; + try { + stmt = + connection.prepareStatement("UPDATE `plot_settings` SET `position` = ? WHERE `plot_plot_id` = ?"); + stmt.setString(1, position); + stmt.setInt(2, getId(world, plot.id)); + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to set position for plot " + plot.id); + e.printStackTrace(); + } + } + }); + } + + /** + * @param id + * @return + */ + @Override + public HashMap getSettings(int id) { + HashMap h = new HashMap(); + PreparedStatement stmt = null; + try { + stmt = connection.prepareStatement("SELECT * FROM `plot_settings` WHERE `plot_plot_id` = ?"); + stmt.setInt(1, id); + ResultSet r = stmt.executeQuery(); + String var; + Object val; + while (r.next()) { + var = "biome"; + val = r.getObject(var); + h.put(var, val); + var = "rain"; + val = r.getObject(var); + h.put(var, val); + var = "custom_time"; + val = r.getObject(var); + h.put(var, val); + var = "time"; + val = r.getObject(var); + h.put(var, val); + var = "deny_entry"; + val = r.getObject(var); + h.put(var, (short) 0); + var = "alias"; + val = r.getObject(var); + h.put(var, val); + var = "position"; + val = r.getObject(var); + h.put(var, val); + var = "flags"; + val = r.getObject(var); + h.put(var, val); + var = "merged"; + val = r.getObject(var); + h.put(var, val); + } + stmt.close(); + ; + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to load settings for plot: " + id); + e.printStackTrace(); + } + return h; + } + /** + * @param id + * @return + */ + private ArrayList plotDenied(int id) { + ArrayList l = new ArrayList(); + PreparedStatement stmt = null; + try { + stmt = connection.prepareStatement("SELECT `user_uuid` FROM `plot_denied` WHERE `plot_plot_id` = ?"); + stmt.setInt(1, id); + ResultSet r = stmt.executeQuery(); + UUID u; + while (r.next()) { + u = UUID.fromString(r.getString("user_uuid")); + l.add(u); + } + stmt.close(); + } + catch (Exception e) { + Logger.add(LogLevel.DANGER, "Failed to load denied for plot: " + id); + e.printStackTrace(); + } + return l; + } + + /** + * @param id + * @return + */ + private ArrayList plotHelpers(int id) { + ArrayList l = new ArrayList(); + Statement stmt = null; + try { + stmt = connection.createStatement(); + ResultSet r = stmt.executeQuery("SELECT `user_uuid` FROM `plot_helpers` WHERE `plot_plot_id` = " + id); + UUID u; + while (r.next()) { + u = UUID.fromString(r.getString("user_uuid")); + l.add(u); + } + stmt.close(); + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to load helpers for plot: " + id); + e.printStackTrace(); + } + return l; + } + + /** + * @param id + * @return + */ + private ArrayList plotTrusted(int id) { + ArrayList l = new ArrayList(); + Statement stmt = null; + try { + stmt = connection.createStatement(); + ResultSet r = stmt.executeQuery("SELECT `user_uuid` FROM `plot_trusted` WHERE `plot_plot_id` = " + id); + UUID u; + while (r.next()) { + u = UUID.fromString(r.getString("user_uuid")); + l.add(u); + } + stmt.close(); + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to load trusted users for plot: " + id); + e.printStackTrace(); + } + return l; + } + + /** + * @param plot + * @param player + */ + @Override + public void removeHelper(final String world, final Plot plot, final OfflinePlayer player) { + runTask(new Runnable() { + @Override + public void run() { + try { + PreparedStatement statement = + connection.prepareStatement("DELETE FROM `plot_helpers` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); + statement.setInt(1, getId(world, plot.id)); + statement.setString(2, player.getUniqueId().toString()); + statement.executeUpdate(); + statement.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.WARNING, "Failed to remove helper for plot " + plot.id); + } + } + }); + } + + /** + * @param plot + * @param player + */ + @Override + public void removeTrusted(final String world, final Plot plot, final OfflinePlayer player) { + runTask(new Runnable() { + @Override + public void run() { + try { + PreparedStatement statement = + connection.prepareStatement("DELETE FROM `plot_trusted` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); + statement.setInt(1, getId(world, plot.id)); + statement.setString(2, player.getUniqueId().toString()); + statement.executeUpdate(); + statement.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.WARNING, "Failed to remove trusted user for plot " + plot.id); + } + } + }); + } + + /** + * @param plot + * @param player + */ + @Override + public void setHelper(final String world, final Plot plot, final OfflinePlayer player) { + runTask(new Runnable() { + @Override + public void run() { + try { + PreparedStatement statement = + connection.prepareStatement("INSERT INTO `plot_helpers` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); + statement.setInt(1, getId(world, plot.id)); + statement.setString(2, player.getUniqueId().toString()); + statement.executeUpdate(); + statement.close(); + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to set helper for plot " + plot.id); + e.printStackTrace(); + } + } + }); + } + + /** + * @param plot + * @param player + */ + @Override + public void setTrusted(final String world, final Plot plot, final OfflinePlayer player) { + runTask(new Runnable() { + @Override + public void run() { + try { + PreparedStatement statement = + connection.prepareStatement("INSERT INTO `plot_trusted` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); + statement.setInt(1, getId(world, plot.id)); + statement.setString(2, player.getUniqueId().toString()); + statement.executeUpdate(); + statement.close(); + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to set plot trusted for plot " + plot.id); + e.printStackTrace(); + } + } + }); + } + + /** + * @param plot + * @param player + */ + @Override + public void removeDenied(final String world, final Plot plot, final OfflinePlayer player) { + runTask(new Runnable() { + @Override + public void run() { + try { + PreparedStatement statement = + connection.prepareStatement("DELETE FROM `plot_denied` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); + statement.setInt(1, getId(world, plot.id)); + statement.setString(2, player.getUniqueId().toString()); + statement.executeUpdate(); + statement.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.WARNING, "Failed to remove denied for plot " + plot.id); + } + } + }); + } + + /** + * @param plot + * @param player + */ + @Override + public void setDenied(final String world, final Plot plot, final OfflinePlayer player) { + runTask(new Runnable() { + @Override + public void run() { + try { + PreparedStatement statement = + connection.prepareStatement("INSERT INTO `plot_denied` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); + statement.setInt(1, getId(world, plot.id)); + statement.setString(2, player.getUniqueId().toString()); + statement.executeUpdate(); + statement.close(); + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to set denied for plot " + plot.id); + e.printStackTrace(); + } + } + }); + } + + @Override + public double getRatings(final Plot plot) { + try { + PreparedStatement statement = + connection.prepareStatement("SELECT AVG(`rating`) AS `rating` FROM `plot_ratings` WHERE `plot_plot_id` = ? "); + statement.setInt(1, getId(plot.getWorld().getName(), plot.id)); + ResultSet set = statement.executeQuery(); + double rating = 0; + while (set.next()) { + rating = set.getDouble("rating"); + } + statement.close(); + return rating; + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to fetch rating for plot " + plot.getId().toString()); + e.printStackTrace(); + } + return 0.0d; + } +}