# Source code
source: package: co.aikar.commands, class: BukkitCommandCompletions

    001/*
    002 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
    003 *
    004 *  Permission is hereby granted, free of charge, to any person obtaining
    005 *  a copy of this software and associated documentation files (the
    006 *  "Software"), to deal in the Software without restriction, including
    007 *  without limitation the rights to use, copy, modify, merge, publish,
    008 *  distribute, sublicense, and/or sell copies of the Software, and to
    009 *  permit persons to whom the Software is furnished to do so, subject to
    010 *  the following conditions:
    011 *
    012 *  The above copyright notice and this permission notice shall be
    013 *  included in all copies or substantial portions of the Software.
    014 *
    015 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    016 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    017 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    018 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    019 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    020 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    021 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    022 */
    023
    024package co.aikar.commands;
    025
    026import co.aikar.commands.bukkit.contexts.OnlinePlayer;
    027import org.apache.commons.lang.Validate;
    028import org.bukkit.Bukkit;
    029import org.bukkit.ChatColor;
    030import org.bukkit.DyeColor;
    031import org.bukkit.World;
    032import org.bukkit.command.CommandSender;
    033import org.bukkit.entity.EntityType;
    034import org.bukkit.entity.Player;
    035import org.bukkit.util.StringUtil;
    036
    037import java.util.ArrayList;
    038import java.util.Arrays;
    039import java.util.Set;
    040import java.util.stream.Collectors;
    041import java.util.stream.Stream;
    042
    043@SuppressWarnings("WeakerAccess")
    044public class BukkitCommandCompletions extends CommandCompletions<BukkitCommandCompletionContext> {
    045    public BukkitCommandCompletions(BukkitCommandManager manager) {
    046        super(manager);
    047        registerAsyncCompletion("mobs", c -> {
    048            final Stream<String> normal = Stream.of(EntityType.values())
    049                    .map(entityType -> ACFUtil.simplifyString(entityType.getName()));
    050            return normal.collect(Collectors.toList());
    051        });
    052        registerAsyncCompletion("chatcolors", c -> {
    053            Stream<ChatColor> colors = Stream.of(ChatColor.values());
    054            if (c.hasConfig("colorsonly")) {
    055                colors = colors.filter(color -> color.ordinal() <= 0xF);
    056            }
    057            String filter = c.getConfig("filter");
    058            if (filter != null) {
    059                Set<String> filters = Arrays.stream(ACFPatterns.COLON.split(filter))
    060                        .map(ACFUtil::simplifyString).collect(Collectors.toSet());
    061
    062                colors = colors.filter(color -> filters.contains(ACFUtil.simplifyString(color.name())));
    063            }
    064
    065            return colors.map(color -> ACFUtil.simplifyString(color.name())).collect(Collectors.toList());
    066        });
    067        registerAsyncCompletion("dyecolors", c -> ACFUtil.enumNames(DyeColor.values()));
    068        registerCompletion("worlds", c -> (
    069                Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList())
    070        ));
    071
    072        registerCompletion("players", c -> {
    073            CommandSender sender = c.getSender();
    074            Validate.notNull(sender, "Sender cannot be null");
    075
    076            Player senderPlayer = sender instanceof Player ? (Player) sender : null;
    077
    078            ArrayList<String> matchedPlayers = new ArrayList<>();
    079            for (Player player : Bukkit.getOnlinePlayers()) {
    080                String name = player.getName();
    081                if ((senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(name, c.getInput())) {
    082                    matchedPlayers.add(name);
    083                }
    084            }
    085
    086
    087            matchedPlayers.sort(String.CASE_INSENSITIVE_ORDER);
    088            return matchedPlayers;
    089        });
    090
    091        setDefaultCompletion("players", OnlinePlayer.class, co.aikar.commands.contexts.OnlinePlayer.class, Player.class);
    092        setDefaultCompletion("worlds", World.class);
    093    }
    094
    095}