Beispiel #1
0
  @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);
  }
Beispiel #2
0
    @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);
      }
    }
 /**
  * Finds the insert position of the given <code>command</command> within the <code>popupMenu</code>.
  *
  * @param popupMenu The popup menu.
  * @param command The command to insert.
  * @param manager The command manager.
  */
 public static int findMenuInsertPosition(
     final JPopupMenu popupMenu, final Command command, final CommandManager manager) {
   int pbi = popupMenu.getComponentCount();
   String placeBefore = command.getPlaceBefore();
   if (placeBefore != null) {
     pbi = UIUtils.findMenuItemPosition(popupMenu, placeBefore);
   }
   int pai = -1;
   String placeAfter = command.getPlaceAfter();
   if (placeAfter != null) {
     pai = UIUtils.findMenuItemPosition(popupMenu, placeAfter) + 1;
   }
   final int componentCount = popupMenu.getComponentCount();
   for (int i = 0; i < componentCount; i++) {
     final Component component = popupMenu.getComponent(i);
     if (!(component instanceof JMenuItem)) {
       continue;
     }
     final JMenuItem item = (JMenuItem) component;
     final String name = item.getName();
     final Command menuCommand = manager.getCommand(name);
     if (menuCommand == null) {
       continue;
     }
     placeBefore = menuCommand.getPlaceBefore();
     if (command.getCommandID().equals(placeBefore)) {
       if (pbi > i) {
         pbi = i + 1;
       }
     }
     placeAfter = menuCommand.getPlaceAfter();
     if (command.getCommandID().equals(placeAfter)) {
       if (pai < i) {
         pai = i;
       }
     }
   }
   int insertPos = -1;
   if (pbi >= pai) {
     insertPos = pai;
   }
   if (insertPos == -1) {
     insertPos = popupMenu.getComponentCount();
   }
   return insertPos;
 }
Beispiel #4
0
  @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");
  }
 private static Command[] getCommands(
     final JPopupMenu popupMenu, final CommandManager manager, final Command command) {
   final List<Command> commands = new ArrayList<Command>();
   final int count = popupMenu.getComponentCount();
   for (int i = 0; i < count; i++) {
     final Component component = popupMenu.getComponent(i);
     if (!(component instanceof JMenuItem)) {
       continue;
     }
     final JMenuItem item = (JMenuItem) component;
     final String name = item.getName();
     final Command menuCommand = manager.getCommand(name);
     if (menuCommand != null) {
       commands.add(menuCommand);
     }
   }
   commands.add(command);
   return commands.toArray(new Command[commands.size()]);
 }
Beispiel #6
0
  @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");
  }