Geyser/core/src/main/java/org/geysermc/geyser/network/GameProtocol.java

163 lines
5.9 KiB
Java

/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.network;
import com.github.steveice10.mc.protocol.codec.MinecraftCodec;
import com.github.steveice10.mc.protocol.codec.PacketCodec;
import com.nukkitx.protocol.bedrock.BedrockPacketCodec;
import com.nukkitx.protocol.bedrock.v527.Bedrock_v527;
import com.nukkitx.protocol.bedrock.v534.Bedrock_v534;
import com.nukkitx.protocol.bedrock.v544.Bedrock_v544;
import com.nukkitx.protocol.bedrock.v545.Bedrock_v545;
import com.nukkitx.protocol.bedrock.v554.Bedrock_v554;
import com.nukkitx.protocol.bedrock.v557.Bedrock_v557;
import com.nukkitx.protocol.bedrock.v560.Bedrock_v560;
import org.geysermc.geyser.session.GeyserSession;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
/**
* Contains information about the supported protocols in Geyser.
*/
public final class GameProtocol {
/**
* Default Bedrock codec that should act as a fallback. Should represent the latest available
* release of the game that Geyser supports.
*/
public static final BedrockPacketCodec DEFAULT_BEDROCK_CODEC = Bedrock_v560.V560_CODEC;
/**
* A list of all supported Bedrock versions that can join Geyser
*/
public static final List<BedrockPacketCodec> SUPPORTED_BEDROCK_CODECS = new ArrayList<>();
/**
* Java codec that is supported. We only ever support one version for
* Java Edition.
*/
private static final PacketCodec DEFAULT_JAVA_CODEC = MinecraftCodec.CODEC;
static {
SUPPORTED_BEDROCK_CODECS.add(Bedrock_v544.V544_CODEC);
SUPPORTED_BEDROCK_CODECS.add(Bedrock_v545.V545_CODEC.toBuilder()
.minecraftVersion("1.19.21/1.19.22")
.build());
SUPPORTED_BEDROCK_CODECS.add(Bedrock_v554.V554_CODEC.toBuilder()
.minecraftVersion("1.19.30/1.19.31")
.build());
SUPPORTED_BEDROCK_CODECS.add(Bedrock_v557.V557_CODEC);
SUPPORTED_BEDROCK_CODECS.add(DEFAULT_BEDROCK_CODEC);
}
/**
* Gets the {@link BedrockPacketCodec} of the given protocol version.
* @param protocolVersion The protocol version to attempt to find
* @return The packet codec, or null if the client's protocol is unsupported
*/
public static BedrockPacketCodec getBedrockCodec(int protocolVersion) {
for (BedrockPacketCodec packetCodec : SUPPORTED_BEDROCK_CODECS) {
if (packetCodec.getProtocolVersion() == protocolVersion) {
return packetCodec;
}
}
return null;
}
/* Bedrock convenience methods to gatekeep features and easily remove the check on version removal */
public static boolean supports1_19_30(GeyserSession session) {
return session.getUpstream().getProtocolVersion() >= Bedrock_v554.V554_CODEC.getProtocolVersion();
}
public static boolean supports1_19_50(GeyserSession session) {
return session.getUpstream().getProtocolVersion() >= Bedrock_v560.V560_CODEC.getProtocolVersion();
}
/**
* Gets the {@link PacketCodec} for Minecraft: Java Edition.
*
* @return the packet codec for Minecraft: Java Edition
*/
public static PacketCodec getJavaCodec() {
return DEFAULT_JAVA_CODEC;
}
/**
* Gets the supported Minecraft: Java Edition version names.
*
* @return the supported Minecraft: Java Edition version names
*/
public static List<String> getJavaVersions() {
return List.of(DEFAULT_JAVA_CODEC.getMinecraftVersion(), "1.19.2");
}
/**
* Gets the supported Minecraft: Java Edition protocol version.
*
* @return the supported Minecraft: Java Edition protocol version
*/
public static int getJavaProtocolVersion() {
return DEFAULT_JAVA_CODEC.getProtocolVersion();
}
/**
* Gets the supported Minecraft: Java Edition version.
*
* @return the supported Minecraft: Java Edition version
*/
public static String getJavaMinecraftVersion() {
return DEFAULT_JAVA_CODEC.getMinecraftVersion();
}
/**
* @return a string showing all supported Bedrock versions for this Geyser instance
*/
public static String getAllSupportedBedrockVersions() {
StringJoiner joiner = new StringJoiner(", ");
for (BedrockPacketCodec packetCodec : SUPPORTED_BEDROCK_CODECS) {
joiner.add(packetCodec.getMinecraftVersion());
}
return joiner.toString();
}
/**
* @return a string showing all supported Java versions for this Geyser instance
*/
public static String getAllSupportedJavaVersions() {
StringJoiner joiner = new StringJoiner(", ");
for (String version : getJavaVersions()) {
joiner.add(version);
}
return joiner.toString();
}
private GameProtocol() {
}
}