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.configuration.Settings;
022import com.plotsquared.core.configuration.caption.Caption;
023import com.plotsquared.core.configuration.caption.StaticCaption;
024import com.plotsquared.core.configuration.caption.TranslatableCaption;
025import com.plotsquared.core.database.DBFunc;
026import com.plotsquared.core.permissions.Permission;
027import com.plotsquared.core.player.PlotPlayer;
028import com.plotsquared.core.plot.Plot;
029import com.plotsquared.core.plot.flag.implementations.HideInfoFlag;
030import com.plotsquared.core.util.TabCompletions;
031import net.kyori.adventure.text.minimessage.Template;
032
033import java.util.Collection;
034import java.util.Collections;
035import java.util.LinkedList;
036import java.util.List;
037import java.util.stream.Collectors;
038
039@CommandDeclaration(command = "info",
040        aliases = "i",
041        usage = "/plot info <id> [-f to force info]",
042        category = CommandCategory.INFO)
043public class Info extends SubCommand {
044
045    @Override
046    public boolean onCommand(final PlotPlayer<?> player, String[] args) {
047        Plot plot;
048        String arg;
049        if (args.length > 0) {
050            arg = args[0];
051            switch (arg) {
052                // TODO: (re?)implement /plot info inv. (it was never properly implemented)
053                case "trusted", "alias", "biome", "denied", "flags", "id", "size", "members", "creationdate", "seen", "owner", "rating", "likes" -> plot = Plot
054                        .getPlotFromString(player, null, false);
055                default -> {
056                    plot = Plot.getPlotFromString(player, arg, false);
057                    if (args.length == 2) {
058                        arg = args[1];
059                    } else {
060                        arg = null;
061                    }
062                }
063            }
064            if (plot == null) {
065                plot = player.getCurrentPlot();
066            }
067        } else {
068            arg = null;
069            plot = player.getCurrentPlot();
070        }
071        if (plot == null) {
072            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
073            return false;
074        }
075
076        if (arg != null) {
077            if (args.length == 1) {
078                args = new String[0];
079            } else {
080                args = new String[]{args[1]};
081            }
082        }
083
084        // hide-info flag
085        if (plot.getFlag(HideInfoFlag.class)) {
086            boolean allowed = false;
087            for (final String argument : args) {
088                if (argument.equalsIgnoreCase("-f")) {
089                    if (!player
090                            .hasPermission(Permission.PERMISSION_AREA_INFO_FORCE.toString())) {
091                        player.sendMessage(
092                                TranslatableCaption.of("permission.no_permission"),
093                                Template.of("node", Permission.PERMISSION_AREA_INFO_FORCE.toString())
094                        );
095                        return true;
096                    }
097                    allowed = true;
098                    break;
099                }
100            }
101            if (!allowed) {
102                player.sendMessage(TranslatableCaption.of("info.plot_info_hidden"));
103                return true;
104            }
105        }
106
107        boolean hasOwner = plot.hasOwner();
108        // Wildcard player {added}
109        boolean containsEveryone = plot.getTrusted().contains(DBFunc.EVERYONE);
110        boolean trustedEveryone = plot.getMembers().contains(DBFunc.EVERYONE);
111        // Unclaimed?
112        if (!hasOwner && !containsEveryone && !trustedEveryone) {
113            player.sendMessage(
114                    TranslatableCaption.of("info.plot_info_unclaimed"),
115                    Template.of("plot", plot.getId().getX() + ";" + plot.getId().getY())
116            );
117            return true;
118        }
119        Caption info = TranslatableCaption.of("info.plot_info_format");
120        boolean full;
121        if (arg != null) {
122            info = getCaption(arg);
123            if (info == null) {
124                if (Settings.Ratings.USE_LIKES) {
125                    player.sendMessage(StaticCaption.of(
126                            "&6Categories&7: &amembers&7, &aalias&7, &abiome&7, &aseen&7, &adenied&7, &aflags&7, &aid&7, &asize&7, &atrusted&7, "
127                                    + "&aowner&7, " + " &alikes"));
128                } else {
129                    player.sendMessage(StaticCaption.of(
130                            "&6Categories&7: &amembers&7, &aalias&7, &abiome&7, &aseen&7, &adenied&7, &aflags&7, &aid&7, &asize&7, &atrusted&7, "
131                                    + "&aowner&7, " + " &arating"));
132                }
133                return false;
134            }
135            full = true;
136        } else {
137            full = false;
138        }
139        plot.format(info, player, full).thenAcceptAsync(player::sendMessage);
140        return true;
141    }
142
143    @Override
144    public Collection<Command> tab(PlotPlayer<?> player, String[] args, boolean space) {
145        final List<String> completions = new LinkedList<>();
146        if (player.hasPermission(Permission.PERMISSION_AREA_INFO_FORCE)) {
147            completions.add("-f");
148        }
149
150        final List<Command> commands = completions.stream().filter(completion -> completion
151                        .toLowerCase()
152                        .startsWith(args[0].toLowerCase()))
153                .map(completion -> new Command(null, true, completion, "", RequiredType.PLAYER, CommandCategory.INFO) {
154                }).collect(Collectors.toCollection(LinkedList::new));
155
156        if (player.hasPermission(Permission.PERMISSION_AREA_INFO_FORCE) && args[0].length() > 0) {
157            commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList()));
158        }
159
160        return commands;
161    }
162
163    private Caption getCaption(String string) {
164        return switch (string) {
165            case "trusted" -> TranslatableCaption.of("info.plot_info_trusted");
166            case "alias" -> TranslatableCaption.of("info.plot_info_alias");
167            case "biome" -> TranslatableCaption.of("info.plot_info_biome");
168            case "denied" -> TranslatableCaption.of("info.plot_info_denied");
169            case "flags" -> TranslatableCaption.of("info.plot_info_flags");
170            case "id" -> TranslatableCaption.of("info.plot_info_id");
171            case "size" -> TranslatableCaption.of("info.plot_info_size");
172            case "members" -> TranslatableCaption.of("info.plot_info_members");
173            case "owner" -> TranslatableCaption.of("info.plot_info_owner");
174            case "rating" -> TranslatableCaption.of("info.plot_info_rating");
175            case "likes" -> TranslatableCaption.of("info.plot_info_likes");
176            case "seen" -> TranslatableCaption.of("info.plot_info_seen");
177            case "creationdate" -> TranslatableCaption.of("info.plot_info_creationdate");
178            default -> null;
179        };
180    }
181
182}