2014-12-14 23:26:48 +01:00
|
|
|
From 5b2eb900e278b76b5d0af2090c94b38268d53d6d Mon Sep 17 00:00:00 2001
|
2014-10-20 00:58:49 +02:00
|
|
|
From: Aikar <aikar@aikar.co>
|
2014-11-28 02:17:45 +01:00
|
|
|
Date: Fri, 28 Nov 2014 13:43:11 -0600
|
2014-10-20 00:58:49 +02:00
|
|
|
Subject: [PATCH] Player lookup improvements
|
|
|
|
|
|
|
|
Minecraft and CraftBukkit both use Arrays to store online players,
|
|
|
|
and any time a player needs to be looked up by name or UUID,
|
|
|
|
the system iterates all online players and does a name or UUID comparison.
|
|
|
|
|
|
|
|
This is very ineffecient and can reduce performance on servers with high player count.
|
|
|
|
|
|
|
|
By using a map based approach for player lookups, player lookup should
|
|
|
|
be consistent in performance regardless of how many players are online.
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java
|
2014-12-14 23:25:11 +01:00
|
|
|
index c4b11c9..cc2cc30 100644
|
2014-10-20 00:58:49 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/PlayerList.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/PlayerList.java
|
2014-12-13 05:52:49 +01:00
|
|
|
@@ -49,6 +49,31 @@ public abstract class PlayerList {
|
2014-11-28 02:17:45 +01:00
|
|
|
private static final SimpleDateFormat i = new SimpleDateFormat("yyyy-MM-dd \'at\' HH:mm:ss z");
|
2014-10-20 00:58:49 +02:00
|
|
|
private final MinecraftServer server;
|
|
|
|
public final List players = new java.util.concurrent.CopyOnWriteArrayList(); // CraftBukkit - ArrayList -> CopyOnWriteArrayList: Iterator safety
|
|
|
|
+ // PaperSpigot start - Player lookup improvements
|
|
|
|
+ public final Map<String, EntityPlayer> playerMap = new java.util.HashMap<String, EntityPlayer>() {
|
|
|
|
+ @Override
|
|
|
|
+ public EntityPlayer put(String key, EntityPlayer value) {
|
|
|
|
+ return super.put(key.toLowerCase(), value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public EntityPlayer get(Object key) {
|
|
|
|
+ // put the .playerConnection check done in other places here
|
2014-11-28 02:17:45 +01:00
|
|
|
+ EntityPlayer player = super.get(key instanceof String ? ((String) key).toLowerCase() : key);
|
2014-10-20 00:58:49 +02:00
|
|
|
+ return (player != null && player.playerConnection != null) ? player : null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean containsKey(Object key) {
|
|
|
|
+ return get(key) != null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public EntityPlayer remove(Object key) {
|
2014-11-28 02:17:45 +01:00
|
|
|
+ return super.remove(key instanceof String ? ((String) key).toLowerCase() : key);
|
2014-10-20 00:58:49 +02:00
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ // PaperSpigot end
|
2014-11-28 02:17:45 +01:00
|
|
|
public final Map f = Maps.newHashMap();
|
|
|
|
private final GameProfileBanList k;
|
|
|
|
private final IpBanList l;
|
2014-12-13 05:52:49 +01:00
|
|
|
@@ -254,6 +279,7 @@ public abstract class PlayerList {
|
2014-11-28 02:17:45 +01:00
|
|
|
|
|
|
|
public void onPlayerJoin(EntityPlayer entityplayer) {
|
2014-10-20 00:58:49 +02:00
|
|
|
this.players.add(entityplayer);
|
|
|
|
+ this.playerMap.put(entityplayer.getName(), entityplayer); // PaperSpigot
|
2014-11-28 02:17:45 +01:00
|
|
|
this.f.put(entityplayer.getUniqueID(), entityplayer);
|
|
|
|
// this.sendAll(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, new EntityPlayer[] { entityplayer})); // CraftBukkit - replaced with loop below
|
2014-10-20 00:58:49 +02:00
|
|
|
WorldServer worldserver = this.server.getWorldServer(entityplayer.dimension);
|
2014-12-13 05:52:49 +01:00
|
|
|
@@ -325,6 +351,7 @@ public abstract class PlayerList {
|
2014-10-20 00:58:49 +02:00
|
|
|
worldserver.kill(entityplayer);
|
|
|
|
worldserver.getPlayerChunkMap().removePlayer(entityplayer);
|
|
|
|
this.players.remove(entityplayer);
|
|
|
|
+ this.playerMap.remove(entityplayer.getName()); // PaperSpigot
|
2014-11-28 02:17:45 +01:00
|
|
|
this.f.remove(entityplayer.getUniqueID());
|
|
|
|
this.o.remove(entityplayer.getUniqueID());
|
|
|
|
// CraftBukkit start
|
2014-12-13 05:52:49 +01:00
|
|
|
@@ -356,6 +383,8 @@ public abstract class PlayerList {
|
2014-10-20 00:58:49 +02:00
|
|
|
|
|
|
|
EntityPlayer entityplayer;
|
|
|
|
|
2014-11-28 02:17:45 +01:00
|
|
|
+ // PaperSpigot - Use exact lookup below
|
|
|
|
+ /*
|
2014-10-20 00:58:49 +02:00
|
|
|
for (int i = 0; i < this.players.size(); ++i) {
|
|
|
|
entityplayer = (EntityPlayer) this.players.get(i);
|
|
|
|
if (entityplayer.getUniqueID().equals(uuid)) {
|
2014-12-13 05:52:49 +01:00
|
|
|
@@ -367,6 +396,8 @@ public abstract class PlayerList {
|
2014-10-20 00:58:49 +02:00
|
|
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
entityplayer = (EntityPlayer) iterator.next();
|
2014-12-07 20:28:45 +01:00
|
|
|
+ */
|
|
|
|
+ if ((entityplayer = this.a(uuid)) != null) {
|
|
|
|
savePlayerFile(entityplayer); // CraftBukkit - Force the player's inventory to be saved
|
2014-10-20 00:58:49 +02:00
|
|
|
entityplayer.playerConnection.disconnect("You logged in from another location");
|
|
|
|
}
|
2014-12-13 05:52:49 +01:00
|
|
|
@@ -958,6 +989,7 @@ public abstract class PlayerList {
|
2014-10-20 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public EntityPlayer getPlayer(String s) {
|
|
|
|
+ if (true) { return playerMap.get(s); } // PaperSpigot
|
|
|
|
Iterator iterator = this.players.iterator();
|
|
|
|
|
|
|
|
EntityPlayer entityplayer;
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java b/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
|
2014-12-07 20:29:55 +01:00
|
|
|
index 8be0698..4a908c6 100644
|
2014-10-20 00:58:49 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
|
|
|
|
@@ -144,14 +144,10 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa
|
|
|
|
}
|
|
|
|
|
|
|
|
public Player getPlayer() {
|
|
|
|
- for (Object obj : server.getHandle().players) {
|
|
|
|
- EntityPlayer player = (EntityPlayer) obj;
|
|
|
|
- if (player.getUniqueID().equals(getUniqueId())) {
|
|
|
|
- return (player.playerConnection != null) ? player.playerConnection.getPlayer() : null;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return null;
|
|
|
|
+ // PaperSpigot - Improved player lookup, replace entire method
|
2014-12-07 20:28:45 +01:00
|
|
|
+ final EntityPlayer playerEntity = server.getHandle().a(getUniqueId());
|
2014-10-20 00:58:49 +02:00
|
|
|
+ return playerEntity != null ? playerEntity.getBukkitEntity() : null;
|
|
|
|
+ // PaperSpigot end
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
2014-12-14 23:25:11 +01:00
|
|
|
index 5f6f1cf..f10f41a 100644
|
2014-10-20 00:58:49 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
2014-12-14 23:25:11 +01:00
|
|
|
@@ -400,7 +400,12 @@ public final class CraftServer implements Server {
|
2014-10-20 00:58:49 +02:00
|
|
|
public Player getPlayer(final String name) {
|
|
|
|
Validate.notNull(name, "Name cannot be null");
|
|
|
|
|
|
|
|
- Player found = null;
|
|
|
|
+ // PaperSpigot start - Improved player lookup changes
|
|
|
|
+ Player found = getPlayerExact(name);
|
|
|
|
+ if (found != null) {
|
|
|
|
+ return found;
|
|
|
|
+ }
|
|
|
|
+ // PaperSpigot end
|
|
|
|
String lowerName = name.toLowerCase();
|
|
|
|
int delta = Integer.MAX_VALUE;
|
|
|
|
for (Player player : getOnlinePlayers()) {
|
2014-12-14 23:25:11 +01:00
|
|
|
@@ -421,15 +426,10 @@ public final class CraftServer implements Server {
|
2014-10-20 00:58:49 +02:00
|
|
|
public Player getPlayerExact(String name) {
|
2014-11-28 02:17:45 +01:00
|
|
|
Validate.notNull(name, "Name cannot be null");
|
|
|
|
|
2014-10-20 00:58:49 +02:00
|
|
|
- String lname = name.toLowerCase();
|
|
|
|
-
|
|
|
|
- for (Player player : getOnlinePlayers()) {
|
|
|
|
- if (player.getName().equalsIgnoreCase(lname)) {
|
|
|
|
- return player;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return null;
|
|
|
|
+ // PaperSpigot start - Improved player lookup, replace whole method
|
|
|
|
+ EntityPlayer player = playerList.playerMap.get(name);
|
|
|
|
+ return player != null ? player.getBukkitEntity() : null;
|
2014-12-07 20:28:45 +01:00
|
|
|
+ // PaperSpigot end
|
2014-11-28 02:17:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-10-20 00:58:49 +02:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
2014-12-14 23:25:11 +01:00
|
|
|
index 7358519..e9bb266 100644
|
2014-10-20 00:58:49 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
2014-12-14 23:25:11 +01:00
|
|
|
@@ -104,13 +104,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
2014-10-20 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOnline() {
|
|
|
|
- for (Object obj : server.getHandle().players) {
|
|
|
|
- EntityPlayer player = (EntityPlayer) obj;
|
|
|
|
- if (player.getName().equalsIgnoreCase(getName())) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false;
|
2014-12-07 20:28:45 +01:00
|
|
|
+ return server.getHandle().a(getUniqueId()) != null; // PaperSpigot - replace whole method
|
2014-10-20 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public InetSocketAddress getAddress() {
|
|
|
|
--
|
2014-12-07 20:29:55 +01:00
|
|
|
1.9.1
|
|
|
|
|