Files
behavior_packs
resource_packs
Code
11 426
behavior_packs/x_dimension_bp/scripts/main.js
import { system, world } from "@minecraft/server";
const FRAME_BLOCK = "minecraft:diamond_block";
const PORTAL_DIMENSION = "x_dimension:x_dimension";
const cooldowns = new Set();
world.afterEvents.playerInteractWithBlock.subscribe((event) => {
if (event.block.typeId !== FRAME_BLOCK) return;
if (event.itemStack?.typeId !== "minecraft:flint_and_steel") return;
const frame = findPortalFrame(event.block);
if (!frame) return;
const player = event.player;
if (cooldowns.has(player.id)) return;
cooldowns.add(player.id);
system.runTimeout(() => cooldowns.delete(player.id), 40);
const entering = event.block.dimension.id !== PORTAL_DIMENSION;
const destination = world.getDimension(entering ? PORTAL_DIMENSION : "minecraft:overworld");
player.sendMessage(entering ? "Entering the X Dimension..." : "Returning to the Overworld...");
system.run(() => {
player.teleport({ x: 0.5, y: 100, z: 0.5 }, { dimension: destination });
});
});
function findPortalFrame(block) {
const origin = block.location;
const dimension = block.dimension;
for (const axis of [
{ x: 1, z: 0 },
{ x: 0, z: 1 }
]) {
for (let left = -3; left <= 0; left++) {
for (let bottom = -4; bottom <= 0; bottom++) {
const x = origin.x + axis.x * left;
const z = origin.z + axis.z * left;
const y = origin.y + bottom;
let valid = true;
for (let width = 0; width < 4 && valid; width++) {
for (let height = 0; height < 5; height++) {
const edge = width === 0 || width === 3 || height === 0 || height === 4;
const location = {
x: x + axis.x * width,
y: y + height,
z: z + axis.z * width
};
const typeId = dimension.getBlock(location)?.typeId;
if (edge ? typeId !== FRAME_BLOCK : typeId !== "minecraft:air" && typeId !== "minecraft:fire") {
valid = false;
break;
}
}
}
if (valid) return { x, y, z };
}
}
}
return undefined;
}