PlotComment API tweaks.

Signed-off-by: matt <4009945+MattBDev@users.noreply.github.com>
This commit is contained in:
matt 2019-02-06 12:33:14 -05:00
parent 6ec6c26a10
commit 9101cb9218
6 changed files with 77 additions and 74 deletions

View File

@ -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.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.StringMan; import com.github.intellectualsites.plotsquared.plot.util.StringMan;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
@CommandDeclaration(command = "inbox", description = "Review the comments for a plot", @CommandDeclaration(command = "inbox", description = "Review the comments for a plot", usage = "/plot inbox [inbox] [delete <index>|clear|page]", permission = "plots.inbox", category = CommandCategory.CHAT, requiredType = RequiredType.NONE)
usage = "/plot inbox [inbox] [delete <index>|clear|page]", permission = "plots.inbox", public class Inbox extends SubCommand {
category = CommandCategory.CHAT, requiredType = RequiredType.NONE) public class Inbox
extends SubCommand {
public void displayComments(PlotPlayer player, List<PlotComment> oldComments, int page) { public void displayComments(PlotPlayer player, List<PlotComment> oldComments, int page) {
if (oldComments == null || oldComments.isEmpty()) { if (oldComments == null || oldComments.isEmpty()) {
@ -150,7 +146,7 @@ import java.util.Optional;
} }
PlotComment comment = value.get(index - 1); PlotComment comment = value.get(index - 1);
inbox.removeComment(plot, comment); inbox.removeComment(plot, comment);
plot.getSettings().removeComment(comment); plot.removeComment(comment);
MainUtil.sendMessage(player, C.COMMENT_REMOVED, comment.comment); MainUtil.sendMessage(player, C.COMMENT_REMOVED, comment.comment);
} }
})) { })) {
@ -163,10 +159,9 @@ import java.util.Optional;
sendMessage(player, C.NO_PERM_INBOX_MODIFY); sendMessage(player, C.NO_PERM_INBOX_MODIFY);
} }
inbox.clearInbox(plot); inbox.clearInbox(plot);
Optional<ArrayList<PlotComment>> comments = List<PlotComment> comments = plot.getComments(inbox.toString());
plot.getSettings().getComments(inbox.toString()); if (!comments.isEmpty()) {
if (comments.isPresent()) { plot.removeComments(comments);
plot.getSettings().removeComments(comments.get());
} }
MainUtil.sendMessage(player, C.COMMENT_REMOVED, "*"); MainUtil.sendMessage(player, C.COMMENT_REMOVED, "*");
return true; return true;

View File

@ -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.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.generator.SquarePlotWorld; import com.github.intellectualsites.plotsquared.plot.generator.SquarePlotWorld;
import com.github.intellectualsites.plotsquared.plot.listener.PlotListener; 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.object.schematic.Schematic;
import com.github.intellectualsites.plotsquared.plot.util.*; import com.github.intellectualsites.plotsquared.plot.util.*;
import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue; import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue;
@ -2991,4 +2992,24 @@ public class Plot {
public boolean hasFlag(Flag<?> flag) { public boolean hasFlag(Flag<?> flag) {
return getFlags().containsKey(flag); return getFlags().containsKey(flag);
} }
@SuppressWarnings("deprecation") public boolean removeComment(PlotComment comment) {
return getSettings().removeComment(comment);
}
@SuppressWarnings("deprecation") public void removeComments(List<PlotComment> comments) {
getSettings().removeComments(comments);
}
@SuppressWarnings("deprecation") public List<PlotComment> getComments(String inbox) {
return getSettings().getComments(inbox);
}
@SuppressWarnings("deprecation") public void addComment(PlotComment comment) {
getSettings().addComment(comment);
}
@SuppressWarnings("deprecation") public void setComments(List<PlotComment> list) {
getSettings().setComments(list);
}
} }

View File

