From 92076e855947820491d5a1523217bddca1ce7fb0 Mon Sep 17 00:00:00 2001 From: booky10 Date: Mon, 21 Oct 2024 00:54:49 +0200 Subject: [PATCH 1/8] Move fabric mod metadata to common sourceset --- fabric/src/{client => main}/resources/fabric.mod.json | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename fabric/src/{client => main}/resources/fabric.mod.json (100%) diff --git a/fabric/src/client/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json similarity index 100% rename from fabric/src/client/resources/fabric.mod.json rename to fabric/src/main/resources/fabric.mod.json From 07137ddb2654f665ac98f04e538410ea66eecef5 Mon Sep 17 00:00:00 2001 From: booky10 Date: Mon, 21 Oct 2024 00:55:17 +0200 Subject: [PATCH 2/8] Call correct packet events depending on packet side --- .../util/PacketEventsImplHelper.java | 30 +++++++++++++------ .../packetevents/handler/PacketDecoder.java | 9 ++++-- .../packetevents/handler/PacketEncoder.java | 9 ++++-- .../packetevents/mixin/ConnectionMixin.java | 6 ++-- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/api/src/main/java/com/github/retrooper/packetevents/util/PacketEventsImplHelper.java b/api/src/main/java/com/github/retrooper/packetevents/util/PacketEventsImplHelper.java index 27ea613326..0578fc1c1f 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/util/PacketEventsImplHelper.java +++ b/api/src/main/java/com/github/retrooper/packetevents/util/PacketEventsImplHelper.java @@ -25,18 +25,30 @@ import com.github.retrooper.packetevents.manager.protocol.ProtocolManager; import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper; import com.github.retrooper.packetevents.netty.buffer.UnpooledByteBufAllocationHelper; +import com.github.retrooper.packetevents.protocol.PacketSide; import com.github.retrooper.packetevents.protocol.player.User; import org.jetbrains.annotations.Nullable; import java.util.UUID; public class PacketEventsImplHelper { - - public static PacketSendEvent handleClientBoundPacket(Object channel, - User user, - Object player, - Object buffer, - boolean autoProtocolTranslation) throws Exception { + + public static Object handlePacket( + Object channel, User user, Object player, Object buffer, + boolean autoProtocolTranslation, PacketSide side + ) throws Exception { + if (side == PacketSide.SERVER) { + handleClientBoundPacket(channel, user, player, buffer, autoProtocolTranslation); + return buffer; + } else { + return handleServerBoundPacket(channel, user, player, buffer, autoProtocolTranslation); + } + } + + public static PacketSendEvent handleClientBoundPacket( + Object channel, User user, Object player, Object buffer, + boolean autoProtocolTranslation + ) throws Exception { if (!ByteBufHelper.isReadable(buffer)) return null; int preProcessIndex = ByteBufHelper.readerIndex(buffer); @@ -72,9 +84,9 @@ public static PacketSendEvent handleClientBoundPacket(Object channel, } public static Object handleServerBoundPacket(Object channel, User user, - Object player, - Object buffer, - boolean autoProtocolTranslation) throws Exception { + Object player, + Object buffer, + boolean autoProtocolTranslation) throws Exception { if (!ByteBufHelper.isReadable(buffer)) return null; int preProcessIndex = ByteBufHelper.readerIndex(buffer); diff --git a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java index 1ea67a9364..4788e6bf45 100644 --- a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java +++ b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java @@ -18,6 +18,7 @@ package io.github.retrooper.packetevents.handler; +import com.github.retrooper.packetevents.protocol.PacketSide; import com.github.retrooper.packetevents.protocol.player.User; import com.github.retrooper.packetevents.util.PacketEventsImplHelper; import io.netty.buffer.ByteBuf; @@ -31,10 +32,12 @@ @ApiStatus.Internal public class PacketDecoder extends MessageToMessageDecoder { + private final PacketSide side; public User user; public Player player; - public PacketDecoder(User user) { + public PacketDecoder(PacketSide side, User user) { + this.side = side.getOpposite(); this.user = user; } @@ -43,8 +46,8 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) if (!msg.isReadable()) { return; } - PacketEventsImplHelper.handleClientBoundPacket(ctx.channel(), this.user, this.player, - msg, false); + PacketEventsImplHelper.handlePacket(ctx.channel(), this.user, this.player, + msg, false, this.side); if (msg.isReadable()) { out.add(msg.retain()); } diff --git a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java index 5ac7ccc32c..f24bacd349 100644 --- a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java +++ b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java @@ -18,6 +18,7 @@ package io.github.retrooper.packetevents.handler; +import com.github.retrooper.packetevents.protocol.PacketSide; import com.github.retrooper.packetevents.protocol.player.User; import com.github.retrooper.packetevents.util.PacketEventsImplHelper; import io.netty.buffer.ByteBuf; @@ -30,10 +31,12 @@ @ApiStatus.Internal public class PacketEncoder extends ChannelOutboundHandlerAdapter { + private final PacketSide side; public User user; public Player player; - public PacketEncoder(User user) { + public PacketEncoder(PacketSide side, User user) { + this.side = side.getOpposite(); this.user = user; } @@ -50,8 +53,8 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) ByteBuf out; try { - out = (ByteBuf) PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), - this.user, this.player, in, false); + out = (ByteBuf) PacketEventsImplHelper.handlePacket(ctx.channel(), + this.user, this.player, in, false, this.side); } finally { in.release(); } diff --git a/fabric/src/main/java/io/github/retrooper/packetevents/mixin/ConnectionMixin.java b/fabric/src/main/java/io/github/retrooper/packetevents/mixin/ConnectionMixin.java index 0d915c74f7..9ae3de7692 100644 --- a/fabric/src/main/java/io/github/retrooper/packetevents/mixin/ConnectionMixin.java +++ b/fabric/src/main/java/io/github/retrooper/packetevents/mixin/ConnectionMixin.java @@ -22,6 +22,7 @@ import com.github.retrooper.packetevents.event.UserConnectEvent; import com.github.retrooper.packetevents.manager.protocol.ProtocolManager; import com.github.retrooper.packetevents.protocol.ConnectionState; +import com.github.retrooper.packetevents.protocol.PacketSide; import com.github.retrooper.packetevents.protocol.player.ClientVersion; import com.github.retrooper.packetevents.protocol.player.User; import com.github.retrooper.packetevents.protocol.player.UserProfile; @@ -67,8 +68,9 @@ private static void configureSerialization( return; } - channel.pipeline().addAfter("splitter", PacketEvents.DECODER_NAME, new PacketDecoder(user)); - channel.pipeline().addAfter("prepender", PacketEvents.ENCODER_NAME, new PacketEncoder(user)); + PacketSide side = PacketEvents.getAPI().getInjector().isServerBound() ? PacketSide.SERVER : PacketSide.CLIENT; + channel.pipeline().addAfter("splitter", PacketEvents.DECODER_NAME, new PacketDecoder(side, user)); + channel.pipeline().addAfter("prepender", PacketEvents.ENCODER_NAME, new PacketEncoder(side, user)); channel.closeFuture().addListener((ChannelFutureListener) future -> PacketEventsImplHelper.handleDisconnection(user.getChannel(), user.getUUID())); } From aa87cf803287415432075d8ae97baa59cc55b290 Mon Sep 17 00:00:00 2001 From: booky10 Date: Mon, 21 Oct 2024 01:00:11 +0200 Subject: [PATCH 3/8] Fix fabric logger level remapping --- .../factory/fabric/FabricLogger.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/fabric/src/main/java/io/github/retrooper/packetevents/factory/fabric/FabricLogger.java b/fabric/src/main/java/io/github/retrooper/packetevents/factory/fabric/FabricLogger.java index eaad805fb3..6c6bdaff23 100644 --- a/fabric/src/main/java/io/github/retrooper/packetevents/factory/fabric/FabricLogger.java +++ b/fabric/src/main/java/io/github/retrooper/packetevents/factory/fabric/FabricLogger.java @@ -22,11 +22,21 @@ import net.kyori.adventure.text.format.NamedTextColor; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; +import org.slf4j.event.Level; -import java.util.logging.Level; +import java.util.Map; public class FabricLogger extends LogManager { + private static final Map LEVEL_CONVERSION = Map.of( + java.util.logging.Level.FINEST, Level.TRACE, + java.util.logging.Level.FINER, Level.TRACE, + java.util.logging.Level.FINE, Level.DEBUG, + java.util.logging.Level.INFO, Level.INFO, + java.util.logging.Level.WARNING, Level.WARN, + java.util.logging.Level.SEVERE, Level.ERROR + ); + private final Logger logger; public FabricLogger(Logger logger) { @@ -34,9 +44,9 @@ public FabricLogger(Logger logger) { } @Override - protected void log(Level level, @Nullable NamedTextColor color, String message) { + protected void log(java.util.logging.Level level, @Nullable NamedTextColor color, String message) { String plainMessage = STRIP_COLOR_PATTERN.matcher(message).replaceAll(""); - org.slf4j.event.Level logLevel = org.slf4j.event.Level.intToLevel(level.intValue()); + Level logLevel = LEVEL_CONVERSION.getOrDefault(level, Level.INFO); this.logger.makeLoggingEventBuilder(logLevel).log(plainMessage); } } From a5d3cf5957a78a6aca34f2c145225a5d66441a5a Mon Sep 17 00:00:00 2001 From: booky10 Date: Mon, 21 Oct 2024 01:12:59 +0200 Subject: [PATCH 4/8] Fix wrong packetencoder side --- .../io/github/retrooper/packetevents/handler/PacketEncoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java index f24bacd349..acf7d471f3 100644 --- a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java +++ b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java @@ -36,7 +36,7 @@ public class PacketEncoder extends ChannelOutboundHandlerAdapter { public Player player; public PacketEncoder(PacketSide side, User user) { - this.side = side.getOpposite(); + this.side = side; this.user = user; } From f3bdcbec8395a84ace82b0b66fb461481fa3de1e Mon Sep 17 00:00:00 2001 From: booky10 Date: Mon, 21 Oct 2024 01:13:14 +0200 Subject: [PATCH 5/8] Fix PacketDecoder handling of serverbound packets --- .../packetevents/handler/PacketDecoder.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java index 4788e6bf45..bc17644e75 100644 --- a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java +++ b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java @@ -46,10 +46,16 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) if (!msg.isReadable()) { return; } - PacketEventsImplHelper.handlePacket(ctx.channel(), this.user, this.player, + ByteBuf buf = (ByteBuf) PacketEventsImplHelper.handlePacket(ctx.channel(), this.user, this.player, msg, false, this.side); - if (msg.isReadable()) { - out.add(msg.retain()); + try { + if (buf.isReadable()) { + out.add(buf.retain()); + } + } finally { + if (buf != msg) { + buf.release(); + } } } } From 9843e9fd68ebd699f6d0e686b20a1238f99ce757 Mon Sep 17 00:00:00 2001 From: booky10 Date: Mon, 21 Oct 2024 01:13:30 +0200 Subject: [PATCH 6/8] Fix refcount issues with PacketEncoder --- .../github/retrooper/packetevents/handler/PacketEncoder.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java index acf7d471f3..7ddd989bc7 100644 --- a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java +++ b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java @@ -59,9 +59,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) in.release(); } if (out.isReadable()) { - ctx.write(out, promise); - } else { - out.release(); + ctx.write(out.retain(), promise); } } } From 5ef51406ebc3f697b2a6be0ef450e1a4f25519f5 Mon Sep 17 00:00:00 2001 From: booky10 Date: Mon, 21 Oct 2024 01:23:45 +0200 Subject: [PATCH 7/8] Refactor clientbound and serverbound packet handling for same behaviour --- .../util/PacketEventsImplHelper.java | 37 ++++++++++--------- .../packetevents/handler/PacketDecoder.java | 12 ++---- .../packetevents/handler/PacketEncoder.java | 13 ++----- .../handlers/PacketEventsDecoder.java | 4 +- .../handlers/PacketEventsEncoder.java | 5 ++- .../handlers/PacketEventsDecoder.java | 4 +- .../handlers/PacketEventsEncoder.java | 5 ++- 7 files changed, 37 insertions(+), 43 deletions(-) diff --git a/api/src/main/java/com/github/retrooper/packetevents/util/PacketEventsImplHelper.java b/api/src/main/java/com/github/retrooper/packetevents/util/PacketEventsImplHelper.java index 0578fc1c1f..a284ee6da4 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/util/PacketEventsImplHelper.java +++ b/api/src/main/java/com/github/retrooper/packetevents/util/PacketEventsImplHelper.java @@ -21,35 +21,39 @@ import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.event.PacketReceiveEvent; import com.github.retrooper.packetevents.event.PacketSendEvent; +import com.github.retrooper.packetevents.event.ProtocolPacketEvent; import com.github.retrooper.packetevents.event.UserDisconnectEvent; import com.github.retrooper.packetevents.manager.protocol.ProtocolManager; import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper; -import com.github.retrooper.packetevents.netty.buffer.UnpooledByteBufAllocationHelper; import com.github.retrooper.packetevents.protocol.PacketSide; import com.github.retrooper.packetevents.protocol.player.User; import org.jetbrains.annotations.Nullable; import java.util.UUID; -public class PacketEventsImplHelper { +public final class PacketEventsImplHelper { - public static Object handlePacket( + private PacketEventsImplHelper() { + } + + public static @Nullable ProtocolPacketEvent handlePacket( Object channel, User user, Object player, Object buffer, boolean autoProtocolTranslation, PacketSide side ) throws Exception { if (side == PacketSide.SERVER) { - handleClientBoundPacket(channel, user, player, buffer, autoProtocolTranslation); - return buffer; + return handleClientBoundPacket(channel, user, player, buffer, autoProtocolTranslation); } else { return handleServerBoundPacket(channel, user, player, buffer, autoProtocolTranslation); } } - public static PacketSendEvent handleClientBoundPacket( + public static @Nullable PacketSendEvent handleClientBoundPacket( Object channel, User user, Object player, Object buffer, boolean autoProtocolTranslation ) throws Exception { - if (!ByteBufHelper.isReadable(buffer)) return null; + if (!ByteBufHelper.isReadable(buffer)) { + return null; + } int preProcessIndex = ByteBufHelper.readerIndex(buffer); PacketSendEvent packetSendEvent = EventCreationUtil.createSendEvent(channel, user, player, buffer, autoProtocolTranslation); @@ -83,26 +87,25 @@ public static PacketSendEvent handleClientBoundPacket( return packetSendEvent; } - public static Object handleServerBoundPacket(Object channel, User user, - Object player, - Object buffer, - boolean autoProtocolTranslation) throws Exception { - if (!ByteBufHelper.isReadable(buffer)) return null; + public static @Nullable PacketReceiveEvent handleServerBoundPacket( + Object channel, User user, Object player, Object buffer, + boolean autoProtocolTranslation + ) throws Exception { + if (!ByteBufHelper.isReadable(buffer)) { + return null; + } int preProcessIndex = ByteBufHelper.readerIndex(buffer); PacketReceiveEvent packetReceiveEvent = EventCreationUtil.createReceiveEvent(channel, user, player, buffer, autoProtocolTranslation); int processIndex = ByteBufHelper.readerIndex(buffer); - Object finalBuffer = buffer; PacketEvents.getAPI().getEventManager().callEvent(packetReceiveEvent, () -> { - ByteBufHelper.readerIndex(finalBuffer, processIndex); + ByteBufHelper.readerIndex(buffer, processIndex); }); if (!packetReceiveEvent.isCancelled()) { //Did they ever use a wrapper? if (packetReceiveEvent.getLastUsedWrapper() != null) { //Rewrite the buffer ByteBufHelper.clear(buffer); - buffer = UnpooledByteBufAllocationHelper.buffer(); - packetReceiveEvent.getLastUsedWrapper().setBuffer(buffer); packetReceiveEvent.getLastUsedWrapper().writeVarInt(packetReceiveEvent.getPacketId()); packetReceiveEvent.getLastUsedWrapper().write(); } else { @@ -119,7 +122,7 @@ public static Object handleServerBoundPacket(Object channel, User user, task.run(); } } - return buffer; + return packetReceiveEvent; } public static void handleDisconnection(Object channel, @Nullable UUID uuid) { diff --git a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java index bc17644e75..4788e6bf45 100644 --- a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java +++ b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketDecoder.java @@ -46,16 +46,10 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) if (!msg.isReadable()) { return; } - ByteBuf buf = (ByteBuf) PacketEventsImplHelper.handlePacket(ctx.channel(), this.user, this.player, + PacketEventsImplHelper.handlePacket(ctx.channel(), this.user, this.player, msg, false, this.side); - try { - if (buf.isReadable()) { - out.add(buf.retain()); - } - } finally { - if (buf != msg) { - buf.release(); - } + if (msg.isReadable()) { + out.add(msg.retain()); } } } diff --git a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java index 7ddd989bc7..eded37b8ec 100644 --- a/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java +++ b/fabric/src/main/java/io/github/retrooper/packetevents/handler/PacketEncoder.java @@ -51,15 +51,10 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) return; } - ByteBuf out; - try { - out = (ByteBuf) PacketEventsImplHelper.handlePacket(ctx.channel(), - this.user, this.player, in, false, this.side); - } finally { - in.release(); - } - if (out.isReadable()) { - ctx.write(out.retain(), promise); + PacketEventsImplHelper.handlePacket(ctx.channel(), + this.user, this.player, in, false, this.side); + if (in.isReadable()) { + ctx.write(in, promise); } } } diff --git a/spigot/src/main/java/io/github/retrooper/packetevents/injector/handlers/PacketEventsDecoder.java b/spigot/src/main/java/io/github/retrooper/packetevents/injector/handlers/PacketEventsDecoder.java index 443f7c0324..6f36bc7bc6 100644 --- a/spigot/src/main/java/io/github/retrooper/packetevents/injector/handlers/PacketEventsDecoder.java +++ b/spigot/src/main/java/io/github/retrooper/packetevents/injector/handlers/PacketEventsDecoder.java @@ -54,8 +54,8 @@ public PacketEventsDecoder(PacketEventsDecoder decoder) { } public void read(ChannelHandlerContext ctx, ByteBuf input, List out) throws Exception { - Object buffer = PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player, input, true); - out.add(ByteBufHelper.retain(buffer)); + PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player, input, true); + out.add(ByteBufHelper.retain(input)); } @Override diff --git a/spigot/src/main/java/io/github/retrooper/packetevents/injector/handlers/PacketEventsEncoder.java b/spigot/src/main/java/io/github/retrooper/packetevents/injector/handlers/PacketEventsEncoder.java index f3e1526231..a43012816b 100644 --- a/spigot/src/main/java/io/github/retrooper/packetevents/injector/handlers/PacketEventsEncoder.java +++ b/spigot/src/main/java/io/github/retrooper/packetevents/injector/handlers/PacketEventsEncoder.java @@ -44,6 +44,7 @@ import net.kyori.adventure.text.Component; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.InvocationTargetException; import java.util.List; @@ -83,9 +84,9 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf byteBuf, List l list.add(byteBuf.retain()); } - private PacketSendEvent handleClientBoundPacket(Channel channel, User user, Object player, ByteBuf buffer, ChannelPromise promise) throws Exception { + private @Nullable PacketSendEvent handleClientBoundPacket(Channel channel, User user, Object player, ByteBuf buffer, ChannelPromise promise) throws Exception { PacketSendEvent packetSendEvent = PacketEventsImplHelper.handleClientBoundPacket(channel, user, player, buffer, true); - if (packetSendEvent.hasTasksAfterSend()) { + if (packetSendEvent != null && packetSendEvent.hasTasksAfterSend()) { promise.addListener((p) -> { for (Runnable task : packetSendEvent.getTasksAfterSend()) { task.run(); diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsDecoder.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsDecoder.java index f6d9b31fa4..87e95810a0 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsDecoder.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsDecoder.java @@ -56,8 +56,8 @@ public PacketEventsDecoder(PacketEventsDecoder decoder) { } public void read(ChannelHandlerContext ctx, ByteBuf input, List out) throws Exception { - Object buffer = PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player == null ? null : Sponge.server().player(player).orElse(null), input, true); - out.add(ByteBufHelper.retain(buffer)); + PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player == null ? null : Sponge.server().player(player).orElse(null), input, true); + out.add(ByteBufHelper.retain(input)); } @Override diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsEncoder.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsEncoder.java index 72c234d365..0ff9fe5ea2 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsEncoder.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsEncoder.java @@ -36,6 +36,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; import io.netty.handler.codec.MessageToMessageEncoder; +import org.jetbrains.annotations.Nullable; import org.spongepowered.api.Sponge; import java.lang.reflect.InvocationTargetException; @@ -77,9 +78,9 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf byteBuf, List l list.add(byteBuf.retain()); } - private PacketSendEvent handleClientBoundPacket(Channel channel, User user, UUID player, ByteBuf buffer, ChannelPromise promise) throws Exception { + private @Nullable PacketSendEvent handleClientBoundPacket(Channel channel, User user, UUID player, ByteBuf buffer, ChannelPromise promise) throws Exception { PacketSendEvent packetSendEvent = PacketEventsImplHelper.handleClientBoundPacket(channel, user, player == null ? null : Sponge.server().player(player).orElse(null), buffer, true); - if (packetSendEvent.hasTasksAfterSend()) { + if (packetSendEvent != null && packetSendEvent.hasTasksAfterSend()) { promise.addListener((p) -> { for (Runnable task : packetSendEvent.getTasksAfterSend()) { task.run(); From e54035a683301d29004e6bc93409b001d2821af5 Mon Sep 17 00:00:00 2001 From: booky10 Date: Mon, 21 Oct 2024 01:31:04 +0200 Subject: [PATCH 8/8] Work around classloading loop only present on fabric --- .../SynchronizedRegistriesHandler.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/api/src/main/java/com/github/retrooper/packetevents/util/mappings/SynchronizedRegistriesHandler.java b/api/src/main/java/com/github/retrooper/packetevents/util/mappings/SynchronizedRegistriesHandler.java index ec944f6aa8..f084cc6d84 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/util/mappings/SynchronizedRegistriesHandler.java +++ b/api/src/main/java/com/github/retrooper/packetevents/util/mappings/SynchronizedRegistriesHandler.java @@ -54,31 +54,34 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Function; import java.util.function.Supplier; -import java.util.stream.Collectors; import java.util.stream.Stream; @ApiStatus.Internal public final class SynchronizedRegistriesHandler { private static final boolean FORCE_PER_USER_REGISTRIES = Boolean.getBoolean("packetevents.force-per-user-registries"); - private static final Map> REGISTRY_KEYS = Stream.of( - new RegistryEntry<>(Biomes.getRegistry(), Biome::decode), - new RegistryEntry<>(ChatTypes.getRegistry(), ChatType::decode), - new RegistryEntry<>(TrimPatterns.getRegistry(), TrimPattern::decode), - new RegistryEntry<>(TrimMaterials.getRegistry(), TrimMaterial::decode), - new RegistryEntry<>(WolfVariants.getRegistry(), WolfVariant::decode), - new RegistryEntry<>(PaintingVariants.getRegistry(), PaintingVariant::decode), - new RegistryEntry<>(DimensionTypes.getRegistry(), DimensionType::decode), - new RegistryEntry<>(DamageTypes.getRegistry(), DamageType::decode), - new RegistryEntry<>(BannerPatterns.getRegistry(), BannerPattern::decode), - new RegistryEntry<>(EnchantmentTypes.getRegistry(), EnchantmentType::decode), - new RegistryEntry<>(JukeboxSongs.getRegistry(), IJukeboxSong::decode) - ).collect(Collectors.toMap(RegistryEntry::getRegistryKey, Function.identity())); + private static final Map> REGISTRY_KEYS = new HashMap<>(); + + static { + Stream.of( + new RegistryEntry<>(Biomes.getRegistry(), Biome::decode), + new RegistryEntry<>(ChatTypes.getRegistry(), ChatType::decode), + new RegistryEntry<>(TrimPatterns.getRegistry(), TrimPattern::decode), + new RegistryEntry<>(TrimMaterials.getRegistry(), TrimMaterial::decode), + new RegistryEntry<>(WolfVariants.getRegistry(), WolfVariant::decode), + new RegistryEntry<>(PaintingVariants.getRegistry(), PaintingVariant::decode), + new RegistryEntry<>(DimensionTypes.getRegistry(), DimensionType::decode), + new RegistryEntry<>(DamageTypes.getRegistry(), DamageType::decode), + new RegistryEntry<>(BannerPatterns.getRegistry(), BannerPattern::decode), + new RegistryEntry<>(EnchantmentTypes.getRegistry(), EnchantmentType::decode), + new RegistryEntry<>(JukeboxSongs.getRegistry(), IJukeboxSong::decode) + ).forEach(entry -> REGISTRY_KEYS.put(entry.getRegistryKey(), entry)); + } private SynchronizedRegistriesHandler() { }