Skip to content

Commit a643170

Browse files
embeddedtHaHaWTH
andcommitted
Implement more accurate fix for MC-183518
The game now sleeps precisely till the next tick, rather than still waking up periodically Co-authored-by: HaHaWTH <[email protected]>
1 parent 82535da commit a643170

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/fix_loop_spin_waiting/BlockableEventLoopMixin.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.embeddedt.modernfix.common.mixin.perf.fix_loop_spin_waiting;
2+
3+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
4+
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
5+
import net.minecraft.Util;
6+
import net.minecraft.server.MinecraftServer;
7+
import net.minecraft.util.thread.BlockableEventLoop;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.Shadow;
10+
import org.spongepowered.asm.mixin.Unique;
11+
import org.spongepowered.asm.mixin.injection.At;
12+
13+
import java.util.concurrent.locks.LockSupport;
14+
import java.util.function.BooleanSupplier;
15+
16+
@Mixin(value = MinecraftServer.class, priority = 500)
17+
public abstract class MinecraftServerMixin extends BlockableEventLoop<Runnable> {
18+
@Shadow private long nextTickTime;
19+
20+
protected MinecraftServerMixin(String name) {
21+
super(name);
22+
}
23+
24+
@Unique
25+
private boolean mfix$isWaitingForNextTick = false;
26+
27+
@WrapOperation(
28+
method = "waitUntilNextTick",
29+
at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;managedBlock(Ljava/util/function/BooleanSupplier;)V")
30+
)
31+
private void managedBlock(MinecraftServer instance, BooleanSupplier isDone, Operation<Void> original) {
32+
try {
33+
this.mfix$isWaitingForNextTick = true;
34+
original.call(instance, isDone);
35+
} finally {
36+
this.mfix$isWaitingForNextTick = false;
37+
}
38+
}
39+
40+
@Override
41+
protected void waitForTasks() {
42+
if (this.mfix$isWaitingForNextTick) {
43+
LockSupport.parkNanos("waiting for tasks", (this.nextTickTime * 1000000L) - Util.getNanos());
44+
} else {
45+
super.waitForTasks();
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)