Skip to content

Commit

Permalink
fix enqueueWork's logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TropheusJ committed Apr 7, 2024
1 parent f8dcba4 commit 9e3e686
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.simibubi.create.foundation.mixin.fabric;

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
@@ -1,6 +1,6 @@
package com.simibubi.create.foundation.networking;

import java.util.concurrent.Executor;
import com.simibubi.create.foundation.mixin.fabric.BlockableEventLoopAccessor;

import org.jetbrains.annotations.Nullable;

Expand All @@ -15,6 +15,7 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.util.thread.BlockableEventLoop;

public abstract class SimplePacketBase implements C2SPacket, S2CPacket {

Expand Down Expand Up @@ -42,9 +43,16 @@ public enum NetworkDirection {
PLAY_TO_SERVER
}

public record Context(Executor exec, PacketListener listener, @Nullable ServerPlayer sender) {
public record Context(BlockableEventLoop<? extends Runnable> executor, PacketListener listener, @Nullable ServerPlayer sender) {
public void enqueueWork(Runnable runnable) {
exec().execute(runnable);
// Matches Forge's enqueueWork behavior.
// MC will sometimes defer tasks even if already on the right thread.
if (executor.isSameThread()) {
runnable.run();
} else {
// skip extra checks
((BlockableEventLoopAccessor) executor).callSubmitAsync(runnable);
}
}

@Nullable
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/create.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"accessor.ServerLevelAccessor",
"accessor.SystemReportAccessor",
"fabric.AbstractMinecartMixin",
"fabric.BlockableEventLoopAccessor",
"fabric.EntityMixin",
"fabric.RegistryMixin",
"fabric.ServerGamePacketListenerImplAccessor",
Expand Down

0 comments on commit 9e3e686

Please sign in to comment.