Skip to content

Commit

Permalink
Don't store the ServerPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed Oct 13, 2024
1 parent 24ffd9e commit 511ced5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ByteBuf> {

public User user;
public ServerPlayer player;
public UUID player;
public boolean hasBeenRelocated;

public PacketEventsDecoder(User user) {
Expand All @@ -56,7 +56,7 @@ public PacketEventsDecoder(PacketEventsDecoder decoder) {
}

public void read(ChannelHandlerContext ctx, ByteBuf input, List<Object> 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));
}

Expand Down Expand Up @@ -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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ByteBuf> {

public User user;
public ServerPlayer player;
public UUID player;
private boolean handledCompression;
private ChannelPromise promise;

Expand Down Expand Up @@ -76,8 +77,8 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 511ced5

Please sign in to comment.