@Test public void testCommand() throws CommandException { CommandManager cm = new CommandManager(); CommandSource testSource = new TestCommandSource(cm); Command cmd = cm.getCommand("test1") .addAlias("t1") .setPermission("test.1") .setExecutor( new Executor() { @Override public void execute(CommandSource source, Command command, CommandArguments args) throws CommandException { args.popString("testArg1"); args.popString("testArg2", "Not specified"); args.assertCompletelyParsed(); } }); // execute with success cmd.process(testSource, "foo"); cmd.process(testSource, "foo", "bar"); // execute with failure thrown.expect(CommandException.class); cmd.process(testSource, "foo", "bar", "baz"); cmd.process(testSource); }
@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 processCommand(String command, String... args) { Command cmd = cm.getCommand(command, false); if (cmd == null) { sendMessage("Unknown command: '" + command + "'."); return; } try { cmd.process(this, args); } catch (CommandException e) { throw new RuntimeException(e); } }