ryuzakilabb

Act as an expert Minecraft mod developer specializing in the Fabric API for Minecraft Java Edition version 1.21.11 using Yarn mappings and Java 21.

Your task is to write the complete Java code and explain the file structure for a client-side only "Freecam" mod.

The mod must include the following specific features:

  1. Freecam Toggle & Mechanics:
  • Create a customizable keybind (default: 'V') to toggle Freecam on and off.
  • When activated, the camera must detach from the player's physical body. The player's body should remain completely stationary in the world and not respond to movement inputs.
  • The player should be able to fly the detached camera around freely (noclip through blocks) using standard movement keys (W, A, S, D, Space for up, Shift for down).
  • Provide the necessary Mixins (e.g., hooking into Camera and GameRenderer) to separate the camera position from the player entity position.
  1. Dynamic Camera Coordinates HUD:
  • When Freecam is active, the mod must display a custom HUD element on the screen showing the real-time X, Y, and Z coordinates of the Freecam itself, not the stationary player body.
  • Use Fabric's HudRenderCallback to draw this text on the screen (e.g., top-left corner).
  • The coordinates should update smoothly and accurately as the camera moves through the world.
  1. Code Structure and Mixins:
  • Provide the fabric.mod.json file configured for a client-side mod.
  • Provide the Main Client entrypoint class registering the keybind and HUD render events.
  • Provide the Mixins required to intercept camera positioning, disable player input while in freecam, and prevent the server from rubber-banding the player (since this is client-side, the server should only know about the stationary player body).

Please provide all necessary code blocks with clear filenames, and include a brief explanation of how the Mixins achieve the camera detachment.

Kodari AI
KodariMay 25 at 3:38 pm 16,410 (v2)

I have all the information I need. Let me plan the full project:

  • Mixins needed: CameraMixin (override update() to inject freecam position), GameRendererMixin (override updateTargetedEntity to use freecam pos), ClientPlayerEntityMixin (cancel movement input while in freecam), ClientPlayerInputMixin (zero out movement while in freecam)

  • HUD: Use HudElementRegistry.addLast() (1.21.6+ API, NOT HudRenderCallback)

  • KeyBinding: Use KeyBinding.Category.create(Identifier.of(...)) with 3-arg constructor (1.21.9+ API)

  • Structure: Client-only mod with splitEnvironmentSourceSets(), all code in src/client/java/

FreecamMod
A client-side Freecam mod for Fabric 1.21.11. Press V to detach the camera from your player body and fly freely through the world with noclip. Your physical body stays frozen in place while you explore. A live HUD displays the freecam's real-time X, Y, Z coordinates. Uses Mixins to intercept camera positioning, suppress player movement input, and prevent server rubber-banding.

Here’s the complete project. I’ll explain the mixin strategy after the files.


How the Mixins Achieve Camera Detachment

  1. CameraMixin — The core trick. Camera#update() normally computes the camera position from the entity’s interpolated position each frame. We @Inject at HEAD with cancellable = true: when freecam is active, we skip the entire vanilla update and instead set the camera’s position/rotation directly from our FreecamState singleton, which stores the detached position.

  2. ClientPlayerEntityMixin — Cancels tickMovement() at HEAD while freecam is active. This prevents the player entity from moving, applying gravity, or sending movement packets to the server. The server never sees the player move — it only knows about the frozen body position.

  3. ClientPlayerInputMixin — Zeroes out the MovementInput (forward/side/jump/sneak) before it’s consumed. This ensures the W/A/S/D keys don’t move the player body even if tickMovement partially runs.

  4. GameRendererMixin — Cancels updateTargetedEntity() while in freecam so the crosshair doesn’t try to target blocks/entities relative to the frozen player body position.

The freecam movement itself is handled in a ClientTickEvents.END_CLIENT_TICK listener that reads the raw key states (KeyBinding.isPressed()) and translates them into position deltas applied to FreecamState — completely bypassing the normal entity movement pipeline.


Now the main client entrypoint and the freecam state singleton:

Now the four mixins:


File Structure Summary

