I'm trying to register a custom command /spawn in KubeJS that teleports the player to their respawn point. Although the command seems to load without errors, it doesn’t appear in the game. Here’s the code I’m using:
console.log("Spawn command loaded successfully.");
ServerEvents.customCommand(event => {
event.register('spawn', {
description: "Teleports the player to their respawn point",
permissionLevel: 0,
execute: ctx => {
let player = ctx.getSource().getPlayer();
if (!player) {
ctx.getSource().sendFailure("This command can only be executed by a player.");
return 0;
}
let playerString = player.getName().getString();
let spawn = player.getRespawnPosition();
let dimension = player.getRespawnDimension().location().toString();
let countdown = 5;
const initialPosition = player.position().toString();
player.sendMessage(`Teleporting in ${countdown} seconds, please don't move.`, true);
countdown--;
event.server.scheduleInTicks(20, callback => {
if (initialPosition !== player.position().toString()) {
player.sendMessage("You moved, cancelling...", true);
} else if (countdown === 0) {
player.sendMessage("Teleporting!", true);
event.server.runCommandSilent(`execute in ${dimension} run tp ${playerString} ${spawn.x} ${spawn.y} ${spawn.z}`);
} else {
player.sendMessage(`Teleporting in ${countdown} ${countdown === 1 ? "second" : "seconds"}, please don't move.`, true);
countdown--;
callback.reschedule();
}
});
return 1;
}
});
});```