/*
 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package co.aikar.commands;

import co.aikar.commands.annotation.Optional;
import co.aikar.commands.contexts.CommandResultSupplier;
import co.aikar.commands.contexts.OnlinePlayer;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;

@SuppressWarnings("WeakerAccess")
public class SpongeCommandContexts extends CommandContexts<SpongeCommandExecutionContext> {

    public SpongeCommandContexts(final SpongeCommandManager manager) {
        super(manager);

        registerIssuerOnlyContext(CommandResultSupplier.class, c -> new CommandResultSupplier());
        registerContext(OnlinePlayer.class, c -> getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.hasAnnotation(Optional.class)));

        registerIssuerAwareContext(CommandSource.class, SpongeCommandExecutionContext::getSource);
        registerIssuerAwareContext(Player.class, (c) -> {
            Player player = c.getSource() instanceof Player ? (Player) c.getSource() : null;
            if (player == null && !c.hasAnnotation(Optional.class)) {
                throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false);
            }
            /*PlayerInventory inventory = player != null ? player.getInventory() : null;
            if (inventory != null && c.hasFlag("itemheld") && !ACFBukkitUtil.isValidItem(inventory.getItem(inventory.getHeldItemSlot()))) {
                throw new InvalidCommandArgument(MinecraftMessageKeys.YOU_MUST_BE_HOLDING_ITEM, false);
            }*/
            return player;
        });

    }

    @Nullable
    OnlinePlayer getOnlinePlayer(SpongeCommandIssuer issuer, String lookup, boolean allowMissing) throws InvalidCommandArgument {
        CommandSource sender = issuer.getIssuer();
        Player player = ACFSpongeUtil.findPlayerSmart(issuer, lookup);
        //noinspection Duplicates
        if (player == null) {
            if (allowMissing) {
                return null;
            }
            this.manager.sendMessage(sender, MessageType.ERROR, MessageKeys.COULD_NOT_FIND_PLAYER, "{search}", lookup);
            throw new InvalidCommandArgument(false);
        }
        return new OnlinePlayer(player);
    }
}

@@ -1490,16 +1490,97 @@
 layer;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0Aimport java.util.regex.Pattern;%0A%0A
 @Suppres
@@ -2840,16 +2840,1127 @@
     %7D);%0A
+        registerContext(OnlinePlayer%5B%5D.class, (c) -%3E  %7B%0A            SpongeCommandIssuer issuer = c.getIssuer();%0A            final String search = c.popFirstArg();%0A            boolean allowMissing = c.hasFlag(%22allowmissing%22);%0A            Set%3COnlinePlayer%3E players = new HashSet%3C%3E();%0A            Pattern split = ACFPatterns.COMMA;%0A            String splitter = c.getFlagValue(%22splitter%22, (String) null);%0A            if (splitter != null) %7B%0A                split = Pattern.compile(Pattern.quote(splitter));%0A            %7D%0A            for (String lookup : split.split(search)) %7B%0A                OnlinePlayer player = getOnlinePlayer(issuer, lookup, allowMissing);%0A                if (player != null) %7B%0A                    players.add(player);%0A                %7D%0A            %7D%0A            if (players.isEmpty() && !c.hasFlag(%22allowempty%22)) %7B%0A                issuer.sendError(MinecraftMessageKeys.NO_PLAYER_FOUND_SERVER,%0A                        %22%7Bsearch%7D%22, search);%0A%0A                throw new InvalidCommandArgument(false);%0A            %7D%0A            return players.toArray(new OnlinePlayer%5Bplayers.size()%5D);%0A        %7D);
 %0A    %7D%0A%0A

add OnlinePlayer[] to Sponge