Skip to content

Commit

Permalink
handle IEntityAdditionalSpawnData at the right time
Browse files Browse the repository at this point in the history
  • Loading branch information
TropheusJ committed Apr 7, 2024
1 parent 8b53c10 commit 3801c0a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
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 {
@Environment(EnvType.CLIENT)
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 {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Void> callSubmitAsync(Runnable task);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"common.AbstractHorseMixin",
"common.AbstractHurtingProjectileMixin",
"common.AbstractMinecartMixin",
"common.BlockableEventLoopAccessor",
"common.BlockBehaviourMixin",
"common.BundlePacketMixin",
"common.CatSpawnerMixin",
Expand Down

0 comments on commit 3801c0a

Please sign in to comment.