Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 145
src/main/java/dev/kodari/unquiet/TheUnquiet.java
package dev.kodari.unquiet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
public class TheUnquiet implements ModInitializer {
private final Map<UUID, Integer> countdowns = new HashMap<>();
@Override
public void onInitialize() {
ServerTickEvents.END_SERVER_TICK.register(server -> {
Set<UUID> online = new HashSet<>();
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
UUID id = player.getUuid();
online.add(id);
if (!player.isAlive() || player.isCreative() || player.isSpectator()) {
countdowns.remove(id);
continue;
}
int remaining = countdowns.computeIfAbsent(id, ignored -> nextDelay()) - 1;
if (remaining <= 0) {
haunt(player);
remaining = nextDelay();
}
countdowns.put(id, remaining);
}
countdowns.keySet().retainAll(online);
});
}
private static int nextDelay() {
return ThreadLocalRandom.current().nextInt(1800, 4201);
}
private static void haunt(ServerPlayerEntity player) {
ThreadLocalRandom random = ThreadLocalRandom.current();
switch (random.nextInt(4)) {
case 0 -> {
player.playSound(SoundEvents.ENTITY_ENDERMAN_STARE, SoundCategory.AMBIENT, 0.55f, 0.65f);
player.sendMessage(Text.literal("You feel watched."), true);
}
case 1 -> {
player.playSound(SoundEvents.ENTITY_WARDEN_HEARTBEAT, SoundCategory.AMBIENT, 0.8f, 0.8f);
player.addStatusEffect(new StatusEffectInstance(StatusEffects.DARKNESS, 100, 0, false, false, false));
}
case 2 -> {
player.playSound(SoundEvents.ENTITY_WARDEN_TENDRIL_CLICKS, SoundCategory.AMBIENT, 0.65f, 0.7f);
player.getServerWorld().spawnParticles(player, ParticleTypes.SCULK_SOUL, false,
player.getX(), player.getY() + 1.0, player.getZ(), 12, 2.0, 0.8, 2.0, 0.01);
}
default -> {
player.playSound(SoundEvents.ENTITY_WARDEN_DIG, SoundCategory.AMBIENT, 0.45f, 1.2f);
String[] whispers = {"Don't look behind you.", "It remembers you.", "You are not alone.", "Something is listening."};
player.sendMessage(Text.literal(whispers[random.nextInt(whispers.length)]), true);
}
}
}
}