Exemple #1
0
 /**
  * Get a map with all the registered flags.
  *
  * <p>The flag names do not have the '-' in front of it.
  *
  * <p>If this command is a sub command this will include all the flags from the parent.
  *
  * @return Map with registered flags.
  */
 public Map<String, Flag> getAllFlags() {
   if (isBase()) {
     return new HashMap<>(flags);
   }
   Map<String, Flag> flags = new LinkedHashMap<>(getBaseCmd().getFlags());
   flags.putAll(this.flags);
   return flags;
 }
Exemple #2
0
 /**
  * Get a map with all the registered modifiers.
  *
  * <p>If this command is a sub command this will include all the modifiers from the parent.
  *
  * @return Map with registered modifiers.
  */
 public Map<String, Modifier> getAllModifiers() {
   if (isBase()) {
     return new HashMap<>(modifiers);
   }
   Map<String, Modifier> modifiers = new LinkedHashMap<>(getBaseCmd().getModifiers());
   modifiers.putAll(this.modifiers);
   return modifiers;
 }
Exemple #3
0
  /**
   * Register a command flag for this command.
   *
   * <p>A command flag represents a boolean that can be added anywhere in the command. The specified
   * name must be used by the user to specify the flag.
   *
   * <p>For example if the name is 'f' the user can specify '-f' anywhere in the command.
   *
   * <p>You don't have to prefix the name with a '-' If you do it will be removed so if you want to
   * have the user put '--f' you have to set the name as '--f'
   *
   * @param name The flag name/key used to identify the flag. This name must be used with the {@link
   *     CmdData} result to check if the flag was specified. This name is also what the user needs
   *     to use to specify the flag. (-{name}) The name '?' is reserved!
   * @return The added {@link Flag}
   * @throws IllegalArgumentException if a flag with the specified name is already registered for
   *     this command.
   */
  public Flag addFlag(String name) {
    if (name.startsWith("-")) {
      name = name.substring(1);
    }

    Flag flag = new Flag(name);
    if (getAllFlags().containsKey(name.toLowerCase())) {
      throw new IllegalArgumentException(
          "The command already has a flag with the name '-" + name + "'!");
    }

    flags.put(name.toLowerCase(), flag);
    return flag;
  }
Exemple #4
0
  /**
   * Register a command modifier for this command.
   *
   * <p>A command modifier is an optional argument that can be added anywhere in the command. The
   * specified name must be used by the user to specify the modifier.
   *
   * <p>For example if you register a modifier with the name player and the option is a {@link
   * PlayerO} The user would put player:{player} anywhere in the command to set the modifer value.
   *
   * @param name The modifier name/key used to identify the argument. This name must be used with
   *     the {@link CmdData} result to get the modifier value. This name is also what the user needs
   *     to use to specify the modifier. ({name}:{value})
   * @param option The {@link SingleOption} used for parsing the modifier. This option determines
   *     the modifier value and everything else. For example if it's a {@link PlayerO} the modifier
   *     value must be a player and the result value would be a player. This option can't be a sub
   *     command option!
   * @return The added {@link Modifier}
   * @throws IllegalArgumentException if a modifier with the specified name is already registered
   *     for this command or if the modifier has a sub command option.
   */
  public Modifier addModifier(String name, SingleOption option) {
    Modifier modifier = new Modifier(name, option);

    if (option instanceof SubCmdO) {
      throw new IllegalArgumentException(
          "Modifiers can not be a sub command option! [modifier=" + name + "]");
    }

    if (getAllModifiers().containsKey(name.toLowerCase())) {
      throw new IllegalArgumentException(
          "The command already has a modifier with the name '" + name + "'!");
    }

    modifiers.put(name.toLowerCase(), modifier);
    return modifier;
  }
