Paper/CraftBukkit-Patches/0145-Use-Map-for-getPlayer-String-lookup.patch

120 lines
4.9 KiB
Diff
Raw Normal View History

2016-03-06 06:43:38 +01:00
From c6f91ad2c2077d4b5643b5f98cec026d7c55661d Mon Sep 17 00:00:00 2001
2015-07-13 11:05:30 +02:00
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
2016-03-06 06:43:38 +01:00
index 708c94c..08faf3a 100644
2015-07-13 11:05:30 +02:00
--- 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);
2016-02-29 22:33:06 +01:00
@@ -321,6 +322,7 @@ public abstract class PlayerList {
2015-07-13 11:05:30 +02:00
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);
2016-02-29 22:33:06 +01:00
@@ -406,6 +408,7 @@ public abstract class PlayerList {
2015-07-13 11:05:30 +02:00
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);
2016-03-06 04:36:33 +01:00
@@ -557,6 +560,7 @@ public abstract class PlayerList {
2016-02-29 22:33:06 +01:00
// entityplayer.x().getTracker().untrackEntity(entityplayer); // CraftBukkit
entityplayer.x().getPlayerChunkMap().removePlayer(entityplayer);
2015-07-13 11:05:30 +02:00
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();
2016-03-06 04:36:33 +01:00
@@ -661,6 +665,7 @@ public abstract class PlayerList {
2015-07-13 11:05:30 +02:00
worldserver.getPlayerChunkMap().addPlayer(entityplayer1);
worldserver.addEntity(entityplayer1);
this.players.add(entityplayer1);
+ this.playersByName.put(entityplayer1.getName(), entityplayer1); // Spigot
this.j.put(entityplayer1.getUniqueID(), entityplayer1);
}
2016-03-06 06:43:38 +01:00
// entityplayer1.syncInventory();
2016-03-06 04:36:33 +01:00
@@ -1129,19 +1134,7 @@ public abstract class PlayerList {
2015-07-13 11:05:30 +02:00
}
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
}
2016-02-29 22:33:06 +01:00
public void sendPacketNearby(EntityHuman entityhuman, double d0, double d1, double d2, double d3, int i, Packet<?> packet) {
2015-07-13 11:05:30 +02:00
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.5.0
2015-07-13 11:05:30 +02:00