Search in sources :

Example 31 with Skill

use of com.archyx.aureliumskills.skills.Skill in project AureliumSkills by Archy-X.

the class MysqlBackup method saveBackup.

@Override
public void saveBackup(CommandSender sender, boolean savePlayerData) {
    try {
        // Save online players
        if (savePlayerData) {
            for (Player player : Bukkit.getOnlinePlayers()) {
                storageProvider.save(player, false);
            }
        }
        Connection connection = storageProvider.getConnection();
        try (Statement statement = connection.createStatement()) {
            String query = "SELECT * FROM SkillData;";
            try (ResultSet result = statement.executeQuery(query)) {
                createBackupFolder();
                LocalTime time = LocalTime.now();
                File file = new File(plugin.getDataFolder() + "/backups/backup-" + LocalDate.now() + "_" + time.getHour() + "-" + time.getMinute() + "-" + time.getSecond() + ".yml");
                FileConfiguration config = YamlConfiguration.loadConfiguration(file);
                config.set("backup_version", 1);
                while (result.next()) {
                    UUID id = UUID.fromString(result.getString("ID"));
                    for (Skill skill : Skills.values()) {
                        int level = result.getInt(skill.toString().toUpperCase(Locale.ROOT) + "_LEVEL");
                        double xp = result.getDouble(skill.toString().toUpperCase(Locale.ROOT) + "_XP");
                        String path = "player_data." + id + "." + skill.toString().toLowerCase(Locale.ROOT) + ".";
                        config.set(path + "level", level);
                        config.set(path + "xp", xp);
                    }
                }
                config.save(file);
                Locale locale = plugin.getLang().getLocale(sender);
                String message = TextUtil.replace(Lang.getMessage(CommandMessage.BACKUP_SAVE_SAVED, locale), "{type}", "MySQL", "{file}", file.getName());
                if (sender instanceof ConsoleCommandSender) {
                    plugin.getLogger().info(ChatColor.stripColor(message));
                } else {
                    sender.sendMessage(AureliumSkills.getPrefix(locale) + message);
                }
            }
        }
    } catch (Exception e) {
        Locale locale = plugin.getLang().getLocale(sender);
        String message = TextUtil.replace(Lang.getMessage(CommandMessage.BACKUP_SAVE_ERROR, locale), "{type}", "MySQL");
        if (sender instanceof ConsoleCommandSender) {
            plugin.getLogger().warning(ChatColor.stripColor(message));
        } else {
            sender.sendMessage(AureliumSkills.getPrefix(locale) + message);
        }
        e.printStackTrace();
    }
}
Also used : Locale(java.util.Locale) Player(org.bukkit.entity.Player) LocalTime(java.time.LocalTime) Statement(java.sql.Statement) Connection(java.sql.Connection) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) Skill(com.archyx.aureliumskills.skills.Skill) ResultSet(java.sql.ResultSet) UUID(java.util.UUID) File(java.io.File)

Example 32 with Skill

use of com.archyx.aureliumskills.skills.Skill in project AureliumSkills by Archy-X.

the class YamlBackup method saveBackup.

