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.google.inject.Inject;
022import com.plotsquared.core.permissions.Permission;
023import com.plotsquared.core.player.PlotPlayer;
024import com.plotsquared.core.util.TabCompletions;
025import org.checkerframework.checker.nullness.qual.NonNull;
026
027import java.util.Collection;
028import java.util.Collections;
029import java.util.LinkedList;
030import java.util.List;
031import java.util.stream.Collectors;
032
033@CommandDeclaration(command = "dislike",
034        permission = "plots.dislike",
035        usage = "/plot dislike [next|purge]",
036        category = CommandCategory.INFO,
037        requiredType = RequiredType.PLAYER)
038public class Dislike extends SubCommand {
039
040    private final Like like;
041
042    @Inject
043    public Dislike(final @NonNull Like like) {
044        this.like = like;
045    }
046
047    @Override
048    public boolean onCommand(PlotPlayer<?> player, String[] args) {
049        return this.like.handleLike(player, args, false);
050    }
051
052    @Override
053    public Collection<Command> tab(final PlotPlayer<?> player, final String[] args, final boolean space) {
054        if (args.length == 1) {
055            final List<String> completions = new LinkedList<>();
056            if (player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_PURGE_RATINGS)) {
057                completions.add("purge");
058            }
059            final List<Command> commands = completions.stream().filter(completion -> completion
060                            .toLowerCase()
061                            .startsWith(args[0].toLowerCase()))
062                    .map(completion -> new Command(null, true, completion, "", RequiredType.PLAYER, CommandCategory.INFO) {
063                    }).collect(Collectors.toCollection(LinkedList::new));
064            if (player.hasPermission(Permission.PERMISSION_RATE) && args[0].length() > 0) {
065                commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList()));
066            }
067            return commands;
068        }
069        return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList());
070    }
071
072}