Search in sources :

Example 1 with CommentInbox

use of com.plotsquared.core.plot.comment.CommentInbox in project PlotSquared by IntellectualSites.

the class Inbox method onCommand.

@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
    final Plot plot = player.getCurrentPlot();
    if (plot == null) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
        return false;
    }
    if (!plot.hasOwner()) {
        player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
        return false;
    }
    if (args.length == 0) {
        sendUsage(player);
        for (final CommentInbox inbox : CommentManager.inboxes.values()) {
            if (inbox.canRead(plot, player)) {
                if (!inbox.getComments(plot, new RunnableVal<>() {

                    @Override
                    public void run(List<PlotComment> value) {
                        if (value != null) {
                            int total = 0;
                            int unread = 0;
                            for (PlotComment comment : value) {
                                total++;
                                if (comment.timestamp > CommentManager.getTimestamp(player, inbox.toString())) {
                                    unread++;
                                }
                            }
                            if (total != 0) {
                                player.sendMessage(TranslatableCaption.of("comment.inbox_item"), Template.of("value", inbox + " (" + total + '/' + unread + ')'));
                                return;
                            }
                        }
                        player.sendMessage(TranslatableCaption.of("comment.inbox_item"), Template.of("value", inbox.toString()));
                    }
                })) {
                    player.sendMessage(TranslatableCaption.of("comment.inbox_item"), Template.of("value", inbox.toString()));
                }
            }
        }
        return false;
    }
    final CommentInbox inbox = CommentManager.inboxes.get(args[0].toLowerCase());
    if (inbox == null) {
        player.sendMessage(TranslatableCaption.of("comment.invalid_inbox"), Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), ", ")));
        return false;
    }
    final MetaDataKey<Long> metaDataKey = MetaDataKey.of(String.format("inbox:%s", inbox), new TypeLiteral<>() {
    });
    try (final MetaDataAccess<Long> metaDataAccess = player.accessTemporaryMetaData(metaDataKey)) {
        metaDataAccess.set(System.currentTimeMillis());
    }
    final int page;
    if (args.length > 1) {
        switch(args[1].toLowerCase()) {
            case "delete":
                if (!inbox.canModify(plot, player)) {
                    player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox_modify"));
                    return false;
                }
                if (args.length != 3) {
                    player.sendMessage(TranslatableCaption.of("commandconfig.command_syntax"), Template.of("value", "/plot inbox " + inbox + " delete <index>"));
                    return true;
                }
                final int index;
                try {
                    index = Integer.parseInt(args[2]);
                    if (index < 1) {
                        player.sendMessage(TranslatableCaption.of("comment.not_valid_inbox_index"), Templates.of("number", index));
                        return false;
                    }
                } catch (NumberFormatException ignored) {
                    player.sendMessage(TranslatableCaption.of("commandconfig.command_syntax"), Template.of("value", "/plot inbox " + inbox + " delete <index>"));
                    return false;
                }
                if (!inbox.getComments(plot, new RunnableVal<>() {

                    @Override
                    public void run(List<PlotComment> value) {
                        if (index > value.size()) {
                            player.sendMessage(TranslatableCaption.of("comment.not_valid_inbox_index"), Templates.of("number", index));
                            return;
                        }
                        PlotComment comment = value.get(index - 1);
                        inbox.removeComment(plot, comment);
                        boolean success = plot.getPlotCommentContainer().removeComment(comment);
                        if (success) {
                            player.sendMessage(TranslatableCaption.of("comment.comment_removed_success"), Template.of("value", comment.comment));
                        } else {
                            player.sendMessage(TranslatableCaption.of("comment.comment_removed_failure"));
                        }
                    }
                })) {
                    player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
                    return false;
                }
                return true;
            case "clear":
                if (!inbox.canModify(plot, player)) {
                    player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox_modify"));
                }
                inbox.clearInbox(plot);
                List<PlotComment> comments = plot.getPlotCommentContainer().getComments(inbox.toString());
                if (!comments.isEmpty()) {
                    player.sendMessage(TranslatableCaption.of("comment.comment_removed_success"), Template.of("value", String.valueOf(comments)));
                    plot.getPlotCommentContainer().removeComments(comments);
                }
                return true;
            default:
                try {
                    page = Integer.parseInt(args[1]);
                } catch (NumberFormatException ignored) {
                    sendUsage(player);
                    return false;
                }
        }
    } else {
        page = 1;
    }
    if (!inbox.canRead(plot, player)) {
        player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox"));
        return false;
    }
    if (!inbox.getComments(plot, new RunnableVal<>() {

        @Override
        public void run(List<PlotComment> value) {
            displayComments(player, value, page);
        }
    })) {
        player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
        return false;
    }
    return true;
}
Also used : Plot(com.plotsquared.core.plot.Plot) RunnableVal(com.plotsquared.core.util.task.RunnableVal) PlotComment(com.plotsquared.core.plot.comment.PlotComment) CommentInbox(com.plotsquared.core.plot.comment.CommentInbox) List(java.util.List) LinkedList(java.util.LinkedList)