@Override
public void saveBackup(CommandSender sender, boolean savePlayerData) {
    try {
        if (savePlayerData) {
            // Save online players
            for (Player player : Bukkit.getOnlinePlayers()) {
                plugin.getStorageProvider().save(player, false);
            }
        }
        createBackupFolder();
        LocalTime time = LocalTime.now();
        File backupFile = new File(plugin.getDataFolder() + "/backups/backup-" + LocalDate.now() + "_" + time.getHour() + "-" + time.getMinute() + "-" + time.getSecond() + ".yml");
        FileConfiguration backup = YamlConfiguration.loadConfiguration(backupFile);
        backup.set("backup_version", 1);
        File playerDataFolder = new File(plugin.getDataFolder() + "/playerdata");
        if (playerDataFolder.exists() && playerDataFolder.isDirectory()) {
            File[] files = playerDataFolder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.getName().endsWith(".yml")) {
                        FileConfiguration config = YamlConfiguration.loadConfiguration(file);
                        String stringId = config.getString("uuid");
                        if (stringId != null) {
                            for (Skill skill : Skills.values()) {
                                int level = config.getInt("skills." + skill.toString().toLowerCase(Locale.ROOT) + ".level");
                                double xp = config.getInt("skills." + skill.toString().toLowerCase(Locale.ROOT) + ".xp");
                                String path = "player_data." + stringId + "." + skill.toString().toLowerCase(Locale.ROOT) + ".";
                                backup.set(path + "level", level);
                                backup.set(path + "xp", xp);
                            }
                        }
                    }
                }
            }
        }
        backup.save(backupFile);
        Locale locale = plugin.getLang().getLocale(sender);
        String message = AureliumSkills.getPrefix(locale) + TextUtil.replace(Lang.getMessage(CommandMessage.BACKUP_SAVE_SAVED, locale), "{type}", "Yaml", "{file}", backupFile.getName());
        if (sender instanceof ConsoleCommandSender) {
            message = ChatColor.stripColor(message);
        }
        sender.sendMessage(message);
    } catch (Exception e) {
        Locale locale = plugin.getLang().getLocale(sender);
        String message = AureliumSkills.getPrefix(locale) + TextUtil.replace(Lang.getMessage(CommandMessage.BACKUP_SAVE_ERROR, locale), "{type}", "Yaml");
        if (sender instanceof ConsoleCommandSender) {
            Bukkit.getLogger().warning(ChatColor.stripColor(message));
        } else {
            sender.sendMessage(message);
        }
        e.printStackTrace();
    }
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) Locale(java.util.Locale) Player(org.bukkit.entity.Player) Skill(com.archyx.aureliumskills.skills.Skill) LocalTime(java.time.LocalTime) File(java.io.File) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender)

Example 33 with Skill

use of com.archyx.aureliumskills.skills.Skill in project AureliumSkills by Archy-X.

the class MySqlStorageProvider method updateLeaderboards.

@Override
public void updateLeaderboards() {
    LeaderboardManager manager = plugin.getLeaderboardManager();
    manager.setSorting(true);
    // Initialize lists
    Map<Skill, List<SkillValue>> leaderboards = new HashMap<>();
    for (Skill skill : Skills.values()) {
        leaderboards.put(skill, new ArrayList<>());
    }
    List<SkillValue> powerLeaderboard = new ArrayList<>();
    List<SkillValue> averageLeaderboard = new ArrayList<>();
    // Add players already in memory
    Set<UUID> loadedFromMemory = addLoadedPlayersToLeaderboards(leaderboards, powerLeaderboard, averageLeaderboard);
    try {
        try (Statement statement = connection.createStatement()) {
            String query = "SELECT * FROM SkillData;";
            try (ResultSet result = statement.executeQuery(query)) {
                while (result.next()) {
                    try {
                        UUID id = UUID.fromString(result.getString("ID"));
                        if (!loadedFromMemory.contains(id)) {
                            int powerLevel = 0;
                            double powerXp = 0;
                            int numEnabled = 0;
                            for (Skill skill : Skills.values()) {
                                // Load from database
                                int level = result.getInt(skill.toString().toUpperCase(Locale.ROOT) + "_LEVEL");
                                if (level == 0) {
                                    level = 1;
                                }
                                double xp = result.getDouble(skill.toString().toUpperCase(Locale.ROOT) + "_XP");
                                // Add to lists
                                SkillValue skillLevel = new SkillValue(id, level, xp);
                                leaderboards.get(skill).add(skillLevel);
                                if (OptionL.isEnabled(skill)) {
                                    powerLevel += level;
                                    powerXp += xp;
                                    numEnabled++;
                                }
                            }
                            // Add power and average
                            SkillValue powerValue = new SkillValue(id, powerLevel, powerXp);
                            powerLeaderboard.add(powerValue);
                            double averageLevel = (double) powerLevel / numEnabled;
                            SkillValue averageValue = new SkillValue(id, 0, averageLevel);
                            averageLeaderboard.add(averageValue);
                        }
                    } catch (Exception e) {
                        Bukkit.getLogger().warning("[AureliumSkills] Error reading player with uuid " + result.getString("ID") + " from the database!");
                        e.printStackTrace();
                    }
                }
            }
        }
    } catch (Exception e) {
        Bukkit.getLogger().warning("Error while updating leaderboards:");
        e.printStackTrace();
    }
    sortLeaderboards(leaderboards, powerLeaderboard, averageLeaderboard);
}
Also used : LeaderboardManager(com.archyx.aureliumskills.leaderboard.LeaderboardManager) IOException(java.io.IOException) Skill(com.archyx.aureliumskills.skills.Skill) SkillValue(com.archyx.aureliumskills.leaderboard.SkillValue)

