Search in sources :

Example 1 with Config

use of com.electronwill.nightconfig.core.Config in project BYG by AOCAWOL.

the class CommentedConfigBuilder method organizeConfig.

public static CommentedConfig organizeConfig(CommentedConfig config) {
    CommentedConfig newConfig = CommentedConfig.of(Config.getDefaultMapCreator(false, true), TomlFormat.instance());
    List<Map.Entry<String, Object>> organizedCollection = config.valueMap().entrySet().stream().sorted(Comparator.comparing(Objects::toString)).collect(Collectors.toList());
    organizedCollection.forEach((stringObjectEntry -> {
        newConfig.add(stringObjectEntry.getKey(), stringObjectEntry.getValue());
    }));
    newConfig.commentMap().putAll(config.commentMap());
    return newConfig;
}
Also used : Config(com.electronwill.nightconfig.core.Config) TomlWriter(com.electronwill.nightconfig.toml.TomlWriter) Files(java.nio.file.Files) StringRepresentable(net.minecraft.util.StringRepresentable) WritingMode(com.electronwill.nightconfig.core.io.WritingMode) IOException(java.io.IOException) CommentedFileConfig(com.electronwill.nightconfig.core.file.CommentedFileConfig) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig) List(java.util.List) Map(java.util.Map) TomlFormat(com.electronwill.nightconfig.toml.TomlFormat) Comparator(java.util.Comparator) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) Objects(java.util.Objects) CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig)

Example 2 with Config

use of com.electronwill.nightconfig.core.Config in project emuStudio by emustudio.

the class PluginConfig method create.

public static PluginConfig create(String id, PLUGIN_TYPE pluginType, String pluginName, String pluginFile, P schemaLocation, Config pluginSettings) {
    Config config = Config.inMemory();
    config.set("id", id);
    config.set("type", pluginType.toString());
    config.set("name", pluginName);
    config.set("path", pluginFile);
    config.set("schemaPoint", schemaLocation.toSchemaPoint().toString());
    config.set("settings", pluginSettings);
    return new PluginConfig(config);
}
Also used : Config(com.electronwill.nightconfig.core.Config)

Example 3 with Config

use of com.electronwill.nightconfig.core.Config in project Mohist by MohistMC.

the class ForgeConfigSpec method correct.

private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, CorrectionListener commentListener, boolean dryRun) {
    int count = 0;
    Map<String, Object> specMap = spec.valueMap();
    Map<String, Object> configMap = config.valueMap();
    for (Map.Entry<String, Object> specEntry : specMap.entrySet()) {
        final String key = specEntry.getKey();
        final Object specValue = specEntry.getValue();
        final Object configValue = configMap.get(key);
        final CorrectionAction action = configValue == null ? ADD : REPLACE;
        parentPath.addLast(key);
        if (specValue instanceof Config) {
            if (configValue instanceof CommentedConfig) {
                count += correct((Config) specValue, (CommentedConfig) configValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
                if (count > 0 && dryRun)
                    return count;
            } else if (dryRun) {
                return 1;
            } else {
                CommentedConfig newValue = config.createSubConfig();
                configMap.put(key, newValue);
                listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
                count++;
                count += correct((Config) specValue, newValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
            }
            String newComment = levelComments.get(parentPath);
            String oldComment = config.getComment(key);
            if (!stringsMatchIgnoringNewlines(oldComment, newComment)) {
                if (commentListener != null)
                    commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, newComment);
                if (dryRun)
                    return 1;
                config.setComment(key, newComment);
            }
        } else {
            ValueSpec valueSpec = (ValueSpec) specValue;
            if (!valueSpec.test(configValue)) {
                if (dryRun)
                    return 1;
                Object newValue = valueSpec.correct(configValue);
                configMap.put(key, newValue);
                listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
                count++;
            }
            String oldComment = config.getComment(key);
            if (!stringsMatchIgnoringNewlines(oldComment, valueSpec.getComment())) {
                if (commentListener != null)
                    commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, valueSpec.getComment());
                if (dryRun)
                    return 1;
                config.setComment(key, valueSpec.getComment());
            }
        }
        parentPath.removeLast();
    }
    // Second step: removes the unspecified values
    for (Iterator<Map.Entry<String, Object>> ittr = configMap.entrySet().iterator(); ittr.hasNext(); ) {
        Map.Entry<String, Object> entry = ittr.next();
        if (!specMap.containsKey(entry.getKey())) {
            if (dryRun)
                return 1;
            ittr.remove();
            parentPath.addLast(entry.getKey());
            listener.onCorrect(REMOVE, parentPathUnmodifiable, entry.getValue(), null);
            parentPath.removeLast();
            count++;
        }
    }
    return count;
}
Also used : CorrectionAction(com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction) FileConfig(com.electronwill.nightconfig.core.file.FileConfig) Config(com.electronwill.nightconfig.core.Config) CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig) UnmodifiableConfig(com.electronwill.nightconfig.core.UnmodifiableConfig) CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with Config

use of com.electronwill.nightconfig.core.Config in project Magma-1.16.x by magmafoundation.

the class ForgeConfigSpec method correct.