Example 2 with CommentInbox

use of com.plotsquared.core.plot.comment.CommentInbox in project PlotSquared by IntellectualSites.

the class Comment method onCommand.

@Override
public boolean onCommand(PlotPlayer<?> player, String[] args) {
    if (args.length < 2) {
        player.sendMessage(TranslatableCaption.of("comment.comment_syntax"), Template.of("command", "/plot comment [X;Z]"), Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), "|")));
        return false;
    }
    // Attempt to extract a plot out of the first argument
    Plot plot = null;
    if (!CommentManager.inboxes.containsKey(args[0].toLowerCase(Locale.ENGLISH))) {
        plot = Plot.getPlotFromString(player, args[0], false);
    }
    int index;
    if (plot == null) {
        index = 1;
        plot = player.getLocation().getPlotAbs();
    } else {
        if (args.length < 3) {
            player.sendMessage(TranslatableCaption.of("comment.comment_syntax"), Template.of("command", "/plot comment [X;Z]"), Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), "|")));
            return false;
        }
        index = 2;
    }
    CommentInbox inbox = CommentManager.inboxes.get(args[index - 1].toLowerCase());
    if (inbox == null) {
        player.sendMessage(TranslatableCaption.of("comment.comment_syntax"), Template.of("command", "/plot comment [X;Z]"), Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), "|")));
        return false;
    }
    if (!inbox.canWrite(plot, player)) {
        player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox"));
        return false;
    }
    String message = StringMan.join(Arrays.copyOfRange(args, index, args.length), " ");
    PlotComment comment = new PlotComment(player.getLocation().getWorldName(), plot.getId(), message, player.getName(), inbox.toString(), System.currentTimeMillis());
    boolean result = inbox.addComment(plot, comment);
    if (!result) {
        player.sendMessage(TranslatableCaption.of("comment.no_plot_inbox"));
        player.sendMessage(TranslatableCaption.of("comment.comment_syntax"), Template.of("command", "/plot comment [X;Z]"), Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), "|")));
        return false;
    }
    for (final PlotPlayer<?> pp : PlotSquared.platform().playerManager().getPlayers()) {
        if (pp.getAttribute("chatspy")) {
            pp.sendMessage(StaticCaption.of("/plot comment " + StringMan.join(args, " ")));
        }
    }
    player.sendMessage(TranslatableCaption.of("comment.comment_added"));
    return true;
}
Also used : Plot(com.plotsquared.core.plot.Plot) PlotComment(com.plotsquared.core.plot.comment.PlotComment) CommentInbox(com.plotsquared.core.plot.comment.CommentInbox)

Aggregations

Plot (com.plotsquared.core.plot.Plot)2 CommentInbox (com.plotsquared.core.plot.comment.CommentInbox)2 PlotComment (com.plotsquared.core.plot.comment.PlotComment)2 RunnableVal (com.plotsquared.core.util.task.RunnableVal)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1