From 45d8556b87fd7f04e7a64323adf0fd6d993895bf Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sun, 2 Nov 2014 14:45:52 +1100 Subject: [PATCH] [UNTESTED] Added plot commenting --- .../java/com/intellectualcrafters/plot/C.java | 3 + .../intellectualcrafters/plot/Metrics.java | 13 +- .../plot/PlotSettings.java | 17 +- .../plot/commands/Comment.java | 4 +- .../plot/commands/Inbox.java | 129 ++++++++++++-- .../plot/database/AbstractDB.java | 7 + .../plot/database/DBFunc.java | 26 +++ .../plot/database/SQLManager.java | 158 ++++++++++-------- .../plot/generator/DefaultPlotManager.java | 8 +- 9 files changed, 264 insertions(+), 101 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/C.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/C.java index cd911d7ee..5109678e2 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/C.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/C.java @@ -31,6 +31,9 @@ public enum C { * Comment */ COMMENT_SYNTAX("&cUse /plots comment "), + INVALID_INBOX("&cThat is not a valid inbox.\n&6Accepted values: %s"), + NO_PERM_INBOX("&cYou do not have permission to read that inbox."), + COMMENT_REMOVED("&aSuccessfully deleted %s."), /* * Console */ diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/Metrics.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/Metrics.java index 5f56dcc9f..f3e2b7d80 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/Metrics.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/Metrics.java @@ -32,11 +32,13 @@ package com.intellectualcrafters.plot; import org.bukkit.Bukkit; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.scheduler.BukkitTask; import java.io.*; +import java.lang.reflect.InvocationTargetException; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; @@ -326,7 +328,16 @@ public class Metrics { // enabled String pluginVersion = description.getVersion(); String serverVersion = Bukkit.getVersion(); - int playersOnline = 1337; /** it's a collection for me o.o */ + int playersOnline = 0; + try { + if (Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).getReturnType() == Collection.class) + playersOnline = ((Collection)Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null, new Object[0])).size(); + else + playersOnline = ((Player[])Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null, new Object[0])).length; + } + catch (NoSuchMethodException ex){} + catch (InvocationTargetException ex){} + catch (IllegalAccessException ex){} // END server software specific section -- all code below does not use // any code outside of this class / Java // Construct the post data diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSettings.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSettings.java index d61617a92..ceeed855a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSettings.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSettings.java @@ -36,7 +36,7 @@ public class PlotSettings { */ private Biome biome; - private ArrayList comments; + private ArrayList comments = null; /** * */ @@ -169,13 +169,20 @@ public class PlotSettings { public String getLeaveMessage() { return ""; } - public ArrayList getComments() { - if (this.comments == null) { - return new ArrayList(); + public ArrayList getComments(int tier) { + ArrayList c = new ArrayList(); + for (PlotComment comment : this.comments) { + if (comment.tier == tier) { + c.add(comment); + } } - return this.comments; + return c; } + public void setComments(ArrayList comments) { + this.comments = comments; + } + public void addComment(PlotComment comment) { if (this.comments == null) { this.comments = new ArrayList(); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Comment.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Comment.java index 9aec01fd7..a0cb39b4c 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Comment.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Comment.java @@ -9,6 +9,8 @@ package com.intellectualcrafters.plot.commands; import com.intellectualcrafters.plot.*; +import com.intellectualcrafters.plot.database.DBFunc; + import org.apache.commons.lang.StringUtils; import org.bukkit.entity.Player; @@ -45,7 +47,7 @@ public class Comment extends SubCommand { PlotComment comment = new PlotComment(text, plr.getName(), recipients.indexOf(args[0].toLowerCase())); plot.settings.addComment(comment); - //DBFunc.addComment(...) // Can you do this? + DBFunc.setComment(plr.getWorld().getName(), plot, comment); return true; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Inbox.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Inbox.java index 95e2c2f5a..dd421a51a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Inbox.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Inbox.java @@ -14,8 +14,10 @@ import java.util.List; import java.util.UUID; import com.intellectualcrafters.plot.*; +import com.intellectualcrafters.plot.database.DBFunc; import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; /** @@ -24,16 +26,16 @@ import org.bukkit.entity.Player; public class Inbox extends SubCommand { public Inbox() { - super(Command.INBOX, "Comment on a plot", "comment", CommandCategory.ACTIONS, true); + super(Command.INBOX, "Review a the comments for a plot", "comment", CommandCategory.ACTIONS, true); } @Override - public boolean execute(Player plr, String... args) { + public boolean execute(final Player plr, final String... args) { if (!PlayerFunctions.isInPlot(plr)) { PlayerFunctions.sendMessage(plr, C.NOT_IN_PLOT); return false; } - Plot plot = PlayerFunctions.getCurrentPlot(plr); + final Plot plot = PlayerFunctions.getCurrentPlot(plr); if (!plot.hasOwner()) { PlayerFunctions.sendMessage(plr, C.NOT_IN_PLOT); return false; @@ -56,23 +58,114 @@ public class Inbox extends SubCommand { else { tier = 4; } - ArrayList comments = plot.settings.getComments(); - List recipients = Arrays.asList(new String[] {"admin", "owner", "helper", "trusted", "everyone" }); - int counter = 0; - StringBuilder message = new StringBuilder(); - String prefix = ""; - for (PlotComment comment : comments) { - if (comment.tier>=tier) { - message.append(prefix + "&6[&c" + recipients.get(tier) + "&6] &7"+comment.senderName+"&f: "+comment.comment); - prefix = "\n"; + if (args.length > 0) { + switch (args[0].toLowerCase()) { + case "admin": + if (tier<=0) { + tier = 0; + } + else { + PlayerFunctions.sendMessage(plr, C.NO_PERM_INBOX); + return false; + } + break; + case "owner": + if (tier<=1) { + tier = 1; + } + else { + PlayerFunctions.sendMessage(plr, C.NO_PERM_INBOX); + return false; + } + break; + case "helper": + if (tier<=2) { + tier = 2; + } + else { + PlayerFunctions.sendMessage(plr, C.NO_PERM_INBOX); + return false; + } + break; + case "trusted": + if (tier<=3) { + tier = 3; + } + else { + PlayerFunctions.sendMessage(plr, C.NO_PERM_INBOX); + return false; + } + break; + case "everyone": + if (tier<=4) { + tier = 4; + } + else { + PlayerFunctions.sendMessage(plr, C.NO_PERM_INBOX); + return false; + } + break; + case "default": + PlayerFunctions.sendMessage(plr, C.INVALID_INBOX, Arrays.copyOfRange(new String[] {"admin", "owner", "helper", "trusted", "everyone" }, tier, 4)); + return false; } } - if (counter==0) { - PlayerFunctions.sendMessage(plr, "&cNo messages."); - return false; - } - PlayerFunctions.sendMessage(plr, message.toString()); - return true; + + final String world = plr.getWorld().getName(); + final int tier2 = tier; + + Bukkit.getScheduler().runTaskAsynchronously(PlotMain.getMain(), new Runnable() { + @Override + public void run() { + ArrayList comments = plot.settings.getComments(tier2); + if (comments == null) { + comments = DBFunc.getCommenst(world, plot, tier2); + plot.settings.setComments(comments); + } + + if (args.length == 2) { + String[] split = args[1].toLowerCase().split(":"); + if (!split[0].equals("clear")) { + PlayerFunctions.sendMessage(plr, "&c/plot inbox [tier] [clear][:#]"); + return; + } + if (split.length > 1) { + try { + int index = Integer.parseInt(split[1]); + PlotComment comment = comments.get(index-1); + DBFunc.removeComment(world, plot, comment); + PlayerFunctions.sendMessage(plr, C.COMMENT_REMOVED, "1 comment"); + return; + } + catch (Exception e) { + PlayerFunctions.sendMessage(plr, "&cInvalid index:\n/plot inbox [tier] [clear][:#]"); + return; + } + } + for (PlotComment comment : comments) { + DBFunc.removeComment(world, plot, comment); + } + PlayerFunctions.sendMessage(plr, C.COMMENT_REMOVED, "all comments in that category"); + return; + } + else { + final List recipients = Arrays.asList(new String[] {"A", "O", "H", "T", "E" }); + int count = 1; + StringBuilder message = new StringBuilder(); + String prefix = ""; + for (PlotComment comment : comments) { + message.append(prefix + "["+count+"]&6[&c" + recipients.get(tier2) + "&6] &7"+comment.senderName+"&f: "+comment.comment); + prefix = "\n"; + count++; + } + if (comments.size()==0) { + message.append("&cNo messages."); + } + PlayerFunctions.sendMessage(plr, message.toString()); + } + } + }); + return true; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java index 7df99b374..8967eb188 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java @@ -17,6 +17,7 @@ import org.bukkit.OfflinePlayer; import com.intellectualcrafters.plot.Flag; import com.intellectualcrafters.plot.Plot; +import com.intellectualcrafters.plot.PlotComment; import com.intellectualcrafters.plot.PlotId; /** @@ -149,4 +150,10 @@ public abstract class AbstractDB { public abstract void setDenied(final String world, final Plot plot, final OfflinePlayer player); public abstract double getRatings(final Plot plot); + + public abstract void removeComment(String world, Plot plot, PlotComment comment); + + public abstract void setComment(String world, Plot plot, PlotComment comment); + + public abstract ArrayList getComments(String world, Plot plot, int tier); } 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 9e06bd96e..7fc7cf900 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java @@ -13,8 +13,10 @@ 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.PlotComment; import com.intellectualcrafters.plot.PlotId; /** @@ -159,6 +161,30 @@ public class DBFunc { */ public static UUID everyone = UUID.fromString("1-1-3-3-7"); + /** + * @param plot + * @param comment + */ + public static void removeComment(final String world, final Plot plot, final PlotComment comment) { + dbManager.removeComment(world, plot, comment); + } + + /** + * @param plot + * @param comment + */ + public static void setComment(final String world, final Plot plot, final PlotComment comment) { + dbManager.setComment(world, plot, comment); + } + + /** + * @param plot + * @param comment + */ + public static ArrayList getCommenst(final String world, final Plot plot, final int tier) { + return dbManager.getComments(world, plot, tier); + } + /** * @param plot * @param player 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 4625c6e9b..5cdac3e54 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -10,6 +10,7 @@ package com.intellectualcrafters.plot.database; import com.intellectualcrafters.plot.*; import com.intellectualcrafters.plot.Logger.LogLevel; + import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @@ -64,7 +65,9 @@ public class SQLManager extends AbstractDB { @Override public void createAllSettingsAndHelpers(ArrayList plots) { + // TODO SEVERE [ More than 5000 plots will fail in a single SQLite query. + HashMap> stored = new HashMap>(); HashMap> helpers = new HashMap>(); try { @@ -235,7 +238,11 @@ public class SQLManager extends AbstractDB { stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + PREFIX + "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 `" + PREFIX + "plot_helpers` (" + "`plot_plot_id` INT(11) NOT NULL," - + "`user_uuid` VARCHAR(40) NOT NULL" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); + + "`user_uuid` VARCHAR(40) NOT NULL" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + 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" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + PREFIX + "plot_trusted` (" + "`plot_plot_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + PREFIX + "plot_settings` (" + " `plot_plot_id` INT(11) NOT NULL," @@ -262,6 +269,10 @@ public class SQLManager extends AbstractDB { + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + PREFIX + "plot_trusted` (" + "`plot_plot_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ")"); + stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + 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 `" + 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'," @@ -515,8 +526,6 @@ public class SQLManager extends AbstractDB { stmt = connection.createStatement(); r = stmt.executeQuery("SELECT * FROM `" + PREFIX + "plot_settings`"); - String var; - Object val; while (r.next()) { id = r.getInt("plot_plot_id"); Plot plot = plots.get(id); @@ -542,7 +551,6 @@ public class SQLManager extends AbstractDB { plot.settings.setAlias(alias); } - PlotHomePosition position = null; String pos = r.getString("position"); if (pos!=null) { for (PlotHomePosition plotHomePosition : PlotHomePosition.values()) { @@ -950,79 +958,80 @@ public class SQLManager extends AbstractDB { } 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 `" + PREFIX + "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 `" + PREFIX + "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; - } + @Override + public void removeComment(final String world, final Plot plot, final PlotComment comment) { + runTask(new Runnable() { + @Override + public void run() { + try { + PreparedStatement statement = + connection.prepareStatement("DELETE FROM `"+PREFIX+"plot_comments` WHERE `plot_plot_id` = ? AND `comment` = ? AND `tier` = ? AND `sender` = ?"); + statement.setInt(1, getId(world, plot.id)); + statement.setString(2, comment.comment); + statement.setInt(3, comment.tier); + statement.setString(4, comment.senderName); + statement.executeUpdate(); + statement.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.WARNING, "Failed to remove helper for plot " + plot.id); + } + } + }); + } - /** - * @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 `" + PREFIX + "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; - } + @Override + public ArrayList getComments(String world, Plot plot, int tier) { + ArrayList comments = new ArrayList(); + try { + PreparedStatement statement = + connection.prepareStatement("SELECT `*` FROM `" + PREFIX + "plot_comments` WHERE `plot_plot_id` = ? AND `tier` = ?"); + statement.setInt(1, getId(plot.getWorld().getName(), plot.id)); + statement.setInt(2, tier); + ResultSet set = statement.executeQuery(); + PlotComment comment; + while (set.next()) { + String sender = set.getString("sender"); + String msg = set.getString("comment"); + comment = new PlotComment(msg, sender, tier); + comments.add(comment); + } + statement.close(); + } + catch (SQLException e) { + Logger.add(LogLevel.WARNING, "Failed to fetch rating for plot " + plot.getId().toString()); + e.printStackTrace(); + } + return comments; + } + + @Override + public void setComment(final String world, final Plot plot, final PlotComment comment) { + runTask(new Runnable() { + @Override + public void run() { + try { + PreparedStatement statement = + connection.prepareStatement("INSERT INTO `" + PREFIX + "plot_comments` (`plot_plot_id`, `comment`, `tier`, `sender`) VALUES(?,?,?,?)"); + statement.setInt(1, getId(world, plot.id)); + statement.setString(2, comment.comment); + statement.setInt(3, comment.tier); + statement.setString(4, comment.senderName); + 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 @@ -1193,4 +1202,5 @@ public class SQLManager extends AbstractDB { } return 0.0d; } + } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/DefaultPlotManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/DefaultPlotManager.java index cbec390c4..1ceacd67f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/DefaultPlotManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/DefaultPlotManager.java @@ -384,7 +384,9 @@ public class DefaultPlotManager extends PlotManager { @Override public boolean setWallFilling(World w, PlotWorld plotworld, PlotId plotid, PlotBlock plotblock) { DefaultPlotWorld dpw = (DefaultPlotWorld) plotworld; - + if (dpw.ROAD_WIDTH==0) { + return false; + } Location bottom = PlotHelper.getPlotBottomLoc(w, plotid); Location top = PlotHelper.getPlotTopLoc(w, plotid); @@ -428,7 +430,9 @@ public class DefaultPlotManager extends PlotManager { @Override public boolean setWall(World w, PlotWorld plotworld, PlotId plotid, PlotBlock plotblock) { DefaultPlotWorld dpw = (DefaultPlotWorld) plotworld; - + if (dpw.ROAD_WIDTH==0) { + return false; + } Location bottom = PlotHelper.getPlotBottomLoc(w, plotid); Location top = PlotHelper.getPlotTopLoc(w, plotid);