Build a Fabric mod for Minecraft 1.21.11 that lets the player use a connected Sony PS5 DualSense controller (assuming the OS exposes it as a standard gamepad) to play Minecraft Java. Implement reliable controller detection/polling and map left stick to movement, right stick to camera look, face buttons to jump/interact/attack/sneak or sprint, triggers to attack/use as appropriate, and buttons for inventory/pause/hotbar navigation. Use the project's existing Fabric/Loom setup and compatible APIs; add only needed dependencies and configuration. Avoid breaking normal keyboard/mouse input, avoid repeated toggles from held buttons, and handle controller disconnect gracefully. Include sensible deadzones and document supported connection/setup plus controls in the project README or mod description. Inspect existing project files first; make the implementation integrated with this project, and do not run builds or claim build verification. Done means the mod source/config/docs are updated and ready for workspace compilation.

Kodari AI
KodariOct 9 at 2:11 pm 3
DualSenseControllerMod
This Fabric mod for Minecraft 1.21.11 enables PlayStation 5 DualSense controller support, allowing players to use the gamepad for full gameplay. It implements reliable controller detection and polling, mapping the left stick to movement, right stick to camera look, face buttons to jump, interact, attack, and sneak, triggers to attack or use items, and additional buttons for inventory, pause, and hotbar navigation. The mod includes deadzone configuration, prevents repeated toggles from held buttons, gracefully handles controller disconnects, and preserves normal keyboard and mouse input. It provides comprehensive documentation for setup and controls, ensuring a seamless and integrated gaming experience.
  1. Set up the Fabric 1.21.11/Loom project and client-only entrypoint.

  2. Poll standard GLFW-mapped gamepads, apply deadzones, map controls, and release cleanly on disconnect.

  3. Document controller setup and controls.

Created a client-only Fabric 1.21.11 mod that polls GLFW-mapped gamepads, applies stick deadzones, and maps DualSense controls to movement, looking, actions, hotbar navigation, inventory, and pause. Held actions are released when the controller disconnects; keyboard and mouse input are preserved alongside controller input.

Added the Loom/Fabric project configuration and a README with setup steps and controls. Build verification was not run, as requested.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
6 287
src/client/java/com/kodari/dualsense/DualSenseControllerClient.java
package com.kodari.dualsense;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Click;
import net.minecraft.client.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.client.input.KeyInput;
import net.minecraft.client.input.MouseInput;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.util.Window;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWGamepadState;

public final class DualSenseControllerClient implements ClientModInitializer {
    private static final float STICK_DEADZONE = 0.18f;
    private static final float TRIGGER_DEADZONE = 0.15f;
    private static final float LOOK_SPEED = 3.0f;

    private boolean previousStart;
    private boolean previousBack;
    private boolean previousHotbarPrevious;
    private boolean previousHotbarNext;

    @Override
    public void onInitializeClient() {
        ClientTickEvents.END_CLIENT_TICK.register(this::tick);
    }

    private void tick(MinecraftClient client) {
        int joystick = findGamepad();
        if (joystick < 0) {
            updateController(client, null);
            return;
        }

        GLFWGamepadState state = GLFWGamepadState.create();
        try {
            updateController(client, GLFW.glfwGetGamepadState(joystick, state) ? state : null);
        } finally {
            state.free();
        }
    }

    private static int findGamepad() {
        for (int joystick = 0; joystick <= GLFW.GLFW_JOYSTICK_LAST; joystick++) {
            if (GLFW.glfwJoystickPresent(joystick) && GLFW.glfwJoystickIsGamepad(joystick)) {
                return joystick;
            }
        }
        return -1;
    }

