const triggeredPlayers = {}

BlockEvents.rightClicked(event => {
    const { block, level, player } = event
    if (level.isClientSide()) return

    const state = level.getBlockState(block.pos)
    const blockId = state.getBlock().getId()
    const server = level.getServer()
    const playerName = player.name.string

    if (blockId !== 'enderrift:rift') return
    if (!state.toString().includes('assembled=true')) return
    if (level.dimension.toString() !== 'minecraft:overworld') {
        player.tell(Text.of('Сингулярность должна быть активирована в Верхнем Мире').red())
        return
    }

    if (triggeredPlayers[playerName]) return
    triggeredPlayers[playerName] = true

    const x = Math.floor(player.x)
    const y = Math.floor(player.y)
    const z = Math.floor(player.z)

    player.potionEffects.add('minecraft:blindness', 1000000, 0, true, false)
    server.runCommandSilent(`gamemode spectator ${playerName}`)

    pendingActions.push({
        ticks: 10,
        run: () => {
            server.runCommandSilent(`summon alexscaves:nuclear_explosion ${x} ${y} ${z}`)
        }
    })

    pendingActions.push({
        ticks: 100,
        run: () => {
            server.runCommandSilent(`ftbquests change_progress ${playerName} complete 5715D343487C4302`)
            server.runCommandSilent(`kick @a The void has consumed your world. Bye.`)
            server.runCommandSilent(`ban @a The void has consumed your world. Bye.`)

            player.runCommand(`credits show`)
            triggeredPlayers[playerName] = false
        }
    })
})