Search in sources :

Example 16 with YamlConfiguration

use of org.bukkit.configuration.file.YamlConfiguration in project acidisland by tastybento.

the class TopTen method topTenSave.

public static void topTenSave() {
    if (topTenList == null) {
        return;
    }
    plugin.getLogger().info("Saving top ten list");
    // Make file
    File topTenFile = new File(plugin.getDataFolder(), "topten.yml");
    // Make configuration
    YamlConfiguration config = new YamlConfiguration();
    // Save config
    int rank = 0;
    for (Map.Entry<UUID, Long> m : topTenList.entrySet()) {
        if (rank++ == 10) {
            break;
        }
        config.set("topten." + m.getKey().toString(), m.getValue());
    }
    try {
        config.save(topTenFile);
        plugin.getLogger().info("Saved top ten list");
    } catch (Exception e) {
        plugin.getLogger().severe("Could not save top ten list!");
        e.printStackTrace();
    }
}
Also used : YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) UUID(java.util.UUID) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with YamlConfiguration

use of org.bukkit.configuration.file.YamlConfiguration in project acidisland by tastybento.

the class TopTen method topTenCreate.

/**
 * Creates the top ten list from scratch. Does not get the level of each island. Just
 * takes the level from the player's file.
 * Runs asynchronously from the main thread.
 * @param sender
 */
public static void topTenCreate(final CommandSender sender) {
    plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {

        @Override
        public void run() {
            // This map is a list of owner and island level
            YamlConfiguration player = new YamlConfiguration();
            int index = 0;
            for (final File f : plugin.getPlayersFolder().listFiles()) {
                // Need to remove the .yml suffix
                String fileName = f.getName();
                if (fileName.endsWith(".yml")) {
                    try {
                        String playerUUIDString = fileName.substring(0, fileName.length() - 4);
                        final UUID playerUUID = UUID.fromString(playerUUIDString);
                        if (playerUUID == null) {
                            plugin.getLogger().warning("Player file contains erroneous UUID data.");
                            plugin.getLogger().info("Looking at " + playerUUIDString);
                        }
                        player.load(f);
                        index++;
                        if (index % 1000 == 0) {
                            plugin.getLogger().info("Processed " + index + " players for top ten");
                        }
                        // Players player = new Players(this, playerUUID);
                        int islandLevel = player.getInt("islandLevel", 0);
                        String teamLeaderUUID = player.getString("teamLeader", "");
                        if (islandLevel > 0) {
                            if (!player.getBoolean("hasTeam") || (!teamLeaderUUID.isEmpty() && teamLeaderUUID.equals(playerUUIDString))) {
                                // Only enter team leaders into the top ten
                                topTenAddEntry(playerUUID, islandLevel);
                            }
                        }
                    } catch (Exception e) {
                        plugin.getLogger().severe("Error when reading player file. File is " + fileName);
                        plugin.getLogger().severe("Look at the stack trace and edit the file - it probably has broken YAML in it for some reason.");
                        e.printStackTrace();
                    }
                }
            }
            plugin.getLogger().info("Processed " + index + " players for top ten");
            // Save the top ten
            topTenSave();
            plugin.getServer().getScheduler().runTask(plugin, new Runnable() {

                @Override
                public void run() {
                    if (sender != null) {
                        Util.sendMessage(sender, ChatColor.YELLOW + plugin.myLocale().adminTopTenfinished);
                    } else {
                        plugin.getLogger().warning("Completed top ten creation.");
                    }
                }
            });
        }
    });
}
Also used : YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) UUID(java.util.UUID) File(java.io.File)

Example 18 with YamlConfiguration

use of org.bukkit.configuration.file.YamlConfiguration in project WildernessTp by AcmeProject.

the class PortalzGoneWild method loadConfig.

public void loadConfig() {
    WildTP.debug("Got Load Portal Config");
    portalFile = new File(WildTP.instace.getDataFolder(), "Portals.yml");
    portalConf = new YamlConfiguration();
    if (!portalFile.exists()) {
        try {
            portalFile.createNewFile();
            portalConf.load(portalFile);
            portalConf.set("Version", 0.1);
            portalConf.save(portalFile);
        } catch (IOException | InvalidConfigurationException e) {
            e.printStackTrace();
        }
    } else {
        try {
            portalConf.load(portalFile);
        } catch (IOException | InvalidConfigurationException e) {
            e.printStackTrace();
        }
    }
    if (portalConf.getConfigurationSection("Portals") == null) {
        WildTP.debug("No portals saved.");
        return;
    }
    for (String s : portalConf.getConfigurationSection("Portals").getKeys(false)) {
        ports.put(s, portalConf.getString("Portals." + s).split("~"));
        WildTP.debug("Converting portal " + s);
        WildTP.debug(ports.values());
    }
}
Also used : IOException(java.io.IOException) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) File(java.io.File) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 19 with YamlConfiguration

