Teleportation and Homes System
Let players set homes and teleport between them.
Homes are crucial for survival servers - they give players a safe place to return to. A teleportation system needs:
- Limits: Can't let players teleport infinitely (breaks survival challenge)
- Cooldowns: Prevent teleport spam exploits
- Validation: Make sure the home location is safe
- Persistence: Homes survive server restarts
How It Works
Players can set up to 5 named locations (homes). When they type !home myhouse, they're instantly teleported there. To prevent abuse, teleporting has a cooldown - they can only teleport once every 3 seconds.
We store homes in SuperDB so they persist, with coordinates and dimension info.
Setup
Configure the system parameters:
import { SuperDB } from "@minecraft/server";
import { world } from "@minecraft/server";
const homeDB = new SuperDB({
name: "homes",
immediateWrite: true
});
const MAX_HOMES = 5; // Limit homes per player
const TELEPORT_DELAY = 3; // Cooldown in seconds
Why limit homes? Without a limit, players could set 100 homes and bypass the map's design. Limits encourage choice - "which 5 locations matter most?"
Why a cooldown? Spam teleporting is annoying and can lag the server. A 3-second delay prevents abuse while still feeling responsive.
Set Home
function setHome(playerId, homeName, location) {
const homes = homeDB.get(playerId) || {};
if (Object.keys(homes).length >= MAX_HOMES) {
return { success: false, message: "Too many homes!" };
}
if (homeName.length > 20) {
return { success: false, message: "Name too long!" };
}
homes[homeName] = {
x: Math.round(location.x),
y: Math.round(location.y),
z: Math.round(location.z),
dimension: "overworld"
};
homeDB.set(playerId, homes);
return { success: true, message: `Home '${homeName}' saved!` };
}
// Command to set home
world.beforeEvents.chatSend.subscribe((event) => {
if (event.message.startsWith("!sethome ")) {
event.cancel = true;
const homeName = event.message.split(" ")[1];
const result = setHome(event.sender.nameTag, homeName, event.sender.location);
event.sender.sendMessage(result.message);
}
});
Teleport to Home
let teleportCooldowns = new Map();
function teleportHome(player, homeName) {
const playerId = player.nameTag;
const homes = homeDB.get(playerId);
if (!homes || !homes[homeName]) {
player.sendMessage("Home not found!");
return;
}
// Check cooldown
const lastTeleport = teleportCooldowns.get(playerId) || 0;
const timeSinceLastTeleport = (Date.now() - lastTeleport) / 1000;
if (timeSinceLastTeleport < TELEPORT_DELAY) {
const remaining = Math.ceil(TELEPORT_DELAY - timeSinceLastTeleport);
player.sendMessage(`Wait ${remaining} seconds before teleporting again!`);
return;
}
const home = homes[homeName];
player.teleport({ x: home.x, y: home.y, z: home.z });
teleportCooldowns.set(playerId, Date.now());
player.sendMessage(`Teleported to ${homeName}!`);
}
// Command to go to home
world.beforeEvents.chatSend.subscribe((event) => {
if (event.message.startsWith("!home ")) {
event.cancel = true;
const homeName = event.message.split(" ")[1];
teleportHome(event.sender, homeName);
}
});
List Homes
world.beforeEvents.chatSend.subscribe((event) => {
if (event.message === "!homes") {
event.cancel = true;
const playerId = event.sender.nameTag;
const homes = homeDB.get(playerId);
if (!homes || Object.keys(homes).length === 0) {
event.sender.sendMessage("You have no homes!");
return;
}
event.sender.sendMessage("Your homes:");
Object.entries(homes).forEach(([name, coords]) => {
event.sender.sendMessage(`- ${name} (${coords.x}, ${coords.y}, ${coords.z})`);
});
}
});
Delete Home
function deleteHome(playerId, homeName) {
const homes = homeDB.get(playerId);
if (!homes || !homes[homeName]) {
return { success: false, message: "Home not found!" };
}
delete homes[homeName];
homeDB.set(playerId, homes);
return { success: true, message: `Home '${homeName}' deleted!` };
}
world.beforeEvents.chatSend.subscribe((event) => {
if (event.message.startsWith("!delhome ")) {
event.cancel = true;
const homeName = event.message.split(" ")[1];
const result = deleteHome(event.sender.nameTag, homeName);
event.sender.sendMessage(result.message);
}
});
Warp System
const warpDB = new SuperDB({
name: "warps",
immediateWrite: true
});
function createWarp(warpName, location) {
const warps = warpDB.get("_warps") || {};
warps[warpName] = {
x: Math.round(location.x),
y: Math.round(location.y),
z: Math.round(location.z),
createdBy: "admin"
};
warpDB.set("_warps", warps);
return true;
}
function teleportWarp(player, warpName) {
const warps = warpDB.get("_warps") || {};
if (!warps[warpName]) {
player.sendMessage("Warp not found!");
return;
}
const warp = warps[warpName];
player.teleport({ x: warp.x, y: warp.y, z: warp.z });
player.sendMessage(`Warped to ${warpName}!`);
}
world.beforeEvents.chatSend.subscribe((event) => {
if (event.message.startsWith("!warp ")) {
event.cancel = true;
const warpName = event.message.split(" ")[1];
teleportWarp(event.sender, warpName);
}
});