MeepMeep

make it gradle jar

Kodari AI
KodariNov 8 at 3:51 pm 1

Compile the Fabric mod as a JAR:

  1. Click Compile in the top bar (or ⋮ → Compile Project on phone).

  2. Click Compile in the window.

  3. When it succeeds, click Download on the build.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
12 898
src/main/java/com/spidersense/SpiderSenseClient.java
package com.spidersense;

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.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.hud.VanillaHudElements;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.particle.DustParticleEffect;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import org.lwjgl.glfw.GLFW;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class SpiderSenseClient implements ClientModInitializer {
    public static final String ID = "spidersense";
    public static final TrapScanner SCANNER = new TrapScanner();
    public static final int PULSE_TICKS = 45;
    public static final double WAVE_RADIUS = 40;
    /** Ticks since the current wave left your head (-1 = no wave). */
    public static volatile int pulse = -1;
    public static volatile long pulseStart = 0;
    public static volatile Threat pulseThreat = Threat.LOW;

    private static KeyBinding toggleKey, scanKey, radiusKey;
    private final Set<Long> announced = new HashSet<>();
    private ClientWorld lastWorld;
    private int tick, lastSerial;
    private boolean hadHigh;

    @Override
    public void onInitializeClient() {
        Config.load();
        KeyBinding.Category cat = KeyBinding.Category.create(Identifier.of(ID, "main"));
        toggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.spidersense.toggle", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_V, cat));
        scanKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.spidersense.scan", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_G, cat));
        radiusKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.spidersense.radius", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_B, cat));
        ClientTickEvents.END_CLIENT_TICK.register(this::onTick);
        HudElementRegistry.attachElementAfter(VanillaHudElements.MISC_OVERLAYS, Identifier.of(ID, "effects"), SenseHud::render);
    }

    public static void startPulse(Threat t) {
        pulse = 0;
        pulseThreat = t;
        pulseStart = System.currentTimeMillis();
    }

    private void onTick(MinecraftClient mc) {
        Config cfg = Config.get();
        while (toggleKey.wasPressed()) {
            cfg.enabled = !cfg.enabled;
            Config.save();
            say(mc, cfg.enabled ? "\u00a7cSpider Sense \u00a7aonline." : "\u00a7cSpider Sense \u00a77offline.");
            if (!cfg.enabled) { SCANNER.reset(); pulse = -1; }
        }
        while (scanKey.wasPressed()) {
            if (cfg.enabled && mc.player != null && !SCANNER.scanning) {
                SCANNER.forceScan();
                mc.player.sendMessage(Text.literal("\u00a7bSpider Sense is scanning..."), true);
            }
        }
        while (radiusKey.wasPressed()) {
            Config.cycleRadius();
            say(mc, "\u00a7cSpider Sense \u00a77scan radius: \u00a7b" + cfg.radius + " blocks");
        }
        if (!cfg.enabled || mc.world == null || mc.player == null) return;

        if (mc.world != lastWorld) { // joined a world / changed dimension
            lastWorld = mc.world;
            announced.clear();
            SCANNER.reset();
            lastSerial = SCANNER.serial;
            pulse = -1;
            hadHigh = false;
            say(mc, "\u00a7cSpider Sense \u00a7aonline. \u00a77Press G to scan, V to toggle, B for radius.");
        }

        SCANNER.tick(mc);
        tick++;

        int s = SCANNER.serial;
        boolean scanDone = s != lastSerial;
        if (scanDone) lastSerial = s;
        if (scanDone) alerts(mc, true); // only ever after a G scan

        if (pulse >= 0) {
            if (pulse <= PULSE_TICKS) {
                if (pulse == 0) {
                    mc.player.playSound(SoundEvents.BLOCK_AMETHYST_BLOCK_RESONATE, 0.8f, 0.55f);
                    if (pulseThreat == Threat.HIGH) mc.player.playSound(SoundEvents.BLOCK_BELL_RESONATE, 0.6f, 1.4f);
                }
                if (cfg.particles) wave(mc, pulse, pulseThreat);
                pulse++;
            } else {
                pulse = -1;
            }
        }
        if (cfg.particles && tick % 3 == 0) aura(mc);
        if (cfg.particles && tick % 4 == 0) beacons(mc);
    }

    // ---------- warnings ----------

    private static final String[] DIRS = {"east", "south-east", "south", "south-west", "west", "north-west", "north", "north-east"};

    private static String where(Cluster c, double px, double py, double pz) {
        double dx = c.x() - px, dz = c.z() - pz;
        int d = (int) Math.round(Math.hypot(dx, dz));
        int i = ((int) Math.round(Math.toDegrees(Math.atan2(dz, dx)) / 45.0) + 8) % 8;
        String vert = c.y() - py > 12 ? ", above you" : c.y() - py < -12 ? ", below you" : "";
        return d <= 2 ? "right next to you" : d + " blocks " + DIRS[i] + vert;
    }

    private static boolean isTrap(Cluster c) {
        return !c.label().equals("REDSTONE") && !c.label().equals("REDSTONE MACHINE");
    }

    private static String noun(Cluster c) {
        return switch (c.label()) {
            case "ARROW RAILGUN" -> "Arrow railgun";
            case "TNT CANNON" -> "TNT cannon";
            case "TNT TRAP" -> "TNT trap";
            case "REDSTONE", "REDSTONE MACHINE" -> "Redstone";
            default -> "Trap";
        };
    }

    private static long key(Cluster c) {
        return c.label().hashCode() * 31L + BlockPos.asLong(
                (int) Math.floor(c.x()) >> 4, (int) Math.floor(c.y()) >> 4, (int) Math.floor(c.z()) >> 4);
    }

    private static String plural(int n, String one, String many) { return n + " " + (n == 1 ? one : many); }

    private void alerts(MinecraftClient mc, boolean forced) {
        var p = mc.player;
        double px = p.getX(), py = p.getY(), pz = p.getZ();
        List<Cluster> all = new ArrayList<>(SCANNER.clusters);
        all.addAll(SCANNER.arrowClusters);
        List<Cluster> fresh = all; // every G press reports everything it finds

        if (fresh.isEmpty()) {
            say(mc, "\u00a7bYour Spider Sense is quiet. \u00a77Nothing suspicious nearby.");
            startPulse(Threat.LOW);
            return;
        }
        fresh.sort(Comparator.comparingInt((Cluster c) -> -c.threat().ordinal())
                .thenComparingDouble(c -> Math.hypot(c.x() - px, c.z() - pz)));
        Threat top = fresh.get(0).threat();

        if (top == Threat.HIGH) {
            int shown = 0;
            for (Cluster c : fresh) {
                if (c.threat() != Threat.HIGH || shown >= 3) break;
                say(mc, "\u00a7c\u00a7lYour Spider Sense is tingling! \u00a7r\u00a7c" + noun(c) + " detected \u00a77" + where(c, px, py, pz) + ".");
                shown++;
            }
            if (fresh.size() > shown) say(mc, "\u00a77...and " + (fresh.size() - shown) + " more detections nearby.");
            p.sendMessage(Text.literal("\u00a7cYour Spider Sense is tingling!"), true);
        } else if (fresh.size() == 1) {
            Cluster c = fresh.get(0);
            say(mc, "\u00a79Your Spider Sense is tingling. \u00a77" + noun(c) + " detected " + where(c, px, py, pz) + ".");
        } else {
            int traps = 0, red = 0;
            for (Cluster c : fresh) { if (isTrap(c)) traps++; else red++; }
            String parts = traps > 0 && red > 0 ? plural(traps, "trap", "traps") + " and " + plural(red, "redstone build", "redstone builds")
                    : traps > 0 ? plural(traps, "trap", "traps") : plural(red, "redstone build", "redstone builds");
            Cluster c = fresh.get(0);
            say(mc, "\u00a79Your Spider Sense is tingling. \u00a77" + parts + " detected nearby. Closest: "
                    + noun(c).toLowerCase() + ", " + where(c, px, py, pz) + ".");
        }
        startPulse(top);
    }

    private static void say(MinecraftClient mc, String s) {
        if (mc.player != null) mc.player.sendMessage(Text.literal(s), false);
    }

    // ---------- visuals ----------

    private static void spawn(MinecraftClient mc, ParticleEffect e, double x, double y, double z, double vx, double vy, double vz) {
        mc.particleManager.addParticle(e, x, y, z, vx, vy, vz);
    }

    private static double groundY(ClientWorld w, double x, double y, double z) {
        BlockPos.Mutable m = new BlockPos.Mutable();
        int bx = (int) Math.floor(x), bz = (int) Math.floor(z), by = (int) Math.floor(y) + 2;
        for (int i = 0; i < 9; i++) {
            m.set(bx, by - i, bz);
            if (!w.getBlockState(m).isAir()) return by - i + 1;