Skip to content

Commit

Permalink
Update to JDK 21 and support latest Sponge API
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed May 30, 2024
1 parent 0198994 commit aaf5959
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/gradle-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
java-version: '21'
distribution: 'temurin'
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
settings-path: ${{ github.workspace }} # location for the settings.xml file

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ subprojects {
"net.kyori:adventure-nbt:${adventureVersion}"]

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
disableAutoTargetJvm()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ public static void relocateHandlers(Channel ctx, PacketEventsDecoder decoder, Us
// We are targeting the encoder and decoder since we don't want to target specific plugins
// (ProtocolSupport has changed its handler name in the past)
// I don't like the hacks required for compression but that's on vanilla, we can't fix it.
ctx.pipeline().addBefore("decoder", PacketEvents.DECODER_NAME, decoder);
ctx.pipeline().addBefore("encoder", PacketEvents.ENCODER_NAME, encoder);
String decoderName = ctx.pipeline().names().contains("inbound_config") ? "inbound_config" : "decoder";
ctx.pipeline().addBefore(decoderName, PacketEvents.DECODER_NAME, decoder);
String encoderName = ctx.pipeline().names().contains("outbound_config") ? "outbound_config" : "encoder";
ctx.pipeline().addBefore(encoderName, PacketEvents.ENCODER_NAME, encoder);
} catch (NoSuchElementException ex) {
String handlers = ChannelHelper.pipelineHandlerNamesAsString(ctx);
throw new IllegalStateException("PacketEvents failed to add a decoder to the netty pipeline. Pipeline handlers: " + handlers, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@
import io.github.retrooper.packetevents.sponge.util.viaversion.ViaVersionUtil;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.api.network.ServerConnectionState;
import org.spongepowered.api.network.ServerSideConnection;

import java.util.UUID;

public class PlayerManagerImpl implements PlayerManager {
@Override
public int getPing(@NotNull Object player) {
return ((ServerPlayer) player).connection().latency();
final ServerSideConnection connection = ((ServerPlayer) player).connection();
return connection.state().map(state -> {
if (state instanceof ServerConnectionState.Game) {
final ServerConnectionState.Game game = (ServerConnectionState.Game) state;
return game.latency();
}
return -1;
}).orElse(-1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,28 @@ public Object buffer() {
return Unpooled.buffer();
}

@Override
public Object buffer(int initialCapacity) {
return Unpooled.buffer(initialCapacity);
}

@Override
public Object directBuffer() {
return Unpooled.directBuffer();
}

@Override
public Object directBuffer(int initialCapacity) {
return Unpooled.directBuffer(initialCapacity);
}

@Override
public Object compositeBuffer() {
return Unpooled.compositeBuffer();
}

@Override
public Object compositeBuffer(int maxNumComponents) {
return Unpooled.compositeBuffer(maxNumComponents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,29 @@ public final class SpongeReflectionUtil {
ENTITY_PLAYER_CLASS,
NMS_MINECRAFT_KEY_CLASS,
PLAYER_CONNECTION_CLASS, SERVER_COMMON_PACKETLISTENER_IMPL_CLASS, SERVER_CONNECTION_CLASS, NETWORK_MANAGER_CLASS,
NMS_NBT_COMPOUND_CLASS, NBT_COMPRESSION_STREAM_TOOLS_CLASS;
NMS_NBT_COMPOUND_CLASS, NBT_COMPRESSION_STREAM_TOOLS_CLASS,
STREAM_CODEC, STREAM_DECODER, STREAM_ENCODER, REGISTRY_FRIENDLY_BYTE_BUF,
REGISTRY_ACCESS, REGISTRY_ACCESS_FROZEN;

// Fields
public static Field BYTE_BUF_IN_PACKET_DATA_SERIALIZER, NMS_MK_KEY_FIELD;

// Methods
public static Method IS_DEBUGGING,
READ_ITEM_STACK_IN_PACKET_DATA_SERIALIZER_METHOD,
WRITE_ITEM_STACK_IN_PACKET_DATA_SERIALIZER_METHOD,
READ_NBT_FROM_STREAM_METHOD, WRITE_NBT_TO_STREAM_METHOD;
READ_NBT_FROM_STREAM_METHOD, WRITE_NBT_TO_STREAM_METHOD,
STREAM_DECODER_DECODE, STREAM_ENCODER_ENCODE;

// Constructors
private static Constructor<?> NMS_PACKET_DATA_SERIALIZER_CONSTRUCTOR;
private static Constructor<?> REGISTRY_FRIENDLY_BYTE_BUF_CONSTRUCTOR;

private static Object MINECRAFT_SERVER_CONNECTION_INSTANCE;
private static Object ITEM_STACK_OPTIONAL_STREAM_CODEC;
private static Object MINECRAFT_SERVER_REGISTRY_ACCESS;

private static void initConstructors() {
try {
NMS_PACKET_DATA_SERIALIZER_CONSTRUCTOR = NMS_PACKET_DATA_SERIALIZER_CLASS.getConstructor(ByteBuf.class);
REGISTRY_FRIENDLY_BYTE_BUF_CONSTRUCTOR = REGISTRY_FRIENDLY_BYTE_BUF.getConstructor(
ByteBuf.class, REGISTRY_ACCESS);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Expand All @@ -83,14 +87,14 @@ private static void initConstructors() {
private static void initMethods() {
IS_DEBUGGING = Reflection.getMethod(MINECRAFT_SERVER_CLASS, "isDebugging", 0);

READ_ITEM_STACK_IN_PACKET_DATA_SERIALIZER_METHOD = Reflection.getMethod(NMS_PACKET_DATA_SERIALIZER_CLASS, NMS_ITEM_STACK_CLASS, 0);
WRITE_ITEM_STACK_IN_PACKET_DATA_SERIALIZER_METHOD = Reflection.getMethod(NMS_PACKET_DATA_SERIALIZER_CLASS, NMS_PACKET_DATA_SERIALIZER_CLASS, 0, NMS_ITEM_STACK_CLASS);

READ_NBT_FROM_STREAM_METHOD = Reflection.getMethod(NBT_COMPRESSION_STREAM_TOOLS_CLASS, 0, DataInputStream.class);
if (READ_NBT_FROM_STREAM_METHOD == null) {
READ_NBT_FROM_STREAM_METHOD = Reflection.getMethod(NBT_COMPRESSION_STREAM_TOOLS_CLASS, 0, DataInput.class);
}
WRITE_NBT_TO_STREAM_METHOD = Reflection.getMethod(NBT_COMPRESSION_STREAM_TOOLS_CLASS, 0, NMS_NBT_COMPOUND_CLASS, DataOutput.class);

STREAM_DECODER_DECODE = STREAM_DECODER.getMethods()[0];
STREAM_ENCODER_ENCODE = STREAM_ENCODER.getMethods()[0];
}

private static void initFields() {
Expand All @@ -108,14 +112,31 @@ private static void initClasses() {

PLAYER_CONNECTION_CLASS = getServerClass("server.network.ServerGamePacketListenerImpl");

//Only on 1.20.2
SERVER_COMMON_PACKETLISTENER_IMPL_CLASS = getServerClass("server.network.ServerCommonPacketListenerImpl");

SERVER_CONNECTION_CLASS = getServerClass("server.network.ServerConnectionListener");
NETWORK_MANAGER_CLASS = getServerClass("network.Connection");

NMS_NBT_COMPOUND_CLASS = getServerClass("nbt.CompoundTag");
NBT_COMPRESSION_STREAM_TOOLS_CLASS = getServerClass("nbt.NbtIo");

STREAM_CODEC = Reflection.getClassByNameWithoutException("net.minecraft.network.codec.StreamCodec");
STREAM_DECODER = Reflection.getClassByNameWithoutException("net.minecraft.network.codec.StreamDecoder");
STREAM_ENCODER = Reflection.getClassByNameWithoutException("net.minecraft.network.codec.StreamEncoder");
REGISTRY_FRIENDLY_BYTE_BUF = Reflection.getClassByNameWithoutException("net.minecraft.network.RegistryFriendlyByteBuf");

REGISTRY_ACCESS = getServerClass("core.RegistryAccess");
REGISTRY_ACCESS_FROZEN = getServerClass("core.RegistryAccess$Frozen");
}

private static void initObjects() {
try {
if (VERSION.isNewerThanOrEquals(ServerVersion.V_1_20_5)) {
ITEM_STACK_OPTIONAL_STREAM_CODEC = Reflection.getField(NMS_ITEM_STACK_CLASS, STREAM_CODEC, 0).get(null);
}
} catch (IllegalAccessException exception) {
exception.printStackTrace();
}
}

public static void init() {
Expand All @@ -125,6 +146,7 @@ public static void init() {
initFields();
initMethods();
initConstructors();
initObjects();
}

@Nullable
Expand Down Expand Up @@ -242,7 +264,7 @@ public static ItemStack encodeSpongeItemStack(com.github.retrooper.packetevents.

public static Object createPacketDataSerializer(Object byteBuf) {
try {
return NMS_PACKET_DATA_SERIALIZER_CONSTRUCTOR.newInstance(byteBuf);
return REGISTRY_FRIENDLY_BYTE_BUF_CONSTRUCTOR.newInstance(byteBuf, getFrozenRegistryAccess());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
Expand All @@ -251,16 +273,30 @@ public static Object createPacketDataSerializer(Object byteBuf) {

public static Object readNMSItemStackPacketDataSerializer(Object packetDataSerializer) {
try {
return READ_ITEM_STACK_IN_PACKET_DATA_SERIALIZER_METHOD.invoke(packetDataSerializer);
return STREAM_DECODER_DECODE.invoke(ITEM_STACK_OPTIONAL_STREAM_CODEC, packetDataSerializer);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}

public static Object getFrozenRegistryAccess() {
if (MINECRAFT_SERVER_REGISTRY_ACCESS == null) {
try {
MINECRAFT_SERVER_REGISTRY_ACCESS = Reflection.getMethod(MINECRAFT_SERVER_CLASS,
REGISTRY_ACCESS_FROZEN,
0)
.invoke(Sponge.server());
} catch (IllegalAccessException | InvocationTargetException exception) {
exception.printStackTrace();
}
}
return MINECRAFT_SERVER_REGISTRY_ACCESS;
}

public static Object writeNMSItemStackPacketDataSerializer(Object packetDataSerializer, Object nmsItemStack) {
try {
return WRITE_ITEM_STACK_IN_PACKET_DATA_SERIALIZER_METHOD.invoke(packetDataSerializer, nmsItemStack);
return STREAM_ENCODER_ENCODE.invoke(ITEM_STACK_OPTIONAL_STREAM_CODEC, packetDataSerializer, nmsItemStack);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit aaf5959

Please sign in to comment.