@ -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.Flag;
import com.github.intellectualsites.plotsquared.plot.flag.Flags; import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment; import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment;
import com.google.common.collect.ImmutableList;
import java.util.*; import java.util.*;
@ -25,12 +26,8 @@ public class PlotSettings {
* @deprecated Raw access * @deprecated Raw access
*/ */
@Deprecated public String alias = ""; @Deprecated public String alias = "";
/**
* Comments. private List<PlotComment> comments = null;
*
* @deprecated Raw access
*/
@Deprecated public List<PlotComment> comments = null;
/** /**
* The ratings for a plot. * The ratings for a plot.
@ -142,36 +139,31 @@ public class PlotSettings {
} }
} }
public Optional<ArrayList<PlotComment>> getComments(String inbox) { @SuppressWarnings({"UnstableApiUsage"}) public List<PlotComment> getComments(String inbox) {
ArrayList<PlotComment> c = new ArrayList<>();
if (this.comments == null) { if (this.comments == null) {
return Optional.empty(); return Collections.emptyList();
} }
for (PlotComment comment : this.comments) {
if (comment.inbox.equals(inbox)) { return this.comments.stream().filter(comment -> comment.inbox.equals(inbox))
c.add(comment); .collect(ImmutableList.toImmutableList());
}
}
return Optional.of(c);
} }
public void setComments(List<PlotComment> comments) { void setComments(List<PlotComment> comments) {
this.comments = comments; this.comments = comments;
} }
public void removeComment(PlotComment comment) { boolean removeComment(PlotComment comment) {
if (this.comments.contains(comment)) { if (this.comments == null) {
this.comments.remove(comment); return false;
} }
return this.comments.remove(comment);
} }
public void removeComments(List<PlotComment> comments) { void removeComments(List<PlotComment> comments) {
for (PlotComment comment : comments) { comments.forEach(this::removeComment);
removeComment(comment);
}
} }
public void addComment(PlotComment comment) { void addComment(PlotComment comment) {
if (this.comments == null) { if (this.comments == null) {
this.comments = new ArrayList<>(); this.comments = new ArrayList<>();
} }

View File

@ -7,15 +7,14 @@ import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
public class InboxOwner extends CommentInbox { public class InboxOwner extends CommentInbox {
@Override @Override
public boolean getComments(final Plot plot, final RunnableVal<List<PlotComment>> whenDone) { public boolean getComments(final Plot plot, final RunnableVal<List<PlotComment>> whenDone) {
Optional<ArrayList<PlotComment>> comments = plot.getSettings().getComments(toString()); List<PlotComment> comments = plot.getComments(toString());
if (comments.isPresent()) { if (!comments.isEmpty()) {
whenDone.value = comments.get(); whenDone.value = comments;
TaskManager.runTask(whenDone); TaskManager.runTask(whenDone);
return true; return true;
} }
@ -24,10 +23,10 @@ public class InboxOwner extends CommentInbox {
whenDone.value = value; whenDone.value = value;
if (value != null) { if (value != null) {
for (PlotComment comment : value) { for (PlotComment comment : value) {
plot.getSettings().addComment(comment); plot.addComment(comment);
} }
} else { } else {
plot.getSettings().setComments(new ArrayList<PlotComment>()); plot.setComments(new ArrayList<>());
} }
TaskManager.runTask(whenDone); TaskManager.runTask(whenDone);
} }
@ -39,7 +38,7 @@ public class InboxOwner extends CommentInbox {
if (plot.owner == null) { if (plot.owner == null) {
return false; return false;
} }
plot.getSettings().addComment(comment); plot.addComment(comment);
DBFunc.setComment(plot, comment); DBFunc.setComment(plot, comment);
return true; return true;
} }

View File

@ -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.object.RunnableVal;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager; import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
public class InboxPublic extends CommentInbox { public class InboxPublic extends CommentInbox {
@Override @Override
public boolean getComments(final Plot plot, final RunnableVal<List<PlotComment>> whenDone) { public boolean getComments(final Plot plot, final RunnableVal<List<PlotComment>> whenDone) {
Optional<ArrayList<PlotComment>> comments = plot.getSettings().getComments(toString()); List<PlotComment> comments = plot.getComments(toString());
if (comments.isPresent()) { if (!comments.isEmpty()) {
whenDone.value = comments.get(); whenDone.value = comments;
TaskManager.runTask(whenDone); TaskManager.runTask(whenDone);
return true; return true;
} }
@ -24,7 +22,7 @@ public class InboxPublic extends CommentInbox {
whenDone.value = value; whenDone.value = value;
if (value != null) { if (value != null) {
for (PlotComment comment : value) { for (PlotComment comment : value) {
plot.getSettings().addComment(comment); plot.addComment(comment);
} }
} }
TaskManager.runTask(whenDone); TaskManager.runTask(whenDone);
@ -34,7 +32,7 @@ public class InboxPublic extends CommentInbox {
} }
@Override public boolean addComment(Plot plot, PlotComment comment) { @Override public boolean addComment(Plot plot, PlotComment comment) {
plot.getSettings().addComment(comment); plot.addComment(comment);
DBFunc.setComment(plot, comment); DBFunc.setComment(plot, comment);
return true; return true;
} }

View File

@ -20,34 +20,32 @@ public class CommentManager {
if (!Settings.Enabled_Components.COMMENT_NOTIFIER || !plot.isOwner(player.getUUID())) { if (!Settings.Enabled_Components.COMMENT_NOTIFIER || !plot.isOwner(player.getUUID())) {
return; return;
} }
TaskManager.runTaskLaterAsync(new Runnable() { TaskManager.runTaskLaterAsync(() -> {
@Override public void run() { Collection<CommentInbox> boxes = CommentManager.inboxes.values();
Collection<CommentInbox> boxes = CommentManager.inboxes.values(); final AtomicInteger count = new AtomicInteger(0);
final AtomicInteger count = new AtomicInteger(0); final AtomicInteger size = new AtomicInteger(boxes.size());
final AtomicInteger size = new AtomicInteger(boxes.size()); for (final CommentInbox inbox : inboxes.values()) {
for (final CommentInbox inbox : inboxes.values()) { inbox.getComments(plot, new RunnableVal<List<PlotComment>>() {
inbox.getComments(plot, new RunnableVal<List<PlotComment>>() { @Override public void run(List<PlotComment> value) {
@Override public void run(List<PlotComment> value) { int total;
int total; if (value != null) {
if (value != null) { int num = 0;
int num = 0; for (PlotComment comment : value) {
for (PlotComment comment : value) { if (comment.timestamp > getTimestamp(player,
if (comment.timestamp > getTimestamp(player, inbox.toString())) {
inbox.toString())) { num++;
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); }, 20);
} }