/** * Get a new ConfigFile instance with all paths removed (recursively by prefix). * @param config * @param removePaths * @return */ public static ConfigFile removePaths(final ConfigFile config, final Collection<String> removePaths) { final SimpleCharPrefixTree prefixes = new SimpleCharPrefixTree(); for (final String path : removePaths) { prefixes.feed(path); } final ConfigFile newConfig = new ConfigFile(); for (final Entry<String, Object> entry : config.getValues(true).entrySet()) { final String path = entry.getKey(); final Object value = entry.getValue(); // TODO: To support moving entire sections, this needs to be changed. if (value instanceof ConfigurationSection) { continue; } if (!prefixes.hasPrefix(path)) { newConfig.set(path, value); } } return newConfig; }
/** * Run all warning checks and alter the configuration file if necessary (GlobalConfig, Deprecated, Moved). * @param file * @param configName */ public static void processPaths(File file, String configName, boolean isWorldConfig) { ConfigFile config = new ConfigFile(); try { config.load(file); ConfigFile newConfig = processPaths(config, configName, isWorldConfig); if (newConfig != null) { config = newConfig; try{ config.save(file); } catch(Throwable t) { // Do log this one. StaticLog.logSevere("Failed to save configuration (" + configName + ") with changes: " + t.getClass().getSimpleName()); StaticLog.logSevere(t); } } } catch (FileNotFoundException e) { } catch (IOException e) { } catch (InvalidConfigurationException e) { } }
/** * A config file only containing the entries that are not set as global only. * @param defaultConfig * @return */ public static MemoryConfiguration getWorldsDefaultConfig(final MemoryConfiguration defaultConfig) { final char sep = defaultConfig.options().pathSeparator(); final MemoryConfiguration config = new ConfigFile(); config.options().pathSeparator(sep); final Map<String, Object> defaults = defaultConfig.getValues(false); for (final Entry<String, Object> entry : defaults.entrySet()) { final String part = entry.getKey(); if (!part.isEmpty() && !mayBeInWorldConfig(part)) { continue; } final Object value = entry.getValue(); if (value instanceof ConfigurationSection) { addWorldConfigSection(config, (ConfigurationSection) value, part, sep); } else { config.set(part, value); } } return config; }
final ConfigFile globalConfig = new ConfigFile(); globalConfig.setDefaults(defaultConfig); globalConfig.options().copyDefaults(true); final File worldFile = worldEntry.getValue(); PathUtils.processPaths(worldFile, "world " + worldEntry.getKey(), true); final ConfigFile worldConfig = new ConfigFile(); worldConfig.setDefaults(worldDefaults); worldConfig.options().copyDefaults(true);