diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/SpongeChannelInjector.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/SpongeChannelInjector.java index 4f06f781d7..baf2eb19a2 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/SpongeChannelInjector.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/SpongeChannelInjector.java @@ -200,16 +200,17 @@ public void updateUser(Object channel, User user) { @Override public void setPlayer(Object channel, Object player) { + final ServerPlayer spongePlayer = (ServerPlayer) player; PacketEventsEncoder encoder = getEncoder((Channel) channel); if (encoder != null) { - encoder.player = (ServerPlayer) player; + encoder.player = spongePlayer.uniqueId(); } PacketEventsDecoder decoder = getDecoder((Channel) channel); if (decoder != null) { - decoder.player = (ServerPlayer) player; - decoder.user.getProfile().setName(((ServerPlayer) player).name()); - decoder.user.getProfile().setUUID(((ServerPlayer) player).uniqueId()); + decoder.player = spongePlayer.uniqueId(); + decoder.user.getProfile().setName(spongePlayer.name()); + decoder.user.getProfile().setUUID(spongePlayer.uniqueId()); } } 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 31ec1c12d9..f6d9b31fa4 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 @@ -33,16 +33,16 @@ import io.netty.handler.codec.MessageToMessageDecoder; import net.kyori.adventure.text.Component; import org.spongepowered.api.Sponge; -import org.spongepowered.api.entity.living.player.server.ServerPlayer; import org.spongepowered.api.scheduler.Task; import org.spongepowered.plugin.PluginContainer; import java.util.List; +import java.util.UUID; public class PacketEventsDecoder extends MessageToMessageDecoder { public User user; - public ServerPlayer player; + public UUID player; public boolean hasBeenRelocated; public PacketEventsDecoder(User user) { @@ -56,7 +56,7 @@ 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); + Object buffer = PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player == null ? null : Sponge.server().player(player).orElse(null), input, true); out.add(ByteBufHelper.retain(buffer)); } @@ -89,7 +89,8 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E } user.closeConnection(); if (player != null) { - Sponge.server().scheduler().submit(Task.builder().plugin((PluginContainer) PacketEvents.getAPI().getPlugin()).execute(() -> player.kick(Component.text("Invalid packet"))).build()); + Sponge.server().scheduler().submit(Task.builder().plugin((PluginContainer) PacketEvents.getAPI().getPlugin()) + .execute(() -> Sponge.server().player(player).get().kick(Component.text("Invalid packet"))).build()); } PacketEvents.getAPI().getLogManager().warn("Disconnected " + user.getProfile().getName() + " due to invalid packet!"); 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 8c086639c5..72c234d365 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,15 +36,16 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; import io.netty.handler.codec.MessageToMessageEncoder; -import org.spongepowered.api.entity.living.player.server.ServerPlayer; +import org.spongepowered.api.Sponge; import java.lang.reflect.InvocationTargetException; import java.util.List; +import java.util.UUID; public class PacketEventsEncoder extends MessageToMessageEncoder { public User user; - public ServerPlayer player; + public UUID player; private boolean handledCompression; private ChannelPromise promise; @@ -76,8 +77,8 @@ 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 { - PacketSendEvent packetSendEvent = PacketEventsImplHelper.handleClientBoundPacket(channel, user, player, buffer, true); + private 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()) { promise.addListener((p) -> { for (Runnable task : packetSendEvent.getTasksAfterSend()) { diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/SpongeReflectionUtil.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/SpongeReflectionUtil.java index c808100809..6b98f49c9e 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/SpongeReflectionUtil.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/SpongeReflectionUtil.java @@ -254,14 +254,16 @@ public static com.github.retrooper.packetevents.protocol.item.ItemStack decodeSp public static ItemStack encodeSpongeItemStack(com.github.retrooper.packetevents.protocol.item.ItemStack in) { Object buffer = PooledByteBufAllocator.DEFAULT.buffer(); - PacketWrapper wrapper = PacketWrapper.createUniversalPacketWrapper(buffer); - wrapper.writeItemStack(in); - //3 reflection calls - Object packetDataSerializer = createPacketDataSerializer(wrapper.getBuffer()); - Object nmsItemStack = readNMSItemStackPacketDataSerializer(packetDataSerializer); - ItemStack stack = (ItemStack) nmsItemStack; - ByteBufHelper.release(buffer); - return stack; + try { + PacketWrapper wrapper = PacketWrapper.createUniversalPacketWrapper(buffer); + wrapper.writeItemStack(in); + //3 reflection calls + Object packetDataSerializer = createPacketDataSerializer(wrapper.getBuffer()); + Object nmsItemStack = readNMSItemStackPacketDataSerializer(packetDataSerializer); + return (ItemStack) nmsItemStack; + } finally { + ByteBufHelper.release(buffer); + } } public static Object createPacketDataSerializer(Object byteBuf) {