Example #1
0
  public void help(CommandSender sender, Command command, String[] args) {
    Integer page = 1;

    if (args != null && args.length > 1) {
      try {
        page = Integer.valueOf(args[1]);
      } catch (Exception e) {
        sendMessage(
            sender, ChatColor.RED + " " + args[1] + " is not a number, showing help for page 1.");
      }
    }

    List<String> available = new ArrayList<String>();
    List<String> unavailable = new ArrayList<String>();
    List<String> onlyop = new ArrayList<String>();
    Set<Method> dups = new HashSet<Method>();
    for (MethodWrapper mw : usage) {
      if (!dups.add(mw.method)) continue;
      MCCommand cmd = mw.getCommand();
      final String use = "&6/" + command.getName() + " " + mw.usage;
      if (cmd.op() && !sender.isOp()) continue;
      else if (cmd.admin() && !hasAdminPerms(sender)) continue;
      else if (!cmd.perm().isEmpty() && !sender.hasPermission(cmd.perm())) unavailable.add(use);
      else available.add(use);
    }
    int npages = available.size() + unavailable.size();
    if (sender.isOp()) npages += onlyop.size();
    npages = (int) Math.ceil((float) npages / LINES_PER_PAGE);
    if (page > npages || page <= 0) {
      if (npages <= 0) {
        sendMessage(sender, "&4There are no methods for this command");
      } else {
        sendMessage(sender, "&4That page doesnt exist, try 1-" + npages);
      }
      return;
    }
    if (command != null && command.getAliases() != null && !command.getAliases().isEmpty()) {
      String aliases = StringUtils.join(command.getAliases(), ", ");
      sendMessage(
          sender,
          "&eShowing page &6"
              + page
              + "/"
              + npages
              + "&6 : /"
              + command.getName()
              + " help <page number>");
      sendMessage(sender, "&e    command &6" + command.getName() + "&e has aliases: &6" + aliases);
    } else {
      sendMessage(
          sender, "&eShowing page &6" + page + "/" + npages + "&6 : /cmd help <page number>");
    }
    int i = 0;
    for (String use : available) {
      i++;
      if (i < (page - 1) * LINES_PER_PAGE || i >= page * LINES_PER_PAGE) continue;
      sendMessage(sender, use);
    }
    for (String use : unavailable) {
      i++;
      if (i < (page - 1) * LINES_PER_PAGE || i >= page * LINES_PER_PAGE) continue;
      sendMessage(sender, ChatColor.RED + "[Insufficient Perms] " + use);
    }
    if (sender.isOp()) {
      for (String use : onlyop) {
        i++;
        if (i < (page - 1) * LINES_PER_PAGE || i >= page * LINES_PER_PAGE) continue;
        sendMessage(sender, ChatColor.AQUA + "[OP only] &6" + use);
      }
    }
  }
Example #2
0
  protected Arguments verifyArgs(
      MethodWrapper mwrapper,
      MCCommand cmd,
      CommandSender sender,
      Command command,
      String label,
      String[] args,
      int startIndex)
      throws IllegalArgumentException {
    if (DEBUG) {
      Log.info(
          " method="
              + mwrapper.method.getName()
              + " verifyArgs "
              + cmd
              + " sender="
              + sender
              + ", label="
              + label
              + " args="
              + args);
      for (String arg : args) {
        Log.info(" -- arg=" + arg);
      }
      for (Class<?> t : mwrapper.method.getParameterTypes()) {
        Log.info(" -- type=" + t);
      }
    }
    final int paramLength = mwrapper.method.getParameterTypes().length;

    /// Check our permissions
    if (!cmd.perm().isEmpty()
        && !sender.hasPermission(cmd.perm())
        && !(cmd.admin() && hasAdminPerms(sender)))
      throw new IllegalArgumentException("You don't have permission to use this command");

    /// Verify min number of arguments
    if (args.length < cmd.min()) {
      throw new IllegalArgumentException("You need at least " + cmd.min() + " arguments");
    }
    /// Verfiy max number of arguments
    if (args.length > cmd.max()) {
      throw new IllegalArgumentException("You need less than " + cmd.max() + " arguments");
    }
    /// Verfiy max number of arguments
    if (cmd.exact() != -1 && args.length != cmd.exact()) {
      throw new IllegalArgumentException("You need exactly " + cmd.exact() + " arguments");
    }
    final boolean isPlayer = sender instanceof Player;
    final boolean isOp =
        (isPlayer && sender.isOp()) || sender == null || sender instanceof ConsoleCommandSender;

    if (cmd.op() && !isOp)
      throw new IllegalArgumentException("You need to be op to use this command");

    if (cmd.admin() && !isOp && (isPlayer && !hasAdminPerms(sender)))
      throw new IllegalArgumentException("You need to be an Admin to use this command");

    Class<?> types[] = mwrapper.method.getParameterTypes();

    //		/// In game check
    if (types[0] == Player.class && !isPlayer) {
      throw new IllegalArgumentException(ONLY_INGAME);
    }
    int strIndex = startIndex /*skip the label*/, objIndex = 1;

    Arguments newArgs = new Arguments(); // / Our return value
    Object[] objs = new Object[paramLength]; // / Our new array of castable arguments

    newArgs.args = objs; // / Set our return object with the new castable arguments
    objs[0] = verifySender(sender, types[0]);
    AtomicBoolean usedString = new AtomicBoolean();
    for (int i = 1; i < types.length; i++) {
      Class<?> clazz = types[i];
      usedString.set(false);
      try {
        if (CommandSender.class == clazz) {
          objs[objIndex] = sender;
        } else if (String[].class == clazz) {
          objs[objIndex] = args;
        } else if (Object[].class == clazz) {
          objs[objIndex] = args;
        } else {
          String str = strIndex < args.length ? args[strIndex] : null;
          objs[objIndex] = verifyArg(clazz, command, str, usedString);
          if (objs[objIndex] == null) {
            throw new IllegalArgumentException("Argument " + args[strIndex] + " can not be null");
          }
        }
        if (DEBUG)
          Log.info(
              "   "
                  + objIndex
                  + " : "
                  + strIndex
                  + "  "
                  + (args.length > strIndex ? args[strIndex] : null)
                  + " <-> "
                  + objs[objIndex]
                  + " !!!!!!!!!!!!!!!!!!!!!!!!!!! Cs = "
                  + clazz.getCanonicalName());
        if (usedString.get()) {
          strIndex++;
        }
      } catch (ArrayIndexOutOfBoundsException e) {
        throw new IllegalArgumentException("You didnt supply enough arguments for this method");
      }
      objIndex++;
    }

    /// Verify alphanumeric
    if (cmd.alphanum().length > 0) {
      for (int index : cmd.alphanum()) {
        if (index >= args.length) throw new IllegalArgumentException("String Index out of range. ");
        if (!args[index].matches("[a-zA-Z0-9_]*")) {
          throw new IllegalArgumentException(
              "argument '" + args[index] + "' can only be alphanumeric with underscores");
        }
      }
    }
    return newArgs; /// Success
  }