use of org.bukkit.configuration.file.YamlConfiguration in project WildernessTp by AcmeProject.

the class TooWildForEnums method loadConfig.

public void loadConfig() {
    langFile = new File(WildTP.instace.getDataFolder(), "Messages.yml");
    langConf = new YamlConfiguration();
    if (!langFile.exists()) {
        try {
            langFile.createNewFile();
            langConf.load(langFile);
            langConf.set("NO_PERMS", "&4You do not have permission!");
            langConf.set("NO_SIGN_PERMS", "&4You do not have permission to make a wild sign");
            langConf.set("NO_BREAK", "&4Hey! You can not break WildTp sign!");
            langConf.set("NO_LOCATION", "&4No suitable locations found.");
            langConf.set("NO_BIOME", "&4You may not put signs in %BIOME%");
            langConf.set("YES_SIGN", "&aSuccessfully made a new WildTP sign");
            langConf.set("BREAK_SIGN", "&aYou have broken a WildTP sign");
            langConf.set("COOLDOWN", "&4You must wait %TIME% seconds until you can use the command/sign again");
            langConf.set("RELOADED", "&aPlugin config has successfuly been reloaded.");
            langConf.set("WAIT_MSG", "&6Teleporting in {wait} seconds");
            langConf.set("DIDNT_WAIT", "&cYou need to hold still while you wait!");
            langConf.set("NO_WE", "&cWorldEdit must be installed for this command.");
            langConf.save(langFile);
        } catch (IOException | InvalidConfigurationException e) {
            e.printStackTrace();
        }
    }
    setStrings();
}
Also used : IOException(java.io.IOException) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) File(java.io.File) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 20 with YamlConfiguration

use of org.bukkit.configuration.file.YamlConfiguration in project Jobs by GamingMesh.

the class JobConfig method loadJobSettings.

/**
     * Method to load the jobs configuration
     * 
     * loads from Jobs/jobConfig.yml
     */
