Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
15 849
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 org.lwjgl.glfw.GLFW;
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 = 48;
/** Ticks since the current scan pulse started (-1 = no pulse). */
public static volatile int pulse = -1;
private static KeyBinding toggleKey, scanKey, radiusKey;
private int tick;
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, "radar"), SenseHud::render);
}
private void onTick(MinecraftClient mc) {
Config cfg = Config.get();
while (toggleKey.wasPressed()) {
cfg.enabled = !cfg.enabled;
Config.save();
msg(mc, cfg.enabled ? "\u00a7cSpider Sense \u00a7aONLINE" : "\u00a7cSpider Sense \u00a77offline");
if (!cfg.enabled) { SCANNER.reset(); SoundSense.clear(); pulse = -1; }
}
while (scanKey.wasPressed()) { if (cfg.enabled) SCANNER.forceScan(); }
while (radiusKey.wasPressed()) {
Config.cycleRadius();
msg(mc, "\u00a7cSpider Sense \u00a77radius \u00a7b" + cfg.radius + " blocks");
}
if (!cfg.enabled || mc.world == null || mc.player == null) return;
SCANNER.tick(mc);
tick++;
// the pulse clock always runs (HUD wave depends on it), particles are optional
if (pulse >= 0) {
if (pulse <= PULSE_TICKS) {
if (pulse == 0) {
mc.player.playSound(SoundEvents.BLOCK_AMETHYST_BLOCK_RESONATE, 0.7f, 0.6f);
}
if (cfg.particles) wave(mc, pulse);
pulse++;
} else {
pulse = -1;
}
}
if (cfg.particles && tick % 4 == 0) beacons(mc);
// alarm bell the moment a new RUN-level threat shows up
boolean high = SCANNER.clusters.stream().anyMatch(c -> c.threat() == Threat.HIGH);
if (high && !hadHigh) mc.player.playSound(SoundEvents.BLOCK_BELL_RESONATE, 0.6f, 1.4f);
hadHigh = high;
}
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;
}
return y;
}
/** One tick of the flowing spider-web wave. */
private void wave(MinecraftClient mc, int pulse) {
ClientWorld w = mc.world;
var p = mc.player;
var rnd = ThreadLocalRandom.current();
double px = p.getX(), py = p.getY(), pz = p.getZ();
double prog = pulse / (double) PULSE_TICKS, fade = 1.0 - prog;
double r = WAVE_RADIUS * (1 - Math.pow(1 - prog, 2.2)); // ease-out surge
if (pulse == 0) {
for (int i = 0; i < 48; i++) {
double a = rnd.nextDouble() * Math.PI * 2, up = 0.05 + rnd.nextDouble() * 0.25, sp = 0.15 + rnd.nextDouble() * 0.25;
spawn(mc, ParticleTypes.GLOW, px, py + 1.0, pz, Math.cos(a) * sp, up, Math.sin(a) * sp);
}
for (int i = 0; i < 20; i++)
spawn(mc, ParticleTypes.ELECTRIC_SPARK, px, py + 1.2, pz,
(rnd.nextDouble() - 0.5) * 0.6, rnd.nextDouble() * 0.3, (rnd.nextDouble() - 0.5) * 0.6);
}
// twin helix of web-thread light spiralling up around you
if (pulse < 30) {
for (int j = 0; j < 2; j++) {
double a = pulse * 0.55 + j * Math.PI;
double hy = py + 0.1 + pulse * 0.1;
spawn(mc, new DustParticleEffect(j == 0 ? 0xFF1F3D : 0x9FDFFF, 1.1f),
px + Math.cos(a) * 1.3, hy, pz + Math.sin(a) * 1.3, 0, 0, 0);
spawn(mc, ParticleTypes.GLOW, px + Math.cos(a) * 1.3, hy, pz + Math.sin(a) * 1.3, 0, 0.02, 0);
}
}
// three trailing wave fronts: foam crest -> blue body -> red wake
for (int k = 0; k < 3; k++) {
double rk = r - k * 2.2;
if (rk < 1.2) continue;
int n = Math.min(120, (int) (rk * 5) + 24);
int col = k == 0 ? 0x9FDFFF : k == 1 ? 0x3D7BFF : 0xFF1F3D;
float sc = (float) (0.7 + fade * (k == 0 ? 1.1 : 0.7));
for (int i = 0; i < n; i++) {
double a = i * Math.PI * 2 / n + k * 0.4;
double cx = Math.cos(a), sz = Math.sin(a);
double x = px + cx * rk, z = pz + sz * rk;
double gy = groundY(w, x, py, z);
double swell = Math.sin(a * 6 + pulse * 0.45 - k * 0.8) * 0.35 + 0.45 + k * 0.2 + prog * 0.6;
spawn(mc, new DustParticleEffect(col, sc), x, gy + swell, z, 0, 0, 0);
if (k == 0 && i % 6 == 0)
spawn(mc, ParticleTypes.ELECTRIC_SPARK, x, gy + swell + 0.2, z, cx * 0.05, 0.04, sz * 0.05);
if (k == 1 && i % 9 == 0)
spawn(mc, ParticleTypes.GLOW, x, gy + swell, z, 0, 0.05, 0);
}
}
// web spokes: 8 radial threads trailing the crest, so the wave reads as a spider web
for (int s = 0; s < 8; s++) {
double a = s * Math.PI / 4 + prog * 0.6; // spokes slowly twist as they travel
for (double d = Math.max(1.5, r - 7); d < r - 1; d += 1.6) {
double x = px + Math.cos(a) * d, z = pz + Math.sin(a) * d;
spawn(mc, new DustParticleEffect(0xDDEEFF, (float) (0.5 + fade * 0.5)), x, groundY(w, x, py, z) + 0.15, z, 0, 0, 0);
}
}
// floating droplets drifting up inside the wave
for (int i = 0; i < 8; i++) {
double a = rnd.nextDouble() * Math.PI * 2, rr = r * Math.sqrt(rnd.nextDouble());
spawn(mc, ParticleTypes.GLOW, px + Math.cos(a) * rr, py + 0.3 + rnd.nextDouble() * 2.5,
pz + Math.sin(a) * rr, 0, 0.03 + rnd.nextDouble() * 0.03, 0);
}
// the wave washes over threats and they flash as it passes
int flashed = 0;
for (Cluster c : SCANNER.clusters) {
if (flashed >= 16) break;
double d = Math.hypot(c.x() - px, c.z() - pz);
if (d > WAVE_RADIUS + 3 || Math.abs(d - r) > 2.5) continue;
flashed++;
for (int i = 0; i < 14; i++)
spawn(mc, ParticleTypes.ELECTRIC_SPARK, c.x(), c.y() + 0.5, c.z(),
(rnd.nextDouble() - 0.5) * 0.4, 0.1 + rnd.nextDouble() * 0.3, (rnd.nextDouble() - 0.5) * 0.4);
for (int i = 0; i < 10; i++)
spawn(mc, new DustParticleEffect(c.threat().rgb, 1.8f),
c.x() + (rnd.nextDouble() - 0.5) * 0.6, c.y() + i * 0.8, c.z() + (rnd.nextDouble() - 0.5) * 0.6, 0, 0, 0);
}
}
private void beacons(MinecraftClient mc) {
var p = mc.player;
var rnd = ThreadLocalRandom.current();
int shown = 0;
for (Cluster c : SCANNER.clusters) {
if (shown >= 12) break;
double dx = c.x() - p.getX(), dz = c.z() - p.getZ();
if (dx * dx + dz * dz > 40 * 40) continue;
shown++;
for (int i = 0; i < 6; i++) {
double y = c.y() + 1 + i * (c.threat() == Threat.HIGH ? 2.0 : 1.0);
spawn(mc, new DustParticleEffect(c.threat().rgb, c.threat() == Threat.HIGH ? 1.6f : 1.0f),
c.x() + (rnd.nextDouble() - 0.5) * 0.4, y, c.z() + (rnd.nextDouble() - 0.5) * 0.4, 0, 0, 0);
}
if (c.threat() == Threat.HIGH) spawn(mc, ParticleTypes.END_ROD, c.x(), c.y() + 1, c.z(), 0, 0.12, 0);
}
}
private static void msg(MinecraftClient mc, String s) {
if (mc.player != null) mc.player.sendMessage(Text.literal(s), true);