Search in sources :

Example 1 with CommentedConfig

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

the class CommentedConfigBuilder method addMap.

public Map<?, ?> addMap(String comment, String key, Map<?, Number> defaultValue) {
    if (config.get(key) == null) {
        CommentedConfig subConfig = config.createSubConfig();
        defaultValue.forEach((a, b) -> {
            String subConfigKey = a.toString();
            if (subConfig.get(a.toString()) == null) {
                subConfig.set(subConfigKey, b);
            }
        });
        config.set(key, subConfig);
    }
    CommentedConfig subConfig = config.get(key);
    String commentValue = config.getComment(key);
    if (commentValue == null) {
        config.setComment(key, comment);
    }
    return subConfig.valueMap();
}
Also used : CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig)

Example 2 with CommentedConfig

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

the class CommentedConfigBuilder method addSubConfig.

public CommentedConfigBuilder addSubConfig(String comment, String key, CommentedConfigBuilder defaultValue) {
    if (config.get(key) == null) {
        config.set(key, organizeConfig(defaultValue.config));
    }
    CommentedConfig subConfig = config.get(key);
    String commentValue = config.getComment(key);
    if (commentValue == null) {
        config.setComment(key, comment);
    }
    return new CommentedConfigBuilder(subConfig);
}
Also used : CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig)

Example 3 with CommentedConfig

use of com.electronwill.nightconfig.core.CommentedConfig 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 4 with CommentedConfig

use of com.electronwill.nightconfig.core.CommentedConfig in project ProtectionStones by espidev.

the class PSConfig method initConfig.

