Skip to content

Commit 0bd94b6

Browse files
committed
Fix cfps (I hope)
1 parent bd8e3b0 commit 0bd94b6

File tree

7 files changed

+68
-28
lines changed

7 files changed

+68
-28
lines changed

src/main/java/net/earthcomputer/clientcommands/Configs.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package net.earthcomputer.clientcommands;
22

3+
import com.google.common.base.Suppliers;
4+
import dev.xpple.betterconfig.api.BetterConfigAPI;
35
import dev.xpple.betterconfig.api.Config;
6+
import dev.xpple.betterconfig.api.ModConfig;
47
import net.earthcomputer.clientcommands.command.ReplyCommand;
58
import net.earthcomputer.clientcommands.features.ChorusManipulation;
69
import net.earthcomputer.clientcommands.features.EnchantmentCracker;
710
import net.earthcomputer.clientcommands.features.FishingCracker;
811
import net.earthcomputer.clientcommands.features.PlayerRandCracker;
912
import net.earthcomputer.clientcommands.features.ServerBrandManager;
1013
import net.earthcomputer.clientcommands.util.MultiVersionCompat;
14+
import net.minecraft.network.chat.Component;
1115
import net.minecraft.util.Mth;
1216
import net.minecraft.util.StringRepresentable;
1317

1418
import java.util.Locale;
19+
import java.util.function.Supplier;
1520

