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