ANosa Mostafa (niceguylovesaxe)

Create a Fabric client mod for Minecraft. Register a custom keybinding set to the 'U' key. When the player presses the 'U' key, it should automatically check an internal state to toggle. If the game is not frozen, it must send the text command '/tick freeze' to the game chat. If it is already frozen, it must send the command '/tick unfreeze' to the chat instantly

Kodari AI
KodariOct 18 at 7:47 pm 1
FreezeToggle
FreezeToggle is a Fabric client mod for Minecraft 1.21.1 that registers a custom keybinding on the 'U' key. Pressing the key toggles an internal frozen state, automatically sending '/tick freeze' or '/tick unfreeze' to chat based on the current state, enabling instant server tick freezing and unfreezing for testing and building purposes.
  1. Create the Fabric client mod scaffold and register U to toggle an internal frozen state, sending /tick freeze or /tick unfreeze immediately on press.

Created a Fabric 1.21.11 client mod with a U keybinding. Each press toggles the internal frozen state and immediately sends tick freeze or tick unfreeze to the server. The server must permit those commands.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 115
src/client/java/com/kodari/ticktoggle/TickToggleClient.java
package com.kodari.ticktoggle;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;

public class TickToggleClient implements ClientModInitializer {
    private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(Identifier.of("ticktoggle", "main"));
    private static final KeyBinding TOGGLE_FREEZE = KeyBindingHelper.registerKeyBinding(
            new KeyBinding("key.ticktoggle.toggle_freeze", GLFW.GLFW_KEY_U, CATEGORY)
    );

    private boolean frozen;

    @Override
    public void onInitializeClient() {
        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            var networkHandler = client.getNetworkHandler();
            if (networkHandler == null) {
                while (TOGGLE_FREEZE.wasPressed()) {
                }
                return;
            }

            while (TOGGLE_FREEZE.wasPressed()) {
                networkHandler.sendChatCommand(frozen ? "tick unfreeze" : "tick freeze");
                frozen = !frozen;
            }
        });
    }
}