예제 #1
0
  /**
   * List all the available MobArena commands for the CommandSender.
   *
   * @param sender a player or the console
   */
  private void showHelp(CommandSender sender) {
    StringBuilder user = new StringBuilder();
    StringBuilder admin = new StringBuilder();

    for (Command cmd : commands.values()) {
      CommandInfo info = cmd.getClass().getAnnotation(CommandInfo.class);
      if (!PermHandler.hasPerm(sender, info.permission())) continue;

      StringBuilder buffy;
      if (info.permission().contains("admin")) buffy = admin;
      else buffy = user;

      buffy
          .append("\n")
          .append(ChatColor.RESET)
          .append(info.usage())
          .append(" ")
          .append(ChatColor.YELLOW)
          .append(info.desc());
    }

    if (admin.length() == 0) {
      Messenger.sendMessage(sender, "Available Commands: " + user.toString());
    } else {
      Messenger.sendMessage(sender, "User Commands: " + user.toString());
      Messenger.sendMessage(sender, "Admin Commands: " + admin.toString());
    }
  }
예제 #2
0
  @Override
  public boolean onCommand(
      CommandSender sender, org.bukkit.command.Command bcmd, String label, String[] args) {
    // Grab the base and arguments.
    String base = bcmd.getName();
    // String base = (args.length > 0 ? args[0] : "");
    String last = (args.length > 0 ? args[args.length - 1] : "");

    // If there's no base argument, show a helpful message.
    if (base.equals("")) {
      Messenger.sendMessage(sender, "/rpge help|?");
      return true;
    }

    // The help command is a little special
    if (base.equals("?") || base.equalsIgnoreCase("help")) {
      showHelp(sender);
      return true;
    }

    // Get all commands that match the base.
    List<Command> matches = getMatchingCommands(base);

    // If there's more than one match, display them.
    if (matches.size() > 1) {
      Messenger.sendMessage(sender, "Multiple command matches");
      for (Command cmd : matches) {
        showUsage(cmd, sender, false);
      }
      return true;
    }

    // If there are no matches at all, notify.
    if (matches.size() == 0) {
      Messenger.sendMessage(sender, "Command not found");
      return true;
    }

    // Grab the only match.
    Command command = matches.get(0);
    CommandInfo info = command.getClass().getAnnotation(CommandInfo.class);

    // First check if the sender has permission.
    if (!PermHandler.hasPerm(sender, info.permission())) {
      Messenger.sendMessage(sender, "No Permission");
      return true;
    }

    // Check if the last argument is a ?, in which case, display usage and description
    if (last.equals("?") || last.equals("help")) {
      showUsage(command, sender, true);
      return true;
    }

    // Otherwise, execute the command!
    String[] params = args; // trimFirstArg(args);
    if (!command.execute(plugin, sender, params)) {
      showUsage(command, sender, true);
    }
    return true;
  }