Invoke a command handler with the given arguments. @param player The player invoking the command or null for the server console. @param rawMessage The messaged used to invoke the command.
(Player player, Player targetPlayer, String rawMessage)
| 194 | * @param rawMessage The messaged used to invoke the command. |
| 195 | */ |
| 196 | public void invoke(Player player, Player targetPlayer, String rawMessage) { |
| 197 | rawMessage = rawMessage.trim(); |
| 198 | if (rawMessage.length() == 0) { |
| 199 | CommandHandler.sendTranslatedMessage(player, "commands.generic.not_specified"); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | // Parse message. |
| 204 | String[] split = rawMessage.split(" "); |
| 205 | List<String> args = new LinkedList<>(Arrays.asList(split)); |
| 206 | String label = args.remove(0); |
| 207 | String playerId = (player == null) ? consoleId : player.getAccount().getId(); |
| 208 | |
| 209 | // Check for special cases - currently only target command. |
| 210 | if (label.startsWith("@")) { // @[UID] |
| 211 | this.setPlayerTarget(playerId, player, label.substring(1)); |
| 212 | return; |
| 213 | } else if (label.equalsIgnoreCase("target")) { // target [[@]UID] |
| 214 | if (args.size() > 0) { |
| 215 | String targetUidStr = args.get(0); |
| 216 | if (targetUidStr.startsWith("@")) { |
| 217 | targetUidStr = targetUidStr.substring(1); |
| 218 | } |
| 219 | this.setPlayerTarget(playerId, player, targetUidStr); |
| 220 | return; |
| 221 | } else { |
| 222 | this.setPlayerTarget(playerId, player, ""); |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Get command handler. |
| 228 | CommandHandler handler = this.getHandler(label); |
| 229 | |
| 230 | // Check if the handler is null. |
| 231 | if (handler == null) { |
| 232 | CommandHandler.sendTranslatedMessage(player, "commands.generic.unknown_command", label); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | // Get the command's annotation. |
| 237 | Command annotation = this.annotations.get(label); |
| 238 | |
| 239 | // Resolve targetPlayer |
| 240 | try{ |
| 241 | targetPlayer = getTargetPlayer(playerId, player, targetPlayer, args); |
| 242 | } catch (IllegalArgumentException e) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | // Check for permissions. |
| 247 | if (!Grasscutter.getPermissionHandler().checkPermission(player, targetPlayer, annotation.permission(), this.annotations.get(label).permissionTargeted())) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | // Check if command has unfulfilled constraints on targetPlayer |
| 252 | Command.TargetRequirement targetRequirement = annotation.targetRequirement(); |
| 253 | if (targetRequirement != Command.TargetRequirement.NONE) { |
no test coverage detected