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