Exemplo n.º 1
0
 /**
  * Private helper method to add an option. Will check that an option with the same name has not
  * previously been added.
  *
  * @param type
  * @param flag
  * @param field
  * @param option
  * @param c
  * @throws IllegalArgumentException
  */
 private void addOption(FieldType type, Flag flag, Field field, OptionSpec<?> option, Class<?> c)
     throws IllegalArgumentException {
   if (options.containsKey(flag.name())) {
     throw new IllegalArgumentException(
         "Flag named " + flag.name() + " is defined more than once.");
   }
   options.put(flag.name(), new OptionHolder(type, flag, field, option, c));
 }
Exemplo n.º 2
0
 public static Flag construct(String name, Boolean value) {
   Flag result = new Flag();
   result.type = Boolean.class;
   result.name = name;
   result.defaultValue = value;
   return result;
 }
Exemplo n.º 3
0
  private Flags loadOpts(Object o, boolean instanced) {
    final Field[] declaredFields;
    Class<?> c = null;
    if (instanced) {
      declaredFields = o.getClass().getDeclaredFields();
    } else {
      c = ((Class<?>) o);
      declaredFields = c.getDeclaredFields();
    }

    for (Field field : declaredFields) {
      Flag flag = field.getAnnotation(Flag.class);
      // Check if we found a flag annotation for this field.
      if (null == flag) {
        continue;
      }

      // Flag fields must be static if you are initializing the flags through a Class instance.
      if (!instanced && !Modifier.isStatic(field.getModifiers())) {
        throw new IllegalArgumentException(
            "Field "
                + field.toGenericString()
                + " is not static. Flag fields "
                + "must be static when initializing through a Class instance.");
      }

      String name = flag.name();
      String description = flag.description();

      // Determine the type of field
      FieldType type = fieldTypeOf(field, flag);

      switch (type) {
        case INTEGER:
          OptionSpec<Integer> intOption;
          if (flag.required()) {
            intOption =
                optionParser.accepts(name, description).withRequiredArg().ofType(Integer.class);
          } else {
            intOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(Integer.class);
          }
          if (instanced) {
            addInstancedOption(type, flag, field, intOption, o);
          } else {
            addOption(type, flag, field, intOption, c);
          }
          break;

        case STRING:
          OptionSpec<String> stringOption;
          if (flag.required()) {
            stringOption =
                optionParser.accepts(name, description).withRequiredArg().ofType(String.class);
          } else {
            stringOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(String.class);
          }
          if (instanced) {
            addInstancedOption(type, flag, field, stringOption, o);
          } else {
            addOption(type, flag, field, stringOption, c);
          }
          break;

        case BOOLEAN:
          OptionSpec<Boolean> booleanOption;
          if (flag.required()) {
            booleanOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(Boolean.class);
          } else {
            booleanOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(Boolean.class);
          }
          if (instanced) {
            addInstancedOption(type, flag, field, booleanOption, o);
          } else {
            addOption(type, flag, field, booleanOption, c);
          }
          break;

        case LONG:
          OptionSpec<Long> longOption;
          if (flag.required()) {
            longOption =
                optionParser.accepts(name, description).withRequiredArg().ofType(Long.class);
          } else {
            longOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(Long.class);
          }
          if (instanced) {
            addInstancedOption(type, flag, field, longOption, o);
          } else {
            addOption(type, flag, field, longOption, c);
          }
          break;

        case ENUM:
          Class<? extends Enum<?>> enumClass = flag.options();
          Object[] enumConstants = enumClass.getEnumConstants();
          if (enumConstants == null) {
            throw new IllegalArgumentException(
                "Field " + field.toGenericString() + " is not an enum type.");
          }
          for (Object object : enumConstants) {
            addEnumOption(enumClass, object.toString());
          }
          OptionSpec<?> enumOption;
          if (flag.required()) {
            enumOption =
                optionParser.accepts(name, description).withRequiredArg().ofType(enumClass);
          } else {
            enumOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(enumClass);
          }
          if (instanced) {
            addInstancedOption(type, flag, field, enumOption, o);
          } else {
            addOption(type, flag, field, enumOption, c);
          }
          break;

        case UNKNOWN:
        default:
          throw new IllegalArgumentException(
              "Field " + field.toGenericString() + " is not of a supported type.");
      }
    }
    return this;
  }
Exemplo n.º 4
0
Arquivo: Cmd.java Projeto: Rojoss/Boxx
  /**
   * 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);
  }