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

world.beforeEvents.worldInitialize.subscribe(event => {
  event.itemComponentRegistry.registerCustomComponent('cyd_dino:drak_horn', {
    onUse(event) {
      const player = event.source

      player.addEffect('strength', 30 * 20, { amplifier: 2 })
      player.addEffect('speed', 30 * 20, { amplifier: 2 })

      player.dimension.getEntities({
        location: player.location,
        maxDistance: 12,
        families: ['dino']
      }).forEach(dinosaur => {
        const rideable = dinosaur.getComponent('minecraft:rideable')
        const tamed = dinosaur.getComponent('minecraft:tameable')

        if (!rideable || !tamed) return

        if (rideable.getRiders().length >= 1)
          if (tamed.tamedToPlayerId === player.id) {
            dinosaur.addEffect('strength', 30 * 20, { amplifier: 2 })
            dinosaur.addEffect('speed', 30 * 20, { amplifier: 2 })
          }
      })
    }
  })
})