From 4d23408e97278c1c9dfcf5f0bc5da9580013b80f Mon Sep 17 00:00:00 2001 From: PikaMug Date: Sat, 11 Jan 2020 03:32:34 -0500 Subject: [PATCH] Run lookup for /quests top asynchronously --- .../quests/listeners/CmdExecutor.java | 73 +++++++++---------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java b/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java index d7fdaf38a..76320bfb5 100644 --- a/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java +++ b/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java @@ -537,47 +537,44 @@ public class CmdExecutor implements CommandExecutor { .replace("", String.valueOf(plugin.getSettings().getTopLimit()))); return true; } - File folder = new File(plugin.getDataFolder(), "data"); - File[] playerFiles = folder.listFiles(); - Map questPoints = new HashMap(); - if (playerFiles != null) { - for (File f : playerFiles) { - if (!f.isDirectory()) { - FileConfiguration data = new YamlConfiguration(); - try { - data.load(f); - } catch (IOException e) { - e.printStackTrace(); - } catch (InvalidConfigurationException e) { - e.printStackTrace(); + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + File folder = new File(plugin.getDataFolder(), "data"); + File[] playerFiles = folder.listFiles(); + Map questPoints = new HashMap(); + if (playerFiles != null) { + for (File f : playerFiles) { + if (!f.isDirectory()) { + FileConfiguration data = new YamlConfiguration(); + try { + data.load(f); + } catch (IOException e) { + e.printStackTrace(); + } catch (InvalidConfigurationException e) { + e.printStackTrace(); + } + questPoints.put(data.getString("lastKnownName", "Unknown"), + data.getInt("quest-points", 0)); + } + } + } + LinkedHashMap sortedMap = (LinkedHashMap) sort(questPoints); + int numPrinted = 0; + String msg = Lang.get("topQuestersTitle"); + msg = msg.replace("", ChatColor.DARK_PURPLE + "" + topNumber + ChatColor.GOLD); + cs.sendMessage(ChatColor.GOLD + msg); + for (Entry entry : sortedMap.entrySet()) { + numPrinted++; + cs.sendMessage(ChatColor.YELLOW + String.valueOf(numPrinted) + ". " + entry.getKey() + " - " + + ChatColor.DARK_PURPLE + entry.getValue() + ChatColor.YELLOW + " " + + Lang.get("questPoints")); + if (numPrinted == topNumber) { + break; } - String name = f.getName().substring(0, (f.getName().indexOf("."))); - questPoints.put(name, data.getInt("quest-points")); } } - } - LinkedHashMap sortedMap = (LinkedHashMap) sort(questPoints); - int numPrinted = 0; - String msg = Lang.get("topQuestersTitle"); - msg = msg.replace("", ChatColor.DARK_PURPLE + "" + topNumber + ChatColor.GOLD); - cs.sendMessage(ChatColor.GOLD + msg); - for (String s : sortedMap.keySet()) { - int i = (Integer) sortedMap.get(s); - s = s.trim(); - try { - UUID id = UUID.fromString(s); - s = Bukkit.getOfflinePlayer(id).getName(); - } catch (IllegalArgumentException e) { - plugin.getLogger().warning("File name \"" + s + "\"in /data folder is not a valid player UUID!"); - break; - } - numPrinted++; - cs.sendMessage(ChatColor.YELLOW + String.valueOf(numPrinted) + ". " + s + " - " + ChatColor.DARK_PURPLE - + i + ChatColor.YELLOW + " " + Lang.get("questPoints")); - if (numPrinted == topNumber) { - break; - } - } + }); } return true; }