1621
public class Configs {
22+
private static final Supplier<ModConfig<Component>> CONFIG_REF = Suppliers.memoize(() -> BetterConfigAPI.getInstance().getModConfig("clientcommands"));
23+
24+
public static void save() {
25+
CONFIG_REF.get().save();
26+
}
1727

1828
@Config(readOnly = true, temporary = true)
1929
public static double calcAnswer = 0;
@@ -182,4 +192,7 @@ public enum PacketDumpMethod {
182192
public static void setMinimumReplyDelaySeconds(float minimumReplyDelaySeconds) {
183193
Configs.minimumReplyDelaySeconds = Math.clamp(minimumReplyDelaySeconds, 0.0f, ReplyCommand.MAXIMUM_REPLY_DELAY_SECONDS);
184194
}
195+
196+
@Config(readOnly = true)
197+
public static int overriddenFps = 0;
185198
}

src/main/java/net/earthcomputer/clientcommands/command/FramerateCommand.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package net.earthcomputer.clientcommands.command;
22

3+
import com.mojang.brigadier.Command;
34
import com.mojang.brigadier.CommandDispatcher;
5+
import net.earthcomputer.clientcommands.Configs;
46
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
57
import net.minecraft.client.Minecraft;
68
import net.minecraft.network.chat.Component;
@@ -18,7 +20,7 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
1820
.executes(ctx -> getMaxFps(ctx.getSource()))
1921
.then(literal("unlimited")
2022
.executes(ctx -> maxFps(ctx.getSource(), Integer.MAX_VALUE)))
21-
.then(argument("maxfps", integer())
23+
.then(argument("maxfps", integer(1))
2224
.suggests((context, builder) -> {
2325
int maxFps = getDisplayMaxFramerate();
2426
for (int refreshRate : COMMON_REFRESH_RATES) {
@@ -31,12 +33,18 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
3133
})
3234
.executes(ctx -> maxFps(ctx.getSource(), getInteger(ctx, "maxfps")))
3335
)
36+
.then(literal("reset")
37+
.executes(ctx -> resetMaxFps(ctx.getSource()))
38+
)
3439
);
3540

3641
}
3742

3843
private static int getMaxFps(FabricClientCommandSource source) {
39-
int framerateLimit = source.getClient().getFramerateLimitTracker().getFramerateLimit();
44+
int framerateLimit = Configs.overriddenFps;
45+
if (framerateLimit <= 0) {
46+
framerateLimit = source.getClient().getFramerateLimitTracker().getFramerateLimit();
47+
}
4048
if (framerateLimit < Integer.MAX_VALUE) {
4149
source.sendFeedback(Component.translatable("commands.cfps.getMaxFps", framerateLimit));
4250
} else {
@@ -46,7 +54,8 @@ private static int getMaxFps(FabricClientCommandSource source) {
4654
}
4755

4856
private static int maxFps(FabricClientCommandSource source, int maxFps) {
49-
source.getClient().getFramerateLimitTracker().setFramerateLimit(maxFps);
57+
Configs.overriddenFps = maxFps;
58+
Configs.save();
5059
if (maxFps == Integer.MAX_VALUE) {
5160
source.sendFeedback(Component.translatable("commands.cfps.setMaxFps.unlimited"));
5261
} else {
@@ -55,10 +64,16 @@ private static int maxFps(FabricClientCommandSource source, int maxFps) {
5564
return maxFps;
5665
}
5766

67+
private static int resetMaxFps(FabricClientCommandSource source) {
68+
Configs.overriddenFps = 0;
69+
Configs.save();
70+
source.sendFeedback(Component.translatable("commands.cfps.resetMaxFps.success"));
71+
return Command.SINGLE_SUCCESS;
72+
}
73+
5874
private static int getDisplayMaxFramerate() {
5975
return Minecraft.getInstance().virtualScreen.screenManager.monitors.values().stream()
6076
.mapToInt(monitor -> monitor.getCurrentMode().getRefreshRate())
6177
.max().orElseThrow();
6278
}
63-
6479
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package net.earthcomputer.clientcommands.mixin.commands.fps;
2+
3+
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
4+
import com.mojang.blaze3d.platform.FramerateLimitTracker;
5+
import net.earthcomputer.clientcommands.Configs;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
10+
11+
@Mixin(FramerateLimitTracker.class)
12+
public class FramerateLimitTrackerMixin {
13+
@ModifyExpressionValue(method = "getFramerateLimit", at = @At(value = "FIELD", target = "Lcom/mojang/blaze3d/platform/FramerateLimitTracker;framerateLimit:I"))
14+
private int fixFps(int fps) {
15+
if (Configs.overriddenFps > 0) {
16+
return Configs.overriddenFps;
17+
}
18+
19+
return fps;
20+
}
21+
22+
@Inject(method = "setFramerateLimit", at = @At("HEAD"))
23+
private void onSetFramerateLimit(CallbackInfo ci) {
24+
// Avoid overriding the FPS when something else (e.g. the user) has explicitly set it
25+
Configs.overriddenFps = 0;
26+
Configs.save();
27+
}
28+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package net.earthcomputer.clientcommands.mixin.commands.fps;
22

33
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
4+
import net.earthcomputer.clientcommands.Configs;
45
import net.minecraft.client.Minecraft;
56
import net.minecraft.client.Options;
67
import org.spongepowered.asm.mixin.Mixin;
78
import org.spongepowered.asm.mixin.injection.At;
89

910
@Mixin(Minecraft.class)
1011
public class MinecraftMixin {
11-
1212
@ModifyExpressionValue(method = "runTick", at = @At(value = "CONSTANT", args = "intValue=" + Options.UNLIMITED_FRAMERATE_CUTOFF))
1313
private int fixMaxFps(int fps) {
14-
return Integer.MAX_VALUE;
15-
}
14+
if (Configs.overriddenFps > 0) {
15+
return Integer.MAX_VALUE;
16+
}
1617

18+
return fps;
19+
}
1720
}

src/main/java/net/earthcomputer/clientcommands/mixin/commands/fps/OptionsMixin.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/main/resources/assets/clientcommands/lang/en_us.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131

132132
"commands.cfps.getMaxFps": "Max FPS is %s",
133133
"commands.cfps.getMaxFps.unlimited": "Max FPS is unlimited",
134+
"commands.cfps.resetMaxFps.success": "Stopped overriding max FPS",
134135
"commands.cfps.setMaxFps": "Set max FPS to %s",
135136
"commands.cfps.setMaxFps.unlimited": "Set max FPS to unlimited",
136137

src/main/resources/mixins.clientcommands.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
"commands.alias.ClientSuggestionProviderMixin",
6969
"commands.enchant.MultiPlayerGameModeMixin",
7070
"commands.findblock.ClientLevelMixin",
71+
"commands.fps.FramerateLimitTrackerMixin",
7172
"commands.fps.MinecraftMixin",
72-
"commands.fps.OptionsMixin",
7373
"commands.generic.CommandSuggestionsMixin",
7474
"commands.glow.LivingEntityRenderStateMixin",
7575
"commands.reply.ClientPacketListenerMixin",

0 commit comments

Comments
 (0)