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";
//check the cache
String playerName = GriefPrevention.uuidToNameMap.get(playerID);
if(playerName != null) return playerName;
//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 player = GriefPrevention.instance.getServer().getOfflinePlayer(playerID);
if(player.hasPlayedBefore())
{
OfflinePlayer [] players = GriefPrevention.instance.getServer().getOfflinePlayers();
for(int i = 0; i < players.length; i++)
return player.getName();
}
else
{
if(players[i].getUniqueId().equals(playerID))
{
playerName = players[i].getName();
break;
return "someone";
}
}
}
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
static ConcurrentHashMap<UUID, String> uuidToNameMap = new ConcurrentHashMap<UUID, String>();
static void cacheUUIDNamePair(UUID playerID, String playerName)
{
//limit memory footprint
if(GriefPrevention.uuidToNameMap.size() >= 500) GriefPrevention.uuidToNameMap.clear();
GriefPrevention.uuidToNameMap.put(playerID, playerName);
//always store the reverse mapping
//store the reverse mapping
GriefPrevention.instance.playerNameToIDMap.put(playerName, 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_lockTrapDoors && (
clickedBlockType == Material.TRAP_DOOR ||
clickedBlockType == Material.IRON_TRAPDOOR)) ||
clickedBlockType == Material.TRAP_DOOR)) ||
(GriefPrevention.instance.config_claims_lockFenceGates && (
clickedBlockType == Material.FENCE_GATE ||