    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
    026
    027import co.aikar.commands.annotation.Optional;
    028import co.aikar.commands.bungee.contexts.OnlinePlayer;
    029import net.md_5.bungee.api.ChatColor;
    030import net.md_5.bungee.api.CommandSender;
    031import net.md_5.bungee.api.connection.ProxiedPlayer;
    032import org.jetbrains.annotations.Nullable;
    033
    034import java.util.stream.Collectors;
    035import java.util.stream.Stream;
    036
    037public class BungeeCommandContexts extends CommandContexts<BungeeCommandExecutionContext> {
    038
    039    BungeeCommandContexts(CommandManager manager) {
    040        super(manager);
    041        registerContext(OnlinePlayer.class, this::getOnlinePlayer);
    042        registerContext(co.aikar.commands.contexts.OnlineProxiedPlayer.class, c -> {
    043            OnlinePlayer onlinePlayer = getOnlinePlayer(c);
    044            return onlinePlayer != null ? new co.aikar.commands.contexts.OnlineProxiedPlayer(onlinePlayer.getPlayer()) : null;
    045        });
    046        registerIssuerAwareContext(CommandSender.class, BungeeCommandExecutionContext::getSender);
    047        registerIssuerAwareContext(ProxiedPlayer.class, (c) -> {
    048            ProxiedPlayer proxiedPlayer = c.getSender() instanceof ProxiedPlayer ? (ProxiedPlayer) c.getSender() : null;
    049            if (proxiedPlayer == null && !c.hasAnnotation(Optional.class)) {
    050                throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false);
    051            }
    052            return proxiedPlayer;
    053        });
    054
    055        registerContext(ChatColor.class, c -> {
    056            String first = c.popFirstArg();
    057            Stream<ChatColor> colors = Stream.of(ChatColor.values());
    058            if (c.hasFlag("colorsonly")) {
    059                colors = colors.filter(color -> color.ordinal() <= 0xF);
    060            }
    061            String filter = c.getFlagValue("filter", (String) null);
    062            if (filter != null) {
    063                filter = ACFUtil.simplifyString(filter);
    064                String finalFilter = filter;
    065                colors = colors.filter(color -> finalFilter.equals(ACFUtil.simplifyString(color.name())));
    066            }
    067
    068            ChatColor match = ACFUtil.simpleMatch(ChatColor.class, first);
    069            if (match == null) {
    070                String valid = colors
    071                        .map(color -> "<c2>" + ACFUtil.simplifyString(color.name()) + "</c2>")
    072                        .collect(Collectors.joining("<c1>,</c1> "));
    073
    074                throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", valid);
    075            }
    076            return match;
    077        });
    078    }
    079
    080    @Nullable
    081    private co.aikar.commands.contexts.OnlineProxiedPlayer getOnlinePlayer(BungeeCommandExecutionContext c) throws InvalidCommandArgument {
    082        ProxiedPlayer proxiedPlayer = ACFBungeeUtil.findPlayerSmart(c.getIssuer(), c.popFirstArg());
    083        if (proxiedPlayer == null) {
    084            if (c.hasAnnotation(Optional.class)) {
    085                return null;
    086            }
    087            throw new InvalidCommandArgument(false);
    088        }
    089        return new co.aikar.commands.contexts.OnlineProxiedPlayer(proxiedPlayer);
    090    }
    091}