Search in sources :

Example 1 with ConfigurationYAML

use of de.Keyle.MyPet.api.util.configuration.ConfigurationYAML in project MyPet by xXKeyleXx.

the class WorldListener method on.

@EventHandler
public void on(WorldInitEvent event) {
    if (WorldGroup.getGroupByWorld(event.getWorld().getName()) == null) {
        WorldGroup defaultGroup = WorldGroup.getGroupByName("default");
        if (defaultGroup == null) {
            defaultGroup = new WorldGroup("default");
            defaultGroup.registerGroup();
        }
        if (defaultGroup.addWorld(event.getWorld().getName())) {
            File groupsFile = new File(MyPetApi.getPlugin().getDataFolder().getPath() + File.separator + "worldgroups.yml");
            ConfigurationYAML yamlConfiguration = new ConfigurationYAML(groupsFile);
            FileConfiguration config = yamlConfiguration.getConfig();
            config.set("Groups.default", defaultGroup.getWorlds());
            yamlConfiguration.saveConfig();
            MyPetApi.getLogger().info("added " + ChatColor.YELLOW + event.getWorld().getName() + ChatColor.RESET + " to '" + ChatColor.YELLOW + "default" + ChatColor.RESET + "' group.");
        } else {
            MyPetApi.getLogger().warning("An error occured while adding " + ChatColor.YELLOW + event.getWorld().getName() + ChatColor.RESET + " to '" + ChatColor.YELLOW + "default" + ChatColor.RESET + "' group. Please restart the server.");
        }
    }
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) WorldGroup(de.Keyle.MyPet.api.WorldGroup) ConfigurationYAML(de.Keyle.MyPet.api.util.configuration.ConfigurationYAML) File(java.io.File) EventHandler(org.bukkit.event.EventHandler)

Example 2 with ConfigurationYAML

use of de.Keyle.MyPet.api.util.configuration.ConfigurationYAML in project MyPet by xXKeyleXx.

the class MyPetPlugin method loadGroups.

private int loadGroups(File f) {
    ConfigurationYAML yamlConfiguration = new ConfigurationYAML(f);
    FileConfiguration config = yamlConfiguration.getConfig();
    if (config == null) {
        return 0;
    }
    WorldGroup.clearGroups();
    Set<String> nodes;
    try {
        nodes = config.getConfigurationSection("Groups").getKeys(false);
    } catch (NullPointerException e) {
        nodes = new HashSet<>();
        getLogger().info("No groups found. Everything will be in 'default' group.");
    }
    getLogger().info("--- Loading WorldGroups ---------------------------");
    if (nodes.size() == 0) {
        List<String> worldNames = new ArrayList<>();
        WorldGroup defaultGroup = new WorldGroup("default");
        defaultGroup.registerGroup();
        for (World world : this.getServer().getWorlds()) {
            getLogger().info("added " + ChatColor.GOLD + world.getName() + ChatColor.RESET + " to 'default' group.");
            worldNames.add(world.getName());
            defaultGroup.addWorld(world.getName());
        }
        config.set("Groups.default", worldNames);
        yamlConfiguration.saveConfig();
    } else {
        for (String node : nodes) {
            List<String> worlds = config.getStringList("Groups." + node);
            if (worlds.size() > 0) {
                WorldGroup newGroup = new WorldGroup(node);
                for (String world : worlds) {
                    getLogger().info("   added '" + world + "' to '" + newGroup.getName() + "'");
                    newGroup.addWorld(world);
                }
                if (newGroup.getWorlds().size() > 0) {
                    newGroup.registerGroup();
                }
            }
        }
        WorldGroup defaultGroup = WorldGroup.getGroupByName("default");
        if (defaultGroup == null) {
            defaultGroup = new WorldGroup("default");
            defaultGroup.registerGroup();
        }
        boolean saveConfig = false;
        for (World world : getServer().getWorlds()) {
            if (WorldGroup.getGroupByWorld(world.getName()) == null) {
                getLogger().info("added " + ChatColor.GOLD + world.getName() + ChatColor.RESET + " to 'default' group.");
                defaultGroup.addWorld(world.getName());
                saveConfig = true;
            }
        }
        if (saveConfig) {
            config.set("Groups.default", defaultGroup.getWorlds());
            yamlConfiguration.saveConfig();
        }
    }
    getLogger().info("-------------------------------------------------");
    return 0;
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) ConfigurationYAML(de.Keyle.MyPet.api.util.configuration.ConfigurationYAML) World(org.bukkit.World)

Aggregations

ConfigurationYAML (de.Keyle.MyPet.api.util.configuration.ConfigurationYAML)2 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)2 WorldGroup (de.Keyle.MyPet.api.WorldGroup)1 File (java.io.File)1 World (org.bukkit.World)1 EventHandler (org.bukkit.event.EventHandler)1