From 0611f5d6954cb35cf10ea5a1163113deb83d2bfa Mon Sep 17 00:00:00 2001 From: Len <40720638+destro174@users.noreply.github.com> Date: Tue, 24 May 2022 21:03:47 +0200 Subject: [PATCH] revert e18ec1 --- .../GriefPrevention/GriefPrevention.java | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java b/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java index 8ad2a1c..e2258d7 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java @@ -374,6 +374,12 @@ public class GriefPrevention extends JavaPlugin economyHandler = new EconomyHandler(this); pluginManager.registerEvents(economyHandler, this); + //cache offline players + OfflinePlayer[] offlinePlayers = this.getServer().getOfflinePlayers(); + CacheOfflinePlayerNamesThread namesThread = new CacheOfflinePlayerNamesThread(offlinePlayers, this.playerNameToIDMap); + namesThread.setPriority(Thread.MIN_PRIORITY); + namesThread.start(); + new AltitudeListener(this.dataStore, this); if (getServer().getPluginManager().isPluginEnabled("Pl3xMap")) { pl3xmapHook = new Pl3xMapHook(this); @@ -2744,7 +2750,50 @@ public class GriefPrevention extends JavaPlugin } //helper method to resolve a player by name - ConcurrentHashMap playerNameToIDMap = new ConcurrentHashMap<>(); // TODO REMOVE ME + ConcurrentHashMap playerNameToIDMap = new ConcurrentHashMap<>(); + + //thread to build the above cache + private class CacheOfflinePlayerNamesThread extends Thread + { + private final OfflinePlayer[] offlinePlayers; + private final ConcurrentHashMap playerNameToIDMap; + + CacheOfflinePlayerNamesThread(OfflinePlayer[] offlinePlayers, ConcurrentHashMap playerNameToIDMap) + { + this.offlinePlayers = offlinePlayers; + this.playerNameToIDMap = playerNameToIDMap; + } + + public void run() + { + long now = System.currentTimeMillis(); + final long millisecondsPerDay = 1000 * 60 * 60 * 24; + for (OfflinePlayer player : offlinePlayers) + { + try + { + UUID playerID = player.getUniqueId(); + if (playerID == null) continue; + long lastSeen = player.getLastPlayed(); + + //if the player has been seen in the last 90 days, cache his name/UUID pair + long diff = now - lastSeen; + long daysDiff = diff / millisecondsPerDay; + if (daysDiff <= config_advanced_offlineplayer_cache_days) + { + String playerName = player.getName(); + if (playerName == null) continue; + this.playerNameToIDMap.put(playerName, playerID); + this.playerNameToIDMap.put(playerName.toLowerCase(), playerID); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + } + } public OfflinePlayer resolvePlayerByName(String name)