No chat to show

This project's files are all here to explore.

Files
animation_controllers
animations
entities
items
loot_tables
recipes
scripts
spawn_rules
xxxfelpc_RP
manifest.json
Code
17 1,309
scripts/main.js
import { world, system, Entity, Player, Dimension, Vector3 } from "@minecraft/server";

// ============ CONFIG ============
const CONFIG = {
    STALKER_ID: "xxxfelpc:stalker",
    HOLY_SWORD_ID: "xxxfelpc:holy_sword",
    ANCIENT_ARTIFACT_ID: "xxxfelpc:ancient_artifact",
    CURSED_RELIC_ID: "xxxfelpc:cursed_relic",
    STALKER_EYE_ID: "xxxfelpc:stalker_eye",
    LOOK_ANGLE_THRESHOLD: 20,
    CHASE_TRIGGER_TICKS: 200,
    TELEPORT_INTERVAL_TICKS: 600,
    STALKER_HEALTH: 150,
    HOLY_DAMAGE_MULTIPLIER: 3.5,
    CHASE_SPEED: 0.55,
    NORMAL_SPEED: 0.35,
    MIN_TELEPORT_DIST: 15,
    MAX_TELEPORT_DIST: 50,
    CHASE_ESCAPE_DIST: 64,
    JUMBSCARE_DIST: 2.5,
    FOG_INTENSITY: 0.6,
    NIGHT_AGGRESSION_CHANCE: 0.008
};

// ============ STATE ============
const stalkerData = new Map();
const artifactLocations = [];
let artifactCount = 0;
let stalkerDefeated = false;

// ============ UTILITY ============
function distance(a, b) {
    return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2 + (a.z - b.z) ** 2);
}

function getWorldAge() {
    const time = world.getTime();
    return time % 24000;
}

function isNight() {
    const t = getWorldAge();
    return t >= 13000 || t < 0;
}

function getRandomPosition(center, minDist, maxDist) {
    const angle = Math.random() * Math.PI * 2;
    const dist = minDist + Math.random() * (maxDist - minDist);
    const x = center.x + Math.cos(angle) * dist;
    const z = center.z + Math.sin(angle) * dist;
    return { x: Math.floor(x) + 0.5, y: Math.floor(center.y) + 1, z: Math.floor(z) + 0.5 };
}

function getSafeTeleportPos(center, minDist, maxDist) {
    const dim = world.getDimension("overworld");
    for (let i = 0; i < 10; i++) {
        const pos = getRandomPosition(center, minDist, maxDist);
        try {
            const block = dim.getBlock(pos);
            if (block && block.isAir) {
                const below = dim.getBlock({ x: pos.x, y: pos.y - 1, z: pos.z });
                if (below && !below.isAir) {
                    return pos;
                }
            }
        } catch (e) {
            continue;
        }
    }
    return getRandomPosition(center, minDist, maxDist);
}

function isPlayerLookingAt(player, entity) {
    const playerLoc = player.location;
    const entityLoc = entity.location;
    const toEntity = {
        x: entityLoc.x - playerLoc.x,
        y: (entityLoc.y + 1.2) - (playerLoc.y + 1.6),
        z: entityLoc.z - playerLoc.z
    };
    const lookVec = player.getViewDirection();
    const lenE = Math.sqrt(toEntity.x ** 2 + toEntity.y ** 2 + toEntity.z ** 2);
    const lenL = Math.sqrt(lookVec.x ** 2 + lookVec.y ** 2 + lookVec.z ** 2);
    if (lenE === 0 || lenL === 0) return false;
    const dot = (toEntity.x * lookVec.x + toEntity.y * lookVec.y + toEntity.z * lookVec.z) / (lenE * lenL);
    const angle = Math.acos(Math.max(-1, Math.min(1, dot))) * (180 / Math.PI);
    return angle < CONFIG.LOOK_ANGLE_THRESHOLD;
}

