/** * Create an exception with the default message. */ public CommandPermissionException() { this(t("You do not have permission to use this command!")); }
/** * Create an exception with the default message. * * @param command The command that was queried for */ public CommandNotFoundException(String command) { this(t("No such command"), command); }
/** * Require an argument to be a long (base 10). * * <p>Gives values of type {@link Long}. This will return only one value. * </p> * * @param key The key to store the parsed argument under * @return the element to match the input */ public static CommandElement longNum(Text key) { return new NumericElement<>(key, Long::parseLong, Long::parseLong, input -> t("Expected a long, but input '%s' was not", input)); }
/** * Require an argument to be an double-precision floating point number. * * <p>Gives values of type {@link Double}. This will return only one value. * </p> * * @param key The key to store the parsed argument under * @return the element to match the input */ public static CommandElement doubleNum(Text key) { return new NumericElement<>(key, Double::parseDouble, null, input -> t("Expected a number, but input '%s' was not", input)); }
/** * Require an argument to be an integer (base 10). * * <p>Gives values of type {@link Integer}. This will return only one value. * </p> * * @param key The key to store the parsed argument under * @return the element to match the input */ public static CommandElement integer(Text key) { return new NumericElement<>(key, Integer::parseInt, Integer::parseInt, input -> t("Expected an integer, but input '%s' was not", input)); }
@Override public Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException { Object value = this.valueSupplier.apply(args.next()); if (value == null) { throw args.createError(t("Argument was not a valid choice. Valid choices: %s", this.keySupplier.get().toString())); } return value; }
@Nullable @Override protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException { for (String arg : this.expectedArgs) { String current; if (!(current = args.next()).equalsIgnoreCase(arg)) { throw args.createError(t("Argument %s did not match expected next argument %s", current, arg)); } } return this.putValue; }
private Entity tryReturnTarget(CommandSource source, CommandArgs args) throws ArgumentParseException { Entity entity = tryReturnSource(source, args, false); return entity.getWorld().getIntersectingEntities(entity, 10).stream() .filter(e -> !e.getEntity().equals(entity)).map(EntityUniverse.EntityHit::getEntity) .filter(this::checkEntity).findFirst() .orElseThrow(() -> args.createError(t("No entities matched and source was not looking at a valid entity!"))); }
/** * Perform a permissions check, throwing an exception if the required * permissions are not present. * * @param commander the source to check against * @param permission The permission to check * @throws CommandException if the source does not have permission */ public void checkPermission(CommandSource commander, String permission) throws CommandException { if (!commander.hasPermission(permission)) { throw new CommandException(t("You do not have permission to use this command!")); } }
@Override public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { this.element.parse(source, args, context); if (context.getAll(this.element.getUntranslatedKey()).size() > 1) { Text key = this.element.getKey(); throw args.createError(t("Argument %s may have only one value!", key != null ? key : t("unknown"))); } }
/** * Try to read the next argument, advancing the current index if successful. * * @return The next argument * @throws ArgumentParseException if not enough arguments are present */ public String next() throws ArgumentParseException { if (!hasNext()) { throw createError(t("Not enough arguments!")); } return this.args.get(++this.index).getValue(); }
/** * Try to read the next argument without advancing the current index. * * @return The next argument * @throws ArgumentParseException if not enough arguments are present */ public String peek() throws ArgumentParseException { if (!hasNext()) { throw createError(t("Not enough arguments")); } return this.args.get(this.index + 1).getValue(); }
@Nullable @Override protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException { final String unformattedPattern = args.next(); Iterable<String> choices = getChoices(source); // Check for an exact match before we create the regex. // We do this because anything with ^[abc] would not match [abc] Optional<Object> exactMatch = getExactMatch(choices, unformattedPattern); if (exactMatch.isPresent()) { // Return this as a collection as this can get transformed by the subclass. return Collections.singleton(exactMatch.get()); } Pattern pattern = getFormattedPattern(unformattedPattern); Iterable<Object> ret = Iterables.transform(Iterables.filter(choices, element -> pattern.matcher(element).find()), this::getValue); if (!ret.iterator().hasNext()) { throw args.createError(t("No values matching pattern '%s' present for %s!", unformattedPattern, getKey() == null ? nullKeyArg : getKey())); } return ret; }
private Player tryReturnSource(CommandSource source, CommandArgs args) throws ArgumentParseException { if (source instanceof Player) { return ((Player) source); } else if (source instanceof ProxySource && ((ProxySource) source).getOriginalSource() instanceof Player) { return (Player) ((ProxySource) source).getOriginalSource(); } else { throw args.createError(t("No players matched and source was not a player!")); } }
private User tryReturnSource(CommandSource source, CommandArgs args) throws ArgumentParseException { if (source instanceof User) { return ((User) source); } else if (source instanceof ProxySource && ((ProxySource) source).getOriginalSource() instanceof User) { return (User) ((ProxySource) source).getOriginalSource(); } else { throw args.createError(t("No users matched and source was not a user!")); } } }
private Entity tryReturnSource(CommandSource source, CommandArgs args, boolean check) throws ArgumentParseException { if (source instanceof Entity && (!check || this.checkEntity((Entity) source))) { return (Entity) source; } if (source instanceof ProxySource) { CommandSource proxy = ((ProxySource) source).getOriginalSource(); if (proxy instanceof Entity && (!check || this.checkEntity((Entity) proxy))) { return (Entity) proxy; } } throw args.createError(t("No entities matched and source was not an entity!")); }
/** * Process this command with existing arguments and context objects. * * * @param source The source to populate the context with * @param args The arguments to process with * @param context The context to put data in * @throws ArgumentParseException if an invalid argument is provided */ public void populateContext(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { this.args.parse(source, args, context); if (args.hasNext()) { args.next(); throw args.createError(t("Too many arguments!")); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { CommandMapping mapping = args.<CommandMapping>getOne(getUntranslatedKey()).orElse(null); if (mapping == null) { if (this.fallbackExecutor == null) { throw new CommandException(t("Invalid subcommand state -- no more than one mapping may be provided for child arg %s", getKey())); } return this.fallbackExecutor.execute(src, args); } if (mapping.getCallable() instanceof CommandSpec) { CommandSpec spec = ((CommandSpec) mapping.getCallable()); spec.checkPermission(src); return spec.getExecutor().execute(src, args); } final String arguments = args.<String>getOne(getUntranslatedKey() + "_args").orElse(""); return mapping.getCallable().process(src, arguments); }
@Test public void testFlaggedCommand() throws CommandException { CommandSpec command = CommandSpec.builder() .arguments(flags() .flag("a").valueFlag(integer(t("quot")), "q").buildWith(string(t("key")))) .executor((src, args) -> { assertEquals(true, args.getOne("a").get()); assertEquals(42, args.getOne("quot").get()); assertEquals("something", args.getOne("key").get()); return CommandResult.builder().successCount(3).build(); }) .build(); process(command, "-a -q 42 something"); process(command, "-aq 42 something"); process(command, "-a something -q 42"); }