|
25 | 25 | import java.sql.SQLException; |
26 | 26 | import java.text.MessageFormat; |
27 | 27 | import java.util.*; |
| 28 | +import java.util.function.Consumer; |
28 | 29 |
|
29 | 30 | import static fr.communaywen.core.commands.economy.BaltopCommand.getColor; |
30 | 31 |
|
@@ -131,27 +132,28 @@ public static void createLeaderboardTeamTop() { |
131 | 132 | public static void updateLeaderboardTeamTop() { |
132 | 133 | if (textDisplayTeamTop == null) return; |
133 | 134 |
|
134 | | - List<Map.Entry<String, Long>> topTeam = getTopTeam(10); |
| 135 | + getTopTeam(10, topTeam -> { |
135 | 136 |
|
136 | | - List<String> lines = new ArrayList<>(); |
137 | | - lines.add("§dLes §f10 §dTeams les plus riches sur le serveur"); |
| 137 | + List<String> lines = new ArrayList<>(); |
| 138 | + lines.add("§dLes §f10 §dTeams les plus riches sur le serveur"); |
138 | 139 |
|
139 | | - int index = 1; |
140 | | - for (Map.Entry<String, Long> entry : topTeam) { |
141 | | - String teamName = entry.getKey(); |
142 | | - long balance = entry.getValue(); |
| 140 | + int index = 1; |
| 141 | + for (Map.Entry<String, Long> entry : topTeam) { |
| 142 | + String teamName = entry.getKey(); |
| 143 | + long balance = entry.getValue(); |
143 | 144 |
|
144 | | - lines.add(MessageFormat.format("{0}# {1}: {2}", |
145 | | - getColor(index) + index, |
146 | | - ChatColor.GRAY + teamName, |
147 | | - "§5" + balance)); |
| 145 | + lines.add(MessageFormat.format("{0}# {1}: {2}", |
| 146 | + getColor(index) + index, |
| 147 | + ChatColor.GRAY + teamName, |
| 148 | + "§5" + balance)); |
148 | 149 |
|
149 | | - index++; |
150 | | - } |
| 150 | + index++; |
| 151 | + } |
151 | 152 |
|
152 | | - String leaderboardText = String.join("\n", lines); |
| 153 | + String leaderboardText = String.join("\n", lines); |
153 | 154 |
|
154 | | - textDisplayTeamTop.setText(Component.text(leaderboardText).content()); |
| 155 | + textDisplayTeamTop.setText(Component.text(leaderboardText).content()); |
| 156 | + }); |
155 | 157 | } |
156 | 158 |
|
157 | 159 | public static void removeLeaderboardTeamTop() { |
@@ -235,29 +237,29 @@ public static void createLeaderboardPlayTime() { |
235 | 237 | public static void updateLeaderboardPlayTime() { |
236 | 238 | if (textDisplayPlayTime == null) return; |
237 | 239 |
|
238 | | - List<Map.Entry<UUID, Long>> topPlayTime = getTopPlayTime(10); |
239 | | - |
240 | | - List<String> lines = new ArrayList<>(); |
241 | | - lines.add("§dLes §f10 §dMeilleurs Temps de Jeu des Joueurs"); |
| 240 | + getTopPlayTime(10, topPlayTime -> { |
| 241 | + List<String> lines = new ArrayList<>(); |
| 242 | + lines.add("§dLes §f10 §dMeilleurs Temps de Jeu des Joueurs"); |
242 | 243 |
|
243 | | - int index = 1; |
244 | | - for (Map.Entry<UUID, Long> entry : topPlayTime) { |
245 | | - UUID playerUUID = entry.getKey(); |
246 | | - long time = entry.getValue(); |
| 244 | + int index = 1; |
| 245 | + for (Map.Entry<UUID, Long> entry : topPlayTime) { |
| 246 | + UUID playerUUID = entry.getKey(); |
| 247 | + long time = entry.getValue(); |
247 | 248 |
|
248 | | - String playerName = Bukkit.getOfflinePlayer(playerUUID).getName(); |
| 249 | + String playerName = Bukkit.getOfflinePlayer(playerUUID).getName(); |
249 | 250 |
|
250 | | - lines.add(MessageFormat.format("{0}# {1}: {2}", |
251 | | - getColor(index) + index, |
252 | | - ChatColor.GRAY + playerName, |
253 | | - ChatColor.DARK_PURPLE + convertTime(time))); |
| 251 | + lines.add(MessageFormat.format("{0}# {1}: {2}", |
| 252 | + getColor(index) + index, |
| 253 | + ChatColor.GRAY + playerName, |
| 254 | + ChatColor.DARK_PURPLE + convertTime(time))); |
254 | 255 |
|
255 | | - index++; |
256 | | - } |
| 256 | + index++; |
| 257 | + } |
257 | 258 |
|
258 | | - String leaderboardText = String.join("\n", lines); |
| 259 | + String leaderboardText = String.join("\n", lines); |
259 | 260 |
|
260 | | - textDisplayPlayTime.setText(Component.text(leaderboardText).content()); |
| 261 | + textDisplayPlayTime.setText(Component.text(leaderboardText).content()); |
| 262 | + }); |
261 | 263 | } |
262 | 264 |
|
263 | 265 | public static void removeLeaderboardPlayTime() { |
@@ -295,44 +297,52 @@ private static void insertTimePlayed(Player player, long time) { |
295 | 297 | } |
296 | 298 | } |
297 | 299 |
|
298 | | - private static List<Map.Entry<UUID, Long>> getTopPlayTime(int limit) { |
299 | | - List<Map.Entry<UUID, Long>> playTime = new ArrayList<>(); |
300 | | - String sql = "SELECT uuid, time FROM playtime ORDER BY time DESC LIMIT ?"; |
| 300 | + private static void getTopPlayTime(int limit, Consumer<List<Map.Entry<UUID, Long>>> callback) { |
| 301 | + Bukkit.getScheduler().runTaskAsynchronously(plugins, () -> { |
| 302 | + List<Map.Entry<UUID, Long>> playTime = new ArrayList<>(); |
| 303 | + String sql = "SELECT uuid, time FROM playtime ORDER BY time DESC LIMIT ?"; |
301 | 304 |
|
302 | | - try (PreparedStatement statement = connection.prepareStatement(sql)) { |
303 | | - statement.setInt(1, limit); |
304 | | - ResultSet rs = statement.executeQuery(); |
| 305 | + try (PreparedStatement statement = connection.prepareStatement(sql)) { |
| 306 | + statement.setInt(1, limit); |
| 307 | + ResultSet rs = statement.executeQuery(); |
305 | 308 |
|
306 | | - while (rs.next()) { |
307 | | - UUID playerUUID = UUID.fromString(rs.getString("uuid")); |
308 | | - long time = rs.getLong("time"); |
| 309 | + while (rs.next()) { |
| 310 | + UUID playerUUID = UUID.fromString(rs.getString("uuid")); |
| 311 | + long time = rs.getLong("time"); |
309 | 312 |
|
310 | | - playTime.add(new AbstractMap.SimpleEntry<>(playerUUID, time)); |
| 313 | + playTime.add(new AbstractMap.SimpleEntry<>(playerUUID, time)); |
| 314 | + } |
| 315 | + } catch (SQLException e) { |
| 316 | + e.printStackTrace(); |
311 | 317 | } |
312 | | - } catch (SQLException e) { |
313 | | - e.printStackTrace(); |
314 | | - } |
315 | | - return playTime; |
| 318 | + |
| 319 | + List<Map.Entry<UUID, Long>> finalResult = playTime; |
| 320 | + Bukkit.getScheduler().runTask(plugins, () -> callback.accept(finalResult)); |
| 321 | + }); |
316 | 322 | } |
317 | 323 |
|
318 | | - private static List<Map.Entry<String, Long>> getTopTeam(int limit) { |
319 | | - List<Map.Entry<String, Long>> teams = new ArrayList<>(); |
320 | | - String sql = "SELECT teamName, balance FROM teams ORDER BY balance DESC LIMIT ?"; |
| 324 | + private static void getTopTeam(int limit, Consumer<List<Map.Entry<String, Long>>> callback) { |
| 325 | + Bukkit.getScheduler().runTaskAsynchronously(plugins, () -> { |
| 326 | + List<Map.Entry<String, Long>> teams = new ArrayList<>(); |
| 327 | + |
| 328 | + String sql = "SELECT teamName, balance FROM teams ORDER BY balance DESC LIMIT ?"; |
321 | 329 |
|
322 | | - try (PreparedStatement statement = connection.prepareStatement(sql)) { |
323 | | - statement.setInt(1, limit); |
324 | | - ResultSet rs = statement.executeQuery(); |
| 330 | + try (PreparedStatement statement = connection.prepareStatement(sql)) { |
| 331 | + statement.setInt(1, limit); |
| 332 | + ResultSet rs = statement.executeQuery(); |
325 | 333 |
|
326 | | - while (rs.next()) { |
327 | | - String teamName = rs.getString("teamName"); |
328 | | - long money = rs.getLong("balance"); |
| 334 | + while (rs.next()) { |
| 335 | + String teamName = rs.getString("teamName"); |
| 336 | + long money = rs.getLong("balance"); |
329 | 337 |
|
330 | | - teams.add(new AbstractMap.SimpleEntry<>(teamName, money)); |
| 338 | + teams.add(new AbstractMap.SimpleEntry<>(teamName, money)); |
| 339 | + } |
| 340 | + } catch (SQLException e) { |
| 341 | + e.printStackTrace(); |
331 | 342 | } |
332 | | - } catch (SQLException e) { |
333 | | - e.printStackTrace(); |
334 | | - } |
335 | | - return teams; |
| 343 | + List<Map.Entry<String, Long>> finalResult = teams; |
| 344 | + Bukkit.getScheduler().runTask(plugins, () -> callback.accept(finalResult)); |
| 345 | + }); |
336 | 346 | } |
337 | 347 |
|
338 | 348 | public static String convertTime(long ticks) { |
|
0 commit comments