diff --git a/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/client/PortingLibEntityClient.java b/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/client/PortingLibEntityClient.java index bbef80d12..d45940c8b 100644 --- a/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/client/PortingLibEntityClient.java +++ b/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/client/PortingLibEntityClient.java @@ -1,20 +1,21 @@ package io.github.fabricators_of_create.porting_lib.entity.client; +import java.util.Objects; + import io.github.fabricators_of_create.porting_lib.core.PortingLib; import io.github.fabricators_of_create.porting_lib.entity.IEntityAdditionalSpawnData; import io.github.fabricators_of_create.porting_lib.entity.MultiPartEntity; import io.github.fabricators_of_create.porting_lib.entity.PartEntity; +import io.github.fabricators_of_create.porting_lib.entity.mixin.common.BlockableEventLoopAccessor; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientPacketListener; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.protocol.game.ClientboundAddEntityPacket; import net.minecraft.world.entity.Entity; public class PortingLibEntityClient implements ClientModInitializer { @@ -22,8 +23,8 @@ public class PortingLibEntityClient implements ClientModInitializer { private static void handlePacketReceived(Minecraft client, ClientPacketListener handler, FriendlyByteBuf buf, PacketSender responseSender) { int entityId = buf.readVarInt(); buf.retain(); // save for execute - client.execute(() -> { - Entity entity = client.level.getEntity(entityId); + execute(client, () -> { + Entity entity = Objects.requireNonNull(client.level).getEntity(entityId); if (entity instanceof IEntityAdditionalSpawnData extra) { extra.readSpawnData(buf); } else { @@ -32,6 +33,19 @@ private static void handlePacketReceived(Minecraft client, ClientPacketListener buf.release(); }); } + + private static void execute(Minecraft mc, Runnable task) { + // sometimes MC will defer a task even if already on the right thread. + // Forge avoids this when calling enqueueWork. + // We need to replicate this behavior for proper packet ordering. + if (mc.isSameThread()) { + task.run(); + } else { + // Forge also bypasses public methods + ((BlockableEventLoopAccessor) mc).callSubmitAsync(task); + } + } + @Override public void onInitializeClient() { ClientPlayNetworking.registerGlobalReceiver(IEntityAdditionalSpawnData.EXTRA_DATA_PACKET, PortingLibEntityClient::handlePacketReceived); diff --git a/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/mixin/common/BlockableEventLoopAccessor.java b/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/mixin/common/BlockableEventLoopAccessor.java new file mode 100644 index 000000000..0944012ee --- /dev/null +++ b/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/mixin/common/BlockableEventLoopAccessor.java @@ -0,0 +1,14 @@ +package io.github.fabricators_of_create.porting_lib.entity.mixin.common; + +import java.util.concurrent.CompletableFuture; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +import net.minecraft.util.thread.BlockableEventLoop; + +@Mixin(BlockableEventLoop.class) +public interface BlockableEventLoopAccessor { + @Invoker + CompletableFuture callSubmitAsync(Runnable task); +} diff --git a/modules/entity/src/main/resources/porting_lib_entity.mixins.json b/modules/entity/src/main/resources/porting_lib_entity.mixins.json index 7d655e4e4..468985c8c 100644 --- a/modules/entity/src/main/resources/porting_lib_entity.mixins.json +++ b/modules/entity/src/main/resources/porting_lib_entity.mixins.json @@ -8,6 +8,7 @@ "common.AbstractHorseMixin", "common.AbstractHurtingProjectileMixin", "common.AbstractMinecartMixin", + "common.BlockableEventLoopAccessor", "common.BlockBehaviourMixin", "common.BundlePacketMixin", "common.CatSpawnerMixin",