Files
mnt
BlackjackEngine.java
BlackjackModule.java
build.gradle
ClientPlayerEntityMixin.java
ClientPlayerInteractionManagerMixin.java
ClientPlayNetworkHandlerMixin.java
DispenserUtil.java
dispenseryoink.mixins.json
DispenserYoinkClient.java
en_us.json
fabric.mod.json
FiftyFiftyModule.java
gradle-wrapper.properties
ScreenHandlerMixin.java
settings.gradle
SleightHandler.java
SlotSelection.java
SpudClient.java
spudclient.mixins.json
SpudConfig.java
SpudScreen.java
SpudState.java
UniversalPullModule.java
YoinkHandler.java
Code
30 2,821
SpudClient.java
package com.spud.client;

import com.spud.client.gui.SpudScreen;
import com.spud.client.module.games.BlackjackModule;
import com.spud.client.module.universal.UniversalPullModule;
import com.spud.client.util.SpudConfig;
import com.spud.client.util.SpudState;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import org.lwjgl.glfw.GLFW;

/**
 * SpudClient — entrypoint.
 *
 * All keybinds are raw GLFW — not registered with Fabric's KeyBindingHelper.
 * They NEVER appear in MC's Controls settings. Configured only in F7 GUI.
 *
 * Every tick:
 *   1. Read raw GLFW key states
 *   2. Detect just-pressed keys (rising edge only)
 *   3. Route to appropriate module or GUI action
 */
public class SpudClient implements ClientModInitializer {

    // Track key state from previous tick for rising-edge detection
    private static final boolean[] prevKeyState = new boolean[GLFW.GLFW_KEY_LAST + 1];

    @Override
    public void onInitializeClient() {
        SpudConfig.load();

        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            if (client.player == null || client.world == null) return;
            long window = client.getWindow().getHandle();

            SpudState.keysJustPressed.clear();

            // Detect rising edges on all keys we care about
            detectAndRoute(client, window);
        });
    }

    private static void detectAndRoute(MinecraftClient client, long window) {
        SpudConfig cfg = SpudConfig.get();

        // ── Check every configured key for rising edge ────────────────────
        checkKey(window, cfg.guiKey, () -> {
            if (client.currentScreen instanceof SpudScreen) {
                client.currentScreen.close();
            } else {
                client.setScreen(new SpudScreen());
            }
        });

        // ── If GUI is open and in bind-capture mode, consume any key press ─
        if (client.currentScreen instanceof SpudScreen spudScreen) {
            if (SpudState.capturingBindFor != null) {
                for (int key = 32; key <= GLFW.GLFW_KEY_LAST; key++) {
                    if (key == cfg.guiKey) continue;
                    boolean down = GLFW.glfwGetKey(window, key) == GLFW.GLFW_PRESS;
                    boolean prev = key < prevKeyState.length && prevKeyState[key];
                    if (down && !prev) {
                        spudScreen.onKeyCaptured(key);
                        break;
                    }
                }
            }
            // Update prev state and return — don't fire game binds while GUI open
            updatePrevState(window);
            return;
        }

        // ── Slot keys 1-9 (universal force pull) ─────────────────────────
        for (int i = 0; i < 9; i++) {
            int key = cfg.slotKeys[i];
            final int slot = i;
            if (key > 0) checkKey(window, key, () ->
                    UniversalPullModule.onSlotKeyPressed(client, slot));
        }

        // ── Dual dispenser higher output ──────────────────────────────────
        if (cfg.dualLeftHighKey > 0)
            checkKey(window, cfg.dualLeftHighKey, () ->
                    UniversalPullModule.onDualHigherPressed(client, true));
        if (cfg.dualRightHighKey > 0)
            checkKey(window, cfg.dualRightHighKey, () ->
                    UniversalPullModule.onDualHigherPressed(client, false));

        // ── Blackjack binds ───────────────────────────────────────────────
        if (cfg.bjEndViewerTurnKey > 0)
            checkKey(window, cfg.bjEndViewerTurnKey, BlackjackModule::endViewerTurn);
        if (cfg.bjViewerBustKey > 0)
            checkKey(window, cfg.bjViewerBustKey, BlackjackModule::viewerBust);
        if (cfg.bjNewRoundKey > 0)
            checkKey(window, cfg.bjNewRoundKey, () -> {
                // New round — use stored dispenser pos
                String stored = cfg.fiftyFiftyDisp;
                if (!stored.isEmpty()) {
                    String[] parts = stored.split(",");
                    BlackjackModule.startRound(new net.minecraft.util.math.BlockPos(
                            Integer.parseInt(parts[0]),
                            Integer.parseInt(parts[1]),
                            Integer.parseInt(parts[2])
                    ));
                }
            });
        if (cfg.bjEndRoundKey > 0)
            checkKey(window, cfg.bjEndRoundKey, BlackjackModule::endRound);

        // ── Blackjack: host draw prep (bound to same key as force pull or separate) ─
        // When in HOST_TURN phase, any lever interact = host draw
        // This is handled in the lever interact intercept — no separate key needed

        updatePrevState(window);
    }

    private static void checkKey(long window, int key, Runnable action) {
        if (key <= 0 || key > GLFW.GLFW_KEY_LAST) return;
        boolean down = GLFW.glfwGetKey(window, key) == GLFW.GLFW_PRESS;
        boolean prev = prevKeyState[key];
        if (down && !prev) action.run();
    }

    private static void updatePrevState(long window) {
        for (int key = 32; key <= GLFW.GLFW_KEY_LAST; key++) {
            if (key < prevKeyState.length)
                prevKeyState[key] = GLFW.glfwGetKey(window, key) == GLFW.GLFW_PRESS;
        }
    }
}