// Strange Aeons: give each player the starting lore book once.

const BOOK_KEY = 'strange_aeons_starting_lore_book_v1'

function bookPage(text) {
  return JSON.stringify({ text: text })
}

function makeLoreBook() {
  return Item.of('minecraft:written_book', {
    title: 'The Book of Returning',
    author: 'Unknown Hand',
    generation: 0,
    pages: [
      bookPage(
        'You woke where no living thing should wake.\n\n' +
        'A grave. A road. A name you cannot quite hold.\n\n' +
        'The world remembers you, but badly.'
      ),
      bookPage(
        'Death is broken.\n\n' +
        'The dead climb from graves. The living return when they should not. ' +
        'Some come back whole. Some come back wrong.'
      ),
      bookPage(
        'You are one of the Returned.\n\n' +
        'Not chosen. Not blessed. Marked by an age that should have ended.'
      ),
      bookPage(
        'The roads remember better than people do.\n\n' +
        'Waystones still answer. Tombs still whisper. Doors appear where no door was built.'
      ),
      bookPage(
        'The old garden is gone from this world.\n\n' +
        'Living magic no longer blooms under the Overworld sky. Seek the twilight forest if you want flowers that remember life.'
      ),
      bookPage(
        'Blood and moon still have power.\n\n' +
        'Vampires, beasts, and hunters remain. Infection is easy. Mastery is not.'
      ),
      bookPage(
        'The dragons fled.\n\n' +
        'They did not fear men. They feared what the world is becoming.'
      ),
      bookPage(
        'Rule of this age:\n\n' +
        'Atmosphere is open. Power is gated.\n\n' +
        'Survive first. Understand later.'
      ),
      bookPage(
        'Open your inventory and use the Questlog.\n\n' +
        'It will not tell you everything. It will tell you what still matters.'
      )
    ]
  })
}

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

  if (player.persistentData.getBoolean(BOOK_KEY)) return

  player.persistentData.putBoolean(BOOK_KEY, true)
  player.give(makeLoreBook())
})