/** * 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 double parseRelativeDouble(CommandArgs args, String arg, @Nullable Double relativeTo) throws ArgumentParseException { boolean relative = arg.startsWith("~"); if (relative) { if (relativeTo == null) { throw args.createError(t("Relative position specified but source does not have a position")); } arg = arg.substring(1); if (arg.isEmpty()) { return relativeTo; } } try { double ret = Double.parseDouble(arg); return relative ? ret + relativeTo : ret; } catch (NumberFormatException e) { throw args.createError(t("Expected input %s to be a double, but was not", arg)); } } }
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 boolean parseShortFlags(CommandSource source, String shortFlags, CommandArgs args, CommandContext context) throws ArgumentParseException { for (int i = 0; i < shortFlags.length(); i++) { String shortFlag = shortFlags.substring(i, i + 1); CommandElement element = this.shortFlags.get(shortFlag); if (element == null) { switch (this.unknownShortFlagBehavior) { case IGNORE: if (i == 0) { return false; } throw args.createError(t("Unknown short flag %s specified", shortFlag)); case ERROR: throw args.createError(t("Unknown short flag %s specified", shortFlag)); case ACCEPT_NONVALUE: context.putArg(shortFlag, true); break; case ACCEPT_VALUE: context.putArg(shortFlag, args.next()); break; default: throw new Error("New UnknownFlagBehavior added without corresponding case clauses"); } } else { element.parse(source, args, context); } } return true; }
private boolean parseLongFlag(CommandSource source, String longFlag, CommandArgs args, CommandContext context) throws ArgumentParseException { String[] flagSplit = longFlag.split("=", 2); String flag = flagSplit[0].toLowerCase(); CommandElement element = this.longFlags.get(flag); if (element == null) { switch (this.unknownLongFlagBehavior) { case ERROR: throw args.createError(t("Unknown long flag %s specified", flagSplit[0])); case ACCEPT_NONVALUE: context.putArg(flag, flagSplit.length == 2 ? flagSplit[1] : true); return true; case ACCEPT_VALUE: context.putArg(flag, flagSplit.length == 2 ? flagSplit[1] : args.next()); return true; case IGNORE: return false; default: throw new Error("New UnknownFlagBehavior added without corresponding case clauses"); } } else if (flagSplit.length == 2) { args.insertArg(flagSplit[1]); } element.parse(source, args, context); return true; }
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!")); }