Skip to content

Commit 75907df

Browse files
committed
Toggleable lag generation.
1 parent aa6099b commit 75907df

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package noobanidus.mods.laggenerator.common.mixins;
2+
3+
import net.minecraft.server.MinecraftServer;
4+
import net.minecraft.world.level.GameRules;
5+
import org.apache.commons.lang3.NotImplementedException;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.gen.Invoker;
8+
9+
import java.util.function.BiConsumer;
10+
11+
@Mixin(GameRules.BooleanValue.class)
12+
public interface AccessorMixinGameRules$BooleanValue {
13+
@Invoker("create")
14+
static GameRules.Type<GameRules.BooleanValue> LagGenerator$invokeCreate(boolean defaultValue, BiConsumer<MinecraftServer, GameRules.BooleanValue> changeListener) {
15+
throw new NotImplementedException();
16+
}
17+
}

common/src/main/java/noobanidus/mods/laggenerator/common/mixins/MixinEntity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class MixinEntity {
1818
if (level == null || level.isClientSide() || level.getServer() == null) {
1919
return;
2020
}
21+
if (!LagGameRules.enabled(level.getServer())) {
22+
return;
23+
}
2124
int entityTickLag = LagGameRules.getValue(level.getServer(), LagGameRules.ENTITY_TICK_LAG);
2225
if (entityTickLag > 0) {
2326
try {

common/src/main/java/noobanidus/mods/laggenerator/common/mixins/MixinMinecraftServer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class MixinMinecraftServer {
1414
@WrapOperation(method = "tickServer", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/ServerTickRateManager;tick()V"))
1515
private void LagGenerator$PreServerTickLag(ServerTickRateManager instance, Operation<Void> original) {
1616
original.call(instance);
17+
if (!LagGameRules.enabled((MinecraftServer) (Object) this)) {
18+
return;
19+
}
1720
int preTickLag = LagGameRules.getValue((MinecraftServer) (Object) this, LagGameRules.SERVER_PRE_TICK_LAG);
1821
if (preTickLag > 0) {
1922
try {
@@ -26,6 +29,9 @@ public class MixinMinecraftServer {
2629
@WrapOperation(method = "tickServer", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/profiling/ProfilerFiller;pop()V", ordinal = 1))
2730
private void LagGenerator$PostServerTickLag(ProfilerFiller instance, Operation<Void> original) {
2831
original.call(instance);
32+
if (!LagGameRules.enabled((MinecraftServer) (Object) this)) {
33+
return;
34+
}
2935
int postTickLag = LagGameRules.getValue((MinecraftServer) (Object) this, LagGameRules.SERVER_POST_TICK_LAG);
3036
if (postTickLag > 0) {
3137
try {

common/src/main/java/noobanidus/mods/laggenerator/common/rules/LagGameRules.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import net.minecraft.server.MinecraftServer;
44
import net.minecraft.world.level.GameRules;
55
import noobanidus.mods.laggenerator.common.mixins.AccessorMixinGameRules;
6+
import noobanidus.mods.laggenerator.common.mixins.AccessorMixinGameRules$BooleanValue;
67
import noobanidus.mods.laggenerator.common.mixins.AccessorMixinGameRules$IntegerValue;
78

89
public class LagGameRules {
10+
public static GameRules.Key<GameRules.BooleanValue> ENABLE_LAG_GENERATOR;
911
public static GameRules.Key<GameRules.IntegerValue> SERVER_PRE_TICK_LAG;
1012
public static GameRules.Key<GameRules.IntegerValue> SERVER_POST_TICK_LAG;
1113
public static GameRules.Key<GameRules.IntegerValue> ENTITY_TICK_LAG;
@@ -14,9 +16,14 @@ public static int getValue (MinecraftServer server, GameRules.Key<GameRules.Inte
1416
return server.getGameRules().getRule(key).get();
1517
}
1618

19+
public static boolean enabled (MinecraftServer server) {
20+
return server.getGameRules().getRule(ENABLE_LAG_GENERATOR).get();
21+
}
22+
1723
public static void init () {
1824
SERVER_PRE_TICK_LAG = AccessorMixinGameRules.LagGenerator$invokeRegister("lag_generator_serverPreTickLag", GameRules.Category.UPDATES, AccessorMixinGameRules$IntegerValue.LagGenerator$invokeCreate(80, 0, Integer.MAX_VALUE, (a, b) -> {}));
1925
SERVER_POST_TICK_LAG = AccessorMixinGameRules.LagGenerator$invokeRegister("lag_generator_serverPostTickLag", GameRules.Category.UPDATES, AccessorMixinGameRules$IntegerValue.LagGenerator$invokeCreate(80, 0, Integer.MAX_VALUE, (a, b) -> {}));
20-
ENTITY_TICK_LAG = AccessorMixinGameRules.LagGenerator$invokeRegister("lag_generator_entityTickLag", GameRules.Category.UPDATES, AccessorMixinGameRules$IntegerValue.LagGenerator$invokeCreate(80, 0, Integer.MAX_VALUE, (a, b) -> {}));
26+
ENTITY_TICK_LAG = AccessorMixinGameRules.LagGenerator$invokeRegister("lag_generator_entityTickLag", GameRules.Category.UPDATES, AccessorMixinGameRules$IntegerValue.LagGenerator$invokeCreate(10, 0, Integer.MAX_VALUE, (a, b) -> {}));
27+
ENABLE_LAG_GENERATOR = AccessorMixinGameRules.LagGenerator$invokeRegister("enable_lag_generator", GameRules.Category.UPDATES, AccessorMixinGameRules$BooleanValue.LagGenerator$invokeCreate(false, (a, b) -> {}));
2128
}
2229
}

common/src/main/resources/laggenerator-common.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"compatibilityLevel": "JAVA_17",
55
"mixins": [
66
"AccessorMixinGameRules",
7+
"AccessorMixinGameRules$BooleanValue",
78
"AccessorMixinGameRules$IntegerValue",
89
"MixinEntity",
910
"MixinMinecraftServer"

0 commit comments

Comments
 (0)