@Override public void load() { super.load(); creativeEnable = node.getNode("use-in-creative").getBoolean(false); cheatMode = node.getNode("cheat-mode").getBoolean(false); try { config.save(node); } catch (IOException e) { logger.warn("Error loading WorldEdit configuration", e); } }
private void saveSync() throws IOException { if (saveSuppressed.get() <= 0) { if (dirty.compareAndSet(true, false)) { permissionsFileLoader.save(permissionsConfig); } } }
public void save() { try { node.setValue(TypeToken.of(BaseConfig.class), this); loader.save(node); } catch (ObjectMappingException | IOException e) { e.printStackTrace(); } }
@Override public void save() throws IOException { try { ObjectMapper.forObject(this).serialize(this.node); } catch (ObjectMappingException e) { throw new IOException(e); } this.loader.save(node); }
/** * Saves configuration */ public void save() { try { mapper.serialize(Objects.requireNonNull(baseNode).getNode(baseNodeName)); loader.save(baseNode); } catch (ObjectMappingException | IOException e) { throw new RuntimeException(e); } }
private void load() throws IOException { try { ObjectMapper.forObject(this).populate(this.node); } catch (ObjectMappingException e) { throw new IOException(e); } this.loader.save(node); }
public void load() { try { if (!mainConfig.exists()) { mainConfig.getParentFile().mkdirs(); mainConfig.createNewFile(); } config = configManager.load(CraftBookPlugin.spongeInst().configurationOptions); enabledMechanics.load(config); dataOnlyMode.load(config); configManager.save(config); } catch (IOException e) { plugin.getLogger().error("The CraftBook Configuration could not be read!", e); } }
private void convertFromBukkit() throws IOException { Path bukkitConfigPath = Paths.get("plugins/PermissionsEx"); if (Files.isDirectory(bukkitConfigPath) && isDirectoryEmpty(configDir)) { logger.info(t("Migrating configuration data from Bukkit")); Files.move(bukkitConfigPath, configDir, StandardCopyOption.REPLACE_EXISTING); } Path bukkitConfigFile = configDir.resolve("config.yml"); if (Files.exists(bukkitConfigFile)) { ConfigurationLoader<ConfigurationNode> yamlReader = YAMLConfigurationLoader.builder().setPath(bukkitConfigFile).build(); ConfigurationNode bukkitConfig = yamlReader.load(); configLoader.save(bukkitConfig); Files.move(bukkitConfigFile, configDir.resolve("config.yml.bukkit")); } }
private Path migrateLegacy(Path permissionsFile, String extension, ConfigurationLoader<?> loader, String formatName) throws PermissionsLoadingException { Path legacyPermissionsFile = permissionsFile; file = file.replace(extension, ".json"); permissionsFile = getManager().getBaseDirectory().resolve(file); permissionsFileLoader = createLoader(permissionsFile); try { permissionsConfig = loader.load(); permissionsFileLoader.save(permissionsConfig); Files.move(legacyPermissionsFile, legacyPermissionsFile.resolveSibling(legacyPermissionsFile.getFileName().toString() + ".legacy-backup")); } catch (IOException e) { throw new PermissionsLoadingException(t("While loading legacy %s permissions from %s", formatName, legacyPermissionsFile), e); } return permissionsFile; }
public void save() { try { // Reload the actual config to account for changes. But don't load it back into the ConfigValues. config = configManager.load(CraftBookPlugin.spongeInst().configurationOptions); enabledMechanics.save(config); dataOnlyMode.save(config); List<String> disabledMechanicsList = plugin.moduleController.getModules().stream().filter(entry -> !enabledMechanics.getValue().contains(entry.getName())).map(ModuleWrapper::getName).collect(Collectors.toList()); disabledMechanics.setValue(disabledMechanicsList); disabledMechanics.save(config); configManager.save(config); } catch (IOException e) { CraftBookPlugin.spongeInst().getLogger().error("The CraftBook configuration could not be saved!", e); } }
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; } }
/** * Disables metrics for the server by setting "opt-out" to true in the config file and canceling the metrics task. * * @throws IOException */ public void disable() throws IOException { // This has to be synchronized or it can collide with the check in the task. synchronized (optOutLock) { // Check if the server owner has already set opt-out, if not, set it. if (!isOptOut()) { config.getNode("mcstats.opt-out").setValue(true); configurationLoader.save(config); } // Disable Task, if it is running if (task != null) { task.cancel(); task = null; } } }
/** * Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task. * * @throws IOException */ public void enable() throws IOException { // This has to be synchronized or it can collide with the check in the task. synchronized (optOutLock) { // Check if the server owner has already set opt-out, if not, set it. if (isOptOut()) { config.getNode("mcstats.opt-out").setValue(false); configurationLoader.save(config); } // Enable Task, if it is not running if (task == null) { start(); } } }
@Test public void testSavingEmptyFile(@TempDirectory.TempDir Path tempDir) throws IOException { final File tempFile = tempDir.resolve("text2.txt").toFile(); tempFile.createNewFile(); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder() .setFile(tempFile) .build(); ConfigurationNode n = SimpleConfigurationNode.root(); loader.save(n); }
/** * Loads the configuration */ private void loadConfiguration() { configurationFile = getConfigFile(); configurationLoader = HoconConfigurationLoader.builder().setFile(configurationFile).build(); try { if (!configurationFile.exists()) { configurationFile.createNewFile(); config = configurationLoader.load(); config.setComment("This contains settings for MCStats: http://mcstats.org"); config.getNode("mcstats.guid").setValue(UUID.randomUUID().toString()); config.getNode("mcstats.opt-out").setValue(false); config.getNode("mcstats.debug").setValue(false); configurationLoader.save(config); } else { config = configurationLoader.load(); } guid = config.getNode("mcstats.guid").getString(); debug = config.getNode("mcstats.debug").getBoolean(); } catch (final IOException e) { e.printStackTrace(); } }
@Test @Disabled("Gson currently makes it rather difficult to get the correct number type") public void testRoundtrippingLong(@TempDirectory.TempDir Path tempDir) throws IOException { final Path tempFile = tempDir.resolve("text5.txt"); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder().setPath(tempFile).build(); ConfigurationNode start = loader.createEmptyNode(); start.getNode("long-num").setValue(TEST_LONG_VAL); loader.save(start); System.out.println(Files.readAllLines(tempFile)); ConfigurationNode ret = loader.load(); System.out.println(ret.getNode("long-num").getValue().getClass()); assertEquals(TEST_LONG_VAL, ret.getNode("long-num").getValue()); } }
@Test public void testSimpleLoading(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example.json"); final Path tempFile = tempDir.resolve("text1.txt"); ConfigurationLoader loader = JSONConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) .setSink(AtomicFiles.createAtomicWriterFactory(tempFile, UTF_8)).build(); ConfigurationNode node = loader.load(ConfigurationOptions.defaults().setMapFactory(MapFactories.sortedNatural())); assertEquals("unicorn", node.getNode("test", "op-level").getValue()); assertEquals("dragon", node.getNode("other", "op-level").getValue()); assertEquals("dog park", node.getNode("other", "location").getValue()); /*CommentedConfigurationNode commentNode = SimpleCommentedConfigurationNode.root(); commentNode.getNode("childOne").setValue("a").setComment("Test comment"); commentNode.getNode("childTwo", "something").setValue("b").setComment("Test comment 2"); commentNode.getNode("childTwo", "another").setValue("b").setComment("Test comment 3"); */ loader.save(node); assertEquals(Resources.readLines(url, UTF_8), Files .readAllLines(tempFile, UTF_8)); }
private void testRoundtripValue(Path tempDir, Object value) throws IOException { final Path tempFile = tempDir.resolve("text2.txt"); ConfigurationLoader<ConfigurationNode> loader = JSONConfigurationLoader.builder().setPath(tempFile).build(); ConfigurationNode start = loader.createEmptyNode(); start.getNode("value").setValue(value); loader.save(start); ConfigurationNode ret = loader.load(); assertEquals(value, ret.getNode("value").getValue()); }
@Test public void testSimpleLoading(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example.json"); final Path tempFile = tempDir.resolve("text1.txt"); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream()))) .setSink(AtomicFiles.createAtomicWriterFactory(tempFile, UTF_8)).setLenient(true).build(); ConfigurationNode node = loader.load(loader.getDefaultOptions().setMapFactory(MapFactories.sortedNatural())); assertEquals("unicorn", node.getNode("test", "op-level").getValue()); assertEquals("dragon", node.getNode("other", "op-level").getValue()); assertEquals("dog park", node.getNode("other", "location").getValue()); assertTrue(node.getNode("int-val").getValue() instanceof Integer); assertTrue(node.getNode("double-val").getValue() instanceof Double); loader.save(node); assertEquals(Resources.readLines(url, UTF_8), Files.readAllLines(tempFile, UTF_8)); }