Skip to content

Commit

Permalink
update to com.github.AlexProgrammerDE:MCProtocolLib:576b311
Browse files Browse the repository at this point in the history
  • Loading branch information
marlester-dev committed Aug 4, 2024
1 parent f9edd60 commit 83fcace
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 51 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ dependencies {
compileOnly "org.jetbrains:annotations:24.1.0"
compileOnly "org.projectlombok:lombok:1.18.32"
annotationProcessor "org.projectlombok:lombok:1.18.32"
implementation "com.github.AlexProgrammerDE:MCProtocolLib:f2d67ef"
implementation "com.github.AlexProgrammerDE:MCProtocolLib:576b311"
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.geysermc.mcprotocollib.protocol.data.handshake.HandshakeIntent;
import org.geysermc.mcprotocollib.protocol.data.status.ServerStatusInfo;
import org.geysermc.mcprotocollib.protocol.data.status.handler.ServerInfoHandler;
import org.geysermc.mcprotocollib.protocol.data.status.handler.ServerPingTimeHandler;
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundDisconnectPacket;
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundKeepAlivePacket;
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundPingPacket;
Expand Down Expand Up @@ -100,30 +101,26 @@ public void packetReceived(Session session, Packet packet) {
throw new IllegalStateException("Failed to generate shared key.", e);
}

SessionService sessionService = session.getFlag(MinecraftConstants.SESSION_SERVICE_KEY,
new SessionService());
String serverId = SessionService.getServerId(helloPacket.getServerId(),
helloPacket.getPublicKey(), key);
SessionService sessionService = session.getFlag(MinecraftConstants.SESSION_SERVICE_KEY, new SessionService());
String serverId = SessionService.getServerId(helloPacket.getServerId(), helloPacket.getPublicKey(), key);

// ODOT: Add generic error, disabled multiplayer and banned from playing online errors
// TODO: Add generic error, disabled multiplayer and banned from playing online errors
try {
sessionService.joinServer(profile, accessToken, serverId);
} catch (IOException e) {
session.disconnect(Component.translatable("disconnect.loginFailedInfo",
Component.text(e.getMessage())), e);
session.disconnect(Component.translatable("disconnect.loginFailedInfo", Component.text(e.getMessage())), e);
return;
}

// MCBotLib start
// mcbotlib start
var keyPacket = new ServerboundKeyPacket(helloPacket.getPublicKey(), key,
helloPacket.getChallenge());
var encryption = new AESEncryption(key);
session.send(keyPacket, () -> session.enableEncryption(encryption));
// MCBotLib end
// mcbotlib end
} else if (packet instanceof ClientboundGameProfilePacket) {
session.switchInboundProtocol(() -> protocol.setInboundState(ProtocolState.CONFIGURATION));
session.send(new ServerboundLoginAcknowledgedPacket(),
() -> protocol.setOutboundState(ProtocolState.CONFIGURATION));
session.switchInboundState(() -> protocol.setInboundState(ProtocolState.CONFIGURATION));
session.send(new ServerboundLoginAcknowledgedPacket(), () -> protocol.setOutboundState(ProtocolState.CONFIGURATION));
} else if (packet instanceof ClientboundLoginDisconnectPacket loginDisconnectPacket) {
session.disconnect(loginDisconnectPacket.getReason());
} else if (packet instanceof ClientboundLoginCompressionPacket loginCompressionPacket) {
Expand All @@ -140,60 +137,51 @@ public void packetReceived(Session session, Packet packet) {
session.send(new ServerboundPingRequestPacket(System.currentTimeMillis()));
} else if (packet instanceof ClientboundPongResponsePacket pongResponsePacket) {
long time = System.currentTimeMillis() - pongResponsePacket.getPingTime();
var handler = session.getFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY);
ServerPingTimeHandler handler = session.getFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY);
if (handler != null) {
handler.handle(session, time);
}

