/** * 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 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 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"); } }
/** * 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; }
/** * 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 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); } }