2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 19 Apr 2020 00:05:46 -0400
Subject: [PATCH] Fix Longstanding Broken behavior of PlayerJoinEvent
For years, plugin developers have had to delay many things they do
inside of the PlayerJoinEvent by 1 tick to make it actually work.
This all boiled down to 1 reason why: The event fired before the
player was fully ready and joined to the world!
Additionally, if that player logged out on a vehicle, the event
fired before the vehicle was even loaded, so that plugins had no
access to the vehicle during this event either.
This change finally fixes this issue, fully preparing the player
into the world as a fully ready entity, vehicle included.
There should be no plugins that break because of this change, but might
improve consistency with other plugins instead.
For example, if 2 plugins listens to this event, and the first one
teleported the player in the event, then the 2nd plugin actually
would be getting a valid player!
This was very non deterministic. This change will ensure every plugin
receives a deterministic result, and should no longer require 1 tick
delays anymore.
2022-11-20 00:53:20 +01:00
== AT ==
public net.minecraft.server.level.ChunkMap addEntity(Lnet/minecraft/world/entity/Entity;)V
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
2023-06-09 06:29:58 +02:00
index a8c4c7298e127e5b60a3b43ad168976d29901bc5..48586780da5d260894fe59efaa97cb1facfe73fe 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
2023-06-08 07:21:04 +02:00
@@ -1037,6 +1037,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
2022-10-19 04:21:07 +02:00
+ ": " + entity + (this.entityMap.containsKey(entity.getId()) ? " ALREADY CONTAINED (This would have crashed your server)" : ""), new Throwable());
2021-06-11 14:02:28 +02:00
return;
}
+ if (entity instanceof ServerPlayer && ((ServerPlayer) entity).supressTrackerForLogin) return; // Delay adding to tracker until after list packets
// Paper end
if (!(entity instanceof EnderDragonPart)) {
EntityType<?> entitytypes = entity.getType();
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2023-07-04 10:22:56 +02:00
index fd34a2ace2efc819b06674ceab44ff78ec11f805..6dcac005b57234983d0ed05fe825a16c98eb4557 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2023-05-12 13:10:08 +02:00
@@ -267,6 +267,7 @@ public class ServerPlayer extends Player {
2021-06-11 14:02:28 +02:00
public double maxHealthCache;
public boolean joining = true;
public boolean sentListPacket = false;
+ public boolean supressTrackerForLogin = false; // Paper
public Integer clientViewDistance;
2022-03-16 16:57:51 +01:00
public String kickLeaveMessage = null; // SPIGOT-3034: Forward leave message to PlayerQuitEvent
2021-06-11 14:02:28 +02:00
// CraftBukkit end
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
2023-07-04 10:22:56 +02:00
index 0d100788312a234616c1401656f09835458e79f6..97199ae30edfaaacb9ea9d29846ee131395815e6 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
2023-06-08 00:41:25 +02:00
@@ -311,6 +311,12 @@ public abstract class PlayerList {
2021-06-11 14:02:28 +02:00
this.playersByUUID.put(player.getUUID(), player);
2022-12-07 21:16:54 +01:00
// this.broadcastAll(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(entityplayer))); // CraftBukkit - replaced with loop below
2021-06-11 14:02:28 +02:00
+ // Paper start - correctly register player BEFORE PlayerJoinEvent, so the entity is valid and doesn't require tick delay hacks
+ player.supressTrackerForLogin = true;
+ worldserver1.addNewPlayer(player);
+ this.server.getCustomBossEvents().onPlayerConnect(player); // see commented out section below worldserver.addPlayerJoin(entityplayer);
+ mountSavedVehicle(player, worldserver1, nbttagcompound);
+ // Paper end
// CraftBukkit start
2021-06-20 21:25:59 +02:00
CraftPlayer bukkitPlayer = player.getBukkitEntity();
2023-06-08 00:41:25 +02:00
@@ -349,6 +355,8 @@ public abstract class PlayerList {
2022-12-07 21:16:54 +01:00
player.connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(entityplayer1)));
2021-06-11 14:02:28 +02:00
}
player.sentListPacket = true;
+ player.supressTrackerForLogin = false; // Paper
2023-06-08 01:21:20 +02:00
+ ((ServerLevel)player.level()).getChunkSource().chunkMap.addEntity(player); // Paper - track entity now
2021-06-11 14:02:28 +02:00
// CraftBukkit end
2022-12-07 21:16:54 +01:00
player.getEntityData().refresh(player); // CraftBukkit - BungeeCord#2321, send complete data to self on spawn
2023-06-08 00:41:25 +02:00
@@ -374,6 +382,11 @@ public abstract class PlayerList {
2021-06-11 14:02:28 +02:00
playerconnection.send(new ClientboundUpdateMobEffectPacket(player.getId(), mobeffect));
}
+ // Paper start - move vehicle into method so it can be called above - short circuit around that code
+ onPlayerJoinFinish(player, worldserver1, s1);
+ }
2021-06-14 03:06:38 +02:00
+ private void mountSavedVehicle(ServerPlayer player, ServerLevel worldserver1, CompoundTag nbttagcompound) {
2021-06-11 14:02:28 +02:00
+ // Paper end
if (nbttagcompound != null && nbttagcompound.contains("RootVehicle", 10)) {
CompoundTag nbttagcompound1 = nbttagcompound.getCompound("RootVehicle");
// CraftBukkit start
2023-06-08 00:41:25 +02:00
@@ -422,6 +435,10 @@ public abstract class PlayerList {
2021-06-11 14:02:28 +02:00
}
}
+ // Paper start
+ }
2021-06-14 03:06:38 +02:00
+ public void onPlayerJoinFinish(ServerPlayer player, ServerLevel worldserver1, String s1) {
2021-06-11 14:02:28 +02:00
+ // Paper end
2021-06-14 03:06:38 +02:00
player.initInventoryMenu();
// CraftBukkit - Moved from above, added world
2021-06-11 14:02:28 +02:00
// Paper start - Add to collideRule team if needed
2023-06-08 00:41:25 +02:00
@@ -431,6 +448,7 @@ public abstract class PlayerList {
2021-06-14 03:06:38 +02:00
scoreboard.addPlayerToTeam(player.getScoreboardName(), collideRuleTeam);
2021-06-11 14:02:28 +02:00
}
// Paper end
2021-06-14 03:06:38 +02:00
+ // CraftBukkit - Moved from above, added world
PlayerList.LOGGER.info("{}[{}] logged in with entity id {} at ([{}]{}, {}, {})", player.getName().getString(), s1, player.getId(), worldserver1.serverLevelData.getLevelName(), player.getX(), player.getY(), player.getZ());
2021-06-11 14:02:28 +02:00
}