com.electronwill.nightconfig.core.file.CommentedFileConfig Java Examples

The following examples show how to use com.electronwill.nightconfig.core.file.CommentedFileConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigFileTypeHandler.java    From patchwork-api with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Function<ModConfig, CommentedFileConfig> reader(Path configBasePath) {
	return (c) -> {
		final Path configPath = configBasePath.resolve(c.getFileName());
		final CommentedFileConfig configData = CommentedFileConfig.builder(configPath).sync()
				.preserveInsertionOrder()
				.autosave()
				.onFileNotFound((newfile, configFormat) -> setupConfigFile(c, newfile, configFormat))
				.writingMode(WritingMode.REPLACE)
				.build();
		LOGGER.debug(CONFIG, "Built TOML config for {}", configPath.toString());
		configData.load();
		LOGGER.debug(CONFIG, "Loaded TOML config file {}", configPath.toString());

		try {
			FileWatcher.defaultInstance().addWatch(configPath, new ConfigWatcher(c, configData, Thread.currentThread().getContextClassLoader()));
			LOGGER.debug(CONFIG, "Watching TOML config file {} for changes", configPath.toString());
		} catch (IOException e) {
			throw new RuntimeException("Couldn't watch config file", e);
		}

		return configData;
	};
}
 
Example #2
Source File: ConfigTracker.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void openConfig(final ModConfig config, final Path configBasePath) {
	LOGGER.debug(CONFIG, "Loading config file type {} at {} for {}", config.getType(), config.getFileName(), config.getModId());
	final CommentedFileConfig configData = config.getHandler().reader(configBasePath).apply(config);
	config.setConfigData(configData);
	config.fireEvent(new ModConfig.Loading(config));
	config.save();
}
 
Example #3
Source File: Config.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void loadConfig(ForgeConfigSpec spec, Path path) {

        final CommentedFileConfig configData = CommentedFileConfig.builder(path)
                .sync()
                .autosave()
                .writingMode(WritingMode.REPLACE)
                .build();

        configData.load();
        spec.setConfig(configData);
    }
 
Example #4
Source File: ConfigFileTypeHandler.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
ConfigWatcher(final ModConfig modConfig, final CommentedFileConfig commentedFileConfig, final ClassLoader classLoader) {
	this.modConfig = modConfig;
	this.commentedFileConfig = commentedFileConfig;
	this.realClassLoader = classLoader;
}
 
Example #5
Source File: ModConfig.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void save() {
	((CommentedFileConfig) this.configData).save();
}
 
Example #6
Source File: ModConfig.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Path getFullPath() {
	return ((CommentedFileConfig) this.configData).getNioPath();
}
 
Example #7
Source File: Config.java    From Sandbox with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Config(Path path) {
    this.config = CommentedFileConfig.builder(path).autoreload().build();
    config.load();
}
 
Example #8
Source File: CommentedFileConfigTest.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void test() {
	File file = new File("test.conf");
	CommentedFileConfig config = CommentedFileConfig.of(file);
	System.out.println(config.size());
}
 
Example #9
Source File: ConversionTable.java    From night-config with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Returns an Config that converts "just-in-time" the values that are read from the specified
 * Config.
 *
 * @param config the config to wrap
 * @return a wrapper that converts the values read from the config
 */
public CommentedFileConfig wrapRead(CommentedFileConfig config) {
	return new ConvertedCommentedFileConfig(config, this::convert, v -> v,
											config.configFormat()::supportsType);
}
 
Example #10
Source File: ConversionTable.java    From night-config with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Returns an Config that converts "just-in-time" the values that are put into the specified
 * Config.
 *
 * @param config                    the config to wrap
 * @param supportValueTypePredicate Predicate that checks if a given class is supported by the
 *                                  returned config
 * @return a wrapper that converts the values put into the config
 */
public CommentedFileConfig wrapWrite(CommentedFileConfig config,
									 Predicate<Class<?>> supportValueTypePredicate) {
	return new ConvertedCommentedFileConfig(config, v -> v, this::convert,
											supportValueTypePredicate);
}