Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 1.20.5/1.20.6 part 2 #2910

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '19'
java-version: '21'
cache: 'gradle'

- name: Run gradle build lifecycle
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '19'
java-version: '21'
cache: 'gradle'

- name: Initialize CodeQL
Expand Down
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ repositories {
}

dependencies {
implementation 'net.bytebuddy:byte-buddy:1.14.9'
implementation 'net.bytebuddy:byte-buddy:1.14.14'
compileOnly 'org.spigotmc:spigot-api:1.20.5-R0.1-SNAPSHOT'
compileOnly 'org.spigotmc:spigot:1.20.5-R0.1-SNAPSHOT'
compileOnly 'io.netty:netty-all:4.0.23.Final'
Expand Down Expand Up @@ -69,6 +69,9 @@ shadowJar {

test {
useJUnitPlatform()
testLogging {
exceptionFormat = 'full'
}
}

processResources {
Expand Down
102 changes: 72 additions & 30 deletions src/main/java/com/comphenix/protocol/PacketType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.*;
import java.util.function.Consumer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.function.BiConsumer;

import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit;

import com.comphenix.protocol.PacketTypeLookup.ClassLookup;
import com.comphenix.protocol.events.ConnectionSide;
Expand All @@ -17,9 +26,6 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Iterables;

import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit;
/**
* Represents the type of a packet in a specific protocol.
* <p>
Expand Down Expand Up @@ -519,7 +525,7 @@ public static class Server extends PacketTypeEnum {
@ForceAsync
public static final PacketType SERVER_INFO = new PacketType(PROTOCOL, SENDER, 0x00, "ServerInfo", "SPacketServerInfo");
@ForceAsync
public static final PacketType PONG = new PacketType(PROTOCOL, SENDER, 0x01, "Pong", "SPacketPong");
public static final PacketType PONG = new PacketType(PROTOCOL, SENDER, 0x01, "PongResponse", "Pong", "SPacketPong");

/**
* @deprecated Renamed to {@link #SERVER_INFO}
Expand Down Expand Up @@ -550,7 +556,7 @@ public static class Client extends PacketTypeEnum {

public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, "Start", "CPacketServerQuery");
@ForceAsync
public static final PacketType PING = new PacketType(PROTOCOL, SENDER, 0x01, "Ping", "CPacketPing");
public static final PacketType PING = new PacketType(PROTOCOL, SENDER, 0x01, "PingRequest", "Ping", "CPacketPing");

private static final Client INSTANCE = new Client();

Expand Down Expand Up @@ -590,6 +596,7 @@ public static class Server extends PacketTypeEnum {
public static final PacketType SUCCESS = new PacketType(PROTOCOL, SENDER, 0x02, "Success", "SPacketLoginSuccess");
public static final PacketType SET_COMPRESSION = new PacketType(PROTOCOL, SENDER, 0x03, "SetCompression", "SPacketEnableCompression");
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x04, "CustomPayload", "SPacketCustomPayload");
public static final PacketType COOKIE_REQUEST = new PacketType(PROTOCOL, SENDER, 0x05, "CookieRequest");

private static final Server INSTANCE = new Server();

Expand All @@ -613,8 +620,9 @@ public static class Client extends PacketTypeEnum {

public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, "Start", "CPacketLoginStart");
public static final PacketType ENCRYPTION_BEGIN = new PacketType(PROTOCOL, SENDER, 0x01, "EncryptionBegin", "CPacketEncryptionResponse");
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x02, "CustomPayload", "CPacketCustomPayload");
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x02, "CustomQueryAnswer", "CustomPayload", "CPacketCustomPayload");
public static final PacketType LOGIN_ACK = new PacketType(PROTOCOL, SENDER, 0x03, "LoginAcknowledged");
public static final PacketType COOKIE_RESPONSE = new PacketType(PROTOCOL, SENDER, 0x04, "CookieResponse");

private static final Client INSTANCE = new Client();

Expand Down Expand Up @@ -647,22 +655,36 @@ public static class Configuration {
public static class Server extends PacketTypeEnum {
private static final Sender SENDER = Sender.SERVER;

public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x00, "CustomPayload");
public static final PacketType DISCONNECT = new PacketType(PROTOCOL, SENDER, 0x01, "Disconnect");
public static final PacketType FINISH_CONFIGURATION = new PacketType(PROTOCOL, SENDER, 0x02, "FinishConfiguration");
public static final PacketType KEEP_ALIVE = new PacketType(PROTOCOL, SENDER, 0x03, "KeepAlive");
public static final PacketType PING = new PacketType(PROTOCOL, SENDER, 0x04, "Ping");
public static final PacketType REGISTRY_DATA = new PacketType(PROTOCOL, SENDER, 0x05, "RegistryData");
public static final PacketType REMOVE_RESOURCE_PACK = new PacketType(PROTOCOL, SENDER, 0x06, "ResourcePackPopPacket");
public static final PacketType ADD_RESOURCE_PACK = new PacketType(PROTOCOL, SENDER, 0x07, "ResourcePackPushPacket");
public static final PacketType UPDATE_ENABLED_FEATURES = new PacketType(PROTOCOL, SENDER, 0x08, "UpdateEnabledFeatures");
public static final PacketType UPDATE_TAGS = new PacketType(PROTOCOL, SENDER, 0x09, "UpdateTags");
public static final PacketType COOKIE_REQUEST = new PacketType(PROTOCOL, SENDER, 0x00, "CookieRequest");
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x01, "CustomPayload");
public static final PacketType DISCONNECT = new PacketType(PROTOCOL, SENDER, 0x02, "Disconnect");
public static final PacketType FINISH_CONFIGURATION = new PacketType(PROTOCOL, SENDER, 0x03, "FinishConfiguration");
public static final PacketType KEEP_ALIVE = new PacketType(PROTOCOL, SENDER, 0x04, "KeepAlive");
public static final PacketType PING = new PacketType(PROTOCOL, SENDER, 0x05, "Ping");
public static final PacketType RESET_CHAT = new PacketType(PROTOCOL, SENDER, 0x06, "ResetChat");
public static final PacketType REGISTRY_DATA = new PacketType(PROTOCOL, SENDER, 0x07, "RegistryData");
public static final PacketType REMOVE_RESOURCE_PACK = new PacketType(PROTOCOL, SENDER, 0x08, "ResourcePackPop");
public static final PacketType ADD_RESOURCE_PACK = new PacketType(PROTOCOL, SENDER, 0x09, "ResourcePackPush");
public static final PacketType STORE_COOKIE = new PacketType(PROTOCOL, SENDER, 0x0A, "StoreCookie");
public static final PacketType TRANSFER = new PacketType(PROTOCOL, SENDER, 0x0B, "Transfer");
public static final PacketType UPDATE_ENABLED_FEATURES = new PacketType(PROTOCOL, SENDER, 0x0C, "UpdateEnabledFeatures");
public static final PacketType UPDATE_TAGS = new PacketType(PROTOCOL, SENDER, 0x0D, "UpdateTags");
public static final PacketType SELECT_KNOWN_PACKS = new PacketType(PROTOCOL, SENDER, 0x0E, "ClientboundSelectKnownPacks");

/**
* @deprecated Removed in 1.20.4: replaced with new packets for removing and sending resource packs
*/
@Deprecated
public static final PacketType RESOURCE_PACK = new PacketType(PROTOCOL, SENDER, 255, "ResourcePack");

private static final Server INSTANCE = new Server();

// Prevent accidental construction
private Server() { super(); }

public static Server getInstance() {
return INSTANCE;
}
}

/**
Expand All @@ -672,11 +694,22 @@ public static class Client extends PacketTypeEnum {
private static final Sender SENDER = Sender.CLIENT;

public static final PacketType CLIENT_INFORMATION = new PacketType(PROTOCOL, SENDER, 0x00, "ClientInformation");
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x01, "CustomPayload");
public static final PacketType FINISH_CONFIGURATION = new PacketType(PROTOCOL, SENDER, 0x02, "FinishConfiguration");
public static final PacketType KEEP_ALIVE = new PacketType(PROTOCOL, SENDER, 0x03, "KeepAlive");
public static final PacketType PONG = new PacketType(PROTOCOL, SENDER, 0x04, "Pong");
public static final PacketType RESOURCE_PACK_ACK = new PacketType(PROTOCOL, SENDER, 0x05, "ResourcePack");
public static final PacketType COOKIE_RESPONSE = new PacketType(PROTOCOL, SENDER, 0x01, "CookieResponse");
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x02, "CustomPayload");
public static final PacketType FINISH_CONFIGURATION = new PacketType(PROTOCOL, SENDER, 0x03, "FinishConfiguration");
public static final PacketType KEEP_ALIVE = new PacketType(PROTOCOL, SENDER, 0x04, "KeepAlive");
public static final PacketType PONG = new PacketType(PROTOCOL, SENDER, 0x05, "Pong");
public static final PacketType RESOURCE_PACK_ACK = new PacketType(PROTOCOL, SENDER, 0x06, "ResourcePack");
public static final PacketType SELECT_KNOWN_PACKS = new PacketType(PROTOCOL, SENDER, 0x07, "ServerboundSelectKnownPacks");

private static final Client INSTANCE = new Client();

// Prevent accidental construction
private Client() { super(); }

public static Client getInstance() {
return INSTANCE;
}
}
}

Expand Down Expand Up @@ -824,7 +857,9 @@ private static PacketTypeLookup getLookup() {
addPacketTypes(Status.Client.getInstance()).
addPacketTypes(Status.Server.getInstance()).
addPacketTypes(Login.Client.getInstance()).
addPacketTypes(Login.Server.getInstance());
addPacketTypes(Login.Server.getInstance()).
addPacketTypes(Configuration.Client.getInstance()).
addPacketTypes(Configuration.Server.getInstance());
}
return LOOKUP;
}
Expand Down Expand Up @@ -931,6 +966,10 @@ public static PacketType findCurrent(Protocol protocol, Sender sender, String na
}
}

private static String formatSimpleClassName(Protocol protocol, Sender sender, String name) {
return sender.getMojangName() + name + "Packet";
}

private static String formatMojangClassName(Protocol protocol, Sender sender, String name) {
return "net.minecraft.network.protocol." + protocol.getMojangName() + "." + sender.getMojangName()
+ name + "Packet";
Expand Down Expand Up @@ -1001,7 +1040,7 @@ public static PacketType fromID(Protocol protocol, Sender sender, int packetId,
return type;
}

static Consumer<String> onDynamicCreate = x -> {};
static BiConsumer<PacketType, String> onDynamicCreate = (type, className) -> {};

/**
* Retrieve a packet type from a protocol, sender, ID, and class for 1.8+
Expand All @@ -1019,22 +1058,23 @@ public static PacketType fromCurrent(Protocol protocol, Sender sender, int packe

// Check the map first
String className = packetClass.getName();
PacketType type = find(map, className);
PacketType type = find(map, packetClass);
if (type == null) {
// Guess we don't support this packet :/
type = new PacketType(protocol, sender, packetId, PROTOCOL_VERSION, className);
type.dynamic = true;

// Many may be scheduled, but only the first will be executed
scheduleRegister(type, "Dynamic-" + UUID.randomUUID().toString());
onDynamicCreate.accept(className);
onDynamicCreate.accept(type, className);
}

return type;
}

private static PacketType find(Map<String, PacketType> map, String clazz) {
PacketType ret = map.get(clazz);
private static PacketType find(Map<String, PacketType> map, Class<?> packetClass) {
String className = packetClass.getName();
PacketType ret = map.get(className);
if (ret != null) {
return ret;
}
Expand All @@ -1044,7 +1084,7 @@ private static PacketType find(Map<String, PacketType> map, String clazz) {
List<String> aliases = check.getClassNames();
if (aliases.size() > 1) {
for (String alias : aliases) {
if (alias.equals(clazz)) {
if (alias.equals(className) || alias.equals(packetClass.getSimpleName())) {
// We have a match!
return check;
}
Expand Down Expand Up @@ -1174,6 +1214,8 @@ public PacketType(Protocol protocol, Sender sender, int currentId, MinecraftVers
} else {
classNames.add(formatClassName(protocol, sender, classname));
classNames.add(formatMojangClassName(protocol, sender, classname));
classNames.add(formatSimpleClassName(protocol, sender, classname));
classNames.add(classname);
}
}

Expand Down
31 changes: 12 additions & 19 deletions src/main/java/com/comphenix/protocol/PacketTypeLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* @author Kristian
*/
class PacketTypeLookup {

public static class ProtocolSenderLookup {
// Unroll lookup for performance reasons
public final IntegerMap<PacketType> HANDSHAKE_CLIENT = new IntegerMap<>();
Expand All @@ -27,6 +28,8 @@ public static class ProtocolSenderLookup {
public final IntegerMap<PacketType> STATUS_SERVER = new IntegerMap<>();
public final IntegerMap<PacketType> LOGIN_CLIENT = new IntegerMap<>();
public final IntegerMap<PacketType> LOGIN_SERVER = new IntegerMap<>();
public final IntegerMap<PacketType> CONFIGURATION_CLIENT = new IntegerMap<>();
public final IntegerMap<PacketType> CONFIGURATION_SERVER = new IntegerMap<>();

/**
* Retrieve the correct integer map for a specific protocol and sender.
Expand All @@ -44,6 +47,8 @@ public IntegerMap<PacketType> getMap(Protocol protocol, Sender sender) {
return sender == Sender.CLIENT ? STATUS_CLIENT : STATUS_SERVER;
case LOGIN:
return sender == Sender.CLIENT ? LOGIN_CLIENT : LOGIN_SERVER;
case CONFIGURATION:
return sender == Sender.CLIENT ? CONFIGURATION_CLIENT : CONFIGURATION_SERVER;
default:
throw new IllegalArgumentException("Unable to find protocol " + protocol);
}
Expand Down Expand Up @@ -87,11 +92,6 @@ public Map<String, PacketType> getMap(Protocol protocol, Sender sender) {
}
}

// Packet IDs from 1.6.4 and below
private final IntegerMap<PacketType> legacyLookup = new IntegerMap<>();
private final IntegerMap<PacketType> serverLookup = new IntegerMap<>();
private final IntegerMap<PacketType> clientLookup = new IntegerMap<>();

// Packets for 1.7.2
private final ProtocolSenderLookup idLookup = new ProtocolSenderLookup();

Expand Down Expand Up @@ -123,9 +123,11 @@ public PacketTypeLookup addPacketTypes(Iterable<? extends PacketType> types) {
* Retrieve a packet type from a legacy (1.6.4 and below) packet ID.
* @param packetId - the legacy packet ID.
* @return The corresponding packet type, or NULL if not found.
* @deprecated no longer works and will always return null
*/
@Deprecated
public PacketType getFromLegacy(int packetId) {
return legacyLookup.get(packetId);
return null;
}

/**
Expand All @@ -142,20 +144,11 @@ public Collection<PacketType> getFromName(String name) {
* @param packetId - the legacy packet ID.
* @param preference - which packet type to look for first.
* @return The corresponding packet type, or NULL if not found.
* @deprecated no longer works and will always return null
*/
public PacketType getFromLegacy(int packetId, Sender preference) {
if (preference == Sender.CLIENT)
return getFirst(packetId, clientLookup, serverLookup);
else
return getFirst(packetId, serverLookup, clientLookup);
}

// Helper method for looking up in two sets
private <T> T getFirst(int packetId, IntegerMap<T> first, IntegerMap<T> second) {
if (first.containsKey(packetId))
return first.get(packetId);
else
return second.get(packetId);
@Deprecated
public PacketType getFromLegacy(int packetId, Sender preference) {
return null;
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/comphenix/protocol/events/PacketContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.injector.StructureCache;
import com.comphenix.protocol.injector.packet.PacketRegistry;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.reflect.ObjectWriter;
import com.comphenix.protocol.reflect.StructureModifier;
Expand All @@ -56,6 +57,7 @@
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.Converters;
import com.comphenix.protocol.wrappers.WrappedStreamCodec;
import com.google.common.collect.Sets;
import io.netty.buffer.ByteBuf;
import io.netty.util.ReferenceCountUtil;
Expand Down Expand Up @@ -345,6 +347,11 @@ public static Object deserializeFromBuffer(PacketType packetType, Object buffer)
}

Function<Object, Object> deserializer = PACKET_DESERIALIZER_METHODS.computeIfAbsent(packetType, type -> {
WrappedStreamCodec streamCodec = PacketRegistry.getStreamCodec(type.getPacketClass());
if (streamCodec != null) {
return streamCodec::decode;
}

if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) {
// best guess - a constructor which takes a buffer as the only argument
ConstructorAccessor bufferConstructor = Accessors.getConstructorAccessorOrNull(
Expand Down Expand Up @@ -392,7 +399,14 @@ public Object serializeToBuffer() {
}

Object targetBuffer = MinecraftReflection.createPacketDataSerializer(0);
MinecraftMethods.getPacketWriteByteBufMethod().invoke(handle, targetBuffer);

WrappedStreamCodec streamCodec = PacketRegistry.getStreamCodec(type.getPacketClass());
if (streamCodec != null) {
streamCodec.encode(targetBuffer, handle);
} else {
MinecraftMethods.getPacketWriteByteBufMethod().invoke(handle, targetBuffer);
}

return targetBuffer;
}

Expand Down
Loading
Loading