PaperMC/CraftBukkit-Patches/0145-Use-Map-for-getPlayer-String-lookup.patch
2016-03-06 16:43:38 +11:00

117 lines
No EOL
5.1 KiB
Diff

From 0000000000000000000000000000000000000000 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/PlayerList.java
+++ b/src/main/java/net/minecraft/server/PlayerList.java
@@ -0,0 +0,0 @@ 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);
@@ -0,0 +0,0 @@ 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);
@@ -0,0 +0,0 @@ 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);
@@ -0,0 +0,0 @@ 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();
@@ -0,0 +0,0 @@ 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();
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/org/spigotmc/CaseInsensitiveHashingStrategy.java
@@ -0,0 +0,0 @@
+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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/org/spigotmc/CaseInsensitiveMap.java
@@ -0,0 +0,0 @@
+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);
+ }
+}
--