Example 34 with Skill

use of com.archyx.aureliumskills.skills.Skill in project AureliumSkills by Archy-X.

the class StorageProvider method getLevelsFromBackup.

protected Map<Skill, Integer> getLevelsFromBackup(ConfigurationSection playerDataSection, String stringId) {
    Map<Skill, Integer> levels = new HashMap<>();
    for (Skill skill : Skills.values()) {
        int level = playerDataSection.getInt(stringId + "." + skill.toString().toLowerCase(Locale.ROOT) + ".level", 1);
        levels.put(skill, level);
    }
    return levels;
}
Also used : Skill(com.archyx.aureliumskills.skills.Skill)

Example 35 with Skill

use of com.archyx.aureliumskills.skills.Skill in project AureliumSkills by Archy-X.

the class StorageProvider method applyData.

protected void applyData(PlayerData playerData, Map<Skill, Integer> levels, Map<Skill, Double> xpLevels) {
    for (Stat stat : plugin.getStatRegistry().getStats()) {
        playerData.setStatLevel(stat, 0);
    }
    // Apply to object if in memory
    for (Skill skill : Skills.values()) {
        int level = levels.get(skill);
        playerData.setSkillLevel(skill, level);
        playerData.setSkillXp(skill, xpLevels.get(skill));
        // Add stat levels
        plugin.getRewardManager().getRewardTable(skill).applyStats(playerData, level);
    }
    // Reload stats
    new StatLeveler(plugin).reloadStat(playerData.getPlayer(), Stats.HEALTH);
    new StatLeveler(plugin).reloadStat(playerData.getPlayer(), Stats.LUCK);
    new StatLeveler(plugin).reloadStat(playerData.getPlayer(), Stats.WISDOM);
    // Immediately save to file
    save(playerData.getPlayer(), false);
}
Also used : Skill(com.archyx.aureliumskills.skills.Skill) Stat(com.archyx.aureliumskills.stats.Stat) StatLeveler(com.archyx.aureliumskills.stats.StatLeveler)

Aggregations

Skill (com.archyx.aureliumskills.skills.Skill)54 PlayerData (com.archyx.aureliumskills.data.PlayerData)15 Locale (java.util.Locale)15 Player (org.bukkit.entity.Player)13 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)12 File (java.io.File)11 IOException (java.io.IOException)9 ItemStack (org.bukkit.inventory.ItemStack)8 EventHandler (org.bukkit.event.EventHandler)7 Stat (com.archyx.aureliumskills.stats.Stat)6 InvalidCommandArgument (co.aikar.commands.InvalidCommandArgument)5 SkillValue (com.archyx.aureliumskills.leaderboard.SkillValue)5 StatModifier (com.archyx.aureliumskills.modifier.StatModifier)5 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)5 AbilityData (com.archyx.aureliumskills.data.AbilityData)4 KeyIntPair (com.archyx.aureliumskills.util.misc.KeyIntPair)4 Map (java.util.Map)4 AureliumSkills (com.archyx.aureliumskills.AureliumSkills)3 OptionL (com.archyx.aureliumskills.configuration.OptionL)3 Lang (com.archyx.aureliumskills.lang.Lang)3