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.velocity.contexts.OnlinePlayer; 027import com.velocitypowered.api.command.CommandSource; 028import com.velocitypowered.api.proxy.Player; 029import com.velocitypowered.api.proxy.ProxyServer; 030import net.kyori.adventure.text.format.NamedTextColor; 031import net.kyori.adventure.text.format.TextDecoration; 032import net.kyori.adventure.text.format.TextFormat; 033 034import net.kyori.adventure.text.format.TextColor; 035import org.jetbrains.annotations.Nullable; 036 037import java.util.stream.Collectors; 038import java.util.stream.Stream; 039 040public class VelocityCommandContexts extends CommandContexts<VelocityCommandExecutionContext> { 041 042 VelocityCommandContexts(ProxyServer server, CommandManager manager) { 043 super(manager); 044 registerContext(OnlinePlayer.class, (c) -> getOnlinePlayer(server, c)); 045 registerContext(co.aikar.commands.contexts.OnlinePlayer.class, c -> { 046 OnlinePlayer onlinePlayer = getOnlinePlayer(server, c); 047 return onlinePlayer != null ? new co.aikar.commands.contexts.OnlinePlayer(onlinePlayer.getPlayer()) : null; 048 }); 049 registerIssuerAwareContext(CommandSource.class, VelocityCommandExecutionContext::getSender); 050 registerIssuerAwareContext(Player.class, (c) -> { 051 Player proxiedPlayer = c.getSender() instanceof Player ? (Player) c.getSender() : null; 052 if (proxiedPlayer == null && !c.isOptional()) { 053 throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false); 054 } 055 return proxiedPlayer; 056 }); 057 058 registerContext(TextFormat.class, c -> { 059 String first = c.popFirstArg(); 060 Stream<TextFormat> colors = NamedTextColor.NAMES.values().stream().map(namedTextColor -> namedTextColor); 061 if (!c.hasFlag("colorsonly")) { 062 colors = Stream.concat(colors, Stream.of(TextDecoration.values())); 063 } 064 String filter = c.getFlagValue("filter", (String) null); 065 if (filter != null) { 066 filter = ACFUtil.simplifyString(filter); 067 String finalFilter = filter; 068 colors = colors.filter(color -> finalFilter.equals(ACFUtil.simplifyString(color.toString()))); 069 } 070 071 TextColor match = NamedTextColor.NAMES.value(ACFUtil.simplifyString(first)); 072 if (match == null) { 073 String valid = colors.map(color -> "<c2>" + ACFUtil.simplifyString(color.toString()) + "</c2>") 074 .collect(Collectors.joining("<c1>,</c1> ")); 075 076 throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", valid); 077 } 078 return match; 079 }); 080 } 081 082 @Nullable 083 private OnlinePlayer getOnlinePlayer(ProxyServer server, VelocityCommandExecutionContext c) throws InvalidCommandArgument { 084 Player proxiedPlayer = ACFVelocityUtil.findPlayerSmart(server, c.getIssuer(), c.popFirstArg()); 085 if (proxiedPlayer == null) { 086 if (c.isOptional()) { 087 return null; 088 } 089 throw new InvalidCommandArgument(false); 090 } 091 return new OnlinePlayer(proxiedPlayer); 092 } 093}