Comment notifications

This commit is contained in:
boy0001 2015-05-14 20:34:08 +10:00
parent f3777bd8c5
commit 9cb06d625d
10 changed files with 82 additions and 36 deletions

View File

@ -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");

View File

@ -92,7 +92,7 @@ public class Inbox extends SubCommand {
int unread = 0;
for (PlotComment comment : (ArrayList<PlotComment>) 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()) {

View File

@ -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>", "Comment"),

View File

@ -46,7 +46,7 @@ public class Settings {
/**
* Comment system
*/
public static int COMMENT_NOTIFICATION_INTERVAL = -1;
public static boolean COMMENT_NOTIFICATIONS = false;
/**
* Chunk processor
*/

View File

@ -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);
}
}

View File

@ -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<String, CommentInbox> inboxes = new HashMap<>();
private static HashMap<String, Long> timestamps = new HashMap<>();
public static void runTask() {
// TaskManager.runTaskRepeat(new Runnable() {
//
// @Override
// public void run() {
//
// }
// }, Settings.COMMENT_NOTIFICATION_INTERVAL * 1200);
public static void sendTitle(final PlotPlayer player, final Plot plot) {
if (!Settings.COMMENT_NOTIFICATIONS) {
return;
}
if (!plot.isOwner(player.getUUID())) {
return;
}
TaskManager.runTaskLaterAsync(new Runnable() {
@Override
public void run() {
Collection<CommentInbox> 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<PlotComment>) 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 long getTimestamp(PlotPlayer player) {
Long time = timestamps.get(player.getName());
if (time == null) {
time = player.getPreviousLogin();
timestamps.put(player.getName(), time);
public static long getTimestamp(PlotPlayer player, String inbox) {
Object meta = player.getMeta("inbox:"+inbox);
if (meta == null) {
return player.getPreviousLogin();
}
return time;
}
public static void setTime(PlotPlayer player) {
timestamps.put(player.getName(), System.currentTimeMillis());
return (Long) meta;
}
public static void addInbox(CommentInbox inbox) {

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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;