Extract comment logic from the plot class

This commit is contained in:
Alexander Söderberg 2020-08-20 16:03:06 +02:00
parent 352136f0c6
commit f391cfd432
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
5 changed files with 113 additions and 39 deletions

View File

@ -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<PlotComment> comments = plot.getComments(inbox.toString());
List<PlotComment> comments = plot.getPlotCommentContainer().getComments(inbox.toString());
if (!comments.isEmpty()) {
plot.removeComments(comments);
plot.getPlotCommentContainer().removeComments(comments);
}
player.sendMessage(
TranslatableCaption.of("comment.comment_removed_success"),

View File

@ -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<PlotComment> comments) {
getSettings().removeComments(comments);
}
public List<PlotComment> getComments(String inbox) {
return getSettings().getComments(inbox);
}
public void addComment(PlotComment comment) {
getSettings().addComment(comment);
}
public void setComments(List<PlotComment> 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<UUID, Integer> 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;
}
}

View File

@ -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<PlotComment> comments) {
this.getSettings().removeComments(comments);
}
/**
* Get all comments in a specific inbox
*
* @param inbox Inbox
* @return List of comments
*/
@Nonnull public List<PlotComment> 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<PlotComment> 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();
}
}

View File

@ -37,7 +37,7 @@ public class InboxOwner extends CommentInbox {
@Override
public boolean getComments(final Plot plot, final RunnableVal<List<PlotComment>> whenDone) {
List<PlotComment> comments = plot.getComments(toString());
List<PlotComment> 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;
}

View File

@ -36,7 +36,7 @@ public class InboxPublic extends CommentInbox {
@Override
public boolean getComments(final Plot plot, final RunnableVal<List<PlotComment>> whenDone) {
List<PlotComment> comments = plot.getComments(toString());
List<PlotComment> 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;
}