import { world, system } from "@minecraft/server";

// Function to check if the world is flat
function isFlatWorld() {
    return world.getDimension("overworld").getBlock({ x: 0, y: 1, z: 0 }).typeId === "minecraft:air"; 
    // In a normal world, y=1 should have grass or stone, but in flat worlds, it's air.
}

// Function to execute the command when a new world is created
function onFirstJoin(event) {
    const player = event.player;

    system.runTimeout(() => {
        if (!isFlatWorld()) {
            player.runCommandAsync("give @a diamond 10"); // Give 10 diamonds
            player.sendMessage("§aYou received 10 Diamonds for starting a new world!");
        }
    }, 40); // Wait a bit to ensure the world is fully loaded
}

// Run when a player joins for the first time
world.afterEvents.playerJoin.subscribe(onFirstJoin);