net.minestom.server.network.packet.client.handshake.ClientHandshakePacket Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of minestom-snapshots Show documentation
Show all versions of minestom-snapshots Show documentation
1.20.4 Lightweight Minecraft server
package net.minestom.server.network.packet.client.handshake;
import net.minestom.server.extras.bungee.BungeeCordProxy;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.network.NetworkBufferTemplate;
import net.minestom.server.network.packet.client.ClientPacket;
import org.jetbrains.annotations.NotNull;
import static net.minestom.server.network.NetworkBuffer.*;
public record ClientHandshakePacket(int protocolVersion, @NotNull String serverAddress,
int serverPort, @NotNull Intent intent) implements ClientPacket {
public ClientHandshakePacket {
if (serverAddress.length() > maxHandshakeLength()) {
throw new IllegalArgumentException("Server address too long: " + serverAddress.length());
}
}
public static final NetworkBuffer.Type SERIALIZER = NetworkBufferTemplate.template(
VAR_INT, ClientHandshakePacket::protocolVersion,
STRING, ClientHandshakePacket::serverAddress,
UNSIGNED_SHORT, ClientHandshakePacket::serverPort,
VAR_INT.transform(Intent::fromId, Intent::id), ClientHandshakePacket::intent,
ClientHandshakePacket::new);
private static int maxHandshakeLength() {
// BungeeGuard limits handshake length to 2500 characters, while vanilla limits it to 255
return BungeeCordProxy.isEnabled() ? (BungeeCordProxy.isBungeeGuardEnabled() ? 2500 : Short.MAX_VALUE) : 255;
}
public enum Intent {
STATUS,
LOGIN,
TRANSFER;
public static @NotNull Intent fromId(int id) {
return switch (id) {
case 1 -> STATUS;
case 2 -> LOGIN;
case 3 -> TRANSFER;
default -> throw new IllegalArgumentException("Unknown connection intent: " + id);
};
}
public int id() {
return ordinal() + 1;
}
}
}