Skip to content

Add cfps command #725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public static void registerCommands(CommandDispatcher<FabricClientCommandSource>
FindItemCommand.register(dispatcher, context);
FishCommand.register(dispatcher, context);
FovCommand.register(dispatcher);
FramerateCommand.register(dispatcher);
GammaCommand.register(dispatcher);
GetDataCommand.register(dispatcher);
GhostBlockCommand.register(dispatcher, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.earthcomputer.clientcommands.command;

import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.Minecraft;
import net.minecraft.client.Options;
import net.minecraft.network.chat.Component;

import java.util.function.IntSupplier;

import static com.mojang.brigadier.arguments.IntegerArgumentType.*;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*;

public class FramerateCommand {

public static final IntSupplier MAX_REFRESH_RATE = () -> Math.max(Options.UNLIMITED_FRAMERATE_CUTOFF, Minecraft.getInstance().virtualScreen.screenManager.monitors.values().stream()
.mapToInt(monitor -> monitor.getCurrentMode().getRefreshRate())
.max().orElseThrow());

public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
dispatcher.register(literal("cfps")
.executes(ctx -> getMaxFps(ctx.getSource()))
.then(argument("maxfps", integer(1))
.suggests((context, builder) -> builder.suggest(MAX_REFRESH_RATE.getAsInt()).buildFuture())
.executes(ctx -> setMaxFps(ctx.getSource(), getInteger(ctx, "maxfps"))))
.then(literal("unlimited")
.executes(ctx -> setMaxFps(ctx.getSource(), MAX_REFRESH_RATE.getAsInt() + 1))));
}

private static int getMaxFps(FabricClientCommandSource source) {
int framerateLimit = source.getClient().getFramerateLimitTracker().getFramerateLimit();
if (framerateLimit > MAX_REFRESH_RATE.getAsInt()) {
source.sendFeedback(Component.translatable("commands.cfps.getMaxFps.unlimited"));
} else {
source.sendFeedback(Component.translatable("commands.cfps.getMaxFps", framerateLimit));
}
return framerateLimit;
}

private static int setMaxFps(FabricClientCommandSource source, int maxFps) {
source.getClient().getFramerateLimitTracker().setFramerateLimit(maxFps);
int maxRefreshRate = MAX_REFRESH_RATE.getAsInt();
if (maxFps > maxRefreshRate) {
source.sendFeedback(Component.translatable("commands.cfps.setMaxFps.unlimited"));
} else {
source.sendFeedback(Component.translatable("commands.cfps.setMaxFps.success", maxFps));
}
return maxFps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.earthcomputer.clientcommands.mixin.commands.fps;

import net.earthcomputer.clientcommands.command.FramerateCommand;
import net.minecraft.client.Minecraft;
import net.minecraft.client.Options;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;

@Mixin(Minecraft.class)
public class MinecraftMixin {
@ModifyConstant(method = "runTick", constant = @Constant(intValue = Options.UNLIMITED_FRAMERATE_CUTOFF))
private int changeCutoff(int original) {
return FramerateCommand.MAX_REFRESH_RATE.getAsInt() + 1;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/assets/clientcommands/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@

"commands.cfov.success": "Set FOV to %s",

"commands.cfps.getMaxFps": "FPS is capped at %s",
"commands.cfps.getMaxFps.unlimited": "FPS is unlimited",
"commands.cfps.setMaxFps.success": "Set max FPS to %s",
"commands.cfps.setMaxFps.unlimited": "Set max FPS to unlimited",

"commands.cfunction.limitReached": "Command limit (%s) reached",
"commands.cfunction.success": "Ran %s commands from function %s",

Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/clientcommands.aw
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ accessible method net/minecraft/world/level/block/ShulkerBoxBlock canOpen (Lnet/
# cfish
accessible method net/minecraft/world/entity/projectile/FishingHook canHitEntity (Lnet/minecraft/world/entity/Entity;)Z

# cfps
accessible field net/minecraft/client/Minecraft virtualScreen Lnet/minecraft/client/renderer/VirtualScreen;
accessible field net/minecraft/client/renderer/VirtualScreen screenManager Lcom/mojang/blaze3d/platform/ScreenManager;
accessible field com/mojang/blaze3d/platform/ScreenManager monitors Lit/unimi/dsi/fastutil/longs/Long2ObjectMap;

# cgive
accessible method net/minecraft/world/entity/player/Inventory addResource (ILnet/minecraft/world/item/ItemStack;)I
accessible method net/minecraft/world/entity/player/Inventory hasRemainingSpaceForItem (Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/item/ItemStack;)Z
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mixins.clientcommands.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"commands.alias.ClientSuggestionProviderMixin",
"commands.enchant.MultiPlayerGameModeMixin",
"commands.findblock.ClientLevelMixin",
"commands.fps.MinecraftMixin",
"commands.generic.CommandSuggestionsMixin",
"commands.glow.LivingEntityRenderStateMixin",
"commands.reply.ClientPacketListenerMixin",
Expand Down