// ============ ATMOSPHERE ============
function applyFogToPlayer(player, intensity) {
    try {
        player.runCommand(`fog @s push xxxfelpc:stalker_fog ${intensity > 0.5 ? "dense" : "light"}`);
    } catch (e) { }
}

function clearFog(player) {
    try {
        player.runCommand("fog @s remove dense");
        player.runCommand("fog @s remove light");
    } catch (e) { }
}

function playSoundAt(pos, sound) {
    try {
        const dim = world.getDimension("overworld");
        dim.runCommand(`playsound ${sound} @a ${pos.x} ${pos.y} ${pos.z} 0.8 1`);
    } catch (e) { }
}

// ============ STALKER AI ============
function updateStalker(stalker, player, tick) {
    let data = stalkerData.get(stalker.id);
    if (!data) {
        data = {
            lookingTime: 0,
            isChasing: false,
            lastTeleportTick: 0,
            nightMode: false,
            lastSoundTick: 0,
            lastAmbientTick: 0
        };
        stalkerData.set(stalker.id, data);
    }

    const night = isNight();
    data.nightMode = night;

    // Stalker already dead or not spawned
    if (!stalker.isValid()) {
        stalkerData.delete(stalker.id);
        return;
    }

    const dist = distance(stalker.location, player.location);

    // ===== LOOKING DETECTION =====
    const isLooking = isPlayerLookingAt(player, stalker);

    if (isLooking && !data.isChasing) {
        data.lookingTime++;

        // Warning messages as player stares
        if (data.lookingTime === 60) {
            player.sendMessage("§7[§8...§7] §8Algo está observando...");
            playSoundAt(stalker.location, "xxxfelpc.ambient_whisper");
        } else if (data.lookingTime === 100) {
            player.sendMessage("§7[§4⚠§7] §8Não olhe...");
            playSoundAt(stalker.location, "xxxfelpc.stalker_breath");
        } else if (data.lookingTime === 140) {
            player.sendMessage("§7[§4⚠⚠§7] §4Ele te viu...");
            playSoundAt(stalker.location, "xxxfelpc.ambient_growl");
        } else if (data.lookingTime === 180) {
            player.sendMessage("§4§lCORRA! CORRA AGORA!");
        }

        // TRIGGER CHASE after 10 seconds (200 ticks)
        if (data.lookingTime >= CONFIG.CHASE_TRIGGER_TICKS) {
            data.isChasing = true;
            data.lookingTime = 0;
            triggerChase(stalker, player);
        }

        // Slowly approach while being watched
        if (data.lookingTime > 40 && Math.random() < 0.05) {
            const closer = getSafeTeleportPos(player.location, 8, 16);
            stalker.teleport(closer);
        }
    } else if (!isLooking && !data.isChasing) {
        // Looking away reduces tension
        data.lookingTime = Math.max(0, data.lookingTime - 3);
    }

    // ===== CHASE BEHAVIOR =====
    if (data.isChasing) {
        stalker.triggerEvent("xxxfelpc:become_aggressive");

        // Jumpscare when very close
        if (dist < CONFIG.JUMBSCARE_DIST) {
            triggerJumpscare(player, stalker);
        }

        // Chase sounds
        if (tick - data.lastSoundTick > 40) {
            data.lastSoundTick = tick;
            const sounds = ["xxxfelpc.stalker_chase", "xxxfelpc.stalker_breath", "xxxfelpc.ambient_growl"];
            playSoundAt(stalker.location, sounds[Math.floor(Math.random() * sounds.length)]);
        }

        // Escape condition - player got too far
        if (dist > CONFIG.CHASE_ESCAPE_DIST) {
            data.isChasing = false;
            stalker.triggerEvent("xxxfelpc:calm_down");
            player.sendMessage("§7[§a...§7] §8Ele te perdeu... por enquanto.");
            const vanishPos = getSafeTeleportPos(player.location, 30, 60);
            stalker.teleport(vanishPos);
            playSoundAt(vanishPos, "xxxfelpc.stalker_disappear");
        }
    }