Files
scripts
manifest.json
Code
2 157
scripts/main.js
import { DisplaySlotId, world, system } from "@minecraft/server";
const CONFIG = {
eggSpawn: { x: 0, y: 65, z: 0 },
redBase: { x: -30, y: 65, z: 0, radius: 3 },
blueBase: { x: 30, y: 65, z: 0, radius: 3 },
eggItem: "minecraft:dragon_egg",
pointsPerCapture: 1,
respawnDelay: 60
};
let eggExists = false;
let isRespawning = false;
function getObjective() {
let objective = world.scoreboard.getObjective("puntos");
if (!objective) {
objective = world.scoreboard.addObjective("puntos", "Puntos");
}
world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.Sidebar, { objective });
return objective;
}
function getDistance(position, target) {
const dx = position.x - target.x;
const dy = position.y - target.y;
const dz = position.z - target.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
function findEggSlot(player) {
const inventory = player.getComponent("minecraft:inventory");
const container = inventory?.container;
if (!container) return -1;
for (let slot = 0; slot < container.size; slot++) {
if (container.getItem(slot)?.typeId === CONFIG.eggItem) return slot;
}
return -1;
}
function spawnEgg() {
try {
world.getDimension("overworld").setBlockType(CONFIG.eggSpawn, CONFIG.eggItem);
eggExists = true;
isRespawning = false;
world.sendMessage("§e¡El huevo ha aparecido en el centro!");
} catch (error) {
isRespawning = false;
console.warn(`No se pudo colocar el huevo: ${error}`);
}
}
function givePoint(player, team, slot) {
if (!eggExists) return;
const objective = getObjective();
const identity = player.scoreboardIdentity;
const inventory = player.getComponent("minecraft:inventory")?.container;
if (!identity || !inventory) return;
const currentScore = objective.getScore(identity) ?? 0;
objective.setScore(identity, currentScore + CONFIG.pointsPerCapture);
inventory.setItem(slot, undefined);
eggExists = false;
const color = team === "rojo" ? "§c" : "§9";
const teamName = team === "rojo" ? "Rojo" : "Azul";
world.sendMessage(`${color}¡${player.name} ha entregado el huevo para el Equipo ${teamName}!`);
if (!isRespawning) {
isRespawning = true;
system.runTimeout(spawnEgg, CONFIG.respawnDelay);
}
}
system.runInterval(() => {
if (!eggExists) return;
for (const player of world.getAllPlayers()) {
const slot = findEggSlot(player);
if (slot < 0) continue;
if (player.hasTag("rojo") && getDistance(player.location, CONFIG.redBase) <= CONFIG.redBase.radius) {
givePoint(player, "rojo", slot);
} else if (player.hasTag("azul") && getDistance(player.location, CONFIG.blueBase) <= CONFIG.blueBase.radius) {
givePoint(player, "azul", slot);
}
if (!eggExists) break;
}
}, 10);
world.beforeEvents.chatSend.subscribe((event) => {
const message = event.message.trim().toLowerCase();
const player = event.sender;
if (message === "!rojo") {
event.cancel = true;
player.removeTag("azul");
player.addTag("rojo");
player.sendMessage("§cTe uniste al Equipo Rojo");
} else if (message === "!azul") {
event.cancel = true;
player.removeTag("rojo");
player.addTag("azul");
player.sendMessage("§9Te uniste al Equipo Azul");
} else if (message === "!resethuevo") {
event.cancel = true;
if (player.isOp()) {
spawnEgg();
} else {
player.sendMessage("§cSolo un operador puede reiniciar el huevo.");
}
} else if (message === "!puntos") {
event.cancel = true;
const objective = world.scoreboard.getObjective("puntos");
const identity = player.scoreboardIdentity;
const score = objective && identity ? objective.getScore(identity) ?? 0 : 0;
player.sendMessage(`§eTus puntos: §f${score}`);
}
});
system.runTimeout(() => {
getObjective();
spawnEgg();
}, 40);