From c6f91ad2c2077d4b5643b5f98cec026d7c55661d Mon Sep 17 00:00:00 2001 From: md_5 Date: Mon, 13 Jul 2015 19:05:15 +1000 Subject: [PATCH] Use Map for getPlayer(String) lookup. diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java index 708c94c..08faf3a 100644 --- a/src/main/java/net/minecraft/server/PlayerList.java +++ b/src/main/java/net/minecraft/server/PlayerList.java @@ -65,6 +65,7 @@ public abstract class PlayerList { // CraftBukkit start private CraftServer cserver; + private final Map playersByName = new org.spigotmc.CaseInsensitiveMap(); public PlayerList(MinecraftServer minecraftserver) { this.cserver = minecraftserver.server = new CraftServer(minecraftserver, this); @@ -321,6 +322,7 @@ public abstract class PlayerList { public void onPlayerJoin(EntityPlayer entityplayer, String joinMessage) { // CraftBukkit added param this.players.add(entityplayer); + this.playersByName.put(entityplayer.getName(), entityplayer); // Spigot this.j.put(entityplayer.getUniqueID(), entityplayer); // this.sendAll(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, new EntityPlayer[] { entityplayer})); // CraftBukkit - replaced with loop below WorldServer worldserver = this.server.getWorldServer(entityplayer.dimension); @@ -406,6 +408,7 @@ public abstract class PlayerList { worldserver.kill(entityplayer); worldserver.getPlayerChunkMap().removePlayer(entityplayer); this.players.remove(entityplayer); + this.playersByName.remove(entityplayer.getName()); // Spigot UUID uuid = entityplayer.getUniqueID(); EntityPlayer entityplayer1 = (EntityPlayer) this.j.get(uuid); @@ -557,6 +560,7 @@ public abstract class PlayerList { // entityplayer.x().getTracker().untrackEntity(entityplayer); // CraftBukkit entityplayer.x().getPlayerChunkMap().removePlayer(entityplayer); this.players.remove(entityplayer); + this.playersByName.remove(entityplayer.getName()); // Spigot this.server.getWorldServer(entityplayer.dimension).removeEntity(entityplayer); BlockPosition blockposition = entityplayer.getBed(); boolean flag1 = entityplayer.isRespawnForced(); @@ -661,6 +665,7 @@ public abstract class PlayerList { worldserver.getPlayerChunkMap().addPlayer(entityplayer1); worldserver.addEntity(entityplayer1); this.players.add(entityplayer1); + this.playersByName.put(entityplayer1.getName(), entityplayer1); // Spigot this.j.put(entityplayer1.getUniqueID(), entityplayer1); } // entityplayer1.syncInventory(); @@ -1129,19 +1134,7 @@ public abstract class PlayerList { } public EntityPlayer getPlayer(String s) { - Iterator iterator = this.players.iterator(); - - EntityPlayer entityplayer; - - do { - if (!iterator.hasNext()) { - return null; - } - - entityplayer = (EntityPlayer) iterator.next(); - } while (!entityplayer.getName().equalsIgnoreCase(s)); - - return entityplayer; + return this.playersByName.get(s); // Spigot } public void sendPacketNearby(EntityHuman entityhuman, double d0, double d1, double d2, double d3, int i, Packet packet) { diff --git a/src/main/java/org/spigotmc/CaseInsensitiveHashingStrategy.java b/src/main/java/org/spigotmc/CaseInsensitiveHashingStrategy.java new file mode 100644 index 0000000..aafdd36 --- /dev/null +++ b/src/main/java/org/spigotmc/CaseInsensitiveHashingStrategy.java @@ -0,0 +1,18 @@ +package org.spigotmc; + +import gnu.trove.strategy.HashingStrategy; + +class CaseInsensitiveHashingStrategy implements HashingStrategy { + + static final CaseInsensitiveHashingStrategy INSTANCE = new CaseInsensitiveHashingStrategy(); + + @Override + public int computeHashCode(Object object) { + return ((String) object).toLowerCase().hashCode(); + } + + @Override + public boolean equals(Object o1, Object o2) { + return o1.equals(o2) || (o1 instanceof String && o2 instanceof String && ((String) o1).toLowerCase().equals(((String) o2).toLowerCase())); + } +} diff --git a/src/main/java/org/spigotmc/CaseInsensitiveMap.java b/src/main/java/org/spigotmc/CaseInsensitiveMap.java new file mode 100644 index 0000000..1934fd5 --- /dev/null +++ b/src/main/java/org/spigotmc/CaseInsensitiveMap.java @@ -0,0 +1,15 @@ +package org.spigotmc; + +import gnu.trove.map.hash.TCustomHashMap; +import java.util.Map; + +public class CaseInsensitiveMap extends TCustomHashMap { + + public CaseInsensitiveMap() { + super(CaseInsensitiveHashingStrategy.INSTANCE); + } + + public CaseInsensitiveMap(Map map) { + super(CaseInsensitiveHashingStrategy.INSTANCE, map); + } +} -- 2.5.0