Fix
This commit is contained in:
19
src/main/scala/io/izzel/tlibscala/AsyncTask.scala
Normal file
19
src/main/scala/io/izzel/tlibscala/AsyncTask.scala
Normal file
@@ -0,0 +1,19 @@
|
||||
package io.izzel.tlibscala
|
||||
|
||||
import org.bukkit.plugin.Plugin
|
||||
|
||||
object AsyncTask {
|
||||
|
||||
def apply(task: => Any)(implicit plugin: Plugin): Int = {
|
||||
ScalaTaskExecutor(task).runTaskAsynchronously(plugin).getTaskId
|
||||
}
|
||||
|
||||
def apply(delay: Long)(task: => Any)(implicit plugin: Plugin): Int = {
|
||||
ScalaTaskExecutor(task).runTaskLaterAsynchronously(plugin, delay).getTaskId
|
||||
}
|
||||
|
||||
def apply(init: Long, period: Long)(task: => Any)(implicit plugin: Plugin): Int = {
|
||||
ScalaTaskExecutor(task).runTaskTimerAsynchronously(plugin, init, period).getTaskId
|
||||
}
|
||||
|
||||
}
|
||||
33
src/main/scala/io/izzel/tlibscala/Example.scala
Normal file
33
src/main/scala/io/izzel/tlibscala/Example.scala
Normal file
@@ -0,0 +1,33 @@
|
||||
package io.izzel.tlibscala
|
||||
|
||||
import Prelude._
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.event.player.PlayerJoinEvent
|
||||
import org.bukkit.event.{EventHandler, Listener}
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.plugin.java.JavaPlugin
|
||||
|
||||
class Example extends JavaPlugin with Listener {
|
||||
|
||||
implicit lazy val plugin: Example = this
|
||||
|
||||
@EventHandler
|
||||
def onJoin(event: PlayerJoinEvent): Unit = {
|
||||
event.getPlayer.sendActionBar("2333")
|
||||
event.getPlayer.displaySidebarUnranked("", "", "")
|
||||
event.getPlayer.setVelocity(1.0, 2.0, 3.0)
|
||||
event.getPlayer.withdraw(100)
|
||||
event.getPlayer.getInventory.addItem(new ItemStack(Material.DIAMOND))
|
||||
event.getPlayer << "locale.node" << "node.2"
|
||||
event.getPlayer.teleport(event.getPlayer.getLocation + (1, 2, 3))
|
||||
Task {
|
||||
event.getPlayer << "test"
|
||||
}
|
||||
AsyncTask(20) {
|
||||
event.getPlayer << "later"
|
||||
}
|
||||
class a
|
||||
assert(this == JavaPlugin.getProvidingPlugin(classOf[a]))
|
||||
}
|
||||
|
||||
}
|
||||
26
src/main/scala/io/izzel/tlibscala/Implicits.scala
Normal file
26
src/main/scala/io/izzel/tlibscala/Implicits.scala
Normal file
@@ -0,0 +1,26 @@
|
||||
package io.izzel.tlibscala
|
||||
|
||||
import io.izzel.tlibscala.runtime.{RichLocation, RichOfflinePlayer, RichPlayer, RichVector}
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.util.Vector
|
||||
import org.bukkit.{Location, OfflinePlayer, World, util}
|
||||
|
||||
abstract class Implicits {
|
||||
|
||||
implicit def player2rich(player: Player): RichPlayer = player
|
||||
|
||||
implicit def offline2rich(player: OfflinePlayer): RichOfflinePlayer = player
|
||||
|
||||
implicit def tuple2location(loc: (World, Double, Double, Double)): Location = new Location(loc._1, loc._2, loc._3, loc._4)
|
||||
|
||||
implicit def tuple2vector(vec: (Double, Double, Double)): Vector = new util.Vector(vec._1, vec._2, vec._3)
|
||||
|
||||
implicit def location2tuple(loc: Location): (Double, Double, Double) = (loc.getX, loc.getY, loc.getZ)
|
||||
|
||||
implicit def vector2tuple(vec: Vector): (Double, Double, Double) = (vec.getX, vec.getY, vec.getZ)
|
||||
|
||||
implicit def location2rich(loc: Location): RichLocation = loc
|
||||
|
||||
implicit def vector2rich(vector: Vector): RichVector = vector
|
||||
|
||||
}
|
||||
47
src/main/scala/io/izzel/tlibscala/Prelude.scala
Normal file
47
src/main/scala/io/izzel/tlibscala/Prelude.scala
Normal file
@@ -0,0 +1,47 @@
|
||||
package io.izzel.tlibscala
|
||||
|
||||
import io.izzel.taboolib.module.locale.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/main/scala/io/izzel/tlibscala/ScalaTaskExecutor.scala
Normal file
21
src/main/scala/io/izzel/tlibscala/ScalaTaskExecutor.scala
Normal file
@@ -0,0 +1,21 @@
|
||||
package io.izzel.tlibscala
|
||||
|
||||
import org.bukkit.scheduler.BukkitRunnable
|
||||
|
||||
private[tlibscala] class ScalaTaskExecutor(task: => Any) extends BukkitRunnable {
|
||||
|
||||
override def run(): Unit = {
|
||||
try task catch {
|
||||
case _: CancelException => cancel()
|
||||
case e: Throwable => throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object ScalaTaskExecutor {
|
||||
def apply(task: => Any): ScalaTaskExecutor = new ScalaTaskExecutor(task)
|
||||
}
|
||||
|
||||
class CancelException extends RuntimeException {
|
||||
override def getMessage: String = "Uncaught cancel task signal! Any Task.cancel() should only be used in a Task."
|
||||
}
|
||||
21
src/main/scala/io/izzel/tlibscala/Task.scala
Normal file
21
src/main/scala/io/izzel/tlibscala/Task.scala
Normal file
@@ -0,0 +1,21 @@
|
||||
package io.izzel.tlibscala
|
||||
|
||||
import org.bukkit.plugin.Plugin
|
||||
|
||||
object Task {
|
||||
|
||||
def apply(task: => Any)(implicit plugin: Plugin): Int = {
|
||||
ScalaTaskExecutor(task).runTask(plugin).getTaskId
|
||||
}
|
||||
|
||||
def apply(delay: Long)(task: => Any)(implicit plugin: Plugin): Int = {
|
||||
ScalaTaskExecutor(task).runTaskLater(plugin, delay).getTaskId
|
||||
}
|
||||
|
||||
def apply(init: Long, period: Long)(task: => Any)(implicit plugin: Plugin): Int = {
|
||||
ScalaTaskExecutor(task).runTaskTimer(plugin, init, period).getTaskId
|
||||
}
|
||||
|
||||
def cancel(): Nothing = throw new CancelException
|
||||
|
||||
}
|
||||
23
src/main/scala/io/izzel/tlibscala/runtime/RichLocation.scala
Normal file
23
src/main/scala/io/izzel/tlibscala/runtime/RichLocation.scala
Normal file
@@ -0,0 +1,23 @@
|
||||
package io.izzel.tlibscala.runtime
|
||||
|
||||
import org.bukkit.Location
|
||||
|
||||
class RichLocation(private val location: Location) {
|
||||
|
||||
def +(loc: (Double, Double, Double)): Location = location.add(loc._1, loc._2, loc._3)
|
||||
|
||||
def -(vec: (Double, Double, Double)): Location = this.+(-vec._1, -vec._2, -vec._3)
|
||||
|
||||
def *(x: Double): Location = location.multiply(x)
|
||||
|
||||
def /(x: Double): Location = this * (1 / x)
|
||||
|
||||
}
|
||||
|
||||
object RichLocation {
|
||||
|
||||
implicit def Location2rich(Location: Location): RichLocation = new RichLocation(Location)
|
||||
|
||||
implicit def rich2Location(richLocation: RichLocation): Location = richLocation.location
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.izzel.tlibscala.runtime
|
||||
|
||||
import io.izzel.taboolib.module.compat.EconomyHook
|
||||
import io.izzel.taboolib.util.item.ItemBuilder
|
||||
import org.bukkit.OfflinePlayer
|
||||
import org.bukkit.inventory.ItemStack
|
||||
|
||||
class RichOfflinePlayer(private val offlinePlayer: OfflinePlayer) {
|
||||
|
||||
def getSkullItem: ItemStack = new ItemBuilder(offlinePlayer).build()
|
||||
|
||||
def getMoney: Double = EconomyHook.get(offlinePlayer)
|
||||
|
||||
def withdraw(x: Double): Unit = EconomyHook.remove(offlinePlayer, x)
|
||||
|
||||
def deposit(x: Double): Unit = EconomyHook.add(offlinePlayer, x)
|
||||
|
||||
def hasMoney(x: Double): Boolean = EconomyHook.get(offlinePlayer) >= x
|
||||
|
||||
}
|
||||
|
||||
object RichOfflinePlayer {
|
||||
|
||||
implicit def player2rich(player: OfflinePlayer): RichOfflinePlayer = new RichOfflinePlayer(player)
|
||||
|
||||
implicit def rich2player(player: RichOfflinePlayer): OfflinePlayer = player.offlinePlayer
|
||||
|
||||
}
|
||||
40
src/main/scala/io/izzel/tlibscala/runtime/RichPlayer.scala
Normal file
40
src/main/scala/io/izzel/tlibscala/runtime/RichPlayer.scala
Normal file
@@ -0,0 +1,40 @@
|
||||
package io.izzel.tlibscala.runtime
|
||||
|
||||
import io.izzel.taboolib.module.locale.TLocale
|
||||
import io.izzel.taboolib.module.compat.PermissionHook
|
||||
import io.izzel.taboolib.util.lite.Scoreboards
|
||||
import org.bukkit.entity.Player
|
||||
|
||||
class RichPlayer(private val player: Player) extends RichOfflinePlayer(player) {
|
||||
|
||||
def sendActionBar(x: String): Unit = TLocale.Display.sendActionBar(player, x)
|
||||
|
||||
def displaySidebarUnranked(title: String, elements: Array[String]): Unit = Scoreboards.display(player, elements: _*)
|
||||
|
||||
def displaySidebarUnranked(title: String, elements: List[String]): Unit = Scoreboards.display(player, elements: _*)
|
||||
|
||||
def displaySidebarUnranked(title: String, elements: String*): Unit = Scoreboards.display(player, elements: _*)
|
||||
|
||||
def addPermission(perm: String): Unit = PermissionHook.addPermission(player, perm)
|
||||
|
||||
def removePermission(perm: String): Unit = PermissionHook.removePermission(player, perm)
|
||||
|
||||
def sendTitle(title: String, subtitle: String, fadein: Int, stay: Int, fadeout: Int): Unit = TLocale.Display.sendTitle(player, title, subtitle, fadein, stay, fadeout)
|
||||
|
||||
def sendLocalizedMessage(node: String, params: String*): Unit = TLocale.sendTo(player, node, params: _*)
|
||||
|
||||
def locale(node: String, params: String*): Unit = sendLocalizedMessage(node, params: _*)
|
||||
|
||||
def <<(text: String): RichPlayer = {
|
||||
player.sendMessage(text)
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
object RichPlayer {
|
||||
|
||||
implicit def player2rich(player: Player): RichPlayer = new RichPlayer(player)
|
||||
|
||||
implicit def rich2player(player: RichPlayer): Player = player.player
|
||||
|
||||
}
|
||||
22
src/main/scala/io/izzel/tlibscala/runtime/RichVector.scala
Normal file
22
src/main/scala/io/izzel/tlibscala/runtime/RichVector.scala
Normal file
@@ -0,0 +1,22 @@
|
||||
package io.izzel.tlibscala.runtime
|
||||
|
||||
import org.bukkit.util.Vector
|
||||
|
||||
class RichVector(private val vector: Vector) {
|
||||
|
||||
def +(vec: (Double, Double, Double)): Vector = vector.setX(vector.getX + vec._1).setY(vector.getY + vec._2).setZ(vector.getZ + vec._3)
|
||||
|
||||
def -(vec: (Double, Double, Double)): Vector = this.+(-vec._1, -vec._2, -vec._3)
|
||||
|
||||
def *(x: Double): Vector = vector.multiply(x)
|
||||
|
||||
def /(x: Double): Vector = this * (1 / x)
|
||||
}
|
||||
|
||||
object RichVector {
|
||||
|
||||
implicit def vector2rich(vector: Vector): RichVector = new RichVector(vector)
|
||||
|
||||
implicit def rich2vector(richVector: RichVector): Vector = richVector.vector
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user