Exemple #5
0
  /**
   * Display a detailed help message to the specified {@link CommandSender}
   *
   * <p>If the sender is a player or console it will display 'all' details. If it's not it will only
   * display the usage string.
   *
   * @param sender The {@link CommandSender} to send the message to.
   */
  public void showHelp(CommandSender sender, String label, String[] args) {
    if ((!(sender instanceof Player) && sender instanceof Entity)
        || sender instanceof BlockCommandSender) {
      Msg.fromString(getUsage(sender, label, args)).send(sender);
      return;
    }
    String none = Msg.getString("command.none");
    String noDesc = Msg.getString("command.no-description");

    List<String> blacklisted = new ArrayList<>();
    for (SenderType type : getSenderBlacklist()) {
      blacklisted.add(Str.camelCase(type.toString()));
    }

    String perm = "";
    if (perm().isEmpty() && getBaseCmd().perm().isEmpty()) {
      perm = none;
    } else {
      if (!perm().isEmpty()) {
        perm = perm();
      }
      if (isSub() && !getBaseCmd().perm().isEmpty()) {
        if (!perm.isEmpty()) {
          perm += " ";
        }
        perm +=
            Msg.getString("command.permission-inherit", Param.P("permission", getBaseCmd().perm()));
      }
    }

    List<String> flagFormats = new ArrayList<>();
    for (Flag flag : getAllFlags().values()) {
      boolean inherit =
          !flags.containsKey(flag.name().toLowerCase())
              || flag.name().equals("?")
              || flag.name().equals("l");
      if (sender instanceof Player) {
        flagFormats.add(
            Msg.getString(
                inherit ? "command.flag-entry-inherit" : "command.flag-entry",
                Param.P("name", flag.name()),
                Param.P("description", flag.desc().isEmpty() ? noDesc : flag.desc()),
                Param.P("permission", flag.perm().isEmpty() ? none : flag.perm())));
      } else {
        flagFormats.add((inherit ? "&a&l-" : "&7&l-") + flag.name());
      }
    }

    List<String> modifierFormats = new ArrayList<>();
    for (Modifier mod : getAllModifiers().values()) {
      boolean inherit =
          !modifiers.containsKey(mod.name().toLowerCase()) || mod.name().equals("page");
      if (sender instanceof Player) {
        modifierFormats.add(
            Msg.getString(
                inherit ? "command.modifier-entry-inherit" : "command.modifier-entry",
                Param.P("name", mod.name()),
                Param.P("description", mod.desc().isEmpty() ? noDesc : mod.desc()),
                Param.P("permission", mod.perm().isEmpty() ? none : mod.perm()),
                Param.P("type", mod.option().getTypeName())));
      } else {
        modifierFormats.add(
            inherit
                ? "&a" + mod.name() + ":&8[&a" + mod.option().getTypeName() + "&8]"
                : "&7" + mod.name() + ":&8[&7" + mod.option().getTypeName() + "&8]");
      }
    }

    String argClr = Msg.getString("command.argument-name-color");
    String msg =
        Msg.getString(
            "command.help",
            Param.P("label", label),
            Param.P("cmd", getBaseCmd().getName()),
            Param.P(
                "usage",
                sender instanceof ConsoleCommandSender
                    ? new CmdUsageParser(this, sender, label, args, argClr).getString()
                    : new CmdUsageParser(this, sender, label, args, argClr).getJSON()),
            Param.P("description", desc().isEmpty() ? noDesc : desc()),
            Param.P("permission", perm),
            Param.P(
                "aliases",
                isSub()
                    ? (((SubCmd) this).getSubAliases().isEmpty()
                        ? none
                        : Str.implode(((SubCmd) this).getSubAliases()))
                    : (getAliases().isEmpty() ? none : Str.implode(getAliases()))),
            Param.P("flags", flagFormats.isEmpty() ? none : Str.implode(flagFormats, " ")),
            Param.P("modifiers", modifierFormats.isEmpty() ? none : Str.implode(modifierFormats)));

    Msg.fromString(msg).send(sender);
  }