Perf: Faster player name lookup by UUID.

This commit is contained in:
ryanhamshire 2014-12-02 19:22:23 -08:00
parent 0bd36427c7
commit b7cb5dba51
2 changed files with 7 additions and 36 deletions

View File

@ -2283,49 +2283,21 @@ public class GriefPrevention extends JavaPlugin
if(playerID == null) return "someone"; if(playerID == null) return "someone";
//check the cache //check the cache
String playerName = GriefPrevention.uuidToNameMap.get(playerID); OfflinePlayer player = GriefPrevention.instance.getServer().getOfflinePlayer(playerID);
if(playerName != null) return playerName; if(player.hasPlayedBefore())
//try online players next
Player player = GriefPrevention.instance.getServer().getPlayer(playerID);
if(player != null) playerName = player.getName();
//then search offline players
if(playerName == null)
{ {
OfflinePlayer [] players = GriefPrevention.instance.getServer().getOfflinePlayers(); return player.getName();
for(int i = 0; i < players.length; i++) }
else
{ {
if(players[i].getUniqueId().equals(playerID)) return "someone";
{
playerName = players[i].getName();
break;
} }
} }
}
if(playerName == null)
{
playerName = "someone";
}
//cache the result
GriefPrevention.cacheUUIDNamePair(playerID, playerName);
//return result
return playerName;
}
//cache for player name lookups, to save searches of all offline players //cache for player name lookups, to save searches of all offline players
static ConcurrentHashMap<UUID, String> uuidToNameMap = new ConcurrentHashMap<UUID, String>();
static void cacheUUIDNamePair(UUID playerID, String playerName) static void cacheUUIDNamePair(UUID playerID, String playerName)
{ {
//limit memory footprint //store the reverse mapping
if(GriefPrevention.uuidToNameMap.size() >= 500) GriefPrevention.uuidToNameMap.clear();
GriefPrevention.uuidToNameMap.put(playerID, playerName);
//always store the reverse mapping
GriefPrevention.instance.playerNameToIDMap.put(playerName, playerID); GriefPrevention.instance.playerNameToIDMap.put(playerName, playerID);
GriefPrevention.instance.playerNameToIDMap.put(playerName.toLowerCase(), playerID); GriefPrevention.instance.playerNameToIDMap.put(playerName.toLowerCase(), playerID);
} }

View File

@ -1180,8 +1180,7 @@ class PlayerEventHandler implements Listener
(GriefPrevention.instance.config_claims_preventButtonsSwitches && clickedBlockType == Material.BED_BLOCK) || (GriefPrevention.instance.config_claims_preventButtonsSwitches && clickedBlockType == Material.BED_BLOCK) ||
(GriefPrevention.instance.config_claims_lockTrapDoors && ( (GriefPrevention.instance.config_claims_lockTrapDoors && (
clickedBlockType == Material.TRAP_DOOR || clickedBlockType == Material.TRAP_DOOR)) ||
clickedBlockType == Material.IRON_TRAPDOOR)) ||
(GriefPrevention.instance.config_claims_lockFenceGates && ( (GriefPrevention.instance.config_claims_lockFenceGates && (
clickedBlockType == Material.FENCE_GATE || clickedBlockType == Material.FENCE_GATE ||