Plain Text
FreecamMod/
├── build.gradle
├── gradle.properties
└── src/
    ├── main/
    │   └── resources/
    │       ├── fabric.mod.json               ← client-only entrypoint
    │       ├── freecam.mixins.json           ← 4 client mixins registered
    │       └── assets/freecam/lang/
    │           └── en_us.json
    └── client/
        └── java/com/example/freecam/
            ├── FreecamClient.java            ← ClientModInitializer
            ├── FreecamState.java             ← Singleton state (pos/rot/active)
            └── mixin/
                ├── CameraMixin.java          ← Overrides Camera#update()
                ├── ClientPlayerEntityMixin.java  ← Cancels tickMovement()
                ├── ClientPlayerInputMixin.java   ← Zeroes movement input
                └── GameRendererMixin.java    ← Cancels targeted entity raycast

Key Design Decisions

Decision
Reason
FreecamState
singleton
Shared mutable state between the tick event (writes position) and the camera mixin (reads position) — no cross-class coupling needed
Cancel
Camera#update()
entirely
The vanilla method interpolates entity position with
tickDelta
— we can’t just modify the result; we must replace the whole call
Mirror
player.getYaw()/getPitch()
into
FreecamState
each tick
Vanilla mouse-look writes directly to the player entity’s yaw/pitch fields; we piggyback on that for free smooth mouse look
Cancel
tickMovement()
not
travel()
tickMovement()
is the outermost movement method — cancelling it prevents gravity, collision, and packet sending in one shot
Input#tick()
at
RETURN
Belt-and-suspenders: even if
tickMovement
partially executes, zero input means zero displacement
ryuzakilabb

The compilation failed with this error:

code
Compilation failed with 8 error(s):

ERROR in src/main/java/client/java/com/example/freecam/mixin/ClientPlayerInputMixin.java at line 23: cannot find symbol
        self.movementForward = 0.0f;
            ^
  symbol: variable movementForward
  location: variable self of type Input

ERROR in src/main/java/client/java/com/example/freecam/mixin/ClientPlayerInputMixin.java at line 24: cannot find symbol
        self.movementSideways = 0.0f;
            ^
  symbol: variable movementSideways
  location: variable self of type Input

ERROR in src/main/java/client/java/com/example/freecam/mixin/ClientPlayerInputMixin.java at line 25: cannot find symbol
        self.jumping = false;
            ^
  symbol: variable jumping
  location: variable self of type Input

ERROR in src/main/java/client/java/com/example/freecam/mixin/ClientPlayerInputMixin.java at line 26: cannot find symbol
        self.sneaking = false;
            ^
  symbol: variable sneaking
  location: variable self of type Input

ERROR in   src/main/java/client/java/com/example/freecam/mixin/ClientPlayerInputMixin.java at line 23: cannot find symbol
          self.movementForward = 0.0f;
              ^
  symbol: variable movementForward
  location: variable self of type Input

ERROR in   src/main/java/client/java/com/example/freecam/mixin/ClientPlayerInputMixin.java at line 24: cannot find symbol
          self.movementSideways = 0.0f;
              ^
  symbol: variable movementSideways
  location: variable self of type Input

ERROR in   src/main/java/client/java/com/example/freecam/mixin/ClientPlayerInputMixin.java at line 25: cannot find symbol
          self.jumping = false;
              ^
  symbol: variable jumping
  location: variable self of type Input

ERROR in   src/main/java/client/java/com/example/freecam/mixin/ClientPlayerInputMixin.java at line 26: cannot find symbol
          self.sneaking = false;
              ^
  symbol: variable sneaking
  location: variable self of type Input


=== COMPILATION ERROR ANALYSIS ===

FILES TO INVESTIGATE:
  - src/main/java/client/java/com/example/freecam/mixin/ClientPlayerInputMixin.java
  -   src/main/java/client/java/com/example/freecam/mixin/ClientPlayerInputMixin.java

Please fix this error by creating a new artifact with the corrected code.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
Code
11 424
src/client/java/com/example/freecam/FreecamClient.java
package com.example.freecam;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.render.RenderTickCounter;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import org.lwjgl.glfw.GLFW;

@Environment(EnvType.CLIENT)
public class FreecamClient implements ClientModInitializer {

    public static final String MOD_ID = "freecam";

    // Speed in blocks per tick
    private static final float MOVE_SPEED = 0.5f;

    public static final KeyBinding.Category CATEGORY =
            KeyBinding.Category.create(Identifier.of(MOD_ID, "main"));

    public static final KeyBinding TOGGLE_KEY = KeyBindingHelper.registerKeyBinding(
            new KeyBinding("key.freecam.toggle", GLFW.GLFW_KEY_V, CATEGORY)
    );

    @Override
    public void onInitializeClient() {
        registerHud();
        registerTickEvent();
    }

