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.annotation.Optional;
027import co.aikar.commands.contexts.CommandResultSupplier;
028import co.aikar.commands.contexts.OnlinePlayer;
029import org.jetbrains.annotations.Nullable;
030import org.spongepowered.api.Sponge;
031import org.spongepowered.api.command.CommandSource;
032import org.spongepowered.api.entity.living.player.Player;
033import org.spongepowered.api.text.format.TextColor;
034import org.spongepowered.api.text.format.TextStyle;
035import org.spongepowered.api.world.World;
036
037import java.util.HashSet;
038import java.util.Set;
039import java.util.regex.Pattern;
040import java.util.stream.Collectors;
041import java.util.stream.Stream;
042
043@SuppressWarnings("WeakerAccess")
044public class SpongeCommandContexts extends CommandContexts<SpongeCommandExecutionContext> {
045
046    public SpongeCommandContexts(final SpongeCommandManager manager) {
047        super(manager);
048
049        registerIssuerOnlyContext(CommandResultSupplier.class, c -> new CommandResultSupplier());
050        registerContext(OnlinePlayer.class, c -> getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.hasAnnotation(Optional.class)));
051        registerContext(TextColor.class, c -> {
052            String first = c.popFirstArg();
053            Stream<TextColor> colours = Sponge.getRegistry().getAllOf(TextColor.class).stream();
054            String filter = c.getFlagValue("filter", (String)null);
055            if(filter != null) {
056                filter = ACFUtil.simplifyString(filter);
057                String finalFilter = filter;
058                colours = colours.filter(colour -> finalFilter.equals(ACFUtil.simplifyString(colour.getName())));
059            }
060            Stream<TextColor> finalColours = colours;
061            TextColor match = Sponge.getRegistry().getType(TextColor.class, ACFUtil.simplifyString(first)).orElseThrow(() -> {
062                String valid = finalColours
063                        .map(colour -> "<c2>" + ACFUtil.simplifyString(colour.getName()) + "</c2>")
064                        .collect(Collectors.joining("<c1>,</c1> "));
065                return new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", valid);
066            });
067            return match;
068        });
069        registerContext(TextStyle.Base.class, c -> {
070            String first = c.popFirstArg();
071            Stream<TextStyle.Base> styles = Sponge.getRegistry().getAllOf(TextStyle.Base.class).stream();
072            String filter = c.getFlagValue("filter", (String)null);
073            if(filter != null) {
074                filter = ACFUtil.simplifyString(filter);
075                String finalFilter = filter;
076                styles = styles.filter(style -> finalFilter.equals(ACFUtil.simplifyString(style.getName())));
077            }
078            Stream<TextStyle.Base> finalStyles = styles;
079            TextStyle.Base match = Sponge.getRegistry().getType(TextStyle.Base.class, ACFUtil.simplifyString(first)).orElseThrow(() -> {
080                String valid = finalStyles
081                        .map(style -> "<c2>" + ACFUtil.simplifyString(style.getName()) + "</c2>")
082                        .collect(Collectors.joining("<c1>,</c1> "));
083                return new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", valid);
084            });
085            return match;
086        });
087
088        registerIssuerAwareContext(CommandSource.class, SpongeCommandExecutionContext::getSource);
089        registerIssuerAwareContext(Player.class, (c) -> {
090            Player player = c.getSource() instanceof Player ? (Player) c.getSource() : null;
091            if (player == null && !c.hasAnnotation(Optional.class)) {
092                throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false);
093            }
094            /*PlayerInventory inventory = player != null ? player.getInventory() : null;
095            if (inventory != null && c.hasFlag("itemheld") && !ACFBukkitUtil.isValidItem(inventory.getItem(inventory.getHeldItemSlot()))) {
096                throw new InvalidCommandArgument(MinecraftMessageKeys.YOU_MUST_BE_HOLDING_ITEM, false);
097            }*/
098            return player;
099        });
100        registerContext(OnlinePlayer[].class, (c) ->  {
101            SpongeCommandIssuer issuer = c.getIssuer();
102            final String search = c.popFirstArg();
103            boolean allowMissing = c.hasFlag("allowmissing");
104            Set<OnlinePlayer> players = new HashSet<>();
105            Pattern split = ACFPatterns.COMMA;
106            String splitter = c.getFlagValue("splitter", (String) null);
107            if (splitter != null) {
108                split = Pattern.compile(Pattern.quote(splitter));
109            }
110            for (String lookup : split.split(search)) {
111                OnlinePlayer player = getOnlinePlayer(issuer, lookup, allowMissing);
112                if (player != null) {
113                    players.add(player);
114                }
115            }
116            if (players.isEmpty() && !c.hasFlag("allowempty")) {
117                issuer.sendError(MinecraftMessageKeys.NO_PLAYER_FOUND_SERVER,
118                        "{search}", search);
119
120                throw new InvalidCommandArgument(false);
121            }
122            return players.toArray(new OnlinePlayer[players.size()]);
123        });
124        registerIssuerAwareContext(World.class, (c) -> {
125            String firstArg = c.getFirstArg();
126            java.util.Optional<World> world = firstArg != null ? Sponge.getServer().getWorld(firstArg) : java.util.Optional.empty();
127            if (world.isPresent()) {
128                c.popFirstArg();
129            }
130            if (!world.isPresent() && c.getSource() instanceof Player) {
131                world = java.util.Optional.of(((Player) c.getSource()).getWorld());
132            }
133            if (!world.isPresent()) {
134                throw new InvalidCommandArgument(MinecraftMessageKeys.INVALID_WORLD);
135            }
136            return world.get();
137        });
138    }
139
140    @Nullable
141    OnlinePlayer getOnlinePlayer(SpongeCommandIssuer issuer, String lookup, boolean allowMissing) throws InvalidCommandArgument {
142        Player player = ACFSpongeUtil.findPlayerSmart(issuer, lookup);
143        //noinspection Duplicates
144        if (player == null) {
145            if (allowMissing) {
146                return null;
147            }
148            throw new InvalidCommandArgument(false);
149        }
150        return new OnlinePlayer(player);
151    }
152}