Search in sources :

Example 1 with CommentedFileConfig

use of com.electronwill.nightconfig.core.file.CommentedFileConfig in project OceanFloorControl by Lothrazar.

the class ConfigOcean method setup.

public static void setup() {
    Path path = FMLPaths.CONFIGDIR.get().resolve(ModOcean.MODID + ".toml");
    final CommentedFileConfig configData = CommentedFileConfig.builder(path).sync().autosave().writingMode(WritingMode.REPLACE).build();
    configData.load();
    COMMON_CONFIG.setConfig(configData);
}
Also used : Path(java.nio.file.Path) CommentedFileConfig(com.electronwill.nightconfig.core.file.CommentedFileConfig)

Example 2 with CommentedFileConfig

use of com.electronwill.nightconfig.core.file.CommentedFileConfig 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 3 with CommentedFileConfig

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

the class ConfigUpgrades method doConfigUpgrades.

// Upgrade the config one version up (ex. 3 -> 4)
public static boolean doConfigUpgrades() {
    boolean leaveLoop = false;
    switch(ProtectionStones.getInstance().getConfigOptions().configVersion) {
        case 3:
            ProtectionStones.config.set("config_version", 4);
            ProtectionStones.config.set("base_command", "ps");
            ProtectionStones.config.set("aliases", Arrays.asList("pstone", "protectionstones", "protectionstone"));
            break;
        case 4:
            ProtectionStones.config.set("config_version", 5);
            ProtectionStones.config.set("async_load_uuid_cache", false);
            ProtectionStones.config.set("ps_view_cooldown", 20);
            break;
        case 5:
            ProtectionStones.config.set("config_version", 6);
            ProtectionStones.config.set("allow_duplicate_region_names", false);
            break;
        case 6:
            ProtectionStones.config.set("config_version", 7);
            ProtectionStones.config.set("drop_item_when_inventory_full", true);
            break;
        case 7:
            ProtectionStones.config.set("config_version", 8);
            ProtectionStones.config.set("regions_must_be_adjacent", false);
            break;
        case 8:
            ProtectionStones.config.set("config_version", 9);
            ProtectionStones.config.set("allow_merging_regions", true);
            break;
        case 9:
            ProtectionStones.config.set("config_version", 10);
            ProtectionStones.config.set("allow_merging_holes", true);
            break;
        case 10:
            ProtectionStones.config.set("config_version", 11);
            for (File file : ProtectionStones.blockDataFolder.listFiles()) {
                CommentedFileConfig c = CommentedFileConfig.builder(file).sync().build();
                c.load();
                c.setComment("type", " Define your protection block below\n" + " Use block type from here: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html\n" + " --------------------------------------------------------------------------------------------------\n" + " If you want to use player heads, you can use \"PLAYER_HEAD:player_name\" (ex. \"PLAYER_HEAD:Notch\")\n" + " To use custom player heads, you need the base64 value of the head. On minecraft-heads.com, you will find this value in the Other section under \"Value:\".\n" + " To use UUIDs for player heads, go to https://sessionserver.mojang.com/session/minecraft/profile/PUT-UUID-HERE and copy the value from the \"value\" field not including quotes.\n" + " When you have the value, you can set the type to \"PLAYER_HEAD:value\"");
                try {
                    c.set("region.home_x_offset", ((Integer) c.get("region.home_x_offset")).doubleValue());
                    c.set("region.home_y_offset", ((Integer) c.get("region.home_y_offset")).doubleValue());
                    c.set("region.home_z_offset", ((Integer) c.get("region.home_z_offset")).doubleValue());
                } catch (Exception e) {
                }
                c.save();
                c.close();
            }
            break;
        case 11:
            ProtectionStones.config.set("config_version", 12);
            ProtectionStones.config.set("economy.max_rent_price", -1.0);
            ProtectionStones.config.set("economy.min_rent_price", 1.0);
            ProtectionStones.config.setComment("economy.max_rent_price", " Set limits on the price for renting. Set to -1.0 to disable.");
            ProtectionStones.config.set("economy.max_rent_period", -1);
            ProtectionStones.config.set("economy.min_rent_period", 1);
            ProtectionStones.config.setComment("economy.max_rent_period", " Set limits on the period between rent payments, in seconds (86400 seconds = 1 day). Set to -1 to disable.");
            ProtectionStones.config.set("economy.tax_enabled", false);
            ProtectionStones.config.set("economy.tax_message_on_join", true);
            ProtectionStones.config.setComment("economy.tax_enabled", " Set taxes on regions.\n" + " Taxes are configured in each individual block config.\n" + " Whether or not to enable the tax command.");
            for (File file : ProtectionStones.blockDataFolder.listFiles()) {
                CommentedFileConfig c = CommentedFileConfig.builder(file).sync().build();
                c.load();
                try {
                    List<String> l = c.get("region.hidden_flags_from_info");
                    l.addAll(Arrays.asList("ps-rent-settings", "ps-tax-payments-due", "ps-tax-last-payment-added", "ps-tax-autopayer"));
                    c.set("region.hidden_flags_from_info", l);
                } catch (Exception e) {
                }
                c.save();
                c.close();
            }
            break;
        case 12:
            ProtectionStones.config.set("config_version", 13);
            ProtectionStones.config.set("default_protection_block_placement_off", false);
            ProtectionStones.config.setComment("default_protection_block_placement_off", " Whether when players join, by default they have protection block placement toggled off (equivalent to running /ps toggle)");
            ProtectionStones.config.set("default_allow_addowner_for_offline_players_without_lp", false);
            ProtectionStones.config.setComment("default_allow_addowner_for_offline_players_without_lp", " If you do not have LuckPerms, ProtectionStones is unable to determine the limits of offline players (since it depends\n" + " on permissions), and so it requires players to be online. Set this to true if your server does not need limits (and so\n" + " the check is unnecessary).");
            break;
        case 13:
            ProtectionStones.config.set("config_version", 14);
            ProtectionStones.config.set("admin.cleanup_delete_regions_with_members_but_no_owners", true);
            ProtectionStones.config.setComment("admin.cleanup_delete_regions_with_members_but_no_owners", "     Whether /ps admin cleanup remove should delete regions that have members, but don't have owners (after inactive\n" + "     owners are removed).\n" + "     Regions that have no owners or members will be deleted regardless.");
            break;
        case 14:
            ProtectionStones.config.set("config_version", 15);
            // fix incorrect value set
            if (ProtectionStones.config.get("allow_addowner_for_offline_players_without_lp") == null) {
                Object value = ProtectionStones.config.get("default_allow_addowner_for_offline_players_without_lp");
                ProtectionStones.config.removeComment("default_allow_addowner_for_offline_players_without_lp");
                ProtectionStones.config.remove("default_allow_addowner_for_offline_players_without_lp");
                ProtectionStones.config.set("allow_addowner_for_offline_players_without_lp", value == null ? false : (boolean) value);
                ProtectionStones.config.setComment("allow_addowner_for_offline_players_without_lp", " If you do not have LuckPerms, ProtectionStones is unable to determine the limits of offline players (since it depends\n" + " on permissions), and so it requires players to be online. Set this to true if your server does not need limits (and so\n" + " the check is unnecessary).");
            }
            break;
        case 15:
            ProtectionStones.config.set("config_version", 16);
            ProtectionStones.config.set("allow_home_teleport_for_members", true);
            ProtectionStones.config.setComment("allow_home_teleport_for_members", " Whether or not members of a region can /ps home to the region.");
            break;
        case ProtectionStones.CONFIG_VERSION:
            leaveLoop = true;
            break;
        default:
            Bukkit.getLogger().info("Invalid config version! The plugin may not load correctly!");
            leaveLoop = true;
            break;
    }
    return leaveLoop;
}
Also used : CommentedFileConfig(com.electronwill.nightconfig.core.file.CommentedFileConfig) File(java.io.File)

