From f391cfd43248b4e4535d154c5c3ebf57e34f5dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Thu, 20 Aug 2020 16:03:06 +0200 Subject: [PATCH] Extract comment logic from the plot class --- .../com/plotsquared/core/command/Inbox.java | 6 +- .../java/com/plotsquared/core/plot/Plot.java | 59 +++++++-------- .../core/plot/PlotCommentContainer.java | 73 +++++++++++++++++++ .../core/plot/comment/InboxOwner.java | 8 +- .../core/plot/comment/InboxPublic.java | 6 +- 5 files changed, 113 insertions(+), 39 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/plot/PlotCommentContainer.java diff --git a/Core/src/main/java/com/plotsquared/core/command/Inbox.java b/Core/src/main/java/com/plotsquared/core/command/Inbox.java index 458df04ec..4062615a6 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Inbox.java +++ b/Core/src/main/java/com/plotsquared/core/command/Inbox.java @@ -208,7 +208,7 @@ public class Inbox extends SubCommand { } PlotComment comment = value.get(index - 1); inbox.removeComment(plot, comment); - boolean success = plot.removeComment(comment); + boolean success = plot.getPlotCommentContainer().removeComment(comment); if (success) { player.sendMessage( TranslatableCaption.of("comment.comment_removed_success"), @@ -231,9 +231,9 @@ public class Inbox extends SubCommand { player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox_modify")); } inbox.clearInbox(plot); - List comments = plot.getComments(inbox.toString()); + List comments = plot.getPlotCommentContainer().getComments(inbox.toString()); if (!comments.isEmpty()) { - plot.removeComments(comments); + plot.getPlotCommentContainer().removeComments(comments); } player.sendMessage( TranslatableCaption.of("comment.comment_removed_success"), diff --git a/Core/src/main/java/com/plotsquared/core/plot/Plot.java b/Core/src/main/java/com/plotsquared/core/plot/Plot.java index f64b37fef..8926eff3b 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/Plot.java +++ b/Core/src/main/java/com/plotsquared/core/plot/Plot.java @@ -53,7 +53,6 @@ import com.plotsquared.core.location.PlotLoc; import com.plotsquared.core.permissions.Permission; import com.plotsquared.core.player.ConsolePlayer; import com.plotsquared.core.player.PlotPlayer; -import com.plotsquared.core.plot.comment.PlotComment; import com.plotsquared.core.plot.expiration.ExpireManager; import com.plotsquared.core.plot.expiration.PlotAnalysis; import com.plotsquared.core.plot.flag.FlagContainer; @@ -153,6 +152,10 @@ public class Plot { * Plot flag container */ private final FlagContainer flagContainer = new FlagContainer(null); + /** + * Utility used to manage plot comments + */ + private final PlotCommentContainer plotCommentContainer = new PlotCommentContainer(this); /** * Has the plot changed since the last save cycle? */ @@ -323,7 +326,9 @@ public class Plot { * @param message If a message should be sent to the player if a plot cannot be found * @return The plot if only 1 result is found, or null */ - @Nullable public static Plot getPlotFromString(PlotPlayer player, String arg, boolean message) { + @Nullable public static Plot getPlotFromString(@Nullable final PlotPlayer player, + @Nullable final String arg, + final boolean message) { if (arg == null) { if (player == null) { if (message) { @@ -365,13 +370,13 @@ public class Plot { return p; } } - if (message) { + if (message && player != null) { player.sendMessage(TranslatableCaption.of("invalid.not_valid_plot_id")); } return null; } if (area == null) { - if (message) { + if (message && player != null) { player.sendMessage(TranslatableCaption.of("errors.invalid_plot_world")); } return null; @@ -386,8 +391,8 @@ public class Plot { * @param string plot id/area + id * @return New or existing plot object */ - public static Plot fromString(PlotArea defaultArea, String string) { - String[] split = string.split(";|,"); + @Nullable public static Plot fromString(@Nullable final PlotArea defaultArea, @Nonnull final String string) { + final String[] split = string.split(";|,"); if (split.length == 2) { if (defaultArea != null) { PlotId id = PlotId.fromString(split[0] + ';' + split[1]); @@ -416,8 +421,8 @@ public class Plot { * @return plot at location or null * @see PlotPlayer#getCurrentPlot() if a player is expected here. */ - public static Plot getPlot(Location location) { - PlotArea pa = location.getPlotArea(); + @Nullable public static Plot getPlot(@Nullable final Location location) { + final PlotArea pa = location.getPlotArea(); if (pa != null) { return pa.getPlot(location); } @@ -3452,26 +3457,6 @@ public class Plot { return true; } - public boolean removeComment(PlotComment comment) { - return getSettings().removeComment(comment); - } - - public void removeComments(List comments) { - getSettings().removeComments(comments); - } - - public List getComments(String inbox) { - return getSettings().getComments(inbox); - } - - public void addComment(PlotComment comment) { - getSettings().addComment(comment); - } - - public void setComments(List list) { - getSettings().setComments(list); - } - public boolean getMerged(Direction direction) { return getMerged(direction.getIndex()); } @@ -3627,8 +3612,9 @@ public class Plot { * - The index corresponds to the index of the category in the config * * @return Average ratings in each category + * @see Settings.Ratings#CATEGORIES Rating categories */ - public double[] getAverageRatings() { + @Nonnull public double[] getAverageRatings() { Map rating; if (this.getSettings().getRatings() != null) { rating = this.getSettings().getRatings(); @@ -3662,8 +3648,23 @@ public class Plot { return ratings; } + /** + * Get the plot flag container + * + * @return Flag container + */ @Nonnull public FlagContainer getFlagContainer() { return this.flagContainer; } + /** + * Get the plot comment container. This can be used to manage + * and access plot comments + * + * @return Plot comment container + */ + @Nonnull public PlotCommentContainer getPlotCommentContainer() { + return this.plotCommentContainer; + } + } diff --git a/Core/src/main/java/com/plotsquared/core/plot/PlotCommentContainer.java b/Core/src/main/java/com/plotsquared/core/plot/PlotCommentContainer.java new file mode 100644 index 000000000..566622cce --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/plot/PlotCommentContainer.java @@ -0,0 +1,73 @@ +package com.plotsquared.core.plot; + +import com.plotsquared.core.plot.comment.PlotComment; + +import javax.annotation.Nonnull; +import java.util.List; + +/** + * Container for {@link com.plotsquared.core.plot.Plot} comments + */ +public final class PlotCommentContainer { + + private final Plot plot; + + PlotCommentContainer(@Nonnull final Plot plot) { + this.plot = plot; + } + + /** + * Remove a comment from the plot + * + * @param comment Comment to remove + * @return {@code true} if the comment was removed, {@code false} if not + */ + public boolean removeComment(@Nonnull final PlotComment comment) { + return this.getSettings().removeComment(comment); + } + + /** + * Remove a list of comments from the plot + * + * @param comments Comments to remove + */ + public void removeComments(@Nonnull final List comments) { + this.getSettings().removeComments(comments); + } + + /** + * Get all comments in a specific inbox + * + * @param inbox Inbox + * @return List of comments + */ + @Nonnull public List getComments(@Nonnull final String inbox) { + return this.getSettings().getComments(inbox); + } + + /** + * Add a comment to the plot + * + * @param comment Comment to add + */ + public void addComment(@Nonnull final PlotComment comment) { + this.getSettings().addComment(comment); + } + + /** + * Set the plot comments + * + * @param list New comments + */ + public void setComments(@Nonnull final List list) { + this.getSettings().setComments(list); + } + + @Nonnull private PlotSettings getSettings() { + if (this.plot.getSettings() == null) { + throw new IllegalStateException("Cannot access comments for unowned plots"); + } + return this.plot.getSettings(); + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/plot/comment/InboxOwner.java b/Core/src/main/java/com/plotsquared/core/plot/comment/InboxOwner.java index 4fe36c527..66f78ca42 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/comment/InboxOwner.java +++ b/Core/src/main/java/com/plotsquared/core/plot/comment/InboxOwner.java @@ -37,7 +37,7 @@ public class InboxOwner extends CommentInbox { @Override public boolean getComments(final Plot plot, final RunnableVal> whenDone) { - List comments = plot.getComments(toString()); + List comments = plot.getPlotCommentContainer().getComments(toString()); if (!comments.isEmpty()) { whenDone.value = comments; TaskManager.runTask(whenDone); @@ -48,10 +48,10 @@ public class InboxOwner extends CommentInbox { whenDone.value = value; if (value != null) { for (PlotComment comment : value) { - plot.addComment(comment); + plot.getPlotCommentContainer().addComment(comment); } } else { - plot.setComments(new ArrayList<>()); + plot.getPlotCommentContainer().setComments(new ArrayList<>()); } TaskManager.runTask(whenDone); } @@ -63,7 +63,7 @@ public class InboxOwner extends CommentInbox { if (plot.getOwner() == null) { return false; } - plot.addComment(comment); + plot.getPlotCommentContainer().addComment(comment); DBFunc.setComment(plot, comment); return true; } diff --git a/Core/src/main/java/com/plotsquared/core/plot/comment/InboxPublic.java b/Core/src/main/java/com/plotsquared/core/plot/comment/InboxPublic.java index 5376aebd9..85bf63b4d 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/comment/InboxPublic.java +++ b/Core/src/main/java/com/plotsquared/core/plot/comment/InboxPublic.java @@ -36,7 +36,7 @@ public class InboxPublic extends CommentInbox { @Override public boolean getComments(final Plot plot, final RunnableVal> whenDone) { - List comments = plot.getComments(toString()); + List comments = plot.getPlotCommentContainer().getComments(toString()); if (!comments.isEmpty()) { whenDone.value = comments; TaskManager.runTask(whenDone); @@ -47,7 +47,7 @@ public class InboxPublic extends CommentInbox { whenDone.value = value; if (value != null) { for (PlotComment comment : value) { - plot.addComment(comment); + plot.getPlotCommentContainer().addComment(comment); } } TaskManager.runTask(whenDone); @@ -57,7 +57,7 @@ public class InboxPublic extends CommentInbox { } @Override public boolean addComment(Plot plot, PlotComment comment) { - plot.addComment(comment); + plot.getPlotCommentContainer().addComment(comment); DBFunc.setComment(plot, comment); return true; }