/** * Get the unconsumed arguments left over, without touching the stack. * * @return the unconsumed arguments */ public String getUnconsumed() { if (index >= context.argsLength()) { return null; } return context.getJoinedStrings(index); }
@Override public String remaining() throws ParameterException { try { String value = context.getJoinedStrings(index); index = context.argsLength(); return value; } catch (IndexOutOfBoundsException e) { throw new MissingParameterException(); } }
builder.append(" ").append(context.getJoinedStrings(0));
@Test public void testExactJoinedStrings() { try { CommandContext context = new CommandContext("r -f \"hello world\" foo bar"); assertTrue(context.hasFlag('f')); assertEquals("\"hello world\" foo bar", context.getJoinedStrings(0)); assertEquals("foo bar", context.getJoinedStrings(1)); CommandContext context2 = new CommandContext("pm name \"hello world\" foo bar"); assertEquals("\"hello world\" foo bar", context2.getJoinedStrings(1)); } catch (CommandException e) { log.log(Level.WARNING, "Error", e); fail("Error creating CommandContext"); } }
Calendar date = session.detectDate(args.getJoinedStrings(0));
Calendar date = session.detectDate(args.getJoinedStrings(0));
@Test public void testBasicArgs() { String command = firstCommand.getCommand(); String argOne = firstCommand.getString(0); String joinedArg = firstCommand.getJoinedStrings(0); assertEquals("herpderp", command); assertEquals("another thing", argOne); assertEquals("'another thing' because something", joinedArg); }
public String getJoinedStrings(int initialIndex, String def) { return initialIndex < originalArgIndices.size() ? getJoinedStrings(initialIndex) : def; }
public Optional<String> tryJoinedStrings(int initialIndex) { return initialIndex < originalArgIndices.size() ? Optional.of(getJoinedStrings(initialIndex)) : Optional.empty(); }
/** * Return the rest of the command line, starting at the given argument index. * * Any flags that appear after the given index are included in the result. * * @throws CommandException if the argument is missing */ public String joinedStrings(int initialIndex) throws CommandException { if(initialIndex >= originalArgIndices.size()) { throw new CommandUsageException("Missing argument"); } return getJoinedStrings(initialIndex); }
@Command(aliases = {"broadcast"}, usage = "<message...>", desc = "Broadcast a message", min = 1, max = -1) @CommandPermissions({"commandbook.broadcast"}) public void broadcast(CommandContext args, CommandSender sender) throws CommandException { BasePlugin.server().broadcastMessage( replaceColorMacros(config.broadcastFormat).replace( "%s", args.getJoinedStrings(0))); } }
@Command(aliases = {"msg", "message", "whisper", "pm", "tell", "w"}, usage = "<target> <message...>", desc = "Private message a user", min = 2, max = -1) @CommandPermissions({"commandbook.msg"}) public void msg(CommandContext args, CommandSender sender) throws CommandException { // This will throw errors as needed messagePlayer(sender, args.getString(0), args.getJoinedStrings(1)); }
@Command(aliases = {"afk", "away"}, usage = "", desc = "Set yourself as away", flags = "", min = 0, max = -1) @CommandPermissions({"commandbook.away"}) public void afk(CommandContext args, CommandSender sender) throws CommandException { Player player = PlayerUtil.checkPlayer(sender); String status = ""; if (args.argsLength() > 0) { status = args.getJoinedStrings(0); // Make sure someone isn't playing around with this if (status.equals("null")) { status = ""; } } sessions.getSession(AFKSession.class, player).setIdleStatus(status); player.sendMessage(ChatColor.YELLOW + (status.isEmpty() ? "Set as away" : "Set away status to \"" + status + "\"") + "."); } }
@Command(aliases = {"reply", "r"}, usage = "<message...>", desc = "Reply to last user", min = 1, max = -1) @CommandPermissions({"commandbook.msg"}) public void reply(CommandContext args, CommandSender sender) throws CommandException { String lastRecipient = sessions.getSession(UserSession.class, sender).getLastRecipient(); if (lastRecipient == null) { throw new CommandException("You haven't messaged anyone."); } messagePlayer(sender, lastRecipient, args.getJoinedStrings(0)); }
@Command(aliases = {"unbanip", "unipban"}, usage = "<target> [reason...]", desc = "Unban an IP address", min = 1, max = -1) @CommandPermissions({"commandbook.bans.unban.ip"}) public void unbanIP(CommandContext args, CommandSender sender) throws CommandException { String addr = args.getString(0) .replace("\r", "") .replace("\n", "") .replace("\0", "") .replace("\b", ""); String message = args.argsLength() >= 2 ? args.getJoinedStrings(1) : "Unbanned!"; if (getBanDatabase().unban(null, addr, sender, message)) { sender.sendMessage(ChatColor.YELLOW + addr + " unbanned."); if (!getBanDatabase().save()) { sender.sendMessage(ChatColor.RED + "Bans database failed to save. See console."); } } else { sender.sendMessage(ChatColor.RED + addr + " was not banned."); } }
@Command(aliases = {"cancel", "c"}, desc = "Cancel a command limitation", usage = "<cmd>", min = 1) public void cancel(CommandContext args, CommandSender sender) throws CommandException { String item = args.getJoinedStrings(0); if (remove(sender, item)) { sender.sendMessage(ChatColor.YELLOW + titleCase(getTypeName()) + " for command '" + item + "' removed."); } else { throw new CommandException("No " + getTypeName() + " for input " + item); } }
@Command(aliases = {"cancel", "c"}, desc = "Cancel a command limitation", usage = "<cmd>", min = 1) public void cancel(CommandContext args, CommandSender sender) throws CommandException { String item = args.getJoinedStrings(0); if (remove(sender, item)) { sender.sendMessage(ChatColor.YELLOW + titleCase(getTypeName()) + " for command '" + item + "' removed."); } else { throw new CommandException("No " + getTypeName() + " for input " + item); } }
@Command(aliases = {"unban"}, usage = "<target>", desc = "Unban a user", min = 1, max = -1) @CommandPermissions({"commandbook.bans.unban"}) public void unban(CommandContext args, CommandSender sender) throws CommandException { String message = args.argsLength() >= 2 ? args.getJoinedStrings(1) : "Unbanned!"; String banName = args.getString(0) .replace("\r", "") .replace("\n", "") .replace("\0", "") .replace("\b", ""); UUID ID = CommandBook.server().getOfflinePlayer(banName).getUniqueId(); if (getBanDatabase().unban(ID, null, sender, message) || getBanDatabase().unbanName(banName, sender, message)) { sender.sendMessage(ChatColor.YELLOW + banName + " unbanned."); if (!getBanDatabase().save()) { sender.sendMessage(ChatColor.RED + "Bans database failed to save. See console."); } } else { sender.sendMessage(ChatColor.RED + banName + " was not banned."); } }
@Command(aliases = {"me"}, usage = "<message...>", desc = "Send an action message", min = 1, max = -1) @CommandPermissions({"commandbook.say.me"}) public void me(CommandContext args, CommandSender sender) throws CommandException { if (sender instanceof Player && sessions.getSession(AdministrativeSession.class, sender).isMute()) { sender.sendMessage(ChatColor.RED + "You are muted."); return; } String name = ChatUtil.toColoredName(sender, ChatColor.YELLOW); String msg = args.getJoinedStrings(0); BasePlugin.callEvent( new SharedMessageEvent(name + " " + msg)); BasePlugin.server().broadcastMessage("* " + name + " " + msg); }
@Command(aliases = {"kick"}, usage = "<target> [reason...]", desc = "Kick a user", flags = "os", min = 1, max = -1) @CommandPermissions({"commandbook.kick"}) public void kick(CommandContext args, CommandSender sender) throws CommandException { Iterable<Player> targets = InputUtil.PlayerParser.matchPlayers(sender, args.getString(0)); String message = args.argsLength() >= 2 ? args.getJoinedStrings(1) : "Kicked!"; String broadcastPlayers = ""; for (Player player : targets) { if (CommandBook.inst().hasPermission(player, "commandbook.kick.exempt") && !(args.hasFlag('o') && CommandBook.inst().hasPermission(sender, "commandbook.kick.exempt.override"))) { sender.sendMessage(ChatColor.RED + "Player " + player.getName() + ChatColor.RED + " is exempt from being kicked!"); continue; } player.kickPlayer(message); broadcastPlayers += ChatUtil.toColoredName(player, ChatColor.YELLOW) + " "; getBanDatabase().logKick(player, sender, message); } if (broadcastPlayers.length() > 0) { sender.sendMessage(ChatColor.YELLOW + "Player(s) kicked."); //Broadcast the Message if (config.broadcastKicks && !args.hasFlag('s')) { BasePlugin.server().broadcastMessage(ChatColor.YELLOW + ChatUtil.toColoredName(sender, ChatColor.YELLOW) + " has kicked " + broadcastPlayers + " - " + message); } } }