Merge remote-tracking branch 'origin/master'

This commit is contained in:
坏黑 2018-12-02 11:32:47 +08:00
commit 45f60aee9d
4 changed files with 51 additions and 4 deletions

View File

@ -1,6 +1,6 @@
package com.ilummc.tlib.scala
import com.ilummc.tlib.scala.Implicits._
import com.ilummc.tlib.scala.Prelude._
import org.bukkit.Material
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.{EventHandler, Listener}

View File

@ -5,7 +5,7 @@ import org.bukkit.entity.Player
import org.bukkit.util.Vector
import org.bukkit.{Location, OfflinePlayer, World, util}
object Implicits {
abstract class Implicits {
implicit def player2rich(player: Player): RichPlayer = player

View File

@ -0,0 +1,47 @@
package com.ilummc.tlib.scala
import com.ilummc.tlib.resources.TLocale.Logger
import org.bukkit.Bukkit
import org.bukkit.event.{Event, EventException, EventPriority, Listener}
import org.bukkit.plugin.{EventExecutor, Plugin}
object Prelude extends Implicits {
def listen[T <: Event](clazz: Class[T],
ignoreCancelled: Boolean = true,
priority: EventPriority = EventPriority.NORMAL)
(handler: T => Unit)
(implicit plugin: Plugin): Unit = {
val listener = new SingleListener(handler)
Bukkit.getPluginManager.registerEvent(clazz, listener, priority, listener, plugin, ignoreCancelled)
}
def runTask(task: => Unit)(implicit plugin: Plugin): Unit = Task(task)
def runTask(delay: Long)(task: => Unit)(implicit plugin: Plugin): Unit = Task(delay)(task)
def runTask(init: Long, period: Long)(task: => Unit)(implicit plugin: Plugin): Unit = Task(init, period)(task)
def runTaskAsync(task: => Unit)(implicit plugin: Plugin): Unit = AsyncTask(task)
def runTaskAsync(delay: Long)(task: => Unit)(implicit plugin: Plugin): Unit = AsyncTask(delay)(task)
def runTaskAsync(init: Long, period: Long)(task: => Unit)(implicit plugin: Plugin): Unit = AsyncTask(init, period)(task)
def info(node: String, params: String*): Unit = Logger.info(node, params: _*)
def error(node: String, params: String*): Unit = Logger.error(node, params: _*)
def fine(node: String, params: String*): Unit = Logger.fine(node, params: _*)
def debug(node: String, params: String*): Unit = Logger.verbose(node, params: _*)
}
private[this] class SingleListener[T <: Event](handler: T => Any) extends Listener with EventExecutor {
override def execute(listener: Listener, event: Event): Unit = {
try handler(event.asInstanceOf[T]) catch {
case e: Throwable => throw new EventException(e)
}
}
}

View File

@ -54,8 +54,8 @@ class RichPlayer(private val player: Player) extends RichOfflinePlayer(player) {
def locale(node: String, params: String*): Unit = sendLocalizedMessage(node, params: _*)
def <<(node: String): RichPlayer = {
TLocale.sendTo(player, node)
def <<(text: String): RichPlayer = {
player.sendMessage(text)
this
}