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();
}
}
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();
}
}
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);
}
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;
}
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);
}
Aggregations