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 return 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 }); 068 registerContext(TextStyle.Base.class, c -> { 069 String first = c.popFirstArg(); 070 Stream<TextStyle.Base> styles = Sponge.getRegistry().getAllOf(TextStyle.Base.class).stream(); 071 String filter = c.getFlagValue("filter", (String)null); 072 if(filter != null) { 073 filter = ACFUtil.simplifyString(filter); 074 String finalFilter = filter; 075 styles = styles.filter(style -> finalFilter.equals(ACFUtil.simplifyString(style.getName()))); 076 } 077 Stream<TextStyle.Base> finalStyles = styles; 078 return Sponge.getRegistry().getType(TextStyle.Base.class, ACFUtil.simplifyString(first)).orElseThrow(() -> { 079 String valid = finalStyles 080 .map(style -> "<c2>" + ACFUtil.simplifyString(style.getName()) + "</c2>") 081 .collect(Collectors.joining("<c1>,</c1> ")); 082 return new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", valid); 083 }); 084 }); 085 086 registerIssuerAwareContext(CommandSource.class, SpongeCommandExecutionContext::getSource); 087 registerIssuerAwareContext(Player.class, (c) -> { 088 Player player = c.getSource() instanceof Player ? (Player) c.getSource() : null; 089 if (player == null && !c.hasAnnotation(Optional.class)) { 090 throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false); 091 } 092 /*PlayerInventory inventory = player != null ? player.getInventory() : null; 093 if (inventory != null && c.hasFlag("itemheld") && !ACFBukkitUtil.isValidItem(inventory.getItem(inventory.getHeldItemSlot()))) { 094 throw new InvalidCommandArgument(MinecraftMessageKeys.YOU_MUST_BE_HOLDING_ITEM, false); 095 }*/ 096 return player; 097 }); 098 registerContext(OnlinePlayer[].class, (c) -> { 099 SpongeCommandIssuer issuer = c.getIssuer(); 100 final String search = c.popFirstArg(); 101 boolean allowMissing = c.hasFlag("allowmissing"); 102 Set<OnlinePlayer> players = new HashSet<>(); 103 Pattern split = ACFPatterns.COMMA; 104 String splitter = c.getFlagValue("splitter", (String) null); 105 if (splitter != null) { 106 split = Pattern.compile(Pattern.quote(splitter)); 107 } 108 for (String lookup : split.split(search)) { 109 OnlinePlayer player = getOnlinePlayer(issuer, lookup, allowMissing); 110 if (player != null) { 111 players.add(player); 112 } 113 } 114 if (players.isEmpty() && !c.hasFlag("allowempty")) { 115 issuer.sendError(MinecraftMessageKeys.NO_PLAYER_FOUND_SERVER, 116 "{search}", search); 117 118 throw new InvalidCommandArgument(false); 119 } 120 return players.toArray(new OnlinePlayer[players.size()]); 121 }); 122 registerIssuerAwareContext(World.class, (c) -> { 123 String firstArg = c.getFirstArg(); 124 java.util.Optional<World> world = firstArg != null ? Sponge.getServer().getWorld(firstArg) : java.util.Optional.empty(); 125 if (world.isPresent()) { 126 c.popFirstArg(); 127 } 128 if (!world.isPresent() && c.getSource() instanceof Player) { 129 world = java.util.Optional.of(((Player) c.getSource()).getWorld()); 130 } 131 if (!world.isPresent()) { 132 throw new InvalidCommandArgument(MinecraftMessageKeys.INVALID_WORLD); 133 } 134 return world.get(); 135 }); 136 } 137 138 @Nullable 139 OnlinePlayer getOnlinePlayer(SpongeCommandIssuer issuer, String lookup, boolean allowMissing) throws InvalidCommandArgument { 140 Player player = ACFSpongeUtil.findPlayerSmart(issuer, lookup); 141 //noinspection Duplicates 142 if (player == null) { 143 if (allowMissing) { 144 return null; 145 } 146 throw new InvalidCommandArgument(false); 147 } 148 return new OnlinePlayer(player); 149 } 150}