Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
14 946
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.sound.PositionedSoundInstance;
import net.minecraft.sound.SoundEvent;
import net.minecraft.client.util.InputUtil;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import org.lwjgl.glfw.GLFW;
import java.util.*;
public class SpiderSenseClient implements ClientModInitializer {
public static final String ID = "spidersense";
public static final TrapScanner SCANNER = new TrapScanner();
public static final int PULSE_TICKS = 30;
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 volatile boolean pulseSound = true;
private static final SoundEvent SENSE_SHORT = SoundEvent.of(Identifier.of(ID, "spider_sense"));
public static final double[] camPrev = new double[6], camCur = new double[6];
public static volatile long tickNanos = System.nanoTime();
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) { startPulse(t, true); }
public static void startPulse(Threat t, boolean sound) {
pulseSound = sound;
pulse = 0;
pulseThreat = t;
pulseStart = System.currentTimeMillis();
}
private void onTick(MinecraftClient mc) {
Config cfg = Config.get();
if (mc.player != null) {
System.arraycopy(camCur, 0, camPrev, 0, 6);
camCur[0] = mc.player.getX(); camCur[1] = mc.player.getY(); camCur[2] = mc.player.getZ();
camCur[3] = mc.player.getYaw(); camCur[4] = mc.player.getPitch();
camCur[5] = mc.player.getEyeY() - mc.player.getY();
if (mc.world != lastWorld) System.arraycopy(camCur, 0, camPrev, 0, 6);
tickNanos = System.nanoTime();
}
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 && pulseSound && cfg.soundVolume > 0) {
// the Spider Sense tingle (plays only when something was actually detected)
mc.getSoundManager().play(PositionedSoundInstance.ui(SENSE_SHORT, 1.0f, cfg.soundVolume));
}
pulse++;
} else {
pulse = -1;
}
}
}
// ---------- 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 "SUSPICIOUS ARROWS" -> "Suspicious arrows";
case "TNT" -> "TNT";
case "TNT MINECART" -> "TNT minecart";
case "PRIMED TNT" -> "Primed TNT";
case "REDSTONE", "REDSTONE MACHINE" -> "Redstone";
default -> "Trap";
};
}
/** Sentence body without the final period. */
private static String describe(Cluster c, String where) {
if (c.label().equals("SUSPICIOUS ARROWS")) return "Suspicious arrows have gathered in one spot, " + where;
return noun(c) + " detected " + where;
}
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, false);
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" + describe(c, "\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" + describe(c, 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) + ", " + where(c, px, py, pz) + ".");
}
startPulse(top);
}