Files
dialogue
scripts
manifest.json
Minecraft.mcaddon
Code
4 258
scripts/main.js
import { system, world } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";
const dialogue = {
main_menu: "main",
menus: {
main: {
title: "Conversación con el aldeano",
body: "El aldeano te mira y espera tu pregunta.",
options: [
{ label: "Pedir un rumor", next: "rumor" },
{ label: "Preguntar por el clima", next: "weather" },
{ label: "Despedirse", close: true }
]
},
rumor: {
title: "Rumores del pueblo",
body: "¿Sobre qué quieres escuchar un rumor?",
options: [
{ label: "Las minas abandonadas", next: "rumor_mines" },
{ label: "El bosque cercano", next: "rumor_forest" },
{ label: "Volver al menú principal", next: "main" }
]
},
rumor_mines: {
title: "Las minas abandonadas",
body: "Dicen que todavía se oyen carros de mina bajo la colina, aunque nadie ha bajado allí en años.",
options: [
{ label: "Volver a los rumores", next: "rumor" },
{ label: "Terminar conversación", close: true }
]
},
rumor_forest: {
title: "El bosque cercano",
body: "Los leñadores aseguran que un zorro blanco aparece cada luna llena junto al río.",
options: [
{ label: "Volver a los rumores", next: "rumor" },
{ label: "Terminar conversación", close: true }
]
},
weather: {
title: "El clima",
body: "El aldeano señala el cielo y murmura sobre el tiempo de hoy.",
options: [
{ label: "¿Lloverá pronto?", next: "weather_rain" },
{ label: "¿Hará frío esta noche?", next: "weather_cold" },
{ label: "Volver al menú principal", next: "main" }
]
},
weather_rain: {
title: "Pronóstico de lluvia",
body: "Las nubes están creciendo sobre las montañas. Lleva un techo contigo por si acaso.",
options: [
{ label: "Volver al clima", next: "weather" },
{ label: "Terminar conversación", close: true }
]
},
weather_cold: {
title: "Pronóstico de frío",
body: "El viento del norte será fuerte esta noche. Una fogata será tu mejor amiga.",
options: [
{ label: "Volver al clima", next: "weather" },
{ label: "Terminar conversación", close: true }
]
}
}
};
const lastInteraction = new Map();
function openDialogue(player, menuId = dialogue.main_menu) {
const menu = dialogue.menus[menuId];
if (!menu) return;
const form = new ActionFormData()
.title(menu.title)
.body(menu.body);
for (const option of menu.options) {
form.button(option.label);
}
form.show(player).then((response) => {
if (response.canceled || response.selection === undefined) return;
const selected = menu.options[response.selection];
if (!selected || selected.close) return;
if (!selected.next) return;
system.run(() => openDialogue(player, selected.next));
}).catch(() => {
// El jugador puede cerrar el formulario al salir del mundo o cambiar de dimensión.
});
}
world.afterEvents.playerInteractWithEntity.subscribe((event) => {
if (event.target.typeId !== "minecraft:villager") return;
const now = Date.now();
const previous = lastInteraction.get(event.player.id) ?? 0;
if (now - previous < 500) return;
lastInteraction.set(event.player.id, now);
system.run(() => openDialogue(event.player));
});