Example 4 with CommentedFileConfig

use of com.electronwill.nightconfig.core.file.CommentedFileConfig in project createaddition by mrh0.

the class Config method loadConfig.

public static void loadConfig(ForgeConfigSpec spec, java.nio.file.Path path) {
    final CommentedFileConfig configData = CommentedFileConfig.builder(path).sync().autosave().writingMode(WritingMode.REPLACE).build();
    configData.load();
    spec.setConfig(configData);
}
Also used : CommentedFileConfig(com.electronwill.nightconfig.core.file.CommentedFileConfig)

Example 5 with CommentedFileConfig

use of com.electronwill.nightconfig.core.file.CommentedFileConfig in project ResourcefulBees by Resourceful-Bees.

the class ConfigLoader method load.

public static void load(ForgeConfigSpec config, String location) {
    Path path = FMLPaths.CONFIGDIR.get().resolve(location);
    CommentedFileConfig data = CommentedFileConfig.builder(path).sync().autosave().writingMode(WritingMode.REPLACE).build();
    data.load();
    config.setConfig(data);
}
Also used : Path(java.nio.file.Path) CommentedFileConfig(com.electronwill.nightconfig.core.file.CommentedFileConfig)

Aggregations

CommentedFileConfig (com.electronwill.nightconfig.core.file.CommentedFileConfig)45 File (java.io.File)12 Path (java.nio.file.Path)2 CommentedConfig (com.electronwill.nightconfig.core.CommentedConfig)1 ObjectConverter (com.electronwill.nightconfig.core.conversion.ObjectConverter)1 WritingMode (com.electronwill.nightconfig.core.io.WritingMode)1 Stopwatch (com.google.common.base.Stopwatch)1 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 TimeUnit (java.util.concurrent.TimeUnit)1 ForgeConfigSpec (net.minecraftforge.common.ForgeConfigSpec)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1