From 9cb06d625d8d0fd307700566a164e88c8b151dfd Mon Sep 17 00:00:00 2001 From: boy0001 Date: Thu, 14 May 2015 20:34:08 +1000 Subject: [PATCH] Comment notifications --- .../plot/PlotSquared.java | 8 +- .../plot/commands/Inbox.java | 3 +- .../intellectualcrafters/plot/config/C.java | 1 + .../plot/config/Settings.java | 2 +- .../plot/listeners/PlotListener.java | 5 +- .../plot/object/comment/CommentManager.java | 75 ++++++++++++++----- .../plot/titles/AbstractTitle.java | 4 +- .../plot/titles/DefaultTitle.java | 7 +- .../plot/titles/DefaultTitle_183.java | 7 +- .../plot/titles/HackTitle.java | 6 +- 10 files changed, 82 insertions(+), 36 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java index d9ff3a956..6f69eee40 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java @@ -583,10 +583,6 @@ public class PlotSquared { ExpireManager.runTask(); } - if (Settings.COMMENT_NOTIFICATION_INTERVAL > 0) { - CommentManager.runTask(); - } - // Copy files copyFile("town.template", "templates"); copyFile("skyblock.template", "templates"); @@ -878,7 +874,7 @@ public class PlotSquared { options.put("chunk-processor.max-entities", Settings.CHUNK_PROCESSOR_MAX_ENTITIES); // Comments - options.put("comments.notifications.interval", Settings.COMMENT_NOTIFICATION_INTERVAL); + options.put("comments.notifications.enabled", Settings.COMMENT_NOTIFICATIONS); // Plot limits options.put("global_limit", Settings.GLOBAL_LIMIT); @@ -954,7 +950,7 @@ public class PlotSquared { Settings.CHUNK_PROCESSOR_MAX_ENTITIES= config.getInt("chunk-processor.max-entities"); // Comments - Settings.COMMENT_NOTIFICATION_INTERVAL = config.getInt("comments.notifications.interval"); + Settings.COMMENT_NOTIFICATIONS = config.getBoolean("comments.notifications.enabled"); // Plot limits Settings.MAX_AUTO_SIZE = config.getInt("claim.max-auto-area"); 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 35808f640..f40b31c6b 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Inbox.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Inbox.java @@ -92,7 +92,7 @@ public class Inbox extends SubCommand { int unread = 0; for (PlotComment comment : (ArrayList) value) { total++; - if (comment.timestamp > player.getPreviousLogin()) { + if (comment.timestamp > CommentManager.getTimestamp(player, inbox.toString())) { unread++; } } @@ -122,6 +122,7 @@ public class Inbox extends SubCommand { sendMessage(player, C.INVALID_INBOX, StringUtils.join(CommentManager.inboxes.keySet(),", ")); return false; } + player.setMeta("inbox:" + inbox.toString(), System.currentTimeMillis()); final int page; if (args.length > 1) { switch (args[1].toLowerCase()) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java index f33e10a96..1286018a1 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -109,6 +109,7 @@ public enum C { /* * Comment */ + INBOX_NOTIFICATION("%s unread messages. Use /plot inbox", "Comment"), NOT_VALID_INBOX_INDEX("$2No comment at index %s", "Comment"), INBOX_ITEM("$2 - $4%s", "Comment"), COMMENT_SYNTAX("$2Use /plots comment [X;Z] <%s> ", "Comment"), 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 ca5ac5ec6..80dc5b21c 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java @@ -46,7 +46,7 @@ public class Settings { /** * Comment system */ - public static int COMMENT_NOTIFICATION_INTERVAL = -1; + public static boolean COMMENT_NOTIFICATIONS = false; /** * Chunk processor */ diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java index e21e230b2..a03b6fdca 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java @@ -39,7 +39,9 @@ import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.object.BukkitPlayer; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.object.comment.CommentManager; import com.intellectualcrafters.plot.titles.AbstractTitle; +import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; /** @@ -122,13 +124,14 @@ public class PlotListener extends APlotListener { final String sTitleMain = C.TITLE_ENTERED_PLOT.s().replaceAll("%x%", plot.id.x + "").replaceAll("%z%", plot.id.y + "").replaceAll("%world%", plot.world + "").replaceAll("%greeting%", greeting); final String sTitleSub = C.TITLE_ENTERED_PLOT_SUB.s().replaceFirst("%s", getName(plot.owner)).replaceAll("%greeting%", greeting); if (AbstractTitle.TITLE_CLASS != null) { - AbstractTitle.TITLE_CLASS.sendTitle(player, sTitleMain, sTitleSub, ChatColor.valueOf(C.TITLE_ENTERED_PLOT_COLOR.s()), ChatColor.valueOf(C.TITLE_ENTERED_PLOT_SUB_COLOR.s())); + AbstractTitle.TITLE_CLASS.sendTitle(pp, sTitleMain, sTitleSub, ChatColor.valueOf(C.TITLE_ENTERED_PLOT_COLOR.s()), ChatColor.valueOf(C.TITLE_ENTERED_PLOT_SUB_COLOR.s())); } } { final PlayerEnterPlotEvent callEvent = new PlayerEnterPlotEvent(player, plot); Bukkit.getPluginManager().callEvent(callEvent); } + CommentManager.sendTitle(pp, plot); } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/comment/CommentManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/comment/CommentManager.java index 91dc35edb..47e735af5 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/comment/CommentManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/comment/CommentManager.java @@ -1,36 +1,71 @@ package com.intellectualcrafters.plot.object.comment; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.commons.lang.mutable.MutableInt; +import org.bukkit.ChatColor; + +import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.config.Settings; +import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.object.RunnableVal; +import com.intellectualcrafters.plot.titles.AbstractTitle; +import com.intellectualcrafters.plot.util.TaskManager; public class CommentManager { public static HashMap inboxes = new HashMap<>(); - private static HashMap timestamps = new HashMap<>(); - - public static void runTask() { -// TaskManager.runTaskRepeat(new Runnable() { -// -// @Override -// public void run() { -// -// } -// }, Settings.COMMENT_NOTIFICATION_INTERVAL * 1200); - } - - public static long getTimestamp(PlotPlayer player) { - Long time = timestamps.get(player.getName()); - if (time == null) { - time = player.getPreviousLogin(); - timestamps.put(player.getName(), time); + public static void sendTitle(final PlotPlayer player, final Plot plot) { + if (!Settings.COMMENT_NOTIFICATIONS) { + return; } - return time; + if (!plot.isOwner(player.getUUID())) { + return; + } + TaskManager.runTaskLaterAsync(new Runnable() { + @Override + public void run() { + Collection boxes = CommentManager.inboxes.values(); + final AtomicInteger count = new AtomicInteger(0); + final AtomicInteger size = new AtomicInteger(boxes.size()); + for (final CommentInbox inbox : inboxes.values()) { + inbox.getComments(plot, new RunnableVal() { + @Override + public void run() { + int total; + if (value != null) { + int num = 0; + for (PlotComment comment : (ArrayList) value) { + if (comment.timestamp > getTimestamp(player, inbox.toString())) { + num++; + } + } + total = count.addAndGet(num); + } + else { + total = count.get(); + } + if (size.decrementAndGet() == 0 && total > 0) { + AbstractTitle.TITLE_CLASS.sendTitle(player, "", C.INBOX_NOTIFICATION.s().replaceAll("%s", "" + total), ChatColor.GOLD, ChatColor.valueOf(C.TITLE_ENTERED_PLOT_SUB_COLOR.s())); + } + } + }); + } + } + }, 20); } - public static void setTime(PlotPlayer player) { - timestamps.put(player.getName(), System.currentTimeMillis()); + public static long getTimestamp(PlotPlayer player, String inbox) { + Object meta = player.getMeta("inbox:"+inbox); + if (meta == null) { + return player.getPreviousLogin(); + } + return (Long) meta; } public static void addInbox(CommentInbox inbox) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/AbstractTitle.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/AbstractTitle.java index 3b1ad443b..420e91a12 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/AbstractTitle.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/AbstractTitle.java @@ -3,8 +3,10 @@ package com.intellectualcrafters.plot.titles; import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import com.intellectualcrafters.plot.object.PlotPlayer; + public abstract class AbstractTitle { public static AbstractTitle TITLE_CLASS; - public abstract void sendTitle(Player player, String head, String sub, ChatColor head_color, ChatColor sub_color); + public abstract void sendTitle(PlotPlayer player, String head, String sub, ChatColor head_color, ChatColor sub_color); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/DefaultTitle.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/DefaultTitle.java index a4238665c..dff6e605b 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/DefaultTitle.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/DefaultTitle.java @@ -3,14 +3,17 @@ package com.intellectualcrafters.plot.titles; import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import com.intellectualcrafters.plot.object.BukkitPlayer; +import com.intellectualcrafters.plot.object.PlotPlayer; + public class DefaultTitle extends AbstractTitle { @Override - public void sendTitle(final Player player, final String head, final String sub, final ChatColor head_color, final ChatColor sub_color) { + public void sendTitle(final PlotPlayer player, final String head, final String sub, final ChatColor head_color, final ChatColor sub_color) { try { final DefaultTitleManager title = new DefaultTitleManager(head, sub, 1, 2, 1); title.setTitleColor(head_color); title.setSubtitleColor(sub_color); - title.send(player); + title.send(((BukkitPlayer) player).player); } catch (final Throwable e) { AbstractTitle.TITLE_CLASS = new DefaultTitle_183(); AbstractTitle.TITLE_CLASS.sendTitle(player, head, sub, head_color, sub_color); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/DefaultTitle_183.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/DefaultTitle_183.java index 3d0fee636..88664dc8f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/DefaultTitle_183.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/DefaultTitle_183.java @@ -3,14 +3,17 @@ package com.intellectualcrafters.plot.titles; import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import com.intellectualcrafters.plot.object.BukkitPlayer; +import com.intellectualcrafters.plot.object.PlotPlayer; + public class DefaultTitle_183 extends AbstractTitle { @Override - public void sendTitle(final Player player, final String head, final String sub, final ChatColor head_color, final ChatColor sub_color) { + public void sendTitle(final PlotPlayer player, final String head, final String sub, final ChatColor head_color, final ChatColor sub_color) { try { final DefaultTitleManager_183 title = new DefaultTitleManager_183(head, sub, 1, 2, 1); title.setTitleColor(head_color); title.setSubtitleColor(sub_color); - title.send(player); + title.send(((BukkitPlayer) player).player); } catch (final Throwable e) { AbstractTitle.TITLE_CLASS = new HackTitle(); AbstractTitle.TITLE_CLASS.sendTitle(player, head, sub, head_color, sub_color); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/HackTitle.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/HackTitle.java index 9534e4444..4cda31f97 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/HackTitle.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/titles/HackTitle.java @@ -5,15 +5,17 @@ import org.bukkit.entity.Player; import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.config.Settings; +import com.intellectualcrafters.plot.object.BukkitPlayer; +import com.intellectualcrafters.plot.object.PlotPlayer; public class HackTitle extends AbstractTitle { @Override - public void sendTitle(final Player player, final String head, final String sub, final ChatColor head_color, final ChatColor sub_color) { + public void sendTitle(final PlotPlayer player, final String head, final String sub, final ChatColor head_color, final ChatColor sub_color) { try { final HackTitleManager title = new HackTitleManager(head, sub, 1, 2, 1); title.setTitleColor(head_color); title.setSubtitleColor(sub_color); - title.send(player); + title.send(((BukkitPlayer) player).player); } catch (final Throwable e) { PlotSquared.log("&cYour server version does not support titles!"); Settings.TITLES = false;