    private void registerHud() {
        HudElementRegistry.addLast(
                Identifier.of(MOD_ID, "coords"),
                (DrawContext context, RenderTickCounter tickCounter) -> {
                    if (!FreecamState.isActive()) return;

                    MinecraftClient client = MinecraftClient.getInstance();
                    if (client.textRenderer == null) return;

                    double x = FreecamState.getX();
                    double y = FreecamState.getY();
                    double z = FreecamState.getZ();

                    String line1 = "§e[Freecam]";
                    String line2 = String.format("§fX: §b%.2f", x);
                    String line3 = String.format("§fY: §b%.2f", y);
                    String line4 = String.format("§fZ: §b%.2f", z);

                    int bgX1 = 4;
                    int bgY1 = 4;
                    int bgX2 = 90;
                    int bgY2 = 52;
                    context.fill(bgX1, bgY1, bgX2, bgY2, 0x80000000);

                    context.drawText(client.textRenderer, line1, 6, 6, 0xFFFFFF, true);
                    context.drawText(client.textRenderer, line2, 6, 17, 0xFFFFFF, true);
                    context.drawText(client.textRenderer, line3, 6, 27, 0xFFFFFF, true);
                    context.drawText(client.textRenderer, line4, 6, 37, 0xFFFFFF, true);
                }
        );
    }

    private void registerTickEvent() {
        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            // Toggle freecam on key press
            while (TOGGLE_KEY.wasPressed()) {
                toggleFreecam(client);
            }

            // Move the freecam camera each tick while active
            if (FreecamState.isActive() && client.player != null) {
                tickFreecamMovement(client);
            }
        });
    }

    private void toggleFreecam(MinecraftClient client) {
        if (client.player == null) return;

        boolean nowActive = !FreecamState.isActive();
        FreecamState.setActive(nowActive);

        if (nowActive) {
            // Seed the freecam position from the player's current eye position
            Vec3d eyePos = client.player.getEyePos();
            FreecamState.setPosition(eyePos.x, eyePos.y, eyePos.z);
            FreecamState.setRotation(client.player.getYaw(), client.player.getPitch());
        }
    }

    private void tickFreecamMovement(MinecraftClient client) {
        float yaw = FreecamState.getYaw();
        float pitch = FreecamState.getPitch();

        // Sync yaw/pitch from the player's mouse look (vanilla handles mouse look
        // against the player entity; we mirror it into FreecamState each tick)
        FreecamState.setRotation(client.player.getYaw(), client.player.getPitch());
        yaw = FreecamState.getYaw();
        pitch = FreecamState.getPitch();

        float yawRad = yaw * (float) (Math.PI / 180.0);
        float pitchRad = pitch * (float) (Math.PI / 180.0);

        // Forward/back direction (horizontal component of look vector)
        float sinYaw = MathHelper.sin(yawRad);
        float cosYaw = MathHelper.cos(yawRad);
        float cosPitch = MathHelper.cos(pitchRad);
        float sinPitch = MathHelper.sin(pitchRad);

        double dx = 0, dy = 0, dz = 0;

        // Forward (W) — move in look direction
        if (client.options.forwardKey.isPressed()) {
            dx -= sinYaw * cosPitch * MOVE_SPEED;
            dy -= sinPitch * MOVE_SPEED;
            dz += cosYaw * cosPitch * MOVE_SPEED;
        }
        // Back (S)
        if (client.options.backKey.isPressed()) {
            dx += sinYaw * cosPitch * MOVE_SPEED;
            dy += sinPitch * MOVE_SPEED;
            dz -= cosYaw * cosPitch * MOVE_SPEED;
        }
        // Strafe left (A)
        if (client.options.leftKey.isPressed()) {
            dx -= cosYaw * MOVE_SPEED;
            dz -= sinYaw * MOVE_SPEED;
        }
        // Strafe right (D)
        if (client.options.rightKey.isPressed()) {
            dx += cosYaw * MOVE_SPEED;
            dz += sinYaw * MOVE_SPEED;
        }
        // Up (Space)
        if (client.options.jumpKey.isPressed()) {
            dy += MOVE_SPEED;
        }
        // Down (Shift)
        if (client.options.sneakKey.isPressed()) {
            dy -= MOVE_SPEED;
        }

        FreecamState.setPosition(
                FreecamState.getX() + dx,
                FreecamState.getY() + dy,
                FreecamState.getZ() + dz
        );
    }
}