Search in sources :

Example 6 with UnmodifiableConfig

use of com.electronwill.nightconfig.core.UnmodifiableConfig in project LoliServer by Loli-Server.

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 7 with UnmodifiableConfig

use of com.electronwill.nightconfig.core.UnmodifiableConfig in project Create by Creators-of-Create.

the class SubMenuConfigScreen method find.

public static SubMenuConfigScreen find(ConfigHelper.ConfigPath path) {
    // TODO 1.17: can be null
    ForgeConfigSpec spec = ConfigHelper.findForgeConfigSpecFor(path.getType(), path.getModID());
    UnmodifiableConfig values = spec.getValues();
    BaseConfigScreen base = new BaseConfigScreen(null, path.getModID());
    SubMenuConfigScreen screen = new SubMenuConfigScreen(base, "root", path.getType(), spec, values);
    List<String> remainingPath = Lists.newArrayList(path.getPath());
    path: while (!remainingPath.isEmpty()) {
        String next = remainingPath.remove(0);
        for (Map.Entry<String, Object> entry : values.valueMap().entrySet()) {
            String key = entry.getKey();
            Object obj = entry.getValue();
            if (!key.equalsIgnoreCase(next))
                continue;
            if (!(obj instanceof AbstractConfig)) {
                // highlight entry
                screen.highlights.add(path.getPath()[path.getPath().length - 1]);
                continue;
            }
            values = (UnmodifiableConfig) obj;
            screen = new SubMenuConfigScreen(screen, toHumanReadable(key), path.getType(), spec, values);
            continue path;
        }
        break;
    }
    ConfigScreen.modID = path.getModID();
    return screen;
}
Also used : AbstractConfig(com.electronwill.nightconfig.core.AbstractConfig) ValueEntry(com.simibubi.create.foundation.config.ui.entries.ValueEntry) SubMenuEntry(com.simibubi.create.foundation.config.ui.entries.SubMenuEntry) EnumEntry(com.simibubi.create.foundation.config.ui.entries.EnumEntry) LabeledEntry(com.simibubi.create.foundation.config.ui.ConfigScreenList.LabeledEntry) BooleanEntry(com.simibubi.create.foundation.config.ui.entries.BooleanEntry) NumberEntry(com.simibubi.create.foundation.config.ui.entries.NumberEntry) ForgeConfigSpec(net.minecraftforge.common.ForgeConfigSpec) UnmodifiableConfig(com.electronwill.nightconfig.core.UnmodifiableConfig)

Aggregations

UnmodifiableConfig (com.electronwill.nightconfig.core.UnmodifiableConfig)7 Map (java.util.Map)5 CommentedConfig (com.electronwill.nightconfig.core.CommentedConfig)4 Config (com.electronwill.nightconfig.core.Config)4 CorrectionAction (com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction)4 FileConfig (com.electronwill.nightconfig.core.file.FileConfig)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 ForgeConfigSpec (net.minecraftforge.common.ForgeConfigSpec)3 AbstractConfig (com.electronwill.nightconfig.core.AbstractConfig)2 LabeledEntry (com.simibubi.create.foundation.config.ui.ConfigScreenList.LabeledEntry)2 BooleanEntry (com.simibubi.create.foundation.config.ui.entries.BooleanEntry)2 EnumEntry (com.simibubi.create.foundation.config.ui.entries.EnumEntry)2 NumberEntry (com.simibubi.create.foundation.config.ui.entries.NumberEntry)2 SubMenuEntry (com.simibubi.create.foundation.config.ui.entries.SubMenuEntry)2 ValueEntry (com.simibubi.create.foundation.config.ui.entries.ValueEntry)2 Lists (com.google.common.collect.Lists)1 PoseStack (com.mojang.blaze3d.vertex.PoseStack)1 AllIcons (com.simibubi.create.foundation.gui.AllIcons)1 ConfirmationScreen (com.simibubi.create.foundation.gui.ConfirmationScreen)1