@Nullable @Override protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException { String argument = (String) super.parseValue(source, args); Callable<BufferedReader> reader = () -> new BufferedReader(new StringReader(argument)); ConfigurationLoader<? extends ConfigurationNode> loader = HoconConfigurationLoader.builder() .setSource(reader).build(); ConfigurationNode node; try { node = loader.load(); } catch (IOException ex) { throw args.createError(Text.of("Node parsing failed: ", ex.getMessage())); } return DataTranslators.CONFIGURATION_NODE.translate(node); }
@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); } }
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 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()); }
/** * Create a new configuration node populated with the appropriate data. * * @return The newly constructed node * @throws java.io.IOException if any sort of error occurs with reading or parsing the configuration */ default NodeType load() throws IOException { return load(getDefaultOptions()); }
@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)); }
public static <T extends BaseConfig> T load(Path path, T defaultConfig) { ConfigurationLoader<CommentedConfigurationNode> loader = HoconConfigurationLoader.builder() .setPath(path) .build(); CommentedConfigurationNode node; try { node = loader.load(); } catch (IOException e) { e.printStackTrace(); node = loader.createEmptyNode(); } T config = null; try { config = (T)node.getValue(TypeToken.of(defaultConfig.getClass())); } catch (ObjectMappingException e) { e.printStackTrace(); } if (config == null) { config = defaultConfig; } config.setLoader(loader); config.setNode(node); config.save(); return config; } }
/** * Return an empty node of the most appropriate type for this loader, using the default options. * * @return The appropriate node type */ default NodeType createEmptyNode() { return createEmptyNode(getDefaultOptions()); }
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")); } }
@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()); } }
/** * Attempts to load a {@link ConfigurationNode} using this loader, from the defined source. * * <p>The resultant node represents the root of the configuration being loaded.</p> * * <p>The {@link #getDefaultOptions() default options} will be used to construct the resultant * configuration nodes.</p> * * @return The newly constructed node * @throws IOException if any sort of error occurs with reading or parsing the configuration */ @NonNull default NodeType load() throws IOException { return load(getDefaultOptions()); }
/** * Return an empty node of the most appropriate type for this loader, using the default options. * * @return The appropriate node type */ @NonNull default NodeType createEmptyNode() { return createEmptyNode(getDefaultOptions()); }
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; }
defaultConfig.getParentFile().mkdirs(); defaultConfig.createNewFile(); rootNode = configManager.createEmptyNode(ConfigurationOptions.defaults()); Prism.getInstance().getLogger().info("Creating new config at mods/Prism/Prism.conf"); } else { rootNode = configManager.load(); configManager.save(rootNode); } catch(IOException e) {
options = options.setShouldCopyDefaults(true); node = config.load(options); } catch (IOException e) { logger.warn("Error loading WorldEdit configuration", e);
/** * Attempts to load a {@link ConfigurationNode} using this loader, from the defined source. * * <p>The resultant node represents the root of the configuration being loaded.</p> * * <p>The {@link #getDefaultOptions() default options} will be used to construct the resultant * configuration nodes.</p> * * @return The newly constructed node * @throws IOException if any sort of error occurs with reading or parsing the configuration */ @NonNull default NodeType load() throws IOException { return load(getDefaultOptions()); }
private void saveSync() throws IOException { if (saveSuppressed.get() <= 0) { if (dirty.compareAndSet(true, false)) { permissionsFileLoader.save(permissionsConfig); } } }
/** * Return an empty node of the most appropriate type for this loader, using the default options. * * @return The appropriate node type */ @NonNull default NodeType createEmptyNode() { return createEmptyNode(getDefaultOptions()); }
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); } }
/** * Loads configuration */ public void load() { try { baseNode = loader.load(); configuration = mapper.populate(Objects.requireNonNull(baseNode).getNode(baseNodeName)); } catch (ObjectMappingException | IOException e) { throw new RuntimeException(e); } }