From bb1a63ea0b7a09470767b428c06c5326e0325060 Mon Sep 17 00:00:00 2001 From: Sensei Date: Sun, 2 Oct 2022 18:03:11 +0100 Subject: [PATCH] Make server net handling run on main thread --- .../carpet/mixins/ServerGamePacketListenerImplMixin.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/carpet/mixins/ServerGamePacketListenerImplMixin.java b/src/main/java/carpet/mixins/ServerGamePacketListenerImplMixin.java index 6c329268..27f74f20 100644 --- a/src/main/java/carpet/mixins/ServerGamePacketListenerImplMixin.java +++ b/src/main/java/carpet/mixins/ServerGamePacketListenerImplMixin.java @@ -3,6 +3,8 @@ package carpet.mixins; import carpet.network.CarpetClient; import carpet.network.ServerNetworkHandler; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.PacketUtils; +import net.minecraft.network.protocol.game.ServerGamePacketListener; import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; @@ -24,6 +26,10 @@ public class ServerGamePacketListenerImplMixin ResourceLocation channel = packet.getIdentifier(); if (CarpetClient.CARPET_CHANNEL.equals(channel)) { + // We should force onto the main thread here + // ServerNetworkHandler.handleData can possibly mutate data that isn't + // thread safe, and also allows for client commands to be executed + PacketUtils.ensureRunningOnSameThread(packet, (ServerGamePacketListener) this, player.getLevel()); ServerNetworkHandler.handleData(new FriendlyByteBuf(packet.getData().copy()), player); ci.cancel(); }