#How to Register a Custom Teleport Command

7 messages · Page 1 of 1 (latest)

open solstice
#

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;
        }
    });
});```
simple merlinBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

clear vale
#

customCommand means you have to use /kubejs custom_command spawn iirc

#

use the command Registry event if you want it to be just /spawn

mental sunBOT
#

Choose a topic below!

clear vale
open solstice
#

Ohh .. okay thanks