/** * Get whether this parameter consumes non-flag arguments. * * @return true if this parameter consumes non-flag arguments */ boolean isNonFlagConsumer() { return getBinding().getBehavior(this) != BindingBehavior.PROVIDES && !isValueFlag(); }
/** * Gets a {@link CommandContext} from a {@link ArgumentStack}. * * @param context the context * @return a selection */ @BindingMatch(type = CommandContext.class, behavior = BindingBehavior.PROVIDES) public CommandContext getCommandContext(ArgumentStack context) { context.markConsumed(); // Consume entire stack return context.getContext(); }
/** * Get whether this parameter is entered by the user. * * @return true if this parameter is entered by the user. */ boolean isUserInput() { return getBinding().getBehavior(this) != BindingBehavior.PROVIDES; }
/** * Get the default value for a parameter. * * @param i the index of the parameter * @param scoped the scoped context * @return a value * @throws ParameterException on an error * @throws CommandException on an error */ private Object getDefaultValue(int i, ContextArgumentStack scoped) throws ParameterException, CommandException, InvocationTargetException { CommandContext context = scoped.getContext(); ParameterData parameter = parameters[i]; String[] defaultValue = parameter.getDefaultValue(); if (defaultValue != null) { try { return parameter.getBinding().bind(parameter, new StringArgumentStack(context, defaultValue, false), false); } catch (MissingParameterException e) { throw new ParametricException( "The default value of the parameter using the binding " + parameter.getBinding().getClass() + " in the method\n" + method.toGenericString() + "\nis invalid"); } } return null; }
/** * Check to see if all arguments, including flag arguments, were consumed. * * @param scoped the argument scope * @throws UnconsumedParameterException thrown if parameters were not consumed */ private void checkUnconsumed(ContextArgumentStack scoped) throws UnconsumedParameterException { CommandContext context = scoped.getContext(); String unconsumed; String unconsumedFlags = getUnusedFlags(context); if ((unconsumed = scoped.getUnconsumed()) != null) { throw new UnconsumedParameterException(unconsumed + " " + unconsumedFlags); } if (unconsumedFlags != null) { throw new UnconsumedParameterException(unconsumedFlags); } }
/** * 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; } }
@ExceptionMatch public void convert(EmptyClipboardException e) throws CommandException { throw new CommandException("Your clipboard is empty. Use //copy first."); }
@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) + "'"); } }
@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) + "'"); } }
/** * Return the number of arguments this binding consumes. * * @return -1 if unknown or unavailable */ int getConsumedCount() { return getBinding().getConsumedCount(this); }
/** * Gets a type from a {@link ArgumentStack}. * * @param context the context * @return the requested type * @throws ParameterException on error */ @BindingMatch(type = { Boolean.class, boolean.class }, behavior = BindingBehavior.CONSUMES, consumedCount = 1) public Boolean getBoolean(ArgumentStack context) throws ParameterException { return context.nextBoolean(); }
@Override public int getConsumedCount(ParameterData parameter) { return match(parameter).annotation.consumedCount(); }
/** * Gets a type from a {@link ArgumentStack}. * * @param context the context * @param modifiers a list of modifiers * @return the requested type * @throws ParameterException on error */ @BindingMatch(type = String.class, behavior = BindingBehavior.CONSUMES, consumedCount = 1, provideModifiers = true) public String getString(ArgumentStack context, Annotation[] modifiers) throws ParameterException { String v = context.next(); validate(v, modifiers); return v; }
/** * Build a {@link CommandCallable} for the given method. * * @param object the object to be invoked on * @param method the method to invoke * @param definition the command definition annotation * @return the command executor * @throws ParametricException thrown on an error */ private CommandCallable build(Object object, Method method, Command definition) throws ParametricException { return new ParametricCallable(this, object, method, definition); }
@Override public List<String> getSuggestions(String arguments, CommandLocals locals) throws CommandException { return builder.getDefaultCompleter().getSuggestions(arguments, locals); }
@ExceptionMatch public void convert(FileSelectionAbortedException e) throws CommandException { throw new CommandException("File selection aborted."); }
@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) + "'"); } }
@ExceptionMatch public void convert(IncompleteRegionException e) throws CommandException { throw new CommandException("Make a region selection first."); }