Search in sources :

Example 1 with ObjectConverter

use of com.electronwill.nightconfig.core.conversion.ObjectConverter 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)

Aggregations

CommentedConfig (com.electronwill.nightconfig.core.CommentedConfig)1 ObjectConverter (com.electronwill.nightconfig.core.conversion.ObjectConverter)1 CommentedFileConfig (com.electronwill.nightconfig.core.file.CommentedFileConfig)1