
world.afterEvents.entitySpawn.subscribe(({entity}) => {
  if (typeId.includes(`pik:throw_`)){
    const { location, dimension } = entity
    const player = dimension.getPlayers({ location, closest:1 })[0]
    
    entity.nameTag = OWNER_NAME_PREFIX+player.name
    if(pikSpicyExtract.getScore(player)>0){
      entity.triggerEvent("add_spice")
    } 
  }
  if (typeId === `minecraft:item`){
    const item = entity.getComponent('item').itemStack
    if(item.typeId.includes(`pikmin_item`)){
    
        const{dimension,location} = entity
        const {x,z} = location
        const pikminTypeId = item.typeId.substring(0,item.typeId.indexOf("_item"))
        const vel = entity.getVelocity()
        const velMultiplier = 2
        for (let i = 0; i < item.amount; i++) {
          let pikmin = dimension.spawnEntity(`${pikminTypeId}<lost>`,location)
          pikmin.applyImpulse({x:vel.x*velMultiplier,y:vel.y*velMultiplier,z:vel.z*velMultiplier})
        }
      entity.teleport({x,y:-100,z})
    }
    if(item.typeId.includes(`onion_item`)){
    
      const{dimension,location} = entity
      const {x,z} = location
      const onionTypeId = item.typeId.substring(0,item.typeId.indexOf("_item"))
      const vel = entity.getVelocity()
      const velMultiplier = 2
      for (let i = 0; i < item.amount; i++) {
        let pikmin = dimension.spawnEntity(`${onionTypeId}<minecraft:entity_spawned>`,location)
        pikmin.applyImpulse({x:vel.x*velMultiplier,y:vel.y*velMultiplier,z:vel.z*velMultiplier})
      }
    entity.teleport({x,y:-100,z})
  }
  }
})
