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.bukkit;
020
021import com.plotsquared.bukkit.util.BukkitUtil;
022import com.plotsquared.core.command.MainCommand;
023import com.plotsquared.core.configuration.Settings;
024import com.plotsquared.core.player.ConsolePlayer;
025import com.plotsquared.core.player.PlotPlayer;
026import org.bukkit.command.Command;
027import org.bukkit.command.CommandExecutor;
028import org.bukkit.command.CommandSender;
029import org.bukkit.command.ConsoleCommandSender;
030import org.bukkit.command.ProxiedCommandSender;
031import org.bukkit.command.RemoteConsoleCommandSender;
032import org.bukkit.command.TabCompleter;
033import org.bukkit.entity.Player;
034
035import java.util.ArrayList;
036import java.util.Collection;
037import java.util.Collections;
038import java.util.List;
039import java.util.Locale;
040
041public class BukkitCommand implements CommandExecutor, TabCompleter {
042
043    @Override
044    public boolean onCommand(
045            CommandSender commandSender, Command command, String commandLabel,
046            String[] args
047    ) {
048        if (commandSender instanceof Player) {
049            return MainCommand.onCommand(BukkitUtil.adapt((Player) commandSender), args);
050        }
051        if (commandSender instanceof ConsoleCommandSender
052                || commandSender instanceof ProxiedCommandSender
053                || commandSender instanceof RemoteConsoleCommandSender) {
054            return MainCommand.onCommand(ConsolePlayer.getConsole(), args);
055        }
056        return false;
057    }
058
059    @Override
060    public List<String> onTabComplete(
061            CommandSender commandSender, Command command, String label,
062            String[] args
063    ) {
064        if (!(commandSender instanceof Player)) {
065            return null;
066        }
067        PlotPlayer<?> player = BukkitUtil.adapt((Player) commandSender);
068        if (args.length == 0) {
069            return Collections.singletonList("plots");
070        }
071        if (!Settings.Enabled_Components.TAB_COMPLETED_ALIASES.contains(label.toLowerCase(Locale.ENGLISH))) {
072            return List.of();
073        }
074        Collection<com.plotsquared.core.command.Command> objects =
075                MainCommand.getInstance().tab(player, args, label.endsWith(" "));
076        if (objects == null) {
077            return null;
078        }
079        List<String> result = new ArrayList<>();
080        for (com.plotsquared.core.command.Command o : objects) {
081            result.add(o.toString());
082        }
083        return result;
084    }
085
086}