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 = null; 069 String simplified = ACFUtil.simplifyString(first); 070 for (ChatColor chatColor : ChatColor.values()) { 071 String simple = ACFUtil.simplifyString(chatColor.name()); 072 if (simplified.equals(simple)) { 073 match = chatColor; 074 break; 075 } 076 } 077 if (match == null) { 078 String valid = colors 079 .map(color -> "<c2>" + ACFUtil.simplifyString(color.name()) + "</c2>") 080 .collect(Collectors.joining("<c1>,</c1> ")); 081 082 throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", valid); 083 } 084 return match; 085 }); 086 } 087 088 @Nullable 089 private co.aikar.commands.contexts.OnlineProxiedPlayer getOnlinePlayer(BungeeCommandExecutionContext c) throws InvalidCommandArgument { 090 ProxiedPlayer proxiedPlayer = ACFBungeeUtil.findPlayerSmart(c.getIssuer(), c.popFirstArg()); 091 if (proxiedPlayer == null) { 092 if (c.hasAnnotation(Optional.class)) { 093 return null; 094 } 095 throw new InvalidCommandArgument(false); 096 } 097 return new co.aikar.commands.contexts.OnlineProxiedPlayer(proxiedPlayer); 098 } 099}