use of com.archyx.aureliumskills.leaderboard.SkillValue in project AureliumSkills by Archy-X.
the class SkillsCommand method onTop.
@Subcommand("top")
@CommandAlias("skilltop")
@CommandCompletion("@skillTop")
@CommandPermission("aureliumskills.top")
@Description("Shows the top players in a skill")
@Syntax("Usage: /sk top <page> or /sk top [skill] <page>")
public void onTop(CommandSender sender, String[] args) {
Locale locale = plugin.getLang().getLocale(sender);
if (args.length == 0) {
List<SkillValue> lb = plugin.getLeaderboardManager().getPowerLeaderboard(1, 10);
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_POWER_HEADER, locale));
for (SkillValue skillValue : lb) {
String name = Bukkit.getOfflinePlayer(skillValue.getId()).getName();
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_POWER_ENTRY, locale).replace("{rank}", String.valueOf(lb.indexOf(skillValue) + 1)).replace("{player}", name != null ? name : "?").replace("{level}", String.valueOf(skillValue.getLevel())));
}
} else if (args.length == 1) {
if (args[0].equalsIgnoreCase("average")) {
List<SkillValue> lb = plugin.getLeaderboardManager().getAverageLeaderboard(1, 10);
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_AVERAGE_HEADER, locale));
sendLeaderboardEntries(sender, locale, lb);
} else {
try {
int page = Integer.parseInt(args[0]);
List<SkillValue> lb = plugin.getLeaderboardManager().getPowerLeaderboard(page, 10);
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_POWER_HEADER_PAGE, locale).replace("{page}", String.valueOf(page)));
for (SkillValue skillValue : lb) {
String name = Bukkit.getOfflinePlayer(skillValue.getId()).getName();
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_POWER_ENTRY, locale).replace("{rank}", String.valueOf((page - 1) * 10 + lb.indexOf(skillValue) + 1)).replace("{player}", name != null ? name : "?").replace("{level}", String.valueOf(skillValue.getLevel())));
}
} catch (Exception e) {
Skill skill = plugin.getSkillRegistry().getSkill(args[0]);
if (skill != null) {
List<SkillValue> lb = plugin.getLeaderboardManager().getLeaderboard(skill, 1, 10);
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_SKILL_HEADER, locale).replace("{skill}", skill.getDisplayName(locale)));
for (SkillValue skillValue : lb) {
String name = Bukkit.getOfflinePlayer(skillValue.getId()).getName();
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_SKILL_ENTRY, locale).replace("{rank}", String.valueOf(lb.indexOf(skillValue) + 1)).replace("{player}", name != null ? name : "?").replace("{level}", String.valueOf(skillValue.getLevel())));
}
} else {
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_USAGE, locale));
}
}
}
} else if (args.length == 2) {
if (args[0].equalsIgnoreCase("average")) {
try {
int page = Integer.parseInt(args[1]);
List<SkillValue> lb = plugin.getLeaderboardManager().getAverageLeaderboard(page, 10);
sender.sendMessage(TextUtil.replace(Lang.getMessage(CommandMessage.TOP_AVERAGE_HEADER_PAGE, locale), "{page}", String.valueOf(page)));
sendLeaderboardEntries(sender, locale, lb);
} catch (Exception e) {
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_USAGE, locale));
}
} else {
Skill skill = plugin.getSkillRegistry().getSkill(args[0]);
if (skill != null) {
try {
int page = Integer.parseInt(args[1]);
List<SkillValue> lb = plugin.getLeaderboardManager().getLeaderboard(skill, page, 10);
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_SKILL_HEADER_PAGE, locale).replace("{page}", String.valueOf(page)).replace("{skill}", skill.getDisplayName(locale)));
for (SkillValue skillValue : lb) {
String name = Bukkit.getOfflinePlayer(skillValue.getId()).getName();
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_SKILL_ENTRY, locale).replace("{rank}", String.valueOf((page - 1) * 10 + lb.indexOf(skillValue) + 1)).replace("{player}", name != null ? name : "?").replace("{level}", String.valueOf(skillValue.getLevel())));
}
} catch (Exception e) {
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_USAGE, locale));
}
} else {
sender.sendMessage(Lang.getMessage(CommandMessage.TOP_USAGE, locale));
}
}
}
}
use of com.archyx.aureliumskills.leaderboard.SkillValue in project AureliumSkills by Archy-X.
the class StorageProvider method addLoadedPlayersToLeaderboards.
protected Set<UUID> addLoadedPlayersToLeaderboards(Map<Skill, List<SkillValue>> leaderboards, List<SkillValue> powerLeaderboard, List<SkillValue> averageLeaderboard) {
Set<UUID> loadedFromMemory = new HashSet<>();
for (PlayerData playerData : playerManager.getPlayerDataMap().values()) {
UUID id = playerData.getPlayer().getUniqueId();
int powerLevel = 0;
double powerXp = 0;
int numEnabled = 0;
for (Skill skill : Skills.values()) {
int level = playerData.getSkillLevel(skill);
double xp = playerData.getSkillXp(skill);
// 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);
loadedFromMemory.add(playerData.getPlayer().getUniqueId());
}
return loadedFromMemory;
}
use of com.archyx.aureliumskills.leaderboard.SkillValue 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.leaderboard.SkillValue in project AureliumSkills by Archy-X.
the class YamlStorageProvider 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);
File playerDataFolder = new File(plugin.getDataFolder() + "/playerdata");
// Load data from files
if (playerDataFolder.exists() && playerDataFolder.isDirectory()) {
File[] files = playerDataFolder.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().endsWith(".yml")) {
UUID id = UUID.fromString(file.getName().substring(0, file.getName().lastIndexOf('.')));
if (!loadedFromMemory.contains(id)) {
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
try {
int powerLevel = 0;
double powerXp = 0;
int numEnabled = 0;
for (Skill skill : Skills.values()) {
// Load from config
String path = "skills." + skill.toString().toLowerCase(Locale.ROOT) + ".";
int level = config.getInt(path + "level", 1);
double xp = config.getDouble(path + "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 playerdata file " + file.getName() + ", see error below for details:");
e.printStackTrace();
}
}
}
}
}
}
sortLeaderboards(leaderboards, powerLeaderboard, averageLeaderboard);
}
use of com.archyx.aureliumskills.leaderboard.SkillValue in project AureliumSkills by Archy-X.
the class SkillsCommand method sendLeaderboardEntries.
private void sendLeaderboardEntries(CommandSender sender, Locale locale, List<SkillValue> lb) {
for (SkillValue skillValue : lb) {
String name = Bukkit.getOfflinePlayer(skillValue.getId()).getName();
sender.sendMessage(TextUtil.replace(Lang.getMessage(CommandMessage.TOP_AVERAGE_ENTRY, locale), "{rank}", String.valueOf(lb.indexOf(skillValue) + 1), "{player}", name != null ? name : "?", "{level}", NumberUtil.format2(skillValue.getXp())));
}
}
Aggregations