@Override
 public Map<String, String[]> getCommandAliases() {
   Map<String, String[]> aliases = new HashMap<>();
   ConfigurationSection section =
       config.getConfigFile(ServerConfig.Key.COMMANDS_FILE).getConfigurationSection("aliases");
   if (section == null) {
     return aliases;
   }
   for (String key : section.getKeys(false)) {
     List<String> list = section.getStringList(key);
     aliases.put(key, list.toArray(new String[list.size()]));
   }
   return aliases;
 }
Beispiel #2
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;
  }
Beispiel #3
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;
  }
  private static ServerConfig parseArguments(String[] args) {
    final Map<ServerConfig.Key, Object> parameters = new EnumMap<>(ServerConfig.Key.class);
    String configDirName = "config";
    String configFileName = "glowstone.yml";

    // Calculate acceptable parameters
    for (int i = 0; i < args.length; i++) {
      final String opt = args[i];

      if (!opt.startsWith("-")) {
        System.err.println("Ignored invalid option: " + opt);
        continue;
      }

      // Help and version
      if ("--help".equals(opt) || "-h".equals(opt) || "-?".equals(opt)) {
        System.out.println("Available command-line options:");
        System.out.println("  --help, -h, -?                 Shows this help message and exits.");
        System.out.println("  --version, -v                  Shows version information and exits.");
        System.out.println("  --configdir <directory>        Sets the configuration directory.");
        System.out.println("  --configfile <file>            Sets the configuration file.");
        System.out.println("  --port, -p <port>              Sets the server listening port.");
        System.out.println("  --host, -H <ip | hostname>     Sets the server listening address.");
        System.out.println("  --onlinemode, -o <onlinemode>  Sets the server's online-mode.");
        System.out.println("  --jline <true/false>           Enables or disables JLine console.");
        System.out.println("  --plugins-dir, -P <directory>  Sets the plugin directory to use.");
        System.out.println("  --worlds-dir, -W <directory>   Sets the world directory to use.");
        System.out.println(
            "  --update-dir, -U <directory>   Sets the plugin update folder to use.");
        System.out.println("  --max-players, -M <director>   Sets the maximum amount of players.");
        System.out.println("  --world-name, -N <name>        Sets the main world name.");
        System.out.println(
            "  --log-pattern, -L <pattern>    Sets the log file pattern (%D for date).");
        return null;
      } else if ("--version".equals(opt) || "-v".equals(opt)) {
        System.out.println(
            "Glowstone version: " + GlowServer.class.getPackage().getImplementationVersion());
        System.out.println(
            "Bukkit version:    " + GlowServer.class.getPackage().getSpecificationVersion());
        System.out.println("Minecraft version: " + GAME_VERSION + " protocol " + PROTOCOL_VERSION);
        return null;
      }

      // Below this point, options require parameters
      if (i == args.length - 1) {
        System.err.println("Ignored option specified without value: " + opt);
        continue;
      }

      switch (opt) {
        case "--configdir":
          configDirName = args[++i];
          break;
        case "--configfile":
          configFileName = args[++i];
          break;
        case "--port":
        case "-p":
          parameters.put(ServerConfig.Key.SERVER_PORT, Integer.valueOf(args[++i]));
          break;
        case "--host":
        case "-H":
          parameters.put(ServerConfig.Key.SERVER_IP, args[++i]);
          break;
        case "--onlinemode":
        case "-o":
          parameters.put(ServerConfig.Key.ONLINE_MODE, Boolean.valueOf(args[++i]));
          break;
        case "--jline":
          parameters.put(ServerConfig.Key.USE_JLINE, Boolean.valueOf(args[++i]));
          break;
        case "--plugins-dir":
        case "-P":
          parameters.put(ServerConfig.Key.PLUGIN_FOLDER, args[++i]);
          break;
        case "--worlds-dir":
        case "-W":
          parameters.put(ServerConfig.Key.WORLD_FOLDER, args[++i]);
          break;
        case "--update-dir":
        case "-U":
          parameters.put(ServerConfig.Key.UPDATE_FOLDER, args[++i]);
          break;
        case "--max-players":
        case "-M":
          parameters.put(ServerConfig.Key.MAX_PLAYERS, Integer.valueOf(args[++i]));
          break;
        case "--world-name":
        case "-N":
          parameters.put(ServerConfig.Key.LEVEL_NAME, args[++i]);
          break;
        case "--log-pattern":
        case "-L":
          parameters.put(ServerConfig.Key.LOG_FILE, args[++i]);
          break;
        default:
          System.err.println("Ignored invalid option: " + opt);
      }
    }

    final File configDir = new File(configDirName);
    final File configFile = new File(configDir, configFileName);

    return new ServerConfig(configDir, configFile, parameters);
  }