Use Map for getPlayer(String) lookup.

By: md_5 <git@md-5.net>
This commit is contained in:
CraftBukkit/Spigot 2015-07-13 19:05:15 +10:00
parent d0a65bc995
commit c4f3fcfce9

View file

@ -63,12 +63,13 @@
public final PlayerDataStorage playerIo;
private boolean doWhiteList;
private final LayeredRegistryAccess<RegistryLayer> registries;
@@ -134,13 +156,23 @@
@@ -134,13 +156,24 @@
private static final boolean ALLOW_LOGOUTIVATOR = false;
private int sendAllPlayerInfoIn;
+ // CraftBukkit start
+ private CraftServer cserver;
+ private final Map<String,ServerPlayer> playersByName = new java.util.HashMap<>();
+
public PlayerList(MinecraftServer server, LayeredRegistryAccess<RegistryLayer> registryManager, PlayerDataStorage saveHandler, int maxPlayers) {
+ this.cserver = server.server = new CraftServer((DedicatedServer) server, this);
@ -89,7 +90,7 @@
this.server = server;
this.registries = registryManager;
this.maxPlayers = maxPlayers;
@@ -150,25 +182,34 @@
@@ -150,25 +183,34 @@
public void placeNewPlayer(Connection connection, ServerPlayer player, CommonListenerCookie clientData) {
GameProfile gameprofile = player.getGameProfile();
GameProfileCache usercache = this.server.getProfileCache();
@ -129,7 +130,7 @@
ServerLevel worldserver = this.server.getLevel(resourcekey);
ServerLevel worldserver1;
@@ -182,10 +223,24 @@
@@ -182,10 +224,24 @@
player.setServerLevel(worldserver1);
String s1 = connection.getLoggableAddress(this.server.logIPs());
@ -156,7 +157,7 @@
ServerGamePacketListenerImpl playerconnection = new ServerGamePacketListenerImpl(this.server, connection, player, clientData);
connection.setupInboundProtocol(GameProtocols.SERVERBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(this.server.registryAccess())), playerconnection);
@@ -194,7 +249,9 @@
@@ -194,7 +250,9 @@
boolean flag1 = gamerules.getBoolean(GameRules.RULE_REDUCEDDEBUGINFO);
boolean flag2 = gamerules.getBoolean(GameRules.RULE_LIMITED_CRAFTING);
@ -167,7 +168,7 @@
playerconnection.send(new ClientboundChangeDifficultyPacket(worlddata.getDifficulty(), worlddata.isDifficultyLocked()));
playerconnection.send(new ClientboundPlayerAbilitiesPacket(player.getAbilities()));
playerconnection.send(new ClientboundSetHeldSlotPacket(player.getInventory().selected));
@@ -213,8 +270,10 @@
@@ -213,8 +271,10 @@
} else {
ichatmutablecomponent = Component.translatable("multiplayer.player.joined.renamed", player.getDisplayName(), s);
}
@ -179,13 +180,14 @@
playerconnection.teleport(player.getX(), player.getY(), player.getZ(), player.getYRot(), player.getXRot());
ServerStatus serverping = this.server.getStatus();
@@ -222,17 +281,70 @@
@@ -222,17 +282,71 @@
player.sendServerStatus(serverping);
}
- player.connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(this.players));
+ // entityplayer.connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(this.players)); // CraftBukkit - replaced with loop below
this.players.add(player);
+ this.playersByName.put(player.getScoreboardName().toLowerCase(java.util.Locale.ROOT), player); // Spigot
this.playersByUUID.put(player.getUUID(), player);
- this.broadcastAll(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(player)));
+ // this.broadcastAll(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(entityplayer))); // CraftBukkit - replaced with loop below
@ -254,7 +256,7 @@
}
public void updateEntireScoreboard(ServerScoreboard scoreboard, ServerPlayer player) {
@@ -269,30 +381,31 @@
@@ -269,30 +383,31 @@
}
public void addWorldborderListener(ServerLevel world) {
@ -291,7 +293,7 @@
}
@Override
@@ -319,14 +432,15 @@
@@ -319,14 +434,15 @@
}
protected void save(ServerPlayer player) {
@ -309,7 +311,7 @@
if (advancementdataplayer != null) {
advancementdataplayer.save();
@@ -334,95 +448,175 @@
@@ -334,95 +450,176 @@
}
@ -372,6 +374,7 @@
+ worldserver.removePlayerImmediately(entityplayer, Entity.RemovalReason.UNLOADED_WITH_PLAYER);
+ entityplayer.getAdvancements().stopListening();
+ this.players.remove(entityplayer);
+ this.playersByName.remove(entityplayer.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot
+ this.server.getCustomBossEvents().onPlayerDisconnect(entityplayer);
+ UUID uuid = entityplayer.getUUID();
ServerPlayer entityplayer1 = (ServerPlayer) this.playersByUUID.get(uuid);
@ -473,10 +476,11 @@
}
- return ichatmutablecomponent;
- } else {
- return this.players.size() >= this.maxPlayers && !this.canBypassPlayerLimit(profile) ? Component.translatable("multiplayer.disconnect.server_full") : null;
+ // return chatmessage;
+ event.disallow(PlayerLoginEvent.Result.KICK_BANNED, CraftChatMessage.fromComponent(ichatmutablecomponent));
} else {
- return this.players.size() >= this.maxPlayers && !this.canBypassPlayerLimit(profile) ? Component.translatable("multiplayer.disconnect.server_full") : null;
+ } else {
+ // return this.players.size() >= this.maxPlayers && !this.canBypassPlayerLimit(gameprofile) ? IChatBaseComponent.translatable("multiplayer.disconnect.server_full") : null;
+ if (this.players.size() >= this.maxPlayers && !this.canBypassPlayerLimit(gameprofile)) {
+ event.disallow(PlayerLoginEvent.Result.KICK_FULL, org.spigotmc.SpigotConfig.serverFullMessage); // Spigot
@ -523,7 +527,7 @@
if (entityplayer1 != null) {
set.add(entityplayer1);
@@ -431,30 +625,49 @@
@@ -431,30 +628,50 @@
Iterator iterator1 = set.iterator();
while (iterator1.hasNext()) {
@ -557,6 +561,7 @@
+ public ServerPlayer respawn(ServerPlayer entityplayer, boolean flag, Entity.RemovalReason entity_removalreason, RespawnReason reason, Location location) {
+ entityplayer.stopRiding(); // CraftBukkit
+ this.players.remove(entityplayer);
+ this.playersByName.remove(entityplayer.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot
+ entityplayer.serverLevel().removePlayerImmediately(entityplayer, entity_removalreason);
+ /* CraftBukkit start
+ TeleportTransition teleporttransition = entityplayer.findRespawnPositionAndUseSpawnBlock(!flag, TeleportTransition.DO_NOTHING);
@ -586,7 +591,7 @@
while (iterator.hasNext()) {
String s = (String) iterator.next();
@@ -462,40 +675,85 @@
@@ -462,41 +679,87 @@
entityplayer1.addTag(s);
}
@ -641,6 +646,7 @@
+ if (!entityplayer.connection.isDisconnected()) {
+ worldserver.addRespawnedPlayer(entityplayer1);
+ this.players.add(entityplayer1);
+ this.playersByName.put(entityplayer1.getScoreboardName().toLowerCase(java.util.Locale.ROOT), entityplayer1); // Spigot
+ this.playersByUUID.put(entityplayer1.getUUID(), entityplayer1);
+ }
+ // entityplayer1.initInventoryMenu();
@ -655,14 +661,14 @@
if (iblockdata.is(Blocks.RESPAWN_ANCHOR)) {
entityplayer1.connection.send(new ClientboundSoundPacket(SoundEvents.RESPAWN_ANCHOR_DEPLETE, SoundSource.BLOCKS, (double) blockposition.getX(), (double) blockposition.getY(), (double) blockposition.getZ(), 1.0F, 1.0F, worldserver.getRandom().nextLong()));
}
+ }
}
+ // Added from changeDimension
+ this.sendAllPlayerInfo(entityplayer); // Update health, etc...
+ entityplayer.onUpdateAbilities();
+ for (MobEffectInstance mobEffect : entityplayer.getActiveEffects()) {
+ entityplayer.connection.send(new ClientboundUpdateMobEffectPacket(entityplayer.getId(), mobEffect, false)); // blend = false
+ }
+
+ // Fire advancement trigger
+ entityplayer.triggerDimensionChangeTriggers(worldserver);
+
@ -675,12 +681,13 @@
+ // Save player file again if they were disconnected
+ if (entityplayer.connection.isDisconnected()) {
+ this.save(entityplayer);
}
+ }
+ // CraftBukkit end
+
return entityplayer1;
}
@@ -524,7 +782,18 @@
@@ -524,7 +787,18 @@
public void tick() {
if (++this.sendAllPlayerInfoIn > 600) {
@ -700,7 +707,7 @@
this.sendAllPlayerInfoIn = 0;
}
@@ -541,6 +810,25 @@
@@ -541,6 +815,25 @@
}
@ -726,7 +733,7 @@
public void broadcastAll(Packet<?> packet, ResourceKey<Level> dimension) {
Iterator iterator = this.players.iterator();
@@ -554,7 +842,7 @@
@@ -554,7 +847,7 @@
}
@ -735,7 +742,7 @@
PlayerTeam scoreboardteam = source.getTeam();
if (scoreboardteam != null) {
@@ -573,7 +861,7 @@
@@ -573,7 +866,7 @@
}
}
@ -744,7 +751,7 @@
PlayerTeam scoreboardteam = source.getTeam();
if (scoreboardteam == null) {
@@ -619,7 +907,7 @@
@@ -619,7 +912,7 @@
}
public void deop(GameProfile profile) {
@ -753,7 +760,7 @@
ServerPlayer entityplayer = this.getPlayer(profile.getId());
if (entityplayer != null) {
@@ -643,6 +931,7 @@
@@ -643,6 +936,7 @@
player.connection.send(new ClientboundEntityEventPacket(player, b0));
}
@ -761,8 +768,22 @@
this.server.getCommands().sendCommands(player);
}
@@ -669,10 +958,16 @@
return null;
@@ -656,23 +950,19 @@
@Nullable
public ServerPlayer getPlayerByName(String name) {
- int i = this.players.size();
-
- for (int j = 0; j < i; ++j) {
- ServerPlayer entityplayer = (ServerPlayer) this.players.get(j);
-
- if (entityplayer.getGameProfile().getName().equalsIgnoreCase(name)) {
- return entityplayer;
- }
- }
-
- return null;
+ return this.playersByName.get(name.toLowerCase(java.util.Locale.ROOT)); // Spigot
}
- public void broadcast(@Nullable Player player, double x, double y, double z, double distance, ResourceKey<Level> worldKey, Packet<?> packet) {
@ -779,7 +800,7 @@
if (entityplayer != player && entityplayer.level().dimension() == worldKey) {
double d4 = x - entityplayer.getX();
double d5 = y - entityplayer.getY();
@@ -712,15 +1007,19 @@
@@ -712,15 +1002,19 @@
public void reloadWhiteList() {}
public void sendLevelInfo(ServerPlayer player, ServerLevel world) {
@ -803,7 +824,7 @@
}
player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.LEVEL_CHUNKS_LOAD_START, 0.0F));
@@ -729,8 +1028,16 @@
@@ -729,8 +1023,16 @@
public void sendAllPlayerInfo(ServerPlayer player) {
player.inventoryMenu.sendAllDataToRemote();
@ -821,7 +842,7 @@
}
public int getPlayerCount() {
@@ -786,12 +1093,22 @@
@@ -786,12 +1088,22 @@
}
public void removeAll() {
@ -846,7 +867,7 @@
public void broadcastSystemMessage(Component message, boolean overlay) {
this.broadcastSystemMessage(message, (entityplayer) -> {
return message;
@@ -849,16 +1166,23 @@
@@ -849,16 +1161,23 @@
return message.hasSignature() && !message.hasExpiredServer(Instant.now());
}
@ -874,7 +895,7 @@
Path path = file2.toPath();
if (FileUtil.isPathNormalized(path) && FileUtil.isPathPortable(path) && path.startsWith(file.getPath()) && file2.isFile()) {
@@ -867,7 +1191,7 @@
@@ -867,7 +1186,7 @@
}
serverstatisticmanager = new ServerStatsCounter(this.server, file1);
@ -883,7 +904,7 @@
}
return serverstatisticmanager;
@@ -875,13 +1199,13 @@
@@ -875,13 +1194,13 @@
public PlayerAdvancements getPlayerAdvancements(ServerPlayer player) {
UUID uuid = player.getUUID();
@ -899,7 +920,7 @@
}
advancementdataplayer.setPlayer(player);
@@ -932,15 +1256,28 @@
@@ -932,15 +1251,28 @@
}
public void reloadResources() {