diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java index d93137f00..867b6665d 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java @@ -98,6 +98,7 @@ import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.titles.AbstractTitle; import com.intellectualcrafters.plot.titles.DefaultTitle; +import com.intellectualcrafters.plot.util.ClusterManager; import com.intellectualcrafters.plot.util.ConsoleColors; import com.intellectualcrafters.plot.util.ExpireManager; import com.intellectualcrafters.plot.util.Lag; @@ -812,6 +813,7 @@ public class PlotMain extends JavaPlugin implements Listener { config.set("version", config_ver); final Map options = new HashMap<>(); options.put("auto_update", false); + options.put("clusters.enabled", Settings.ENABLE_CLUSTERS); options.put("plotme-alias", Settings.USE_PLOTME_ALIAS); options.put("plotme-convert.enabled", Settings.CONVERT_PLOTME); options.put("claim.max-auto-area", Settings.MAX_AUTO_SIZE); @@ -842,6 +844,7 @@ public class PlotMain extends JavaPlugin implements Listener { config.set(node.getKey(), node.getValue()); } } + Settings.ENABLE_CLUSTERS = config.getBoolean("clusters.enabled"); Settings.DEBUG = config.getBoolean("debug"); if (Settings.DEBUG) { sendConsoleSenderMessage(C.PREFIX.s() + "&6Debug Mode Enabled (Default). Edit the config to turn this off."); @@ -1278,6 +1281,9 @@ public class PlotMain extends JavaPlugin implements Listener { return; } plots = DBFunc.getPlots(); + if (Settings.ENABLE_CLUSTERS) { + ClusterManager.clusters = DBFunc.getClusters(); + } } // TODO: Implement mongo else if (Settings.DB.USE_MONGO) { @@ -1310,6 +1316,9 @@ public class PlotMain extends JavaPlugin implements Listener { return; } plots = DBFunc.getPlots(); + if (Settings.ENABLE_CLUSTERS) { + ClusterManager.clusters = DBFunc.getClusters(); + } } else { Logger.add(LogLevel.DANGER, "No storage type is set."); sendConsoleSenderMessage(C.PREFIX + "&cNo storage type is set!"); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java index 162edb9d7..36878a6bd 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java @@ -29,6 +29,7 @@ package com.intellectualcrafters.plot.config; * @author Empire92 */ public class Settings { + public static boolean ENABLE_CLUSTERS = false; /** * Default UUID_FECTHING: false */ 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 78154c985..ab3314048 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java @@ -24,12 +24,15 @@ package com.intellectualcrafters.plot.database; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Set; import java.util.UUID; import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotCluster; +import com.intellectualcrafters.plot.object.PlotClusterId; import com.intellectualcrafters.plot.object.PlotComment; import com.intellectualcrafters.plot.object.PlotId; @@ -90,6 +93,8 @@ public interface AbstractDB { * @param plot Plot that should be deleted */ public void delete(final String world, final Plot plot); + + public void delete(final PlotCluster cluster); /** * Create plot settings @@ -108,12 +113,28 @@ public interface AbstractDB { * @return Integer = Plot Entry Id */ public int getId(final String world, final PlotId id2); - + + /** + * Get the id of a given plot cluster + * + * @param world Which the plot is located in + * @param pos1 bottom Plot ID + * @param pos2 top Plot ID + * + * @return Integer = Cluster Entry Id + */ + public int getClusterId(final String world, final PlotClusterId id); + /** * @return A linked hashmap containing all plots */ public LinkedHashMap> getPlots(); + /** + * @return A hashmap containing all plot clusters + */ + public HashMap> getClusters(); + /** * Set the merged status for a plot * @@ -131,7 +152,22 @@ public interface AbstractDB { * @param flags flags to set (flag[]) */ public void setFlags(final String world, final Plot plot, final Set flags); - + + /** + * Set cluster flags + * + * @param world World in which the plot is located + * @param cluster PlotCluster Object + * @param flags flags to set (flag[]) + */ + public void setFlags(final PlotCluster cluster, final Set flags); + + /** + * Rename a cluster + */ + public void setClusterName(final PlotCluster cluster, final String name); + + /** * Set the plot alias * @@ -162,6 +198,13 @@ public interface AbstractDB { * @param position Plot Home Position */ public void setPosition(final String world, final Plot plot, final String position); + + /** + * + * @param cluster + * @param position + */ + public void setPosition(final PlotCluster cluster, final String position); /** * @param id Plot Entry ID @@ -169,12 +212,25 @@ public interface AbstractDB { * @return Plot Settings */ public HashMap getSettings(final int id); + + /** + * + * @param id + * @return + */ + public HashMap getClusterSettings(final int id); /** * @param plot Plot Object * @param uuid Player that should be removed */ public void removeHelper(final String world, final Plot plot, final UUID uuid); + + /** + * @param cluster PlotCluster Object + * @param uuid Player that should be removed + */ + public void removeHelper(final PlotCluster cluster, final UUID uuid); /** * @param plot Plot Object @@ -187,6 +243,12 @@ public interface AbstractDB { * @param uuid Player that should be removed */ public void setHelper(final String world, final Plot plot, final UUID uuid); + + /** + * @param cluster PlotCluster Object + * @param uuid Player that should be removed + */ + public void setHelper(final PlotCluster cluster, final UUID uuid); /** * @param plot Plot Object @@ -245,4 +307,8 @@ public interface AbstractDB { public ArrayList getComments(final String world, final Plot plot, final int tier, boolean below); public void createPlotAndSettings(Plot plot); + + public void createCluster(PlotCluster cluster); + + public void resizeCluster(PlotCluster current, PlotClusterId resize); } 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 f17edb351..67be668e0 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java @@ -23,12 +23,15 @@ package com.intellectualcrafters.plot.database; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Set; import java.util.UUID; import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotCluster; +import com.intellectualcrafters.plot.object.PlotClusterId; import com.intellectualcrafters.plot.object.PlotComment; import com.intellectualcrafters.plot.object.PlotId; @@ -159,6 +162,10 @@ public class DBFunc { public static void setFlags(final String world, final Plot plot, final Set flags) { dbManager.setFlags(world, plot, flags); } + + public static void setFlags(final PlotCluster cluster, final Set flags) { + dbManager.setFlags(cluster, flags); + } /** * @param plot @@ -223,6 +230,32 @@ public class DBFunc { public static void removeHelper(final String world, final Plot plot, final UUID uuid) { dbManager.removeHelper(world, plot, uuid); } + + /** + * @param cluster + * @param player + */ + public static void removeHelper(final PlotCluster cluster, final UUID uuid) { + dbManager.removeHelper(cluster, uuid); + } + + /** + * @param world + * @param cluster + * @param name + */ + public static void createCluster(String world, PlotCluster cluster) { + dbManager.createCluster(cluster); + } + + /** + * @param world + * @param current + * @param resize + */ + public static void resizeCluster(PlotCluster current, PlotClusterId resize) { + dbManager.resizeCluster(current, resize); + } /** * @param plot @@ -239,6 +272,10 @@ public class DBFunc { public static void setHelper(final String world, final Plot plot, final UUID uuid) { dbManager.setHelper(world, plot, uuid); } + + public static void setHelper(final PlotCluster cluster, final UUID uuid) { + dbManager.setHelper(cluster, uuid); + } /** * @param plot @@ -267,4 +304,16 @@ public class DBFunc { public static double getRatings(final Plot plot) { return dbManager.getRatings(plot); } + + public static HashMap> getClusters() { + return dbManager.getClusters(); + } + + public static void setPosition(PlotCluster cluster, String position) { + dbManager.setPosition(cluster, position); + } + + public static HashMap getClusterSettings(int id) { + return dbManager.getClusterSettings(id); + } } 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 52c293fa6..84cc36b80 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -43,8 +43,11 @@ import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.object.BlockLoc; import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotCluster; +import com.intellectualcrafters.plot.object.PlotClusterId; import com.intellectualcrafters.plot.object.PlotComment; import com.intellectualcrafters.plot.object.PlotId; +import com.intellectualcrafters.plot.util.ClusterManager; import com.intellectualcrafters.plot.util.TaskManager; /** @@ -59,6 +62,7 @@ public class SQLManager implements AbstractDB { public final String CREATE_SETTINGS; public final String CREATE_HELPERS; public final String CREATE_PLOT; + public final String CREATE_CLUSTER; // Private Final private Connection connection; private final String prefix; @@ -83,6 +87,7 @@ public class SQLManager implements AbstractDB { this.CREATE_SETTINGS = "INSERT INTO `" + this.prefix + "plot_settings` (`plot_plot_id`) values "; this.CREATE_HELPERS = "INSERT INTO `" + this.prefix + "plot_helpers` (`plot_plot_id`, `user_uuid`) values "; this.CREATE_PLOT = "INSERT INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) VALUES(?, ?, ?, ?)"; + this.CREATE_CLUSTER = "INSERT INTO `" + this.prefix + "cluster`(`pos1_x`, `pos1_z`, `pos2_x`, `pos2_z`, `owner`, `world`) VALUES(?, ?, ?, ?, ?, ?)"; // schedule reconnect if (PlotMain.getMySQL() != null) { @@ -376,15 +381,22 @@ public class SQLManager implements AbstractDB { 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"); } + 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(45) NOT NULL," + "`world` VARCHAR(45) NOT NULL," + "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "cluster_helpers` (" + "`cluster_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + "`tier` INT(11) NOT NULL" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "cluster_settings` (" + " `cluster_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 (`cluster_id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); } else { - stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "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 `" + this.prefix + "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 `" + this.prefix + "plot_denied` (" + "`plot_plot_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_helpers` (" + "`plot_plot_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); 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` (" + "`plot_plot_id` INT(11) NOT NULL," + "`comment` VARCHAR(40) NOT NULL," + "`tier` 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 + "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(45) 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" + "`tier` INT(11) NOT NULL" + ")"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "cluster_settings` (" + " `cluster_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 (`cluster_id`)" + ")"); } stmt.executeBatch(); stmt.clearBatch(); @@ -1229,4 +1241,253 @@ public class SQLManager implements AbstractDB { } return 0.0d; } + + @Override + public void delete(final PlotCluster cluster) { + ClusterManager.removeCluster(cluster); + TaskManager.runTask(new Runnable() { + @Override + public void run() { + PreparedStatement stmt = null; + final int id = getClusterId(cluster.world, ClusterManager.getClusterId(cluster)); + try { + stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + SQLManager.this.prefix + "cluster_settings` WHERE `cluster_id` = ?"); + stmt.setInt(1, id); + stmt.executeUpdate(); + stmt.close(); + stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + SQLManager.this.prefix + "cluster_helpers` WHERE `cluster_id` = ?"); + stmt.setInt(1, id); + stmt.executeUpdate(); + stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + SQLManager.this.prefix + "cluster` WHERE `id` = ?"); + stmt.setInt(1, id); + stmt.executeUpdate(); + stmt.close(); + } catch (final SQLException e) { + e.printStackTrace(); + PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"Failed to delete plot cluster: " + cluster.pos1 + ":" + cluster.pos2); + } + } + }); + } + + @Override + public int getClusterId(String world, PlotClusterId id) { + PreparedStatement stmt = null; + try { + stmt = this.connection.prepareStatement("SELECT `id` FROM `" + this.prefix + "cluster` WHERE `pos1_x` = ? AND `pos1_z` = ? AND `pos2_x` = ? AND `pos2_z` = ? AND world = ? ORDER BY `timestamp` ASC"); + stmt.setInt(1, id.pos1.x); + stmt.setInt(2, id.pos1.y); + stmt.setInt(3, id.pos1.x); + stmt.setInt(3, id.pos2.y); + stmt.setString(5, world); + final ResultSet r = stmt.executeQuery(); + int c_id = Integer.MAX_VALUE; + while (r.next()) { + c_id = r.getInt("id"); + } + stmt.close(); + return c_id; + } catch (final SQLException e) { + e.printStackTrace(); + } + return Integer.MAX_VALUE; + } + + @Override + public HashMap> getClusters() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setFlags(final PlotCluster cluster, Set flags) { + final StringBuilder flag_string = new StringBuilder(); + int i = 0; + for (final Flag flag : flags) { + if (i != 0) { + flag_string.append(","); + } + flag_string.append(flag.getKey() + ":" + flag.getValueString().replaceAll(":", "\u00AF").replaceAll(",", "\u00B4")); + i++; + } + TaskManager.runTask(new Runnable() { + @Override + public void run() { + try { + final PreparedStatement stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "cluster_settings` SET `flags` = ? WHERE `cluster_id` = ?"); + stmt.setString(1, flag_string.toString()); + stmt.setInt(2, getClusterId(cluster.world, ClusterManager.getClusterId(cluster))); + stmt.execute(); + stmt.close(); + } catch (final SQLException e) { + e.printStackTrace(); + PlotMain.sendConsoleSenderMessage("&7[WARN] "+"Could not set flag for plot " + cluster); + } + } + }); + + } + + @Override + public void setClusterName(final PlotCluster cluster, final String name) { + cluster.settings.setAlias(name); + TaskManager.runTask(new Runnable() { + @Override + public void run() { + PreparedStatement stmt = null; + try { + stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "cluster_settings` SET `alias` = ? WHERE `cluster_id` = ?"); + stmt.setString(1, name); + stmt.setInt(2, getClusterId(cluster.world, ClusterManager.getClusterId(cluster))); + stmt.executeUpdate(); + stmt.close(); + } catch (final SQLException e) { + PlotMain.sendConsoleSenderMessage("&7[WARN] "+"Failed to set alias for cluster " + cluster); + e.printStackTrace(); + } + + } + }); + } + + @Override + public void removeHelper(final PlotCluster cluster, final UUID uuid) { + TaskManager.runTask(new Runnable() { + @Override + public void run() { + try { + final PreparedStatement statement = SQLManager.this.connection.prepareStatement("DELETE FROM `" + SQLManager.this.prefix + "cluster_helpers` WHERE `cluster_id` = ? AND `user_uuid` = ?"); + statement.setInt(1, getClusterId(cluster.world, ClusterManager.getClusterId(cluster))); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); + statement.close(); + } catch (final SQLException e) { + e.printStackTrace(); + PlotMain.sendConsoleSenderMessage("&7[WARN] "+"Failed to remove helper for cluster " + cluster); + } + } + }); + } + + @Override + public void setHelper(final PlotCluster cluster, final UUID uuid) { + TaskManager.runTask(new Runnable() { + @Override + public void run() { + try { + final PreparedStatement statement = SQLManager.this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "cluster_helpers` (`cluster_id`, `user_uuid`) VALUES(?,?)"); + statement.setInt(1, getClusterId(cluster.world, ClusterManager.getClusterId(cluster))); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); + statement.close(); + } catch (final SQLException e) { + PlotMain.sendConsoleSenderMessage("&7[WARN] "+"Failed to set helper for cluster " + cluster); + e.printStackTrace(); + } + } + }); + } + + @Override + public void createCluster(final PlotCluster cluster) { + TaskManager.runTask(new Runnable() { + @Override + public void run() { + PreparedStatement stmt = null; + try { + stmt = SQLManager.this.connection.prepareStatement(SQLManager.this.CREATE_CLUSTER); + stmt.setInt(1, cluster.pos1.x); + stmt.setInt(2, cluster.pos1.y); + stmt.setInt(3, cluster.pos2.x); + stmt.setInt(4, cluster.pos2.y); + stmt.setString(5, cluster.owner.toString()); + stmt.setString(6, cluster.world); + stmt.executeUpdate(); + stmt.close(); + + int id = getClusterId(cluster.world, ClusterManager.getClusterId(cluster)); + stmt = SQLManager.this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "cluster_settings`(`cluster_id`) VALUES(" + "?)"); + stmt.setInt(1, id); + stmt.executeUpdate(); + stmt.close(); + } catch (final Exception e) { + e.printStackTrace(); + PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"Failed to save cluster " + cluster); + } + } + }); + } + + @Override + public void resizeCluster(PlotCluster current, PlotClusterId resize) { + // TODO Auto-generated method stub + + } + + @Override + public void setPosition(final PlotCluster cluster, final String position) { + TaskManager.runTask(new Runnable() { + @Override + public void run() { + PreparedStatement stmt = null; + try { + stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "cluster_settings` SET `position` = ? WHERE `cluster_id` = ?"); + stmt.setString(1, position); + stmt.setInt(2, getClusterId(cluster.world, ClusterManager.getClusterId(cluster))); + stmt.executeUpdate(); + stmt.close(); + } catch (final SQLException e) { + PlotMain.sendConsoleSenderMessage("&7[WARN] "+"Failed to set position for cluster " + cluster); + e.printStackTrace(); + } + } + }); + } + + @Override + public HashMap getClusterSettings(int id) { + final HashMap h = new HashMap(); + PreparedStatement stmt = null; + try { + stmt = this.connection.prepareStatement("SELECT * FROM `" + this.prefix + "cluster_settings` WHERE `cluster_id` = ?"); + stmt.setInt(1, id); + final 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 (final SQLException e) { + PlotMain.sendConsoleSenderMessage("&7[WARN] "+"Failed to load settings for cluster: " + id); + e.printStackTrace(); + } + return h; + } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java index 3846bd77f..17ac3e04c 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java @@ -35,6 +35,8 @@ import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.events.PlotFlagAddEvent; import com.intellectualcrafters.plot.events.PlotFlagRemoveEvent; import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotCluster; +import com.intellectualcrafters.plot.object.PlotSettings; import com.intellectualcrafters.plot.object.PlotWorld; /** @@ -63,18 +65,12 @@ import com.intellectualcrafters.plot.object.PlotWorld; return (getFlag(flag.getKey()) == null) && flags.add(flag); } - /** - * Get the value of a flag for a plot (respects flag defaults) - * @param plot - * @param flag - * @return - */ - public static Flag getPlotFlag(Plot plot, String flag) { - ArrayList flags = new ArrayList<>(); - if (plot.settings.flags != null && plot.settings.flags.size() > 0) { - flags.addAll(plot.settings.flags); + public static Flag getSettingFlag(String world, PlotSettings settings, String flag) { + ArrayList flags = new ArrayList<>(); + if (settings.flags != null && settings.flags.size() > 0) { + flags.addAll(settings.flags); } - PlotWorld plotworld = PlotMain.getWorldSettings(plot.world); + PlotWorld plotworld = PlotMain.getWorldSettings(world); if (plotworld != null && plotworld.DEFAULT_FLAGS != null && plotworld.DEFAULT_FLAGS.length > 0) { flags.addAll(Arrays.asList(plotworld.DEFAULT_FLAGS)); } @@ -86,12 +82,25 @@ import com.intellectualcrafters.plot.object.PlotWorld; return null; } + /** + * Get the value of a flag for a plot (respects flag defaults) + * @param plot + * @param flag + * @return + */ + public static Flag getPlotFlag(Plot plot, String flag) { + return getSettingFlag(plot.world, plot.settings, flag); + } + public static boolean isPlotFlagTrue(Plot plot, String strFlag) { Flag flag = getPlotFlag(plot, strFlag); if (flag == null) { return false; } - return flag.getValue().equals("true"); + if (flag.getValue() instanceof Boolean) { + return (boolean) flag.getValue(); + } + return false; } /** @@ -101,10 +110,14 @@ import com.intellectualcrafters.plot.object.PlotWorld; * @return */ public static Flag getPlotFlagAbs(Plot plot, String flag) { - if (plot.settings.flags == null || plot.settings.flags.size() == 0) { + return getSettingFlagAbs(plot.settings, flag); + } + + public static Flag getSettingFlagAbs(PlotSettings settings, String flag) { + if (settings.flags == null || settings.flags.size() == 0) { return null; } - for (final Flag myflag : plot.settings.flags) { + for (final Flag myflag : settings.flags) { if (myflag.getKey().equals(flag)) { return myflag; } @@ -131,15 +144,30 @@ import com.intellectualcrafters.plot.object.PlotWorld; DBFunc.setFlags(plot.world, plot, plot.settings.flags); return true; } - + + public static boolean addClusterFlag(PlotCluster cluster, final Flag flag) { + //TODO plot cluster flag event + final Flag hasFlag = getSettingFlag(cluster.world, cluster.settings, flag.getKey()); + if (hasFlag != null) { + cluster.settings.flags.remove(hasFlag); + } + cluster.settings.flags.add(flag); + DBFunc.setFlags(cluster, cluster.settings.flags); + return true; + } + /** * * @param plot * @return */ public static Set getPlotFlags(Plot plot) { - Set plotflags = plot.settings.flags; - PlotWorld plotworld = PlotMain.getWorldSettings(plot.world); + return getSettingFlags(plot.world, plot.settings); + } + + public static Set getSettingFlags(String world, PlotSettings settings) { + Set plotflags = settings.flags; + PlotWorld plotworld = PlotMain.getWorldSettings(world); if (plotworld != null && plotworld.DEFAULT_FLAGS != null && plotworld.DEFAULT_FLAGS.length > 0) { plotflags.addAll(Arrays.asList(plotworld.DEFAULT_FLAGS)); } @@ -163,6 +191,20 @@ import com.intellectualcrafters.plot.object.PlotWorld; } return false; } + + public static boolean removeClusterFlag(PlotCluster cluster, String flag) { + final Flag hasFlag = getSettingFlag(cluster.world, cluster.settings, flag); + if (hasFlag != null) { + Flag flagObj = FlagManager.getSettingFlagAbs(cluster.settings, flag); + if (flagObj != null) { + //TODO cluster flag add event + cluster.settings.flags.remove(hasFlag); + DBFunc.setFlags(cluster, cluster.settings.flags); + return true; + } + } + return false; + } public static void setPlotFlags(Plot plot, Set flags) { if (flags == null) { @@ -173,6 +215,16 @@ import com.intellectualcrafters.plot.object.PlotWorld; plot.settings.flags = flags; DBFunc.setFlags(plot.world, plot, plot.settings.flags); } + + public static void setClusterFlags(PlotCluster cluster, Set flags) { + if (flags == null) { + cluster.settings.flags = new HashSet<>(); + DBFunc.setFlags(cluster, cluster.settings.flags); + return; + } + cluster.settings.flags = flags; + DBFunc.setFlags(cluster, cluster.settings.flags); + } public static Flag[] removeFlag(final Flag[] flags, final String r) { final Flag[] f = new Flag[flags.length - 1]; @@ -278,20 +330,4 @@ import com.intellectualcrafters.plot.object.PlotWorld; } return flags; } - - /** - * Get the flags for a plot - * - * @param plot Plot to search in - * - * @return List (AbstractFlag) - */ - public static List getPlotAbstractFlags(final Plot plot) { - final Set plotFlags = getPlotFlags(plot); - final List flags = new ArrayList<>(); - for (final Flag flag : plotFlags) { - flags.add(flag.getAbstractFlag()); - } - return flags; - } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java index 5003be506..6992cb20f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java @@ -13,7 +13,7 @@ import com.intellectualcrafters.plot.object.PlotBlock; import com.intellectualcrafters.plot.object.PlotGenerator; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotManager; -import com.intellectualcrafters.plot.object.PlotRegion; +import com.intellectualcrafters.plot.object.PlotCluster; import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.util.PlotHelper; @@ -22,7 +22,6 @@ public class AugmentedPopulator extends BlockPopulator { public final PlotGenerator generator; public final PlotWorld plotworld; public final PlotManager manager; - public HashSet regions; public BlockWrapper getBlock(int i, int j, short[][] result) { @@ -37,32 +36,17 @@ public class AugmentedPopulator extends BlockPopulator { return new BlockWrapper(x, y, z, id, (byte) 0); } - public AugmentedPopulator(PlotGenerator generator, PlotWorld plotworld, PlotManager manager, HashSet regions) { + public AugmentedPopulator(PlotGenerator generator, PlotWorld plotworld, PlotManager manager) { this.generator = generator; this.plotworld = plotworld; this.manager = manager; - this.regions = regions; - } - - // Check if the augmented populator contains the plot id - public boolean contains(PlotId id) { - // TODO check if any regions contain the id - return false; - } - - /** - * Returns false if the proposed region overlaps with an existing region - */ - public boolean addRegion(PlotRegion region) { - boolean contains = false; //TODO check if any regions contain these plots - if (contains) {// contains - return false; - } - return regions.add(region); } @Override public void populate(World world, Random rand, Chunk chunk) { + + // TODO check if chunk is in any clusters + int X = chunk.getX(); int Z = chunk.getZ(); int x = X << 4; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotCluster.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotCluster.java new file mode 100644 index 000000000..c9c0c858e --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotCluster.java @@ -0,0 +1,36 @@ +package com.intellectualcrafters.plot.object; + +import java.util.UUID; + +public class PlotCluster { + + public PlotSettings settings; + public final String world; + public final PlotId pos1; + public final PlotId pos2; + public final UUID owner; + + public PlotCluster(String world, PlotId pos1, PlotId pos2, UUID owner) { + this.world = world; + this.pos1 = pos1; + this.pos2 = pos2; + this.owner = owner; + } + + @Override + public int hashCode() { + // TODO Auto-generated method stub + return super.hashCode(); + } + + @Override + public boolean equals(Object obj) { + // TODO Auto-generated method stub + return super.equals(obj); + } + + @Override + public String toString() { + return world + ";" + pos1.x + ";" + pos1.y + ";" + pos2.x + ";" + pos2.y; + } +} diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotRegion.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotClusterId.java similarity index 64% rename from PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotRegion.java rename to PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotClusterId.java index d9ae3c4c5..127996f6e 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotRegion.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotClusterId.java @@ -1,11 +1,11 @@ package com.intellectualcrafters.plot.object; -public class PlotRegion { - +public class PlotClusterId { + public final PlotId pos1; public final PlotId pos2; - public PlotRegion(PlotId pos1, PlotId pos2) { + public PlotClusterId(PlotId pos1, PlotId pos2) { this.pos1 = pos1; this.pos2 = pos2; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ClusterManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ClusterManager.java new file mode 100644 index 000000000..1365e6b31 --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ClusterManager.java @@ -0,0 +1,58 @@ +package com.intellectualcrafters.plot.util; + +import java.util.HashMap; +import java.util.HashSet; + +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotCluster; +import com.intellectualcrafters.plot.object.PlotClusterId; +import com.intellectualcrafters.plot.object.PlotId; + +public class ClusterManager { + public static HashMap> clusters; + private static PlotCluster last; + + public static boolean contains(PlotCluster cluster, PlotId id) { + if (cluster.pos1.x <= id.x && cluster.pos1.y <= id.y && cluster.pos2.x >= id.x && cluster.pos2.y >= id.y) { + return true; + } + return false; + } + + public static PlotCluster getCluster(Plot plot) { + if (last != null && last.world.equals(plot.world)) { + if (contains(last, plot.id)) { + return last; + } + } + if (clusters == null) { + return null; + } + HashSet local = clusters.get(plot.world); + if (local == null) { + return null; + } + PlotId id = plot.id; + for (PlotCluster cluster : local) { + if (contains(cluster, id)) { + last = cluster; + return cluster; + } + } + return null; + } + + public static boolean removeCluster(PlotCluster cluster) { + if (clusters != null) { + if (clusters.containsKey(cluster.world)) { + clusters.get(cluster.world).remove(cluster); + return true; + } + } + return false; + } + + public static PlotClusterId getClusterId(PlotCluster cluster) { + return new PlotClusterId(cluster.pos1, cluster.pos2); + } +} \ No newline at end of file