private org.spout.api.command.Command createCommand(
      Named owner, org.spout.api.command.Command parent, AnnotatedElement obj) {
    if (!obj.isAnnotationPresent(Command.class)) {
      return null;
    }

    Command command = obj.getAnnotation(Command.class);
    if (command.aliases().length < 1) {
      throw new IllegalArgumentException("Command must have at least one alias");
    }
    org.spout.api.command.Command child =
        parent
            .addSubCommand(owner, command.aliases()[0])
            .addAlias(command.aliases())
            .addFlags(command.flags())
            .setUsage(command.usage())
            .setHelp(command.desc())
            .setArgBounds(command.min(), command.max());

    if (obj.isAnnotationPresent(CommandPermissions.class)) {
      CommandPermissions perms = obj.getAnnotation(CommandPermissions.class);
      child.setPermissions(perms.requireAll(), perms.value());
    }
    return child;
  }
  private boolean methodRegistration(
      Named owner, Class<?> commands, Object instance, org.spout.api.command.Command parent) {
    boolean success = true;
    for (Method method : commands.getDeclaredMethods()) {
      // Basic checks
      method.setAccessible(true);
      if (!Modifier.isStatic(method.getModifiers()) && instance == null) {
        continue;
      }
      org.spout.api.command.Command child = createCommand(owner, parent, method);
      if (child == null) {
        continue;
      }

      if (method.isAnnotationPresent(NestedCommand.class)) {
        for (Class<?> clazz : method.getAnnotation(NestedCommand.class).value()) {
          success &= create(owner, clazz, child);
        }
      } else {
        child.setExecutor(executorFactory.getAnnotatedCommandExecutor(instance, method));
      }
      child.closeSubCommand();
    }
    return success;
  }
Exemplo n.º 3
0
  /**
   * 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);
    }
  }
Exemplo n.º 4
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);
  }
Exemplo n.º 5
0
 @Override
 public Message getCommandMessage(Command command, ChatArguments arguments) {
   if (command.getPreferredName().equals("kick")) {
     return getKickMessage(arguments);
   } else if (command.getPreferredName().equals("say")) {
     return new PlayerChatMessage(arguments.toFormatString());
   }
   return new PlayerChatMessage(
       '/' + command.getPreferredName() + ' ' + arguments.toFormatString());
 }
Exemplo n.º 6
0
 @Override
 public void processCommand(String command, ChatArguments arguments) {
   final RootCommand rootCmd = Spout.getEngine().getRootCommand();
   Command cmd = rootCmd.getChild(command);
   if (cmd == null) {
     sendMessage(
         rootCmd
             .getMissingChildException(
                 rootCmd.getUsage(command, arguments.toSections(ChatSection.SplitType.WORD), -1))
             .getMessage());
   } else {
     cmd.process(this, command, arguments, false);
   }
 }
Exemplo n.º 7
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);
      }
    }
Exemplo n.º 8
0
  /**
   * Returns a child of this command with the specified. Will create a new unless otherwise
   * specified.
   *
   * @param name name of command
   * @param createIfAbsent true if should create command if non-existent
   * @return new child or existing child
   */
  public Command getChild(String name, boolean createIfAbsent) {
    for (Command child : children) {
      for (String alias : child.getAliases()) {
        if (alias.equalsIgnoreCase(name)) {
          return child;
        }
      }
    }

    Command command = null;
    if (createIfAbsent) {
      children.add(command = new Command(name));
    }

    return command;
  }
  private boolean nestedClassRegistration(
      Named owner, Class<?> commands, Object instance, org.spout.api.command.Command parent) {
    boolean success = true, anyRegistered = false;
    for (Class<?> clazz : commands.getDeclaredClasses()) {
      Object subInstance = null;
      if (!Modifier.isStatic(clazz.getModifiers())) {
        try {
          Constructor<?> constr = getClosestConstructor(clazz, commands);
          if (constr == null) {
            continue;
          }

          constr.setAccessible(true);
          subInstance = constr.newInstance(instance);
        } catch (InvocationTargetException e) {
          e.printStackTrace();
          continue;
        } catch (InstantiationException e) {
          e.printStackTrace();
          continue;
        } catch (IllegalAccessException ignore) {
        }
      }

      org.spout.api.command.Command child = createCommand(owner, parent, clazz);
      if (child == null) {
        continue;
      }
      anyRegistered = true;

      if (!nestedClassRegistration(owner, clazz, subInstance, child)) {
        for (Method method : clazz.getDeclaredMethods()) {
          if (!method.isAnnotationPresent(Executor.class)) {
            continue;
          }

          Platform platform = method.getAnnotation(Executor.class).value();
          child.setExecutor(
              platform, executorFactory.getAnnotatedCommandExecutor(subInstance, method));
        }
      }
    }
    return success && anyRegistered;
  }