Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
10 364
src/client/java/com/kodari/zoment/ZomentClient.java
package com.kodari.zoment;

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.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class ZomentClient implements ClientModInitializer {
    private static final Logger LOGGER = Logger.getLogger("Zoment");
    private static final float DEFAULT_MAX_ZOOM = 50.0f;
    private static final float ABSOLUTE_MAX_ZOOM = 100.0f;

    private static MinecraftClient client;
    private static KeyBinding zoomKey;
    private static Path configPath;
    private static Object observedWorld;
    private static float maxZoom = DEFAULT_MAX_ZOOM;
    private static float selectedZoom = 4.0f;
    private static float scrollSensitivity = 0.5f;
    private static float transitionSpeed = 14.0f;
    private static boolean smoothZoom = true;
    private static boolean toggleMode;
    private static boolean reverseScroll;
    private static boolean toggled;
    private static float previousZoom = 1.0f;
    private static float currentZoom = 1.0f;
    private static long lastUpdateNanos;

    @Override
    public void onInitializeClient() {
        client = MinecraftClient.getInstance();
        configPath = client.runDirectory.toPath().resolve("config").resolve("zoment.properties");
        loadConfig();
        zoomKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
                "key.zoment.zoom",
                InputUtil.GLFW_KEY_C,
                KeyBinding.Category.MISC
        ));
        observedWorld = client.world;
        lastUpdateNanos = System.nanoTime();
        ClientTickEvents.END_CLIENT_TICK.register(ZomentClient::tick);
    }

    private static void tick(MinecraftClient client) {
        if (zoomKey == null) {
            return;
        }

        long now = System.nanoTime();
        boolean worldChanged = client.world != observedWorld;
        if (worldChanged) {
            observedWorld = client.world;
        }

        while (zoomKey.wasPressed()) {
            if (toggleMode) {
                toggled = !toggled;
            }
        }

        boolean resetContext = worldChanged || client.world == null || client.player == null || client.currentScreen != null;
        if (resetContext) {
            toggled = false;
            previousZoom = 1.0f;
            currentZoom = 1.0f;
        }

        boolean active = !resetContext && (toggleMode ? toggled : zoomKey.isPressed());
        float targetZoom = active ? selectedZoom : 1.0f;
        float elapsedSeconds = Math.min((now - lastUpdateNanos) / 1_000_000_000.0f, 0.1f);
        lastUpdateNanos = now;

        previousZoom = currentZoom;
        if (!smoothZoom || resetContext) {
            currentZoom = targetZoom;
            previousZoom = currentZoom;
        } else {
            float blend = 1.0f - (float) Math.exp(-transitionSpeed * elapsedSeconds);
            currentZoom += (targetZoom - currentZoom) * blend;
            if (Math.abs(targetZoom - currentZoom) < 0.0005f) {
                currentZoom = targetZoom;
            }
        }
    }

    public static boolean handleZoomScroll(double verticalAmount) {
        if (!isZoomActive()) {
            return false;
        }

        float direction = (float) (reverseScroll ? -verticalAmount : verticalAmount);
        float updatedZoom = clamp(selectedZoom + direction * scrollSensitivity, 1.0f, maxZoom);
        if (updatedZoom != selectedZoom) {
            selectedZoom = updatedZoom;
            saveConfig();
        }
        return true;
    }

    public static float applyZoom(float fov, float tickProgress) {
        if (!Float.isFinite(fov) || fov <= 0.0f || (currentZoom == 1.0f && previousZoom == 1.0f)) {
            return fov;
        }

        float progress = Math.max(0.0f, Math.min(1.0f, tickProgress));
        float zoom = previousZoom + (currentZoom - previousZoom) * progress;
        if (zoom <= 1.0f) {
            return fov;
        }

        double halfAngle = Math.toRadians(fov * 0.5);
        return (float) Math.toDegrees(2.0 * Math.atan(Math.tan(halfAngle) / zoom));
    }

    private static boolean isZoomActive() {
        return zoomKey != null
                && client != null
                && client.world != null
                && client.player != null
                && client.currentScreen == null
                && (toggleMode ? toggled : zoomKey.isPressed());
    }

    private static void loadConfig() {
        Properties properties = new Properties();
        if (Files.exists(configPath)) {
            try (Reader reader = Files.newBufferedReader(configPath, StandardCharsets.UTF_8)) {
                properties.load(reader);
            } catch (IOException exception) {
                LOGGER.log(Level.WARNING, "Could not read Zoment configuration", exception);
            }
        }

        maxZoom = clamp(readFloat(properties, "maxZoom", DEFAULT_MAX_ZOOM), 1.0f, ABSOLUTE_MAX_ZOOM);
        selectedZoom = clamp(readFloat(properties, "zoomLevel", 4.0f), 1.0f, maxZoom);
        scrollSensitivity = clamp(readFloat(properties, "scrollSensitivity", 0.5f), 0.05f, 5.0f);
        transitionSpeed = clamp(readFloat(properties, "transitionSpeed", 14.0f), 0.1f, 40.0f);
        smoothZoom = Boolean.parseBoolean(properties.getProperty("smoothZoom", "true"));
        toggleMode = Boolean.parseBoolean(properties.getProperty("toggleMode", "false"));
        reverseScroll = Boolean.parseBoolean(properties.getProperty("reverseScroll", "false"));

        if (!Files.exists(configPath)) {
            saveConfig();
        }
    }

    private static void saveConfig() {
        Properties properties = new Properties();
        properties.setProperty("maxZoom", Float.toString(maxZoom));
        properties.setProperty("zoomLevel", Float.toString(selectedZoom));
        properties.setProperty("scrollSensitivity", Float.toString(scrollSensitivity));
        properties.setProperty("transitionSpeed", Float.toString(transitionSpeed));
        properties.setProperty("smoothZoom", Boolean.toString(smoothZoom));
        properties.setProperty("toggleMode", Boolean.toString(toggleMode));
        properties.setProperty("reverseScroll", Boolean.toString(reverseScroll));

        try {
            Files.createDirectories(configPath.getParent());
            try (Writer writer = Files.newBufferedWriter(configPath, StandardCharsets.UTF_8)) {
                properties.store(writer, "Zoment zoom configuration");
            }
        } catch (IOException exception) {
            LOGGER.log(Level.WARNING, "Could not save Zoment configuration", exception);
        }
    }

    private static float readFloat(Properties properties, String key, float fallback) {
        try {
            return Float.parseFloat(properties.getProperty(key, Float.toString(fallback)));
        } catch (NumberFormatException exception) {
            return fallback;
        }
    }

    private static float clamp(float value, float minimum, float maximum) {
        if (!Float.isFinite(value)) {
            return minimum;
        }
        return Math.max(minimum, Math.min(maximum, value));
    }
}