Files
villcas_nights_behavior
villcas_nights_resource
README.md
Code
11 635
villcas_nights_behavior/scripts/main.js
import { system, world } from "@minecraft/server";
const VILLCA_ID = "villca:the_villca";
const AMULET_ID = "villca:taming_amulet";
const states = new Map();
const dimensions = ["overworld", "nether", "the_end"];
const buildBlocks = new Set([
"stone",
"cobblestone",
"deepslate",
"bricks",
"glass",
"oak_planks",
"spruce_planks",
"dark_oak_planks",
"quartz_block",
"iron_block",
"gold_block",
"diamond_block",
"redstone_block",
"glowstone"
]);
function stateFor(villca) {
let state = states.get(villca.id);
if (!state) {
state = { boredom: 100, anger: 0 };
states.set(villca.id, state);
}
return state;
}
function isValid(entity) {
try {
return entity.isValid();
} catch {
return false;
}
}
function hasComponent(entity, id) {
try {
return entity.getComponent(id) !== undefined;
} catch {
return false;
}
}
function isTamed(villca) {
try {
return villca.hasTag("villca_tamed") || hasComponent(villca, "minecraft:is_tamed");
} catch {
return false;
}
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function nearestVillca(player, maxDistance = 24) {
let nearest;
let nearestDistance = maxDistance * maxDistance;
for (const villca of player.dimension.getEntities({ type: VILLCA_ID })) {
const dx = villca.location.x - player.location.x;
const dy = villca.location.y - player.location.y;
const dz = villca.location.z - player.location.z;
const distance = dx * dx + dy * dy + dz * dz;
if (distance <= nearestDistance) {
nearest = villca;
nearestDistance = distance;
}
}
return nearest;
}
function reply(player, text) {
player.sendMessage(`§dVillca§r: ${text}`);
}
function conversationReply(message) {
const clean = message.replace(/§./g, "").trim().slice(0, 180);
const lower = clean.toLowerCase();
if (lower.includes("who are you")) return "I am Villca. I was built to watch the dark, not to trust it.";
if (lower.includes("how are you")) return "My gears are turning. Keep talking and I may stay calm.";
if (lower.includes("help")) return "Bring me the Spirit Taming Amulet. Do not assume I will accept it.";
if (lower.includes("hello") || lower.includes("hi")) return `Hello, ${clean.split(/\s+/)[0] || "there"}. I can hear you.`;
if (lower.includes("thank")) return "You are welcome. For now.";
if (clean.endsWith("?")) return `That is a dangerous question. My answer is: maybe. You said, \"${clean}\"`;
return `I heard you say, \"${clean}\". Keep talking and I might tell you more.`;
}
function tryBuild(player, villca, args) {
if (!isTamed(villca)) {
reply(player, "I build for nobody while I am untamed.");
return;
}
const block = (args[0] || "stone").toLowerCase().replace(/^minecraft:/, "");
const width = clamp(Number.parseInt(args[1] || "5", 10) || 5, 1, 16);
const height = clamp(Number.parseInt(args[2] || "3", 10) || 3, 1, 16);
const depth = clamp(Number.parseInt(args[3] || "5", 10) || 5, 1, 16);
if (!buildBlocks.has(block)) {
reply(player, "I will only build with a safe block from my approved list.");
return;
}
const x = Math.floor(player.location.x);
const y = Math.floor(player.location.y);
const z = Math.floor(player.location.z);
try {
player.dimension.runCommand(`fill ${x} ${y} ${z} ${x + width - 1} ${y + height - 1} ${z + depth - 1} minecraft:${block}`);
reply(player, `Done. I built a ${width}x${height}x${depth} ${block} room.`);
} catch {
reply(player, "The world refused that build here.");
}
}
function handleChat(player, message) {
const match = message.trim().match(/^(?:!villca|villca)\s*(.*)$/i);
if (!match) return;
const villca = nearestVillca(player);
if (!villca) {
reply(player, "I am not close enough to hear you. Summon me first.");
return;
}
const text = match[1].trim();
const state = stateFor(villca);
if (!text || text.toLowerCase() === "help") {
reply(player, "Say `!villca <message>`, `!villca status`, or `!villca build <block> <width> <height> <depth>`.");
return;
}
if (text.toLowerCase() === "status") {
reply(player, `Boredom: ${Math.round(state.boredom)}/100. Anger: ${Math.round(state.anger)}/100.`);
return;
}
if (text.toLowerCase().startsWith("build ")) {
tryBuild(player, villca, text.split(/\s+/).slice(1));
return;
}
if (!isTamed(villca)) {
state.anger = clamp(state.anger + 5, 0, 100);
reply(player, "I am not your friend. Keep your voice low and your amulet ready.");
return;
}
state.boredom = clamp(state.boredom + 12, 0, 100);
state.anger = clamp(state.anger - 6, 0, 100);
system.runTimeout(() => {
if (isValid(player) && isValid(villca)) reply(player, conversationReply(text));
}, 10);
}
function consumeAmulet(player) {
try {
const inventory = player.getComponent("minecraft:inventory");
const container = inventory?.container;
if (!container) return;
for (let slot = 0; slot < container.size; slot++) {
const stack = container.getItem(slot);
if (stack?.typeId !== AMULET_ID) continue;
const remaining = stack.clone();
remaining.amount -= 1;
container.setItem(slot, remaining.amount > 0 ? remaining : undefined);
return;
}
} catch {
return;
}
}
world.afterEvents.playerInteractWithEntity.subscribe((event) => {
const villca = event.target;
if (!villca || villca.typeId !== VILLCA_ID || event.itemStack?.typeId !== AMULET_ID) return;
const state = stateFor(villca);
if (isTamed(villca)) {
state.boredom = clamp(state.boredom + 25, 0, 100);
state.anger = clamp(state.anger - 20, 0, 100);
reply(event.player, "The spirit recognizes you. Do not make me regret it.");
return;
}
state.anger = clamp(state.anger + 12, 0, 100);
consumeAmulet(event.player);
reply(event.player, "The amulet burns. Villca is resisting your spirit...");
});
world.afterEvents.entityHurt.subscribe((event) => {
const villca = event.hurtEntity;
if (!villca || villca.typeId !== VILLCA_ID) return;
const state = stateFor(villca);
state.anger = clamp(state.anger + Math.max(4, event.damage * 2), 0, 100);
});