session.disconnect(Component.translatable("multiplayer.status.finished"));
}
} else if (protocol.getInboundState() == ProtocolState.GAME) {
if (packet instanceof ClientboundKeepAlivePacket keepAlivePacket &&
session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
if (packet instanceof ClientboundKeepAlivePacket keepAlivePacket && session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
session.send(new ServerboundKeepAlivePacket(keepAlivePacket.getPingId()));
} else if (packet instanceof ClientboundPingPacket pingPacket &&
session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
session.send(new ServerboundPongPacket(pingPacket.getId()));
} else if (packet instanceof ClientboundDisconnectPacket disconnectPacket) {
session.disconnect(disconnectPacket.getReason());
} else if (packet instanceof ClientboundStartConfigurationPacket) {
session.switchInboundProtocol(() -> protocol.setInboundState(ProtocolState.CONFIGURATION));
session.send(new ServerboundConfigurationAcknowledgedPacket(),
() -> protocol.setOutboundState(ProtocolState.CONFIGURATION));
session.switchInboundState(() -> protocol.setInboundState(ProtocolState.CONFIGURATION));
session.send(new ServerboundConfigurationAcknowledgedPacket(), () -> protocol.setOutboundState(ProtocolState.CONFIGURATION));
} else if (packet instanceof ClientboundTransferPacket transferPacket) {
if (session.getFlag(MinecraftConstants.FOLLOW_TRANSFERS, true)) {
TcpClientSession newSession = new TcpClientSession(transferPacket.getHost(),
transferPacket.getPort(), session.getPacketProtocol());
TcpClientSession newSession = new TcpClientSession(transferPacket.getHost(), transferPacket.getPort(), session.getPacketProtocol());
newSession.setFlags(session.getFlags());
session.disconnect(Component.translatable("disconnect.transfer"));
newSession.connect(true, true);
}
// mcbotlib start
} else if (packet instanceof ClientboundPingPacket pingPacket) {
session.send(new ServerboundPongPacket(pingPacket.getId()));
}
// mcbotlib end
} else if (protocol.getInboundState() == ProtocolState.CONFIGURATION) {
if (packet instanceof ClientboundKeepAlivePacket keepAlivePacket &&
session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
if (packet instanceof ClientboundKeepAlivePacket keepAlivePacket && session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
session.send(new ServerboundKeepAlivePacket(keepAlivePacket.getPingId()));
} else if (packet instanceof ClientboundPingPacket pingPacket &&
session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
session.send(new ServerboundPongPacket(pingPacket.getId()));
} else if (packet instanceof ClientboundFinishConfigurationPacket) {
session.switchInboundProtocol(() -> protocol.setInboundState(ProtocolState.GAME));
session.send(new ServerboundFinishConfigurationPacket(),
() -> protocol.setOutboundState(ProtocolState.GAME));
session.switchInboundState(() -> protocol.setInboundState(ProtocolState.GAME));
session.send(new ServerboundFinishConfigurationPacket(), () -> protocol.setOutboundState(ProtocolState.GAME));
} else if (packet instanceof ClientboundSelectKnownPacks selectKnownPacks) {
/* MCBotLib start */
// mcbotlib start
if (session.getFlag(MinecraftConstants.SEND_BLANK_KNOWN_PACKS_RESPONSE, true)) {
session.send(new ServerboundSelectKnownPacks(Collections.emptyList()));
} else {
session.send(new ServerboundSelectKnownPacks(BuiltInKnownPackRegistry.INSTANCE
.getMatchingPacks(selectKnownPacks.getKnownPacks())));
}
/* MCBotLib end */
// mcbotlib end
} else if (packet instanceof ClientboundTransferPacket transferPacket) {
if (session.getFlag(MinecraftConstants.FOLLOW_TRANSFERS, true)) {
TcpClientSession newSession =
new TcpClientSession(transferPacket.getHost(), transferPacket.getPort(),
session.getPacketProtocol());
TcpClientSession newSession = new TcpClientSession(transferPacket.getHost(), transferPacket.getPort(), session.getPacketProtocol());
newSession.setFlags(session.getFlags());
session.disconnect(Component.translatable("disconnect.transfer"));
newSession.connect(true, true);
Expand All @@ -206,26 +194,20 @@ public void packetReceived(Session session, Packet packet) {
public void connected(ConnectedEvent event) {
Session session = event.getSession();
MinecraftProtocol protocol = (MinecraftProtocol) session.getPacketProtocol();
ClientIntentionPacket intention =
new ClientIntentionPacket(protocol.getCodec().getProtocolVersion(), session.getHost(),
session.getPort(), switch (targetState) {
case LOGIN -> transferring ? HandshakeIntent.TRANSFER : HandshakeIntent.LOGIN;
case STATUS -> HandshakeIntent.STATUS;
default -> throw new IllegalStateException("Unexpected value: " + targetState);
});
ClientIntentionPacket intention = new ClientIntentionPacket(protocol.getCodec().getProtocolVersion(), session.getHost(), session.getPort(), switch (targetState) {
case LOGIN -> transferring ? HandshakeIntent.TRANSFER : HandshakeIntent.LOGIN;
case STATUS -> HandshakeIntent.STATUS;
default -> throw new IllegalStateException("Unexpected value: " + targetState);
});

session.switchInboundState(() -> protocol.setInboundState(this.targetState));
session.send(intention, () -> protocol.setOutboundState(this.targetState));
switch (this.targetState) {
case LOGIN -> {
session.switchInboundProtocol(() -> protocol.setInboundState(ProtocolState.LOGIN));
session.send(intention, () -> protocol.setOutboundState(ProtocolState.LOGIN));
GameProfile profile = session.getFlag(MinecraftConstants.PROFILE_KEY);
session.send(new ServerboundHelloPacket(profile.getName(), profile.getId()));
}
case STATUS -> {
session.switchInboundProtocol(() -> protocol.setInboundState(ProtocolState.STATUS));
session.send(intention, () -> protocol.setOutboundState(ProtocolState.STATUS));
session.send(new ServerboundStatusRequestPacket());
}
case STATUS -> session.send(new ServerboundStatusRequestPacket());
default -> throw new IllegalStateException("Unexpected value: " + targetState);
}
}
Expand Down

0 comments on commit 83fcace

Please sign in to comment.