/** * 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(); }
/** * 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; }
/** * 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(); }
/** * 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 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 a type from a {@link ArgumentStack}. * * @param context the context * @param text the text annotation * @param modifiers a list of modifiers * @return the requested type * @throws ParameterException on error */ @BindingMatch(classifier = Text.class, type = String.class, behavior = BindingBehavior.CONSUMES, consumedCount = -1, provideModifiers = true) public String getText(ArgumentStack context, Text text, Annotation[] modifiers) throws ParameterException { String v = context.remaining(); validate(v, modifiers); return v; }
/** * 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); } }
/** * 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"); } }
/** * 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 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 = { Integer.class, int.class }, behavior = BindingBehavior.CONSUMES, consumedCount = 1, provideModifiers = true) public Integer getInteger(ArgumentStack context, Annotation[] modifiers) throws ParameterException { Double v = parseNumericInput(context.next()); if (v != null) { int intValue = v.intValue(); validate(intValue, modifiers); return intValue; } else { return null; } }
/** * Get the right {@link ArgumentStack}. * * @param parameter the parameter * @param existing the existing scoped context * @return the context to use */ private static ArgumentStack getScopedContext(Parameter parameter, ArgumentStack existing) { if (parameter.getFlag() != null) { CommandContext context = existing.getContext(); if (parameter.isValueFlag()) { return new StringArgumentStack(context, context.getFlag(parameter.getFlag()), false); } else { String v = context.hasFlag(parameter.getFlag()) ? "true" : "false"; return new StringArgumentStack(context, v, true); } } return existing; }
/** * Get a direction from the player. * * @param context the context * @param direction the direction annotation * @return a pattern * @throws ParameterException on error * @throws UnknownDirectionException on an unknown direction */ @BindingMatch(classifier = Direction.class, type = BlockVector3.class, behavior = BindingBehavior.CONSUMES, consumedCount = 1) public BlockVector3 getDirection(ArgumentStack context, Direction direction) throws ParameterException, UnknownDirectionException { Player sender = getPlayer(context); if (direction.includeDiagonals()) { return worldEdit.getDiagonalDirection(sender, context.next()); } else { return worldEdit.getDirection(sender, context.next()); } }
/** * Gets an {@link EditSession} from a {@link ArgumentStack}. * * @param context the context * @return an edit session * @throws ParameterException on other error */ @BindingMatch(type = EditSession.class, behavior = BindingBehavior.PROVIDES) public EditSession getEditSession(ArgumentStack context) throws ParameterException { Player sender = getPlayer(context); LocalSession session = worldEdit.getSessionManager().get(sender); EditSession editSession = session.createEditSession(sender); editSession.enableStandardMode(); context.getContext().getLocals().put(EditSession.class, editSession); session.tellVersion(sender); return editSession; }
/** * 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 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 = { Double.class, double.class }, behavior = BindingBehavior.CONSUMES, consumedCount = 1, provideModifiers = true) public Double getDouble(ArgumentStack context, Annotation[] modifiers) throws ParameterException { Double v = parseNumericInput(context.next()); if (v != null) { validate(v, modifiers); return v; } else { return null; } }
/** * 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"); } }