static void initConfig() {
    // check if using config v1 or v2 (config.yml -> config.toml)
    if (new File(ProtectionStones.getInstance().getDataFolder() + "/config.yml").exists() && !ProtectionStones.configLocation.exists()) {
        LegacyUpgrade.upgradeFromV1V2();
    }
    // check if config files exist
    try {
        if (!ProtectionStones.getInstance().getDataFolder().exists()) {
            ProtectionStones.getInstance().getDataFolder().mkdir();
        }
        if (!ProtectionStones.blockDataFolder.exists()) {
            ProtectionStones.blockDataFolder.mkdir();
            Files.copy(PSConfig.class.getResourceAsStream("/block1.toml"), Paths.get(ProtectionStones.blockDataFolder.getAbsolutePath() + "/block1.toml"), StandardCopyOption.REPLACE_EXISTING);
        }
        if (!ProtectionStones.configLocation.exists()) {
            Files.copy(PSConfig.class.getResourceAsStream("/config.toml"), Paths.get(ProtectionStones.configLocation.toURI()), StandardCopyOption.REPLACE_EXISTING);
        }
    } catch (IOException ex) {
        Logger.getLogger(ProtectionStones.class.getName()).log(Level.SEVERE, null, ex);
    }
    // keep in mind that there is /ps reload, so clear arrays before adding config options!
    // clear data (for /ps reload)
    ProtectionStones.protectionStonesOptions.clear();
    // create config object
    if (ProtectionStones.config == null) {
        ProtectionStones.config = CommentedFileConfig.builder(ProtectionStones.configLocation).sync().build();
    }
    // loop upgrades until the config has been updated to the latest version
    do {
        // load latest settings
        ProtectionStones.config.load();
        // load config into configOptions object
        ProtectionStones.getInstance().setConfigOptions(new ObjectConverter().toObject(ProtectionStones.config, PSConfig::new));
        // upgrade config if need be (v3+)
        boolean leaveLoop = ConfigUpgrades.doConfigUpgrades();
        // leave loop if config version is correct
        if (leaveLoop)
            break;
        // save config if upgrading
        ProtectionStones.config.save();
    } while (true);
    // load protection stones to options map
    if (ProtectionStones.blockDataFolder.listFiles().length == 0) {
        ProtectionStones.getPluginLogger().warning("The blocks folder is empty! You do not have any protection blocks configured!");
    } else {
        // temp file to load in default ps block config
        File tempFile;
        try {
            tempFile = File.createTempFile("psconfigtemp", ".toml");
            try (FileOutputStream out = new FileOutputStream(tempFile)) {
                IOUtils.copy(PSConfig.class.getResourceAsStream("/block1.toml"), out);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        CommentedFileConfig template = CommentedFileConfig.of(tempFile);
        template.load();
        // iterate over block files and load into map
        ProtectionStones.getPluginLogger().info("Protection Stone Blocks:");
        for (File file : ProtectionStones.blockDataFolder.listFiles()) {
            CommentedFileConfig c = CommentedFileConfig.builder(file).sync().build();
            c.load();
            // check to make sure all options are not null
            boolean updated = false;
            for (String str : template.valueMap().keySet()) {
                if (c.get(str) == null) {
                    c.set(str, template.get(str));
                    c.setComment(str, template.getComment(str));
                    updated = true;
                } else if (c.get(str) instanceof CommentedConfig) {
                    // no DFS for now (since there's only 2 layers of config)
                    CommentedConfig template2 = template.get(str);
                    CommentedConfig c2 = c.get(str);
                    for (String str2 : template2.valueMap().keySet()) {
                        if (c2.get(str2) == null) {
                            c2.add(str2, template2.get(str2));
                            c2.setComment(str2, template2.getComment(str2));
                            updated = true;
                        }
                    }
                }
            }
            if (updated)
                c.save();
            // convert toml data into object
            PSProtectBlock b = new ObjectConverter().toObject(c, PSProtectBlock::new);
            // check if material is valid, and is not a player head (since player heads also have the player name after)
            if (Material.getMaterial(b.type) == null && !(b.type.startsWith(Material.PLAYER_HEAD.toString()))) {
                ProtectionStones.getPluginLogger().warning("Unrecognized material: " + b.type);
                ProtectionStones.getPluginLogger().warning("Block will not be added. Please fix this in your config.");
                continue;
            }
            // check for duplicates
            if (ProtectionStones.isProtectBlockType(b.type)) {
                ProtectionStones.getPluginLogger().warning("Duplicate block type found! Ignoring the extra block " + b.type);
                continue;
            }
            if (ProtectionStones.getProtectBlockFromAlias(b.alias) != null) {
                ProtectionStones.getPluginLogger().warning("Duplicate block alias found! Ignoring the extra block " + b.alias);
                continue;
            }
            ProtectionStones.getPluginLogger().info("- " + b.type + " (" + b.alias + ")");
            // process flags for block and set regionFlags field
            FlagHandler.initDefaultFlagsForBlock(b);
            // for PLAYER_HEAD:base64, we need to change the entry to link to a UUID hash instead of storing the giant base64
            if (BlockUtil.isBase64PSHead(b.type)) {
                String nuuid = BlockUtil.getUUIDFromBase64PS(b);
                BlockUtil.uuidToBase64Head.put(nuuid, b.type.split(":")[1]);
                b.type = "PLAYER_HEAD:" + nuuid;
            }
            // add block
            ProtectionStones.protectionStonesOptions.put(b.type, b);
        }
        // cleanup temp file
        template.close();
        tempFile.delete();
        // setup crafting recipes for all blocks
        setupRecipes();
    }
}
Also used : CommentedFileConfig(com.electronwill.nightconfig.core.file.CommentedFileConfig) ObjectConverter(com.electronwill.nightconfig.core.conversion.ObjectConverter) CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig)

Example 5 with CommentedConfig

use of com.electronwill.nightconfig.core.CommentedConfig in project Champions by TheIllusiveC4.

the class Champions method config.

private void config(final ModConfigEvent evt) {
    if (evt.getConfig().getModId().equals(MODID)) {
        if (evt.getConfig().getType() == Type.SERVER) {
            ChampionsConfig.bake();
            ForgeConfigSpec spec = evt.getConfig().getSpec();
            CommentedConfig commentedConfig = evt.getConfig().getConfigData();
            if (spec == ChampionsConfig.RANKS_SPEC) {
                ChampionsConfig.transformRanks(commentedConfig);
                RankManager.buildRanks();
            } else if (spec == ChampionsConfig.AFFIXES_SPEC) {
                ChampionsConfig.transformAffixes(commentedConfig);
                AffixManager.buildAffixSettings();
            } else if (spec == ChampionsConfig.ENTITIES_SPEC) {
                ChampionsConfig.transformEntities(commentedConfig);
                EntityManager.buildEntitySettings();
            }
        } else if (evt.getConfig().getType() == Type.CLIENT) {
            ClientChampionsConfig.bake();
        }
    }
}
Also used : ForgeConfigSpec(net.minecraftforge.common.ForgeConfigSpec) CommentedConfig(com.electronwill.nightconfig.core.CommentedConfig)

Aggregations

CommentedConfig (com.electronwill.nightconfig.core.CommentedConfig)14 Config (com.electronwill.nightconfig.core.Config)5 Map (java.util.Map)5 CorrectionAction (com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction)4 UnmodifiableConfig (com.electronwill.nightconfig.core.UnmodifiableConfig)4 FileConfig (com.electronwill.nightconfig.core.file.FileConfig)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 CommentedFileConfig (com.electronwill.nightconfig.core.file.CommentedFileConfig)2 ObjectConverter (com.electronwill.nightconfig.core.conversion.ObjectConverter)1 WritingMode (com.electronwill.nightconfig.core.io.WritingMode)1 TomlFormat (com.electronwill.nightconfig.toml.TomlFormat)1 TomlWriter (com.electronwill.nightconfig.toml.TomlWriter)1 IOException (java.io.IOException)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 Collectors (java.util.stream.Collectors)1