PlayerEvents.loggedIn(event => {
    const player = event.player

    if (!player.persistentData.getBoolean('received_starter_items')) {
        player.give(Item.of('securitycraft:security_camera', 4))
        player.give(Item.of('securitycraft:camera_monitor', 1))
        player.give(Item.of('minecraft:bread', 16))
        player.persistentData.putBoolean('received_starter_items', true)
    }

    if (player.persistentData.getBoolean('readme_book_removed')) return

    let attempts = 0
    const maxAttempts = 20

    function checkAndClearBook() {
        attempts++
        const inv = player.inventory
        let found = false

        for (let i = 0; i < inv.containerSize; i++) {
            if (inv.getItem(i).id === 'minecraft:written_book') {
                inv.setItem(i, Item.of('minecraft:air'))
                found = true
            }
        }

        if (found) {
            player.persistentData.putBoolean('readme_book_removed', true)
            return
        }

        if (attempts < maxAttempts) {
            event.server.scheduleInTicks(20, checkAndClearBook)
        }
    }

    event.server.scheduleInTicks(20, checkAndClearBook)
})