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.minimessage.Template;
031
032import java.util.Arrays;
033import java.util.Locale;
034
035@CommandDeclaration(command = "comment",
036        aliases = {"msg"},
037        category = CommandCategory.CHAT,
038        requiredType = RequiredType.PLAYER,
039        permission = "plots.comment")
040public class Comment extends SubCommand {
041
042    @Override
043    public boolean onCommand(PlotPlayer<?> player, String[] args) {
044        if (args.length < 2) {
045            player.sendMessage(
046                    TranslatableCaption.of("comment.comment_syntax"),
047                    Template.of("command", "/plot comment [X;Z]"),
048                    Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), "|"))
049            );
050            return false;
051        }
052
053        // Attempt to extract a plot out of the first argument
054        Plot plot = null;
055        if (!CommentManager.inboxes.containsKey(args[0].toLowerCase(Locale.ENGLISH))) {
056            plot = Plot.getPlotFromString(player, args[0], false);
057        }
058
059        int index;
060        if (plot == null) {
061            index = 1;
062            plot = player.getLocation().getPlotAbs();
063        } else {
064            if (args.length < 3) {
065                player.sendMessage(
066                        TranslatableCaption.of("comment.comment_syntax"),
067                        Template.of("command", "/plot comment [X;Z]"),
068                        Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), "|"))
069                );
070                return false;
071            }
072            index = 2;
073        }
074
075        CommentInbox inbox = CommentManager.inboxes.get(args[index - 1].toLowerCase());
076        if (inbox == null) {
077            player.sendMessage(
078                    TranslatableCaption.of("comment.comment_syntax"),
079                    Template.of("command", "/plot comment [X;Z]"),
080                    Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), "|"))
081            );
082            return false;
083        }
084
085        if (!inbox.canWrite(plot, player)) {
086            player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox"));
087            return false;
088        }
089
090        String message = StringMan.join(Arrays.copyOfRange(args, index, args.length), " ");
091        PlotComment comment =
092                new PlotComment(player.getLocation().getWorldName(), plot.getId(), message,
093                        player.getName(), inbox.toString(), System.currentTimeMillis()
094                );
095        boolean result = inbox.addComment(plot, comment);
096        if (!result) {
097            player.sendMessage(TranslatableCaption.of("comment.no_plot_inbox"));
098            player.sendMessage(
099                    TranslatableCaption.of("comment.comment_syntax"),
100                    Template.of("command", "/plot comment [X;Z]"),
101                    Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), "|"))
102            );
103            return false;
104        }
105
106        for (final PlotPlayer<?> pp : PlotSquared.platform().playerManager().getPlayers()) {
107            if (pp.getAttribute("chatspy")) {
108                pp.sendMessage(StaticCaption.of("/plot comment " + StringMan.join(args, " ")));
109            }
110        }
111
112        player.sendMessage(TranslatableCaption.of("comment.comment_added"));
113        return true;
114    }
115
116}