Exemplo n.º 1
0
  /**
   * Initializes the configuration manager.
   *
   * @param plugin the instance of NoCheatPlus
   */
  public static void init(final NoCheatPlus plugin) {
    // First try to obtain and parse the global configuration file.
    final File folder = plugin.getDataFolder();
    final File globalFile = new File(folder, "config.yml");

    final ConfigFile global = new ConfigFile();
    global.setDefaults(new DefaultConfig());
    global.options().copyDefaults(true);
    global.options().copyHeader(true);

    if (globalFile.exists())
      try {
        global.load(globalFile);
      } catch (final Exception e) {
        e.printStackTrace();
      }

    try {
      global.save(globalFile);
    } catch (final Exception e) {
      e.printStackTrace();
    }

    global.regenerateActionLists();

    // Put the global configuration file on the configurations map.
    worldsMap.put(null, global);

    final Logger logger = Logger.getAnonymousLogger();
    logger.setLevel(Level.INFO);
    logger.setUseParentHandlers(false);
    for (final Handler h : logger.getHandlers()) logger.removeHandler(h);

    if (fileHandler != null) {
      fileHandler.close();
      logger.removeHandler(fileHandler);
      fileHandler = null;
    }

    final File logFile = new File(folder, global.getString(ConfPaths.LOGGING_FILENAME));
    try {
      try {
        logFile.getParentFile().mkdirs();
      } catch (final Exception e) {
        e.printStackTrace();
      }
      fileHandler = new FileHandler(logFile.getCanonicalPath(), true);
      fileHandler.setLevel(Level.ALL);
      fileHandler.setFormatter(LogFileFormatter.newInstance());

      logger.addHandler(fileHandler);
    } catch (final Exception e) {
      e.printStackTrace();
    }

    CheckUtils.fileLogger = logger;

    // Try to find world-specific configuration files.
    final HashMap<String, File> worldFiles = new HashMap<String, File>();

    if (folder.isDirectory())
      for (final File file : folder.listFiles())
        if (file.isFile()) {
          final String filename = file.getName();
          if (filename.matches(".+_config.yml$")) {
            final String worldname = filename.substring(0, filename.length() - 10);
            worldFiles.put(worldname, file);
          }
        }

    for (final Entry<String, File> worldEntry : worldFiles.entrySet()) {
      final File worldConfigFile = worldEntry.getValue();
      final ConfigFile world = new ConfigFile();
      world.setDefaults(global);

      try {
        world.load(worldConfigFile);
        worldsMap.put(worldEntry.getKey(), world);

        // Write the configuration file back to disk immediately.
        world.save(worldConfigFile);
      } catch (final Exception e) {
        System.out.println(
            "[NoCheatPlus] Couldn't load world-specific configuration for "
                + worldEntry.getKey()
                + "!");
        e.printStackTrace();
      }

      world.regenerateActionLists();
    }
  }