/** Loads all plugins, calling onLoad, &c. */
  private void loadPlugins() {
    // clear the map
    commandMap.clearCommands();
    commandMap.register("glowstone", new ColorCommand("colors"));
    commandMap.register("glowstone", new TellrawCommand());

    File folder = new File(config.getString(ServerConfig.Key.PLUGIN_FOLDER));
    if (!folder.isDirectory() && !folder.mkdirs()) {
      logger.log(Level.SEVERE, "Could not create plugins directory: " + folder);
    }

    // clear plugins and prepare to load
    pluginManager.clearPlugins();
    pluginManager.registerInterface(JavaPluginLoader.class);
    Plugin[] plugins = pluginManager.loadPlugins(folder);

    // call onLoad methods
    for (Plugin plugin : plugins) {
      try {
        plugin.onLoad();
      } catch (Exception ex) {
        logger.log(Level.SEVERE, "Error loading " + plugin.getDescription().getFullName(), ex);
      }
    }
  }
  /**
   * Enable all plugins of the given load order type.
   *
   * @param type The type of plugin to enable.
   */
  private void enablePlugins(PluginLoadOrder type) {
    if (type == PluginLoadOrder.STARTUP) {
      helpMap.clear();
      helpMap.initializeGeneralTopics();
    }

    // load all the plugins
    Plugin[] plugins = pluginManager.getPlugins();
    for (Plugin plugin : plugins) {
      if (!plugin.isEnabled() && plugin.getDescription().getLoad() == type) {
        List<Permission> perms = plugin.getDescription().getPermissions();
        for (Permission perm : perms) {
          try {
            pluginManager.addPermission(perm);
          } catch (IllegalArgumentException ex) {
            getLogger()
                .log(
                    Level.WARNING,
                    "Plugin "
                        + plugin.getDescription().getFullName()
                        + " tried to register permission '"
                        + perm.getName()
                        + "' but it's already registered",
                    ex);
          }
        }

        try {
          pluginManager.enablePlugin(plugin);
        } catch (Throwable ex) {
          logger.log(Level.SEVERE, "Error loading " + plugin.getDescription().getFullName(), ex);
        }
      }
    }

    if (type == PluginLoadOrder.POSTWORLD) {
      commandMap.setFallbackCommands();
      commandMap.registerServerAliases();
      DefaultPermissions.registerCorePermissions();
      helpMap.initializeCommands();

      // load permissions.yml
      ConfigurationSection permConfig = config.getConfigFile(ServerConfig.Key.PERMISSIONS_FILE);
      List<Permission> perms =
          Permission.loadPermissions(
              permConfig.getValues(false),
              "Permission node '%s' in permissions config is invalid",
              PermissionDefault.OP);
      for (Permission perm : perms) {
        try {
          pluginManager.addPermission(perm);
        } catch (IllegalArgumentException ex) {
          getLogger()
              .log(
                  Level.WARNING,
                  "Permission config tried to register '"
                      + perm.getName()
                      + "' but it's already registered",
                  ex);
        }
      }
    }
  }