/** * Executes the command in the executor it is currently set to. * * @param source that sent the command * @param args command arguments * @throws CommandException if the command executor is null or if {@link * Executor#execute(CommandSource, Command, CommandArguments)} throws a CommandException. */ public void process(CommandSource source, CommandArguments args) throws CommandException { // check permissions if (permission != null && !source.hasPermission(permission)) { throw new CommandException("You do not have permission to execute this command."); } args.flags().registerFlags(this.flags); // no child found, try to execute for (CommandFilter filter : filters) { filter.validate(this, source, args); } // execute a child if applicable if (children.size() > 0) { Command child = getChild(args.popString("child"), false); if (child != null) { if (executor != null) { executor.execute(source, this, args); } child.process(source, args); } else { throw args.failure("child", "Unknown child!", false); } } else { if (executor == null) { throw new CommandException("CommandDescription exists but has no set executor."); } args.flags().parse(); executor.execute(source, this, args); } }
@Test public void testFilter() { CommandManager cm = new CommandManager(); CommandSource source = new TestCommandSource(cm); cm.getCommand("test3") .addFilter(new PlayerFilter()) .setExecutor( new Executor() { @Override public void execute(CommandSource source, Command command, CommandArguments args) throws CommandException {} }); thrown.expectCause(isA(CommandException.class)); thrown.expectMessage("You must be a Player to execute this command"); source.processCommand("test3"); }
@Test public void testChildren() { CommandManager cm = new CommandManager(); CommandSource testSource = new TestCommandSource(cm); cm.getCommand("test2") .getChild("foo") .getChild("bar") .getChild("baz") .setExecutor( new Executor() { @Override public void execute(CommandSource source, Command command, CommandArguments args) throws CommandException { assertEquals("baz", command.getName()); assertEquals("hello", args.popString("testStr")); } }); testSource.processCommand("test2", "foo", "bar", "baz", "hello"); }
@Override public void giveMoney(final BigDecimal value, final CommandSource initiator) throws MaxMoneyException { if (value.signum() == 0) { return; } setMoney(getMoney().add(value)); sendMessage(tl("addedToAccount", NumberUtil.displayCurrency(value, ess))); if (initiator != null) { initiator.sendMessage( tl( "addedToOthersAccount", NumberUtil.displayCurrency(value, ess), this.getDisplayName(), NumberUtil.displayCurrency(getMoney(), ess))); } }
@Override public void takeMoney(final BigDecimal value, final CommandSource initiator) { if (value.signum() == 0) { return; } try { setMoney(getMoney().subtract(value)); } catch (MaxMoneyException ex) { ess.getLogger() .log( Level.WARNING, "Invalid call to takeMoney, total balance can't be more than the max-money limit.", ex); } sendMessage(tl("takenFromAccount", NumberUtil.displayCurrency(value, ess))); if (initiator != null) { initiator.sendMessage( tl( "takenFromOthersAccount", NumberUtil.displayCurrency(value, ess), this.getDisplayName(), NumberUtil.displayCurrency(getMoney(), ess))); } }