private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, CorrectionListener commentListener, boolean dryRun) {
    int count = 0;
    Map<String, Object> specMap = spec.valueMap();
    Map<String, Object> configMap = config.valueMap();
    for (Map.Entry<String, Object> specEntry : specMap.entrySet()) {
        final String key = specEntry.getKey();
        final Object specValue = specEntry.getValue();
        final Object configValue = configMap.get(key);
        final CorrectionAction action = configValue == null ? ADD : REPLACE;
        parentPath.addLast(key);
        if (specValue instanceof Config) {
            if (configValue instanceof CommentedConfig) {
                count += correct((Config) specValue, (CommentedConfig) configValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
                if (count > 0 && dryRun)
                    return count;
            } else if (dryRun) {
                return 1;
            } else {
                CommentedConfig newValue = config.createSubConfig();
                configMap.put(key, newValue);
                listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
                count++;
                count += correct((Config) specValue, newValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
            }
            String newComment = levelComments.get(parentPath);
            String oldComment = config.getComment(key);
            if (!stringsMatchIgnoringNewlines(oldComment, newComment)) {
                if (commentListener != null)
                    commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, newComment);
                if (dryRun)
                    return 1;
                config.setComment(key, newComment);
            }
        } else {
            ValueSpec valueSpec = (ValueSpec) specValue;
            if (!valueSpec.test(configValue)) {
                if (dryRun)
                    return 1;
                Object newValue = valueSpec.correct(configValue);
                configMap.put(key, newValue);
                listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
                count++;
            }
            String oldComment = config.getComment(key);
            if (!stringsMatchIgnoringNewlines(oldComment, valueSpec.getComment())) {
                if (commentListener != null)
                    commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, valueSpec.getComment());
                if (dryRun)
                    return 1;
                config.setComment(key, valueSpec.getComment());
            }
        }
        parentPath.removeLast();
    }
    // Second step: removes the unspecified values
    for (Iterator<Map.Entry<String, Object>> ittr = configMap.entrySet().iterator(); ittr.hasNext(); ) {
        Map.Entry<String, Object> entry = ittr.next();
        if (!specMap.containsKey(entry.getKey())) {
            if (dryRun)
                return 1;
            ittr.remove();
            parentPath.addLast(entry.getKey());
            listener.onCorrect(REMOVE, parentPathUnmodifiable, entry.getValue(), null);
            parentPath.removeLast();
            count++;
        }
    }
    return count;
}
Also used : CorrectionAction(com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction) FileConfig(com.electronwill.nightconfig.core.file.FileConfig) Config(com.electronwill.nightconfig.core.Config) CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig) UnmodifiableConfig(com.electronwill.nightconfig.core.UnmodifiableConfig) CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with Config

use of com.electronwill.nightconfig.core.Config in project MinecraftForge by MinecraftForge.

the class ForgeConfigSpec method correct.

private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, CorrectionListener commentListener, boolean dryRun) {
    int count = 0;
    Map<String, Object> specMap = spec.valueMap();
    Map<String, Object> configMap = config.valueMap();
    for (Map.Entry<String, Object> specEntry : specMap.entrySet()) {
        final String key = specEntry.getKey();
        final Object specValue = specEntry.getValue();
        final Object configValue = configMap.get(key);
        final CorrectionAction action = configValue == null ? ADD : REPLACE;
        parentPath.addLast(key);
        if (specValue instanceof Config) {
            if (configValue instanceof CommentedConfig) {
                count += correct((Config) specValue, (CommentedConfig) configValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
                if (count > 0 && dryRun)
                    return count;
            } else if (dryRun) {
                return 1;
            } else {
                CommentedConfig newValue = config.createSubConfig();
                configMap.put(key, newValue);
                listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
                count++;
                count += correct((Config) specValue, newValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
            }
            String newComment = levelComments.get(parentPath);
            String oldComment = config.getComment(key);
            if (!stringsMatchIgnoringNewlines(oldComment, newComment)) {
                if (commentListener != null)
                    commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, newComment);
                if (dryRun)
                    return 1;
                config.setComment(key, newComment);
            }
        } else {
            ValueSpec valueSpec = (ValueSpec) specValue;
            if (!valueSpec.test(configValue)) {
                if (dryRun)
                    return 1;
                Object newValue = valueSpec.correct(configValue);
                configMap.put(key, newValue);
                listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
                count++;
            }
            String oldComment = config.getComment(key);
            if (!stringsMatchIgnoringNewlines(oldComment, valueSpec.getComment())) {
                if (commentListener != null)
                    commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, valueSpec.getComment());
                if (dryRun)
                    return 1;
                config.setComment(key, valueSpec.getComment());
            }
        }
        parentPath.removeLast();
    }
    // Second step: removes the unspecified values
    for (Iterator<Map.Entry<String, Object>> ittr = configMap.entrySet().iterator(); ittr.hasNext(); ) {
        Map.Entry<String, Object> entry = ittr.next();
        if (!specMap.containsKey(entry.getKey())) {
            if (dryRun)
                return 1;
            ittr.remove();
            parentPath.addLast(entry.getKey());
            listener.onCorrect(REMOVE, parentPathUnmodifiable, entry.getValue(), null);
            parentPath.removeLast();
            count++;
        }
    }
    return count;
}
Also used : CorrectionAction(com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction) FileConfig(com.electronwill.nightconfig.core.file.FileConfig) Config(com.electronwill.nightconfig.core.Config) CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig) UnmodifiableConfig(com.electronwill.nightconfig.core.UnmodifiableConfig) CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Config (com.electronwill.nightconfig.core.Config)9 CommentedConfig (com.electronwill.nightconfig.core.CommentedConfig)5 FileConfig (com.electronwill.nightconfig.core.file.FileConfig)5 Map (java.util.Map)5 CorrectionAction (com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction)4 UnmodifiableConfig (com.electronwill.nightconfig.core.UnmodifiableConfig)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 CommentedFileConfig (com.electronwill.nightconfig.core.file.CommentedFileConfig)1 WritingMode (com.electronwill.nightconfig.core.io.WritingMode)1 TomlFormat (com.electronwill.nightconfig.toml.TomlFormat)1 TomlWriter (com.electronwill.nightconfig.toml.TomlWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Objects (java.util.Objects)1