44
44
import org .geysermc .mcprotocollib .protocol .data .handshake .HandshakeIntent ;
45
45
import org .geysermc .mcprotocollib .protocol .data .status .ServerStatusInfo ;
46
46
import org .geysermc .mcprotocollib .protocol .data .status .handler .ServerInfoHandler ;
47
+ import org .geysermc .mcprotocollib .protocol .data .status .handler .ServerPingTimeHandler ;
47
48
import org .geysermc .mcprotocollib .protocol .packet .common .clientbound .ClientboundDisconnectPacket ;
48
49
import org .geysermc .mcprotocollib .protocol .packet .common .clientbound .ClientboundKeepAlivePacket ;
49
50
import org .geysermc .mcprotocollib .protocol .packet .common .clientbound .ClientboundPingPacket ;
@@ -100,30 +101,26 @@ public void packetReceived(Session session, Packet packet) {
100
101
throw new IllegalStateException ("Failed to generate shared key." , e );
101
102
}
102
103
103
- SessionService sessionService = session .getFlag (MinecraftConstants .SESSION_SERVICE_KEY ,
104
- new SessionService ());
105
- String serverId = SessionService .getServerId (helloPacket .getServerId (),
106
- helloPacket .getPublicKey (), key );
104
+ SessionService sessionService = session .getFlag (MinecraftConstants .SESSION_SERVICE_KEY , new SessionService ());
105
+ String serverId = SessionService .getServerId (helloPacket .getServerId (), helloPacket .getPublicKey (), key );
107
106
108
- // ODOT : Add generic error, disabled multiplayer and banned from playing online errors
107
+ // TODO : Add generic error, disabled multiplayer and banned from playing online errors
109
108
try {
110
109
sessionService .joinServer (profile , accessToken , serverId );
111
110
} catch (IOException e ) {
112
- session .disconnect (Component .translatable ("disconnect.loginFailedInfo" ,
113
- Component .text (e .getMessage ())), e );
111
+ session .disconnect (Component .translatable ("disconnect.loginFailedInfo" , Component .text (e .getMessage ())), e );
114
112
return ;
115
113
}
116
114
117
- // MCBotLib start
115
+ // mcbotlib start
118
116
var keyPacket = new ServerboundKeyPacket (helloPacket .getPublicKey (), key ,
119
117
helloPacket .getChallenge ());
120
118
var encryption = new AESEncryption (key );
121
119
session .send (keyPacket , () -> session .enableEncryption (encryption ));
122
- // MCBotLib end
120
+ // mcbotlib end
123
121
} else if (packet instanceof ClientboundGameProfilePacket ) {
124
- session .switchInboundProtocol (() -> protocol .setInboundState (ProtocolState .CONFIGURATION ));
125
- session .send (new ServerboundLoginAcknowledgedPacket (),
126
- () -> protocol .setOutboundState (ProtocolState .CONFIGURATION ));
122
+ session .switchInboundState (() -> protocol .setInboundState (ProtocolState .CONFIGURATION ));
123
+ session .send (new ServerboundLoginAcknowledgedPacket (), () -> protocol .setOutboundState (ProtocolState .CONFIGURATION ));
127
124
} else if (packet instanceof ClientboundLoginDisconnectPacket loginDisconnectPacket ) {
128
125
session .disconnect (loginDisconnectPacket .getReason ());
129
126
} else if (packet instanceof ClientboundLoginCompressionPacket loginCompressionPacket ) {
@@ -140,60 +137,51 @@ public void packetReceived(Session session, Packet packet) {
140
137
session .send (new ServerboundPingRequestPacket (System .currentTimeMillis ()));
141
138
} else if (packet instanceof ClientboundPongResponsePacket pongResponsePacket ) {
142
139
long time = System .currentTimeMillis () - pongResponsePacket .getPingTime ();
143
- var handler = session .getFlag (MinecraftConstants .SERVER_PING_TIME_HANDLER_KEY );
140
+ ServerPingTimeHandler handler = session .getFlag (MinecraftConstants .SERVER_PING_TIME_HANDLER_KEY );
144
141
if (handler != null ) {
145
142
handler .handle (session , time );
146
143
}
147
144
148
145
session .disconnect (Component .translatable ("multiplayer.status.finished" ));
149
146
}
150
147
} else if (protocol .getInboundState () == ProtocolState .GAME ) {
151
- if (packet instanceof ClientboundKeepAlivePacket keepAlivePacket &&
152
- session .getFlag (MinecraftConstants .AUTOMATIC_KEEP_ALIVE_MANAGEMENT , true )) {
148
+ if (packet instanceof ClientboundKeepAlivePacket keepAlivePacket && session .getFlag (MinecraftConstants .AUTOMATIC_KEEP_ALIVE_MANAGEMENT , true )) {
153
149
session .send (new ServerboundKeepAlivePacket (keepAlivePacket .getPingId ()));
154
- } else if (packet instanceof ClientboundPingPacket pingPacket &&
155
- session .getFlag (MinecraftConstants .AUTOMATIC_KEEP_ALIVE_MANAGEMENT , true )) {
156
- session .send (new ServerboundPongPacket (pingPacket .getId ()));
157
150
} else if (packet instanceof ClientboundDisconnectPacket disconnectPacket ) {
158
151
session .disconnect (disconnectPacket .getReason ());
159
152
} else if (packet instanceof ClientboundStartConfigurationPacket ) {
160
- session .switchInboundProtocol (() -> protocol .setInboundState (ProtocolState .CONFIGURATION ));
161
- session .send (new ServerboundConfigurationAcknowledgedPacket (),
162
- () -> protocol .setOutboundState (ProtocolState .CONFIGURATION ));
153
+ session .switchInboundState (() -> protocol .setInboundState (ProtocolState .CONFIGURATION ));
154
+ session .send (new ServerboundConfigurationAcknowledgedPacket (), () -> protocol .setOutboundState (ProtocolState .CONFIGURATION ));
163
155
} else if (packet instanceof ClientboundTransferPacket transferPacket ) {
164
156
if (session .getFlag (MinecraftConstants .FOLLOW_TRANSFERS , true )) {
165
- TcpClientSession newSession = new TcpClientSession (transferPacket .getHost (),
166
- transferPacket .getPort (), session .getPacketProtocol ());
157
+ TcpClientSession newSession = new TcpClientSession (transferPacket .getHost (), transferPacket .getPort (), session .getPacketProtocol ());
167
158
newSession .setFlags (session .getFlags ());
168
159
session .disconnect (Component .translatable ("disconnect.transfer" ));
169
160
newSession .connect (true , true );
170
161
}
162
+ // mcbotlib start
163
+ } else if (packet instanceof ClientboundPingPacket pingPacket ) {
164
+ session .send (new ServerboundPongPacket (pingPacket .getId ()));
171
165
}
166
+ // mcbotlib end
172
167
} else if (protocol .getInboundState () == ProtocolState .CONFIGURATION ) {
173
- if (packet instanceof ClientboundKeepAlivePacket keepAlivePacket &&
174
- session .getFlag (MinecraftConstants .AUTOMATIC_KEEP_ALIVE_MANAGEMENT , true )) {
168
+ if (packet instanceof ClientboundKeepAlivePacket keepAlivePacket && session .getFlag (MinecraftConstants .AUTOMATIC_KEEP_ALIVE_MANAGEMENT , true )) {
175
169
session .send (new ServerboundKeepAlivePacket (keepAlivePacket .getPingId ()));
176
- } else if (packet instanceof ClientboundPingPacket pingPacket &&
177
- session .getFlag (MinecraftConstants .AUTOMATIC_KEEP_ALIVE_MANAGEMENT , true )) {
178
- session .send (new ServerboundPongPacket (pingPacket .getId ()));
179
170
} else if (packet instanceof ClientboundFinishConfigurationPacket ) {
180
- session .switchInboundProtocol (() -> protocol .setInboundState (ProtocolState .GAME ));
181
- session .send (new ServerboundFinishConfigurationPacket (),
182
- () -> protocol .setOutboundState (ProtocolState .GAME ));
171
+ session .switchInboundState (() -> protocol .setInboundState (ProtocolState .GAME ));
172
+ session .send (new ServerboundFinishConfigurationPacket (), () -> protocol .setOutboundState (ProtocolState .GAME ));
183
173
} else if (packet instanceof ClientboundSelectKnownPacks selectKnownPacks ) {
184
- /* MCBotLib start */
174
+ // mcbotlib start
185
175
if (session .getFlag (MinecraftConstants .SEND_BLANK_KNOWN_PACKS_RESPONSE , true )) {
186
176
session .send (new ServerboundSelectKnownPacks (Collections .emptyList ()));
187
177
} else {
188
178
session .send (new ServerboundSelectKnownPacks (BuiltInKnownPackRegistry .INSTANCE
189
179
.getMatchingPacks (selectKnownPacks .getKnownPacks ())));
190
180
}
191
- /* MCBotLib end */
181
+ // mcbotlib end
192
182
} else if (packet instanceof ClientboundTransferPacket transferPacket ) {
193
183
if (session .getFlag (MinecraftConstants .FOLLOW_TRANSFERS , true )) {
194
- TcpClientSession newSession =
195
- new TcpClientSession (transferPacket .getHost (), transferPacket .getPort (),
196
- session .getPacketProtocol ());
184
+ TcpClientSession newSession = new TcpClientSession (transferPacket .getHost (), transferPacket .getPort (), session .getPacketProtocol ());
197
185
newSession .setFlags (session .getFlags ());
198
186
session .disconnect (Component .translatable ("disconnect.transfer" ));
199
187
newSession .connect (true , true );
@@ -206,26 +194,20 @@ public void packetReceived(Session session, Packet packet) {
206
194
public void connected (ConnectedEvent event ) {
207
195
Session session = event .getSession ();
208
196
MinecraftProtocol protocol = (MinecraftProtocol ) session .getPacketProtocol ();
209
- ClientIntentionPacket intention =
210
- new ClientIntentionPacket (protocol .getCodec ().getProtocolVersion (), session .getHost (),
211
- session .getPort (), switch (targetState ) {
212
- case LOGIN -> transferring ? HandshakeIntent .TRANSFER : HandshakeIntent .LOGIN ;
213
- case STATUS -> HandshakeIntent .STATUS ;
214
- default -> throw new IllegalStateException ("Unexpected value: " + targetState );
215
- });
197
+ ClientIntentionPacket intention = new ClientIntentionPacket (protocol .getCodec ().getProtocolVersion (), session .getHost (), session .getPort (), switch (targetState ) {
198
+ case LOGIN -> transferring ? HandshakeIntent .TRANSFER : HandshakeIntent .LOGIN ;
199
+ case STATUS -> HandshakeIntent .STATUS ;
200
+ default -> throw new IllegalStateException ("Unexpected value: " + targetState );
201
+ });
216
202
203
+ session .switchInboundState (() -> protocol .setInboundState (this .targetState ));
204
+ session .send (intention , () -> protocol .setOutboundState (this .targetState ));
217
205
switch (this .targetState ) {
218
206
case LOGIN -> {
219
- session .switchInboundProtocol (() -> protocol .setInboundState (ProtocolState .LOGIN ));
220
- session .send (intention , () -> protocol .setOutboundState (ProtocolState .LOGIN ));
221
207
GameProfile profile = session .getFlag (MinecraftConstants .PROFILE_KEY );
222
208
session .send (new ServerboundHelloPacket (profile .getName (), profile .getId ()));
223
209
}
224
- case STATUS -> {
225
- session .switchInboundProtocol (() -> protocol .setInboundState (ProtocolState .STATUS ));
226
- session .send (intention , () -> protocol .setOutboundState (ProtocolState .STATUS ));
227
- session .send (new ServerboundStatusRequestPacket ());
228
- }
210
+ case STATUS -> session .send (new ServerboundStatusRequestPacket ());
229
211
default -> throw new IllegalStateException ("Unexpected value: " + targetState );
230
212
}
231
213
}
0 commit comments