private void loadJobSettings() {
    File f = new File(plugin.getDataFolder(), "jobConfig.yml");
    ArrayList<Job> jobs = new ArrayList<Job>();
    Jobs.setJobs(jobs);
    Jobs.setNoneJob(null);
    if (!f.exists()) {
        try {
            f.createNewFile();
        } catch (IOException e) {
            Jobs.getPluginLogger().severe("Unable to create jobConfig.yml!  No jobs were loaded!");
            return;
        }
    }
    YamlConfiguration conf = new YamlConfiguration();
    conf.options().pathSeparator('/');
    try {
        conf.load(f);
    } catch (Exception e) {
        Bukkit.getServer().getLogger().severe("==================== Jobs ====================");
        Bukkit.getServer().getLogger().severe("Unable to load jobConfig.yml!");
        Bukkit.getServer().getLogger().severe("Check your config for formatting issues!");
        Bukkit.getServer().getLogger().severe("No jobs were loaded!");
        Bukkit.getServer().getLogger().severe("Error: " + e.getMessage());
        Bukkit.getServer().getLogger().severe("==============================================");
        return;
    }
    conf.options().header(new StringBuilder().append("Jobs configuration.").append(System.getProperty("line.separator")).append(System.getProperty("line.separator")).append("Stores information about each job.").append(System.getProperty("line.separator")).append(System.getProperty("line.separator")).append("For example configurations, visit http://dev.bukkit.org/server-mods/jobs/.").append(System.getProperty("line.separator")).toString());
    ConfigurationSection jobsSection = conf.getConfigurationSection("Jobs");
    if (jobsSection == null) {
        jobsSection = conf.createSection("Jobs");
    }
    for (String jobKey : jobsSection.getKeys(false)) {
        ConfigurationSection jobSection = jobsSection.getConfigurationSection(jobKey);
        String jobName = jobSection.getString("fullname");
        if (jobName == null) {
            Jobs.getPluginLogger().warning("Job " + jobKey + " has an invalid fullname property. Skipping job!");
            continue;
        }
        int maxLevel = jobSection.getInt("max-level", 0);
        if (maxLevel < 0)
            maxLevel = 0;
        Integer maxSlots = jobSection.getInt("slots", 0);
        if (maxSlots.intValue() <= 0) {
            maxSlots = null;
        }
        String jobShortName = jobSection.getString("shortname");
        if (jobShortName == null) {
            Jobs.getPluginLogger().warning("Job " + jobKey + " is missing the shortname property.  Skipping job!");
            continue;
        }
        String description = jobSection.getString("description", "");
        ChatColor color = ChatColor.WHITE;
        if (jobSection.contains("ChatColour")) {
            color = ChatColor.matchColor(jobSection.getString("ChatColour", ""));
            if (color == null) {
                color = ChatColor.WHITE;
                Jobs.getPluginLogger().warning("Job " + jobKey + " has an invalid ChatColour property.  Defaulting to WHITE!");
            }
        }
        DisplayMethod displayMethod = DisplayMethod.matchMethod(jobSection.getString("chat-display", ""));
        if (displayMethod == null) {
            Jobs.getPluginLogger().warning("Job " + jobKey + " has an invalid chat-display property. Defaulting to None!");
            displayMethod = DisplayMethod.NONE;
        }
        Parser maxExpEquation;
        String maxExpEquationInput = jobSection.getString("leveling-progression-equation");
        try {
            maxExpEquation = new Parser(maxExpEquationInput);
            // test equation
            maxExpEquation.setVariable("numjobs", 1);
            maxExpEquation.setVariable("joblevel", 1);
            maxExpEquation.getValue();
        } catch (Exception e) {
            Jobs.getPluginLogger().warning("Job " + jobKey + " has an invalid leveling-progression-equation property. Skipping job!");
            continue;
        }
        Parser incomeEquation;
        String incomeEquationInput = jobSection.getString("income-progression-equation");
        try {
            incomeEquation = new Parser(incomeEquationInput);
            // test equation
            incomeEquation.setVariable("numjobs", 1);
            incomeEquation.setVariable("joblevel", 1);
            incomeEquation.setVariable("baseincome", 1);
            incomeEquation.getValue();
        } catch (Exception e) {
            Jobs.getPluginLogger().warning("Job " + jobKey + " has an invalid income-progression-equation property. Skipping job!");
            continue;
        }
        Parser expEquation;
        String expEquationInput = jobSection.getString("experience-progression-equation");
        try {
            expEquation = new Parser(expEquationInput);
            // test equation
            expEquation.setVariable("numjobs", 1);
            expEquation.setVariable("joblevel", 1);
            expEquation.setVariable("baseexperience", 1);
            expEquation.getValue();
        } catch (Exception e) {
            Jobs.getPluginLogger().warning("Job " + jobKey + " has an invalid experience-progression-equation property. Skipping job!");
            continue;
        }
        // Permissions
        ArrayList<JobPermission> jobPermissions = new ArrayList<JobPermission>();
        ConfigurationSection permissionsSection = jobSection.getConfigurationSection("permissions");
        if (permissionsSection != null) {
            for (String permissionKey : permissionsSection.getKeys(false)) {
                ConfigurationSection permissionSection = permissionsSection.getConfigurationSection(permissionKey);
                String node = permissionKey.toLowerCase();
                if (permissionSection == null) {
                    Jobs.getPluginLogger().warning("Job " + jobKey + " has an invalid permission key" + permissionKey + "!");
                    continue;
                }
                boolean value = permissionSection.getBoolean("value", true);
                int levelRequirement = permissionSection.getInt("level", 0);
                jobPermissions.add(new JobPermission(node, value, levelRequirement));
            }
        }
        Job job = new Job(jobName, jobShortName, description, color, maxExpEquation, displayMethod, maxLevel, maxSlots, jobPermissions);
        for (ActionType actionType : ActionType.values()) {
            ConfigurationSection typeSection = jobSection.getConfigurationSection(actionType.getName());
            ArrayList<JobInfo> jobInfo = new ArrayList<JobInfo>();
            if (typeSection != null) {
                for (String key : typeSection.getKeys(false)) {
                    ConfigurationSection section = typeSection.getConfigurationSection(key);
                    String myKey = key.toUpperCase();
                    String type = null;
                    String subType = "";
                    if (myKey.contains("-")) {
                        // uses subType
                        subType = ":" + myKey.split("-")[1];
                        myKey = myKey.split("-")[0];
                    }
                    Material material = Material.matchMaterial(myKey);
                    if (material == null) {
                        // try integer method
                        Integer matId = null;
                        try {
                            matId = Integer.decode(myKey);
                        } catch (NumberFormatException e) {
                        }
                        if (matId != null) {
                            material = Material.getMaterial(matId);
                            if (material != null) {
                                Jobs.getPluginLogger().warning("Job " + jobKey + " " + actionType.getName() + " is using a block by number ID: " + key + "!");
                                Jobs.getPluginLogger().warning("Please switch to using the Material name instead: " + material.toString() + "!");
                                Jobs.getPluginLogger().warning("Blocks by number IDs may break in a future release!");
                            }
                        }
                    }
                    if (material != null) {
                        // Break and Place actions MUST be blocks
                        if (actionType == ActionType.BREAK || actionType == ActionType.PLACE) {
                            if (!material.isBlock()) {
                                Jobs.getPluginLogger().warning("Job " + jobKey + " has an invalid " + actionType.getName() + " type property: " + key + "! Material must be a block!");
                                continue;
                            }
                        }
                        /* 
                             * Historically, GLOWING_REDSTONE_ORE would ONLY work as REDSTONE_ORE, and putting
                             * GLOWING_REDSTONE_ORE in the configuration would not work.  Unfortunately, this is 
                             * completely backwards and wrong.
                             * 
                             * To maintain backwards compatibility, all instances of REDSTONE_ORE should normalize
                             * to GLOWING_REDSTONE_ORE, and warn the user to change their configuration.  In the
                             * future this hack may be removed and anybody using REDSTONE_ORE will have their
                             * configurations broken.
                             */
                        if (material == Material.REDSTONE_ORE) {
                            Jobs.getPluginLogger().warning("Job " + jobKey + " is using REDSTONE_ORE instead of GLOWING_REDSTONE_ORE.");
                            Jobs.getPluginLogger().warning("Automatically changing block to GLOWING_REDSTONE_ORE.  Please update your configuration.");
                            Jobs.getPluginLogger().warning("In vanilla minecraft, REDSTONE_ORE changes to GLOWING_REDSTONE_ORE when interacted with.");
                            Jobs.getPluginLogger().warning("In the future, Jobs using REDSTONE_ORE instead of GLOWING_REDSTONE_ORE may fail to work correctly.");
                            material = Material.GLOWING_REDSTONE_ORE;
                        }
                        // END HACK
                        type = material.toString();
                    } else if (actionType == ActionType.KILL) {
                        // check entities
                        EntityType entity = EntityType.fromName(key);
                        if (entity == null) {
                            try {
                                entity = EntityType.valueOf(key.toUpperCase());
                            } catch (IllegalArgumentException e) {
                            }
                        }
                        if (entity != null && entity.isAlive())
                            type = entity.toString();
                    }
                    if (type == null) {
                        Jobs.getPluginLogger().warning("Job " + jobKey + " has an invalid " + actionType.getName() + " type property: " + key + "!");
                        continue;
                    }
                    double income = section.getDouble("income", 0.0);
                    double experience = section.getDouble("experience", 0.0);
                    jobInfo.add(new JobInfo(type + subType, income, incomeEquation, experience, expEquation));
                }
            }
            job.setJobInfo(actionType, jobInfo);
        }
        if (jobKey.equalsIgnoreCase("none")) {
            Jobs.setNoneJob(job);
        } else {
            jobs.add(job);
        }
    }
    try {
        conf.save(f);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ActionType(com.gamingmesh.jobs.container.ActionType) ArrayList(java.util.ArrayList) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) JobInfo(com.gamingmesh.jobs.container.JobInfo) DisplayMethod(com.gamingmesh.jobs.container.DisplayMethod) Job(com.gamingmesh.jobs.container.Job) JobPermission(com.gamingmesh.jobs.container.JobPermission) Material(org.bukkit.Material) IOException(java.io.IOException) IOException(java.io.IOException) Parser(com.gamingmesh.jobs.resources.jfep.Parser) EntityType(org.bukkit.entity.EntityType) File(java.io.File) ChatColor(com.gamingmesh.jobs.util.ChatColor) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)36 File (java.io.File)24 IOException (java.io.IOException)21 InvalidConfigurationException (org.bukkit.configuration.InvalidConfigurationException)13 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)6 UUID (java.util.UUID)5 ArrayList (java.util.ArrayList)4 Location (org.bukkit.Location)4 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)4 EntityType (org.bukkit.entity.EntityType)4 SettingsFlag (com.wasteofplastic.acidisland.Island.SettingsFlag)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ChatColor (com.gamingmesh.jobs.util.ChatColor)2 DefaultInfo (de.Keyle.MyPet.api.entity.DefaultInfo)2 MyPetType (de.Keyle.MyPet.api.entity.MyPetType)2 InputStreamReader (java.io.InputStreamReader)2 TreeMap (java.util.TreeMap)2 Material (org.bukkit.Material)2