Skip to content

Commit 9401389

Browse files
committed
Mitigate clientside race condition
1 parent 777fa69 commit 9401389

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

plugin/src/main/java/net/elytrium/limboapi/server/LimboSessionHandlerImpl.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import net.elytrium.limboapi.protocol.packets.c2s.MovePacket;
5959
import net.elytrium.limboapi.protocol.packets.c2s.MovePositionOnlyPacket;
6060
import net.elytrium.limboapi.protocol.packets.c2s.MoveRotationOnlyPacket;
61+
import net.elytrium.limboapi.protocol.packets.c2s.PlayerChatSessionPacket;
6162
import net.elytrium.limboapi.protocol.packets.c2s.TeleportConfirmPacket;
6263

6364
public class LimboSessionHandlerImpl implements MinecraftSessionHandler {
@@ -74,6 +75,7 @@ public class LimboSessionHandlerImpl implements MinecraftSessionHandler {
7475
private final Supplier<String> limboName;
7576
private final CompletableFuture<Object> playTransition = new CompletableFuture<>();
7677
private final CompletableFuture<Object> configTransition = new CompletableFuture<>();
78+
private final CompletableFuture<Object> keyFetching = new CompletableFuture<>();
7779

7880
private LimboPlayer limboPlayer;
7981
private ScheduledFuture<?> keepAliveTask;
@@ -151,8 +153,16 @@ public void disconnectToConfig(Runnable runnable) {
151153
this.switching = true;
152154
this.loaded = false;
153155

154-
this.player.getConnection().write(new StartUpdate());
155-
this.configTransition.thenRun(this::disconnected).thenRun(runnable);
156+
if (this.player.isOnlineMode()) {
157+
// Wait for PlayerChatSessionPacket to ensure that client created a chat session and so can't break state switching
158+
this.keyFetching.thenRunAsync(() -> {
159+
this.player.getConnection().write(new StartUpdate());
160+
this.configTransition.thenRun(this::disconnected).thenRun(runnable);
161+
}, this.player.getConnection().eventLoop());
162+
} else {
163+
this.player.getConnection().write(new StartUpdate());
164+
this.configTransition.thenRun(this::disconnected).thenRun(runnable);
165+
}
156166
}
157167

158168
@Override
@@ -308,6 +318,10 @@ public void handleUnknown(ByteBuf packet) {
308318

309319
@Override
310320
public void handleGeneric(MinecraftPacket packet) {
321+
if (packet instanceof PlayerChatSessionPacket chatSession) {
322+
this.keyFetching.complete(this);
323+
}
324+
311325
if (packet instanceof PluginMessage pluginMessage) {
312326
int singleLength = pluginMessage.content().readableBytes() + pluginMessage.getChannel().length() * 4;
313327
this.genericBytes += singleLength;

0 commit comments

Comments
 (0)