From 5605c8851cbf970e68567b18ab9797aa3e235ea6 Mon Sep 17 00:00:00 2001 From: iambibi Date: Thu, 31 Oct 2024 22:30:55 +0100 Subject: [PATCH] Fix getTopPlayTime --- .../core/managers/LeaderboardManager.java | 66 ++++++++++--------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/src/main/java/fr/communaywen/core/managers/LeaderboardManager.java b/src/main/java/fr/communaywen/core/managers/LeaderboardManager.java index bc3b201a..1c24859a 100644 --- a/src/main/java/fr/communaywen/core/managers/LeaderboardManager.java +++ b/src/main/java/fr/communaywen/core/managers/LeaderboardManager.java @@ -237,29 +237,29 @@ public static void createLeaderboardPlayTime() { public static void updateLeaderboardPlayTime() { if (textDisplayPlayTime == null) return; - List> topPlayTime = getTopPlayTime(10); - - List lines = new ArrayList<>(); - lines.add("§dLes §f10 §dMeilleurs Temps de Jeu des Joueurs"); + getTopPlayTime(10, topPlayTime -> { + List lines = new ArrayList<>(); + lines.add("§dLes §f10 §dMeilleurs Temps de Jeu des Joueurs"); - int index = 1; - for (Map.Entry entry : topPlayTime) { - UUID playerUUID = entry.getKey(); - long time = entry.getValue(); + int index = 1; + for (Map.Entry entry : topPlayTime) { + UUID playerUUID = entry.getKey(); + long time = entry.getValue(); - String playerName = Bukkit.getOfflinePlayer(playerUUID).getName(); + String playerName = Bukkit.getOfflinePlayer(playerUUID).getName(); - lines.add(MessageFormat.format("{0}# {1}: {2}", - getColor(index) + index, - ChatColor.GRAY + playerName, - ChatColor.DARK_PURPLE + convertTime(time))); + lines.add(MessageFormat.format("{0}# {1}: {2}", + getColor(index) + index, + ChatColor.GRAY + playerName, + ChatColor.DARK_PURPLE + convertTime(time))); - index++; - } + index++; + } - String leaderboardText = String.join("\n", lines); + String leaderboardText = String.join("\n", lines); - textDisplayPlayTime.setText(Component.text(leaderboardText).content()); + textDisplayPlayTime.setText(Component.text(leaderboardText).content()); + }); } public static void removeLeaderboardPlayTime() { @@ -297,24 +297,28 @@ private static void insertTimePlayed(Player player, long time) { } } - private static List> getTopPlayTime(int limit) { - List> playTime = new ArrayList<>(); - String sql = "SELECT uuid, time FROM playtime ORDER BY time DESC LIMIT ?"; + private static void getTopPlayTime(int limit, Consumer>> callback) { + Bukkit.getScheduler().runTaskAsynchronously(plugins, () -> { + List> playTime = new ArrayList<>(); + String sql = "SELECT uuid, time FROM playtime ORDER BY time DESC LIMIT ?"; - try (PreparedStatement statement = connection.prepareStatement(sql)) { - statement.setInt(1, limit); - ResultSet rs = statement.executeQuery(); + try (PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setInt(1, limit); + ResultSet rs = statement.executeQuery(); - while (rs.next()) { - UUID playerUUID = UUID.fromString(rs.getString("uuid")); - long time = rs.getLong("time"); + while (rs.next()) { + UUID playerUUID = UUID.fromString(rs.getString("uuid")); + long time = rs.getLong("time"); - playTime.add(new AbstractMap.SimpleEntry<>(playerUUID, time)); + playTime.add(new AbstractMap.SimpleEntry<>(playerUUID, time)); + } + } catch (SQLException e) { + e.printStackTrace(); } - } catch (SQLException e) { - e.printStackTrace(); - } - return playTime; + + List> finalResult = playTime; + Bukkit.getScheduler().runTask(plugins, () -> callback.accept(finalResult)); + }); } private static void getTopTeam(int limit, Consumer>> callback) {