Archiviert
13
0
Dieses Repository wurde am 2024-12-25 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
Paper-Old/CraftBukkit-Patches/0145-Use-Map-for-getPlayer-String-lookup.patch
2016-03-03 03:46:56 -06:00

120 Zeilen
4.9 KiB
Diff

From 9b75f018a3dccb9510ac120fda821a1b64e3d0b1 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 6eb509c..50add77 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);
@@ -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);
@@ -556,6 +559,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();
@@ -660,6 +664,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
@@ -1130,19 +1135,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<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