var $BlockPos = Java.loadClass('net.minecraft.core.BlockPos')
var $BlockState = Java.loadClass('net.minecraft.world.level.block.state.BlockState')
let $StructurePlaceSettings = Java.loadClass('net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings')

/*
PlayerEvents.loggedIn(event => {
    let world = event.getEntity().getServer().getLevel('minecraft:overworld')
    let persistentData = world.persistentData
    let sx = Math.floor(Math.random() * 2000) - 1000,
    sz = Math.floor(Math.random() * 2000) - 1000,
    sy = world.getHeight('WORLD_SURFACE', sx, sz),
    spawnblock = world.getBlock(sx, sy-1, sz)

    if (persistentData.get('deadzone_dagger_spawned') === true) {
        return;
    }

    event.entity.tell(`Topmost block at (${sx}, ${sz}): ${spawnblock.id} (y=${sy})`)
    // event.server.runCommand(`/place structure arrzenhanced:downed_satellite ${sx} 64 ${sz}`)
    // event.server.runCommand(`place structure arrzenhanced:downed_satellite ${sx} 64 ${sz}`)
    // world.setChunkForced(sx/16, sz/16, true)
    // event.server.runCommand(`place structure arrzenhanced:downed_satellite ${sx} 64 ${sz}`)
    // world.setChunkForced(sx/16, sz/16, false)
    // event.entity.tell(`place structure arrzenhanced:downed_satellite ${sx} 64 ${sz}`)
    // event.entity.tell(`Chunk at (${sx}, ${sz}) is now force-loaded.`)

    console.log("FLAGGER")
    event.server.runCommand(`forge generate ${sx} ${sy} ${sz} 12 minecraft:overworld 0`)
    console.log(event.getEntity().getServer().structureManager.get('arrzenhanced:downed_satellite'))
    event.server.scheduleInTicks(60000, c => {
        event.entity.tell("bang")
        console.log("HI HELLO")
        console.log(c)
        event.server.structureManager.get('arrzenhanced:downed_satellite').ifPresent(e => {
            let sy = world.getHeight('WORLD_SURFACE', sx, sz),
            spawnPos = new $BlockPos(sx, sy, sz)
            event.entity.tell(`Topmost block at (${sx}, ${sz}): ${spawnblock.id} (y=${sy})`)
            console.log(spawnPos)
            e.placeInWorld(world, spawnPos, spawnPos, new $StructurePlaceSettings(), world.getRandom(), 3)
        }) 
    })
    // world.getChunkAt(spawnblock)
    // persistentData.put('deadzone_dagger_spawned', true);
})
*/


PlayerEvents.loggedIn(event => {
    let world = event.getEntity().getServer()
    let persistentData = world.persistentData

    if (persistentData.get('deadzone_dagger_spawned') === true) {
        return;
    }

    let chestPos = new $BlockPos(0, (Math.floor(Math.random() * (12 - (-12) + 1)) + (-12)), 0)
    world.getLevel('minecraft:overworld').getBlock(chestPos.x, chestPos.y, chestPos.z).set('minecraft:chest');
    let chest = world.getLevel('minecraft:overworld').getBlockEntity(chestPos);

    if (chest) {
        chest.setStackInSlot(13, Item.of('arrzenhanced:deadzone_dagger', 1, {Unbreakable:true}));
    }
    persistentData.put('deadzone_dagger_spawned', true);
});