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