@Override public Double nextDouble() throws ParameterException { try { return Double.parseDouble(next()); } catch (NumberFormatException e) { throw new ParameterException( "Expected a number, got '" + context.getString(index - 1) + "'"); } }
String name = parameter.getName(); throw new InvalidUsageException("For parameter '" + name + "': " + e.getMessage(), this); } catch (InvocationTargetException e) { if (e.getCause() instanceof CommandException) {
@Override public Integer nextInt() throws ParameterException { try { return Integer.parseInt(next()); } catch (NumberFormatException e) { throw new ParameterException( "Expected a number, got '" + context.getString(index - 1) + "'"); } }
@Override public Integer nextInt() throws ParameterException { try { return Integer.parseInt(next()); } catch (NumberFormatException e) { throw new ParameterException( "Expected a number, got '" + context.getString(index - 1) + "'"); } }
@Override public Double nextDouble() throws ParameterException { try { return Double.parseDouble(next()); } catch (NumberFormatException e) { throw new ParameterException( "Expected a number, got '" + context.getString(index - 1) + "'"); } }
/** * Try to parse numeric input as either a number or a mathematical expression. * * @param input input * @return a number * @throws ParameterException thrown on parse error */ private @Nullable Double parseNumericInput(@Nullable String input) throws ParameterException { if (input == null) { return null; } try { return Double.parseDouble(input); } catch (NumberFormatException e1) { try { Expression expression = Expression.compile(input); return expression.evaluate(); } catch (EvaluationException e) { throw new ParameterException(String.format( "Expected '%s' to be a valid number (or a valid mathematical expression)", input)); } catch (ExpressionException e) { throw new ParameterException(String.format( "Expected '%s' to be a number or valid math expression (error: %s)", input, e.getMessage())); } } }
/** * Validate a number value using relevant modifiers. * * @param number the number * @param modifiers the list of modifiers to scan * @throws ParameterException on a validation error */ private static void validate(int number, Annotation[] modifiers) throws ParameterException { for (Annotation modifier : modifiers) { if (modifier instanceof Range) { Range range = (Range) modifier; if (number < range.min()) { throw new ParameterException( String.format( "A valid value is greater than or equal to %s " + "(you entered %s)", range.min(), number)); } else if (number > range.max()) { throw new ParameterException( String.format( "A valid value is less than or equal to %s " + "(you entered %s)", range.max(), number)); } } } }
/** * Validate a number value using relevant modifiers. * * @param number the number * @param modifiers the list of modifiers to scan * @throws ParameterException on a validation error */ private static void validate(double number, Annotation[] modifiers) throws ParameterException { for (Annotation modifier : modifiers) { if (modifier instanceof Range) { Range range = (Range) modifier; if (number < range.min()) { throw new ParameterException( String.format( "A valid value is greater than or equal to %s " + "(you entered %s)", range.min(), number)); } else if (number > range.max()) { throw new ParameterException( String.format( "A valid value is less than or equal to %s " + "(you entered %s)", range.max(), number)); } } } }
/** * Gets an {@link Player} from a {@link ArgumentStack}. * * @param context the context * @return a local player * @throws ParameterException on error */ @BindingMatch(type = Player.class, behavior = BindingBehavior.PROVIDES) public Player getPlayer(ArgumentStack context) throws ParameterException { Actor sender = context.getContext().getLocals().get(Actor.class); if (sender == null) { throw new ParameterException("No player to get a session for"); } else if (sender instanceof Player) { return (Player) sender; } else { throw new ParameterException("Caller is not a player"); } }
/** * Validate a string value using relevant modifiers. * * @param string the string * @param modifiers the list of modifiers to scan * @throws ParameterException on a validation error */ private static void validate(String string, Annotation[] modifiers) throws ParameterException { if (string == null) { return; } for (Annotation modifier : modifiers) { if (modifier instanceof Validate) { Validate validate = (Validate) modifier; if (!validate.regex().isEmpty()) { if (!string.matches(validate.regex())) { throw new ParameterException( String.format( "The given text doesn't match the right " + "format (technically speaking, the 'format' is %s)", validate.regex())); } } } } }
/** * Gets an {@link TreeType} from a {@link ArgumentStack}. * * @param context the context * @return a pattern * @throws ParameterException on error * @throws WorldEditException on error */ @BindingMatch(type = TreeType.class, behavior = BindingBehavior.CONSUMES, consumedCount = 1) public TreeType getTreeType(ArgumentStack context) throws ParameterException, WorldEditException { String input = context.next(); if (input != null) { TreeType type = TreeGenerator.lookup(input); if (type != null) { return type; } else { throw new ParameterException( String.format("Can't recognize tree type '%s' -- choose from: %s", input, Arrays.toString(TreeType.values()))); } } else { return TreeType.TREE; } }
/** * Gets an {@link Actor} from a {@link ArgumentStack}. * * @param context the context * @return a local player * @throws ParameterException on error */ @BindingMatch(type = Actor.class, behavior = BindingBehavior.PROVIDES) public Actor getActor(ArgumentStack context) throws ParameterException { Actor sender = context.getContext().getLocals().get(Actor.class); if (sender == null) { throw new ParameterException("Missing 'Actor'"); } else { return sender; } }
/** * Gets an {@link BaseBiome} from a {@link ArgumentStack}. * * @param context the context * @return a pattern * @throws ParameterException on error * @throws WorldEditException on error */ @BindingMatch(type = BaseBiome.class, behavior = BindingBehavior.CONSUMES, consumedCount = 1) public BaseBiome getBiomeType(ArgumentStack context) throws ParameterException, WorldEditException { String input = context.next(); if (input != null) { BiomeRegistry biomeRegistry = WorldEdit.getInstance().getPlatformManager() .queryCapability(Capability.GAME_HOOKS).getRegistries().getBiomeRegistry(); List<BaseBiome> knownBiomes = biomeRegistry.getBiomes(); BaseBiome biome = Biomes.findBiomeByName(knownBiomes, input, biomeRegistry); if (biome != null) { return biome; } else { throw new ParameterException( String.format("Can't recognize biome type '%s' -- use /biomelist to list available types", input)); } } else { throw new ParameterException( "This command takes a 'default' biome if one is not set, except there is no particular " + "biome that should be 'default', so the command should not be taking a default biome"); } }
/** * Gets an {@link BaseBlock} from a {@link ArgumentStack}. * * @param context the context * @return a pattern * @throws ParameterException on error * @throws WorldEditException on error */ @BindingMatch(type = {BaseBlock.class, BlockState.class, BlockStateHolder.class}, behavior = BindingBehavior.CONSUMES, consumedCount = 1) public BaseBlock getBaseBlock(ArgumentStack context) throws ParameterException, WorldEditException { Actor actor = context.getContext().getLocals().get(Actor.class); ParserContext parserContext = new ParserContext(); parserContext.setActor(context.getContext().getLocals().get(Actor.class)); if (actor instanceof Entity) { Extent extent = ((Entity) actor).getExtent(); if (extent instanceof World) { parserContext.setWorld((World) extent); } } parserContext.setSession(worldEdit.getSessionManager().get(actor)); try { return worldEdit.getBlockFactory().parseFromInput(context.next(), parserContext); } catch (NoMatchException e) { throw new ParameterException(e.getMessage(), e); } }
/** * Gets an {@link Pattern} from a {@link ArgumentStack}. * * @param context the context * @return a pattern * @throws ParameterException on error * @throws WorldEditException on error */ @BindingMatch(type = Pattern.class, behavior = BindingBehavior.CONSUMES, consumedCount = 1) public Pattern getPattern(ArgumentStack context) throws ParameterException, WorldEditException { Actor actor = context.getContext().getLocals().get(Actor.class); ParserContext parserContext = new ParserContext(); parserContext.setActor(context.getContext().getLocals().get(Actor.class)); if (actor instanceof Entity) { Extent extent = ((Entity) actor).getExtent(); if (extent instanceof World) { parserContext.setWorld((World) extent); } } parserContext.setSession(worldEdit.getSessionManager().get(actor)); try { return worldEdit.getPatternFactory().parseFromInput(context.next(), parserContext); } catch (NoMatchException e) { throw new ParameterException(e.getMessage(), e); } }
/** * Gets an {@link Mask} from a {@link ArgumentStack}. * * @param context the context * @return a pattern * @throws ParameterException on error * @throws WorldEditException on error */ @BindingMatch(type = Mask.class, behavior = BindingBehavior.CONSUMES, consumedCount = 1) public Mask getMask(ArgumentStack context) throws ParameterException, WorldEditException { Actor actor = context.getContext().getLocals().get(Actor.class); ParserContext parserContext = new ParserContext(); parserContext.setActor(context.getContext().getLocals().get(Actor.class)); if (actor instanceof Entity) { Extent extent = ((Entity) actor).getExtent(); if (extent instanceof World) { parserContext.setWorld((World) extent); } } parserContext.setSession(worldEdit.getSessionManager().get(actor)); try { return worldEdit.getMaskFactory().parseFromInput(context.next(), parserContext); } catch (NoMatchException e) { throw new ParameterException(e.getMessage(), e); } }