001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.command;
020
021import com.plotsquared.core.PlotSquared;
022import com.plotsquared.core.configuration.caption.StaticCaption;
023import com.plotsquared.core.configuration.caption.TranslatableCaption;
024import com.plotsquared.core.player.PlotPlayer;
025import com.plotsquared.core.plot.Plot;
026import com.plotsquared.core.plot.comment.CommentInbox;
027import com.plotsquared.core.plot.comment.CommentManager;
028import com.plotsquared.core.plot.comment.PlotComment;
029import com.plotsquared.core.util.StringMan;
030import net.kyori.adventure.text.Component;
031import net.kyori.adventure.text.minimessage.tag.Tag;
032import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
033
034import java.util.Arrays;
035import java.util.Locale;
036
037@CommandDeclaration(command = "comment",
038        aliases = {"msg"},
039        category = CommandCategory.CHAT,
040        requiredType = RequiredType.PLAYER,
041        permission = "plots.comment")
042public class Comment extends SubCommand {
043
044    @Override
045    public boolean onCommand(PlotPlayer<?> player, String[] args) {
046        if (args.length < 2) {
047            player.sendMessage(
048                    TranslatableCaption.of("comment.comment_syntax"),
049                    TagResolver.builder()
050                            .tag("command", Tag.inserting(Component.text("/plot comment [X;Z]")))
051                            .tag("list", Tag.inserting(Component.text(StringMan.join(CommentManager.inboxes.keySet(), "|"))))
052                            .build()
053            );
054            return false;
055        }
056
057        // Attempt to extract a plot out of the first argument
058        Plot plot = null;
059        if (!CommentManager.inboxes.containsKey(args[0].toLowerCase(Locale.ENGLISH))) {
060            plot = Plot.getPlotFromString(player, args[0], false);
061        }
062
063        int index;
064        if (plot == null) {
065            index = 1;
066            plot = player.getLocation().getPlotAbs();
067        } else {
068            if (args.length < 3) {
069                player.sendMessage(
070                        TranslatableCaption.of("comment.comment_syntax"),
071                        TagResolver.builder()
072                                .tag("command", Tag.inserting(Component.text("/plot comment [X;Z]")))
073                                .tag("list", Tag.inserting(Component.text(StringMan.join(CommentManager.inboxes.keySet(), "|"))))
074                                .build()
075                );
076                return false;
077            }
078            index = 2;
079        }
080
081        CommentInbox inbox = CommentManager.inboxes.get(args[index - 1].toLowerCase());
082        if (inbox == null) {
083            player.sendMessage(
084                    TranslatableCaption.of("comment.comment_syntax"),
085                    TagResolver.builder()
086                            .tag("command", Tag.inserting(Component.text("/plot comment [X;Z]")))
087                            .tag("list", Tag.inserting(Component.text(StringMan.join(CommentManager.inboxes.keySet(), "|"))))
088                            .build()
089            );
090            return false;
091        }
092
093        if (!inbox.canWrite(plot, player)) {
094            player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox"));
095            return false;
096        }
097
098        String message = StringMan.join(Arrays.copyOfRange(args, index, args.length), " ");
099        PlotComment comment =
100                new PlotComment(player.getLocation().getWorldName(), plot.getId(), message,
101                        player.getName(), inbox.toString(), System.currentTimeMillis()
102                );
103        boolean result = inbox.addComment(plot, comment);
104        if (!result) {
105            player.sendMessage(TranslatableCaption.of("comment.no_plot_inbox"));
106            player.sendMessage(
107                    TranslatableCaption.of("comment.comment_syntax"),
108                    TagResolver.builder()
109                            .tag("command", Tag.inserting(Component.text("/plot comment [X;Z]")))
110                            .tag("list", Tag.inserting(Component.text(StringMan.join(CommentManager.inboxes.keySet(), "|"))))
111                            .build()
112            );
113            return false;
114        }
115
116        for (final PlotPlayer<?> pp : PlotSquared.platform().playerManager().getPlayers()) {
117            if (pp.getAttribute("chatspy")) {
118                pp.sendMessage(StaticCaption.of("/plot comment " + StringMan.join(args, " ")));
119            }
120        }
121
122        player.sendMessage(TranslatableCaption.of("comment.comment_added"));
123        return true;
124    }
125
126}