From 9101cb92184f545963a058884f2d6d67ecb20735 Mon Sep 17 00:00:00 2001 From: matt <4009945+MattBDev@users.noreply.github.com> Date: Wed, 6 Feb 2019 12:33:14 -0500 Subject: [PATCH] PlotComment API tweaks. Signed-off-by: matt <4009945+MattBDev@users.noreply.github.com> --- .../plotsquared/plot/commands/Inbox.java | 17 +++---- .../plotsquared/plot/object/Plot.java | 21 ++++++++ .../plotsquared/plot/object/PlotSettings.java | 40 +++++++--------- .../plot/object/comment/InboxOwner.java | 13 +++-- .../plot/object/comment/InboxPublic.java | 12 ++--- .../plotsquared/plot/util/CommentManager.java | 48 +++++++++---------- 6 files changed, 77 insertions(+), 74 deletions(-) diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Inbox.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Inbox.java index 5c7cfc0ac..5e8a5550c 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Inbox.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Inbox.java @@ -11,14 +11,10 @@ import com.github.intellectualsites.plotsquared.plot.util.CommentManager; import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import com.github.intellectualsites.plotsquared.plot.util.StringMan; -import java.util.ArrayList; import java.util.List; -import java.util.Optional; -@CommandDeclaration(command = "inbox", description = "Review the comments for a plot", - usage = "/plot inbox [inbox] [delete |clear|page]", permission = "plots.inbox", - category = CommandCategory.CHAT, requiredType = RequiredType.NONE) public class Inbox - extends SubCommand { +@CommandDeclaration(command = "inbox", description = "Review the comments for a plot", usage = "/plot inbox [inbox] [delete |clear|page]", permission = "plots.inbox", category = CommandCategory.CHAT, requiredType = RequiredType.NONE) +public class Inbox extends SubCommand { public void displayComments(PlotPlayer player, List oldComments, int page) { if (oldComments == null || oldComments.isEmpty()) { @@ -150,7 +146,7 @@ import java.util.Optional; } PlotComment comment = value.get(index - 1); inbox.removeComment(plot, comment); - plot.getSettings().removeComment(comment); + plot.removeComment(comment); MainUtil.sendMessage(player, C.COMMENT_REMOVED, comment.comment); } })) { @@ -163,10 +159,9 @@ import java.util.Optional; sendMessage(player, C.NO_PERM_INBOX_MODIFY); } inbox.clearInbox(plot); - Optional> comments = - plot.getSettings().getComments(inbox.toString()); - if (comments.isPresent()) { - plot.getSettings().removeComments(comments.get()); + List comments = plot.getComments(inbox.toString()); + if (!comments.isEmpty()) { + plot.removeComments(comments); } MainUtil.sendMessage(player, C.COMMENT_REMOVED, "*"); return true; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index 44b731ec5..fd1e11a90 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -10,6 +10,7 @@ import com.github.intellectualsites.plotsquared.plot.flag.FlagManager; import com.github.intellectualsites.plotsquared.plot.flag.Flags; import com.github.intellectualsites.plotsquared.plot.generator.SquarePlotWorld; import com.github.intellectualsites.plotsquared.plot.listener.PlotListener; +import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment; import com.github.intellectualsites.plotsquared.plot.object.schematic.Schematic; import com.github.intellectualsites.plotsquared.plot.util.*; import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue; @@ -2991,4 +2992,24 @@ public class Plot { public boolean hasFlag(Flag flag) { return getFlags().containsKey(flag); } + + @SuppressWarnings("deprecation") public boolean removeComment(PlotComment comment) { + return getSettings().removeComment(comment); + } + + @SuppressWarnings("deprecation") public void removeComments(List comments) { + getSettings().removeComments(comments); + } + + @SuppressWarnings("deprecation") public List getComments(String inbox) { + return getSettings().getComments(inbox); + } + + @SuppressWarnings("deprecation") public void addComment(PlotComment comment) { + getSettings().addComment(comment); + } + + @SuppressWarnings("deprecation") public void setComments(List list) { + getSettings().setComments(list); + } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotSettings.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotSettings.java index 4f3701f56..f6c479471 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotSettings.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotSettings.java @@ -3,6 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.object; import com.github.intellectualsites.plotsquared.plot.flag.Flag; import com.github.intellectualsites.plotsquared.plot.flag.Flags; import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment; +import com.google.common.collect.ImmutableList; import java.util.*; @@ -25,12 +26,8 @@ public class PlotSettings { * @deprecated Raw access */ @Deprecated public String alias = ""; - /** - * Comments. - * - * @deprecated Raw access - */ - @Deprecated public List comments = null; + + private List comments = null; /** * The ratings for a plot. @@ -142,36 +139,31 @@ public class PlotSettings { } } - public Optional> getComments(String inbox) { - ArrayList c = new ArrayList<>(); + @SuppressWarnings({"UnstableApiUsage"}) public List getComments(String inbox) { if (this.comments == null) { - return Optional.empty(); + return Collections.emptyList(); } - for (PlotComment comment : this.comments) { - if (comment.inbox.equals(inbox)) { - c.add(comment); - } - } - return Optional.of(c); + + return this.comments.stream().filter(comment -> comment.inbox.equals(inbox)) + .collect(ImmutableList.toImmutableList()); } - public void setComments(List comments) { + void setComments(List comments) { this.comments = comments; } - public void removeComment(PlotComment comment) { - if (this.comments.contains(comment)) { - this.comments.remove(comment); + boolean removeComment(PlotComment comment) { + if (this.comments == null) { + return false; } + return this.comments.remove(comment); } - public void removeComments(List comments) { - for (PlotComment comment : comments) { - removeComment(comment); - } + void removeComments(List comments) { + comments.forEach(this::removeComment); } - public void addComment(PlotComment comment) { + void addComment(PlotComment comment) { if (this.comments == null) { this.comments = new ArrayList<>(); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/comment/InboxOwner.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/comment/InboxOwner.java index d3a80013e..426a5ab49 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/comment/InboxOwner.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/comment/InboxOwner.java @@ -7,15 +7,14 @@ import com.github.intellectualsites.plotsquared.plot.util.TaskManager; import java.util.ArrayList; import java.util.List; -import java.util.Optional; public class InboxOwner extends CommentInbox { @Override public boolean getComments(final Plot plot, final RunnableVal> whenDone) { - Optional> comments = plot.getSettings().getComments(toString()); - if (comments.isPresent()) { - whenDone.value = comments.get(); + List comments = plot.getComments(toString()); + if (!comments.isEmpty()) { + whenDone.value = comments; TaskManager.runTask(whenDone); return true; } @@ -24,10 +23,10 @@ public class InboxOwner extends CommentInbox { whenDone.value = value; if (value != null) { for (PlotComment comment : value) { - plot.getSettings().addComment(comment); + plot.addComment(comment); } } else { - plot.getSettings().setComments(new ArrayList()); + plot.setComments(new ArrayList<>()); } TaskManager.runTask(whenDone); } @@ -39,7 +38,7 @@ public class InboxOwner extends CommentInbox { if (plot.owner == null) { return false; } - plot.getSettings().addComment(comment); + plot.addComment(comment); DBFunc.setComment(plot, comment); return true; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/comment/InboxPublic.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/comment/InboxPublic.java index a14954e28..598904b0f 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/comment/InboxPublic.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/comment/InboxPublic.java @@ -5,17 +5,15 @@ import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; import com.github.intellectualsites.plotsquared.plot.util.TaskManager; -import java.util.ArrayList; import java.util.List; -import java.util.Optional; public class InboxPublic extends CommentInbox { @Override public boolean getComments(final Plot plot, final RunnableVal> whenDone) { - Optional> comments = plot.getSettings().getComments(toString()); - if (comments.isPresent()) { - whenDone.value = comments.get(); + List comments = plot.getComments(toString()); + if (!comments.isEmpty()) { + whenDone.value = comments; TaskManager.runTask(whenDone); return true; } @@ -24,7 +22,7 @@ public class InboxPublic extends CommentInbox { whenDone.value = value; if (value != null) { for (PlotComment comment : value) { - plot.getSettings().addComment(comment); + plot.addComment(comment); } } TaskManager.runTask(whenDone); @@ -34,7 +32,7 @@ public class InboxPublic extends CommentInbox { } @Override public boolean addComment(Plot plot, PlotComment comment) { - plot.getSettings().addComment(comment); + plot.addComment(comment); DBFunc.setComment(plot, comment); return true; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/CommentManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/CommentManager.java index 3dc43fe06..cc5021c57 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/CommentManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/CommentManager.java @@ -20,34 +20,32 @@ public class CommentManager { if (!Settings.Enabled_Components.COMMENT_NOTIFIER || !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(List value) { - int total; - if (value != null) { - int num = 0; - for (PlotComment comment : value) { - if (comment.timestamp > getTimestamp(player, - inbox.toString())) { - num++; - } + TaskManager.runTaskLaterAsync(() -> { + 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(List value) { + int total; + if (value != null) { + int num = 0; + for (PlotComment comment : value) { + if (comment.timestamp > getTimestamp(player, + inbox.toString())) { + num++; } - total = count.addAndGet(num); - } else { - total = count.get(); - } - if ((size.decrementAndGet() == 0) && (total > 0)) { - AbstractTitle.sendTitle(player, "", - C.INBOX_NOTIFICATION.s().replaceAll("%s", "" + total)); } + total = count.addAndGet(num); + } else { + total = count.get(); } - }); - } + if ((size.decrementAndGet() == 0) && (total > 0)) { + AbstractTitle.sendTitle(player, "", + C.INBOX_NOTIFICATION.s().replaceAll("%s", "" + total)); + } + } + }); } }, 20); }