    private void updateController(MinecraftClient client, GLFWGamepadState state) {
        boolean start = isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_START);
        boolean back = isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_BACK);
        boolean hotbarPrevious = isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_LEFT_BUMPER)
                || isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_DPAD_LEFT);
        boolean hotbarNext = isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER)
                || isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_DPAD_RIGHT);

        if (client.player != null && start && !previousStart) {
            if (client.currentScreen instanceof GameMenuScreen) {
                client.setScreen(null);
            } else if (client.currentScreen == null) {
                client.setScreen(new GameMenuScreen(true));
            }
        }
        if (client.player != null && back && !previousBack) {
            if (client.currentScreen instanceof InventoryScreen) {
                client.setScreen(null);
            } else if (client.currentScreen == null) {
                client.setScreen(new InventoryScreen(client.player));
            }
        }

        boolean inWorld = client.player != null && client.currentScreen == null;
        if (inWorld && hotbarPrevious != hotbarNext
                && ((hotbarPrevious && !previousHotbarPrevious) || (hotbarNext && !previousHotbarNext))) {
            int direction = hotbarPrevious ? -1 : 1;
            int selectedSlot = client.player.getInventory().getSelectedSlot();
            client.player.getInventory().setSelectedSlot(Math.floorMod(selectedSlot + direction, 9));
        }

        Stick leftStick = readStick(state, GLFW.GLFW_GAMEPAD_AXIS_LEFT_X, GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y);
        Stick rightStick = readStick(state, GLFW.GLFW_GAMEPAD_AXIS_RIGHT_X, GLFW.GLFW_GAMEPAD_AXIS_RIGHT_Y);
        if (inWorld) {
            client.player.setYaw(client.player.getYaw() + rightStick.x() * LOOK_SPEED);
            client.player.setPitch(Math.max(-90.0f,
                    Math.min(90.0f, client.player.getPitch() + rightStick.y() * LOOK_SPEED)));
        }

        setPressed(client, client.options.forwardKey, inWorld && leftStick.y() < 0.0f);
        setPressed(client, client.options.backKey, inWorld && leftStick.y() > 0.0f);
        setPressed(client, client.options.leftKey, inWorld && leftStick.x() < 0.0f);
        setPressed(client, client.options.rightKey, inWorld && leftStick.x() > 0.0f);
        setPressed(client, client.options.jumpKey,
                inWorld && isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_A));
        setPressed(client, client.options.sneakKey,
                inWorld && isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_B));
        setPressed(client, client.options.sprintKey,
                inWorld && isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_LEFT_THUMB));
        setPressed(client, client.options.attackKey,
                inWorld && (isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_X)
                        || isTriggerDown(state, GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER)));
        setPressed(client, client.options.useKey,
                inWorld && (isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_Y)
                        || isTriggerDown(state, GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER)));

        previousStart = start;
        previousBack = back;
        previousHotbarPrevious = hotbarPrevious;
        previousHotbarNext = hotbarNext;
    }

    private static boolean isButtonDown(GLFWGamepadState state, int button) {
        return state != null && state.buttons(button) == GLFW.GLFW_PRESS;
    }

    private static boolean isTriggerDown(GLFWGamepadState state, int axis) {
        return state != null && (state.axes(axis) + 1.0f) * 0.5f > TRIGGER_DEADZONE;
    }

    private static Stick readStick(GLFWGamepadState state, int xAxis, int yAxis) {
        if (state == null) {
            return new Stick(0.0f, 0.0f);
        }

        float x = state.axes(xAxis);
        float y = state.axes(yAxis);
        float magnitude = (float) Math.sqrt(x * x + y * y);
        if (magnitude <= STICK_DEADZONE) {
            return new Stick(0.0f, 0.0f);
        }

        float scaledMagnitude = Math.min(1.0f, (magnitude - STICK_DEADZONE) / (1.0f - STICK_DEADZONE));
        float scale = scaledMagnitude / magnitude;
        return new Stick(x * scale, y * scale);
    }

    private static void setPressed(MinecraftClient client, KeyBinding binding, boolean controllerPressed) {
        binding.setPressed(controllerPressed || isPhysicallyPressed(client.getWindow(), binding));
    }

    private static boolean isPhysicallyPressed(Window window, KeyBinding binding) {
        for (int key = 0; key <= GLFW.GLFW_KEY_LAST; key++) {
            if (!InputUtil.isKeyPressed(window, key)) {
                continue;
            }

            int scanCode = GLFW.glfwGetKeyScancode(key);
            if (binding.matchesKey(new KeyInput(key, scanCode, 0))
                    || (scanCode != 0 && binding.matchesKey(new KeyInput(GLFW.GLFW_KEY_UNKNOWN, scanCode, 0)))) {
                return true;
            }
        }

        for (int button = 0; button <= GLFW.GLFW_MOUSE_BUTTON_LAST; button++) {
            if (GLFW.glfwGetMouseButton(window.getHandle(), button) == GLFW.GLFW_PRESS
                    && binding.matchesMouse(new Click(0.0, 0.0, new MouseInput(button, 0)))) {
                return true;
            }
        }
        return false;
    }

    private record Stick(float x, float y) {
    }
}