Fishing.kt

Branches > main > 039d1df > files / src / main / kotlin / net / bowmap / handlers / Fishing.kt

package net.bowmap.handlers

import net.bowmap.stormbow.*

import net.bowmap.server.loot.*
import org.bukkit.Material.*
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerFishEvent
import org.bukkit.event.player.PlayerFishEvent.State
import org.bukkit.util.Vector

import net.bowmap.server.loot.tables.fishingLootTable
import net.bowmap.server.item.toItemStacks

// TODO f, not e
private fun calculateFishVelocity(e: PlayerCaughtFish): Vector {
  val eye = e.player.eyeLocation
  val distance = eye.distance(e.hook.location)

  return eye.subtract(e.hook.location)
            .toVector()
            .normalize()
            .multiply(0.2 + distance * 0.1)
}

fun handlePlayerFishing(f: PlayerStartedFishing): Effects =
  Effects(SetFishTime(ticks = 20))

fun handlePlayerCatchingFish(f: PlayerCaughtFish): Effects {
  val loot = fishingLootTable.getLoot()

  // TODO use an effect!
  for (stack in toItemStacks(loot)) {
    val item = f.hook.world.dropItem(f.hook.location, stack)
    item.setVelocity(calculateFishVelocity(f))
  }

  return Effects()
}