try { ConfigurationOptions options = ConfigurationOptions.defaults(); options = options.setShouldCopyDefaults(true);
public static void loadConfig(VotifierPlugin pl) { plugin = pl; if (!plugin.getConfigDir().exists()) { plugin.getConfigDir().mkdirs(); } try { File file = new File(plugin.getConfigDir(), "config.yml"); if (!file.exists()) { file.createNewFile(); } ConfigurationLoader loader = YAMLConfigurationLoader.builder().setFile(file).build(); ConfigurationNode config = loader.load(ConfigurationOptions.defaults().setShouldCopyDefaults(true)); spongeConfig = config.getValue(TypeToken.of(SpongeConfig.class), new SpongeConfig()); loader.save(config); } catch (Exception e) { plugin.getLogger().error("Could not load config.", e); } }
public ConfigCategory loadConfig() { try { File file = new File(instance.configDir, "MagiBridge.conf"); if (!file.exists()) { file.createNewFile(); } ConfigurationLoader<CommentedConfigurationNode> loader = HoconConfigurationLoader.builder().setFile(file).build(); CommentedConfigurationNode config = loader.load(ConfigurationOptions.defaults().setObjectMapperFactory(instance.factory).setShouldCopyDefaults(true).setHeader(HEADER)); root = config.getValue(TypeToken.of(ConfigCategory.class), new ConfigCategory()); loader.save(config); return root; } catch (Exception e) { MagiBridge.getLogger().error("Could not load config.", e); return root; } }
private <T> void loadMapper(ObjectMapper<T>.BoundInstance mapper, Path file, ConfigurationOptions options) { ConfigurationNode rootNode; if (mapper != null) { HoconConfigurationLoader loader = HoconConfigurationLoader.builder().setPath(file).build(); try { rootNode = loader.load(options.setShouldCopyDefaults(true)); ConfigurationNode hashNode = rootNode.getNode("hashAlgo"); if ("bcrypt".equalsIgnoreCase(hashNode.getString())) { hashNode.setValue("BCrypt"); } //load the config into the object mapper.populate(rootNode); //add missing default values loader.save(rootNode); } catch (ObjectMappingException objMappingExc) { logger.error("Error loading the configuration", objMappingExc); } catch (IOException ioExc) { logger.error("Error saving the default configuration", ioExc); } } }
@Test public void testDefaultsCopied() { SimpleConfigurationNode subject = SimpleConfigurationNode.root(ConfigurationOptions.defaults().setShouldCopyDefaults(true)); assertNull(subject.getValue()); assertEquals("default value", subject.getValue("default value")); assertEquals("default value", subject.getValue()); }