Use Map for getPlayer(String) lookup.

This commit is contained in:
md_5 2015-07-13 19:05:30 +10:00 committed by Byteflux
parent 62112bb526
commit a457345b27

View file

@ -0,0 +1,119 @@
From 4534a446515a78783aec25149371e8292e1bb7a0 Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
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 ab1d9a5..461e3f9 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<String,EntityPlayer> playersByName = new org.spigotmc.CaseInsensitiveMap<EntityPlayer>();
public PlayerList(MinecraftServer minecraftserver) {
this.cserver = minecraftserver.server = new CraftServer(minecraftserver, this);
@@ -287,6 +288,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);
@@ -358,6 +360,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);
@@ -508,6 +511,7 @@ public abstract class PlayerList {
// entityplayer.u().getTracker().untrackEntity(entityplayer); // CraftBukkit
entityplayer.u().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();
@@ -603,6 +607,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);
}
// Added from changeDimension
@@ -1020,19 +1025,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(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<V> extends TCustomHashMap<String, V> {
+
+ public CaseInsensitiveMap() {
+ super(CaseInsensitiveHashingStrategy.INSTANCE);
+ }
+
+ public CaseInsensitiveMap(Map<? extends String, ? extends V> map) {
+ super(CaseInsensitiveHashingStrategy.INSTANCE, map);
+ }
+}
--
2.1.4