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 org.apache.commons.lang.Validate;
027import org.bukkit.Bukkit;
028import org.bukkit.ChatColor;
029import org.bukkit.DyeColor;
030import org.bukkit.World;
031import org.bukkit.command.CommandSender;
032import org.bukkit.entity.EntityType;
033import org.bukkit.entity.Player;
034import org.bukkit.util.StringUtil;
035
036import java.util.ArrayList;
037import java.util.Arrays;
038import java.util.Set;
039import java.util.stream.Collectors;
040import java.util.stream.Stream;
041
042@SuppressWarnings("WeakerAccess")
043public class BukkitCommandCompletions extends CommandCompletions<BukkitCommandCompletionContext> {
044    public BukkitCommandCompletions(BukkitCommandManager manager) {
045        super(manager);
046        registerCompletion("mobs", c -> {
047            final Stream<String> normal = Stream.of(EntityType.values())
048                    .map(entityType -> ACFUtil.simplifyString(entityType.getName()));
049            return normal.collect(Collectors.toList());
050        });
051        registerCompletion("chatcolors", c -> {
052            Stream<ChatColor> colors = Stream.of(ChatColor.values());
053            if (c.hasConfig("colorsonly")) {
054                colors = colors.filter(color -> color.ordinal() <= 0xF);
055            }
056            String filter = c.getConfig("filter");
057            if (filter != null) {
058                Set<String> filters = Arrays.stream(ACFPatterns.COLON.split(filter))
059                        .map(ACFUtil::simplifyString).collect(Collectors.toSet());
060
061                colors = colors.filter(color -> filters.contains(ACFUtil.simplifyString(color.name())));
062            }
063
064            return colors.map(color -> ACFUtil.simplifyString(color.name())).collect(Collectors.toList());
065        });
066        registerCompletion("dyecolors", c -> ACFUtil.enumNames(DyeColor.values()));
067        registerCompletion("worlds", c -> (
068            Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList())
069        ));
070
071        registerCompletion("players", c -> {
072            CommandSender sender = c.getSender();
073            Validate.notNull(sender, "Sender cannot be null");
074
075            Player senderPlayer = sender instanceof Player ? (Player) sender : null;
076
077            ArrayList<String> matchedPlayers = new ArrayList<String>();
078            for (Player player : Bukkit.getOnlinePlayers()) {
079                String name = player.getName();
080                if ((senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(name, c.getInput())) {
081                    matchedPlayers.add(name);
082                }
083            }
084
085
086            matchedPlayers.sort(String.CASE_INSENSITIVE_ORDER);
087            return matchedPlayers;
088        });
089    }
090
091}