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.bukkit.contexts.OnlinePlayer; 027import org.bukkit.Bukkit; 028import org.bukkit.ChatColor; 029import org.bukkit.Location; 030import org.bukkit.OfflinePlayer; 031import org.bukkit.World; 032import org.bukkit.command.BlockCommandSender; 033import org.bukkit.command.CommandSender; 034import org.bukkit.entity.Entity; 035import org.bukkit.entity.Player; 036import org.bukkit.inventory.PlayerInventory; 037import org.jetbrains.annotations.Nullable; 038 039import java.util.HashSet; 040import java.util.Set; 041import java.util.UUID; 042import java.util.regex.Pattern; 043import java.util.stream.Collectors; 044import java.util.stream.Stream; 045 046@SuppressWarnings("WeakerAccess") 047public class BukkitCommandContexts extends CommandContexts<BukkitCommandExecutionContext> { 048 049 public BukkitCommandContexts(BukkitCommandManager manager) { 050 super(manager); 051 052 registerContext(OnlinePlayer.class, c -> getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.isOptional())); 053 registerContext(co.aikar.commands.contexts.OnlinePlayer.class, c -> { 054 OnlinePlayer onlinePlayer = getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.isOptional()); 055 return onlinePlayer != null ? new co.aikar.commands.contexts.OnlinePlayer(onlinePlayer.getPlayer()) : null; 056 }); 057 registerContext(OnlinePlayer[].class, (c) -> { 058 BukkitCommandIssuer issuer = c.getIssuer(); 059 final String search = c.popFirstArg(); 060 boolean allowMissing = c.hasFlag("allowmissing"); 061 Set<OnlinePlayer> players = new HashSet<>(); 062 Pattern split = ACFPatterns.COMMA; 063 String splitter = c.getFlagValue("splitter", (String) null); 064 if (splitter != null) { 065 split = Pattern.compile(Pattern.quote(splitter)); 066 } 067 for (String lookup : split.split(search)) { 068 OnlinePlayer player = getOnlinePlayer(issuer, lookup, allowMissing); 069 if (player != null) { 070 players.add(player); 071 } 072 } 073 if (players.isEmpty() && !c.hasFlag("allowempty")) { 074 issuer.sendError(MinecraftMessageKeys.NO_PLAYER_FOUND_SERVER, 075 "{search}", search); 076 077 throw new InvalidCommandArgument(false); 078 } 079 return players.toArray(new OnlinePlayer[players.size()]); 080 }); 081 registerIssuerAwareContext(World.class, (c) -> { 082 String firstArg = c.getFirstArg(); 083 World world = firstArg != null ? Bukkit.getWorld(firstArg) : null; 084 if (world != null) { 085 c.popFirstArg(); 086 } 087 if (world == null && c.getSender() instanceof Player) { 088 world = ((Entity) c.getSender()).getWorld(); 089 } 090 if (world == null) { 091 throw new InvalidCommandArgument(MinecraftMessageKeys.INVALID_WORLD); 092 } 093 return world; 094 }); 095 registerIssuerAwareContext(CommandSender.class, BukkitCommandExecutionContext::getSender); 096 registerIssuerAwareContext(Player.class, (c) -> { 097 boolean isOptional = c.isOptional(); 098 CommandSender sender = c.getSender(); 099 boolean isPlayerSender = sender instanceof Player; 100 if (!c.hasFlag("other")) { 101 Player player = isPlayerSender ? (Player) sender : null; 102 if (player == null && !isOptional) { 103 throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false); 104 } 105 PlayerInventory inventory = player != null ? player.getInventory() : null; 106 if (inventory != null && c.hasFlag("itemheld") && !ACFBukkitUtil.isValidItem(inventory.getItem(inventory.getHeldItemSlot()))) { 107 throw new InvalidCommandArgument(MinecraftMessageKeys.YOU_MUST_BE_HOLDING_ITEM, false); 108 } 109 return player; 110 } else { 111 String arg = c.popFirstArg(); 112 if (arg == null && isOptional) { 113 if (c.hasFlag("defaultself")) { 114 if (isPlayerSender) { 115 return (Player) sender; 116 } else { 117 throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false); 118 } 119 } else { 120 return null; 121 } 122 } else if (arg == null) { 123 throw new InvalidCommandArgument(); 124 } 125 126 OnlinePlayer onlinePlayer = getOnlinePlayer(c.getIssuer(), arg, isOptional); 127 return onlinePlayer != null ? onlinePlayer.getPlayer() : null; 128 } 129 }); 130 registerContext(OfflinePlayer.class, c -> { 131 String name = c.popFirstArg(); 132 UUID uuid = null; 133 if (c.hasFlag("uuid")) { 134 uuid = UUID.fromString(name); 135 } 136 OfflinePlayer offlinePlayer = uuid != null ? Bukkit.getOfflinePlayer(uuid) : Bukkit.getOfflinePlayer(name); 137 if (offlinePlayer == null || (!offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline())) { 138 throw new InvalidCommandArgument(MinecraftMessageKeys.NO_PLAYER_FOUND_OFFLINE, 139 "{search}", name); 140 } 141 return offlinePlayer; 142 }); 143 registerContext(ChatColor.class, c -> { 144 String first = c.popFirstArg(); 145 Stream<ChatColor> colors = Stream.of(ChatColor.values()); 146 if (c.hasFlag("colorsonly")) { 147 colors = colors.filter(color -> color.ordinal() <= 0xF); 148 } 149 String filter = c.getFlagValue("filter", (String) null); 150 if (filter != null) { 151 filter = ACFUtil.simplifyString(filter); 152 String finalFilter = filter; 153 colors = colors.filter(color -> finalFilter.equals(ACFUtil.simplifyString(color.name()))); 154 } 155 156 ChatColor match = ACFUtil.simpleMatch(ChatColor.class, first); 157 if (match == null) { 158 String valid = colors 159 .map(color -> "<c2>" + ACFUtil.simplifyString(color.name()) + "</c2>") 160 .collect(Collectors.joining("<c1>,</c1> ")); 161 162 throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", valid); 163 } 164 return match; 165 }); 166 registerContext(Location.class, c -> { 167 String input = c.popFirstArg(); 168 CommandSender sender = c.getSender(); 169 String[] split = ACFPatterns.COLON.split(input, 2); 170 if (split.length == 0) { 171 throw new InvalidCommandArgument(true); 172 } 173 if (split.length < 2 && !(sender instanceof Player) && !(sender instanceof BlockCommandSender)) { 174 throw new InvalidCommandArgument(MinecraftMessageKeys.LOCATION_PLEASE_SPECIFY_WORLD); 175 } 176 final String world; 177 final String rest; 178 Location sourceLoc = null; 179 if (split.length == 2) { 180 world = split[0]; 181 rest = split[1]; 182 } else if (sender instanceof Player) { 183 sourceLoc = ((Player) sender).getLocation(); 184 world = sourceLoc.getWorld().getName(); 185 rest = split[0]; 186 } else if (sender instanceof BlockCommandSender) { 187 sourceLoc = ((BlockCommandSender) sender).getBlock().getLocation(); 188 world = sourceLoc.getWorld().getName(); 189 rest = split[0]; 190 } else { 191 throw new InvalidCommandArgument(true); 192 } 193 194 boolean rel = rest.startsWith("~"); 195 split = ACFPatterns.COMMA.split(rel ? rest.substring(1) : rest); 196 if (split.length < 3) { 197 throw new InvalidCommandArgument(MinecraftMessageKeys.LOCATION_PLEASE_SPECIFY_XYZ); 198 } 199 200 Double x = ACFUtil.parseDouble(split[0]); 201 Double y = ACFUtil.parseDouble(split[1]); 202 Double z = ACFUtil.parseDouble(split[2]); 203 204 if (sourceLoc != null && rel) { 205 x += sourceLoc.getX(); 206 y += sourceLoc.getY(); 207 z += sourceLoc.getZ(); 208 } else if (rel) { 209 throw new InvalidCommandArgument(MinecraftMessageKeys.LOCATION_CONSOLE_NOT_RELATIVE); 210 } 211 212 if (x == null || y == null || z == null) { 213 throw new InvalidCommandArgument(MinecraftMessageKeys.LOCATION_PLEASE_SPECIFY_XYZ); 214 } 215 216 World worldObj = Bukkit.getWorld(world); 217 if (worldObj == null) { 218 throw new InvalidCommandArgument(MinecraftMessageKeys.INVALID_WORLD); 219 } 220 221 if (split.length >= 5) { 222 Float yaw = ACFUtil.parseFloat(split[3]); 223 Float pitch = ACFUtil.parseFloat(split[4]); 224 225 if (pitch == null || yaw == null) { 226 throw new InvalidCommandArgument(MinecraftMessageKeys.LOCATION_PLEASE_SPECIFY_XYZ); 227 } 228 return new Location(worldObj, x, y, z, yaw, pitch); 229 } else { 230 return new Location(worldObj, x, y, z); 231 } 232 }); 233 234 if (manager.mcMinorVersion >= 12) { 235 BukkitCommandContexts_1_12.register(this); 236 } 237 } 238 239 @Nullable 240 OnlinePlayer getOnlinePlayer(BukkitCommandIssuer issuer, String lookup, boolean allowMissing) throws InvalidCommandArgument { 241 Player player = ACFBukkitUtil.findPlayerSmart(issuer, lookup); 242 //noinspection Duplicates 243 if (player == null) { 244 if (allowMissing) { 245 return null; 246 } 247 throw new InvalidCommandArgument(false); 248 } 249 return new OnlinePlayer(player); 250 } 251}