58
58
import net .elytrium .limboapi .protocol .packets .c2s .MovePacket ;
59
59
import net .elytrium .limboapi .protocol .packets .c2s .MovePositionOnlyPacket ;
60
60
import net .elytrium .limboapi .protocol .packets .c2s .MoveRotationOnlyPacket ;
61
+ import net .elytrium .limboapi .protocol .packets .c2s .PlayerChatSessionPacket ;
61
62
import net .elytrium .limboapi .protocol .packets .c2s .TeleportConfirmPacket ;
62
63
63
64
public class LimboSessionHandlerImpl implements MinecraftSessionHandler {
@@ -74,6 +75,7 @@ public class LimboSessionHandlerImpl implements MinecraftSessionHandler {
74
75
private final Supplier <String > limboName ;
75
76
private final CompletableFuture <Object > playTransition = new CompletableFuture <>();
76
77
private final CompletableFuture <Object > configTransition = new CompletableFuture <>();
78
+ private final CompletableFuture <Object > keyFetching = new CompletableFuture <>();
77
79
78
80
private LimboPlayer limboPlayer ;
79
81
private ScheduledFuture <?> keepAliveTask ;
@@ -151,8 +153,16 @@ public void disconnectToConfig(Runnable runnable) {
151
153
this .switching = true ;
152
154
this .loaded = false ;
153
155
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
+ }
156
166
}
157
167
158
168
@ Override
@@ -308,6 +318,10 @@ public void handleUnknown(ByteBuf packet) {
308
318
309
319
@ Override
310
320
public void handleGeneric (MinecraftPacket packet ) {
321
+ if (packet instanceof PlayerChatSessionPacket chatSession ) {
322
+ this .keyFetching .complete (this );
323
+ }
324
+
311
325
if (packet instanceof PluginMessage pluginMessage ) {
312
326
int singleLength = pluginMessage .content ().readableBytes () + pluginMessage .getChannel ().length () * 4 ;
313
327
this .genericBytes += singleLength ;
0 commit comments