Scala API

This commit is contained in:
Izzel_Aliz 2018-09-30 19:10:07 +08:00
parent daa0ef71aa
commit d99bba42fc
12 changed files with 274 additions and 9 deletions

View File

@ -5,10 +5,7 @@ import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
public class ScoreboardUtil { public class ScoreboardUtil {
@ -49,7 +46,7 @@ public class ScoreboardUtil {
return title; return title;
} }
public static HashMap<String, Integer> cutRanked(HashMap<String, Integer> content) { public static HashMap<String, Integer> cutRanked(Map<String, Integer> content) {
HashMap<String, Integer> elements = new HashMap<>(content); HashMap<String, Integer> elements = new HashMap<>(content);
while (elements.size() > 15) { while (elements.size() > 15) {
@ -204,7 +201,7 @@ public class ScoreboardUtil {
} }
} }
public static boolean rankedSidebarDisplay(Player p, String title, HashMap<String, Integer> elements) { public static boolean rankedSidebarDisplay(Player p, String title, Map<String, Integer> elements) {
try { try {
title = cutRankedTitle(title); title = cutRankedTitle(title);
elements = cutRanked(elements); elements = cutRanked(elements);
@ -238,7 +235,7 @@ public class ScoreboardUtil {
} }
} }
public static boolean rankedSidebarDisplay(Collection<Player> players, String title, HashMap<String, Integer> elements) { public static boolean rankedSidebarDisplay(Collection<Player> players, String title, Map<String, Integer> elements) {
for (Player player : players) { for (Player player : players) {
if (!rankedSidebarDisplay(player, title, elements)) { if (!rankedSidebarDisplay(player, title, elements)) {
return false; return false;
@ -248,7 +245,7 @@ public class ScoreboardUtil {
return true; return true;
} }
public static boolean rankedSidebarDisplay(Collection<Player> players, String title, HashMap<String, Integer> elements, Scoreboard board) { public static boolean rankedSidebarDisplay(Collection<Player> players, String title, Map<String, Integer> elements, Scoreboard board) {
try { try {
title = cutRankedTitle(title); title = cutRankedTitle(title);
elements = cutRanked(elements); elements = cutRanked(elements);

View File

@ -1,5 +1,6 @@
package me.skymc.taboolib.sign; package me.skymc.taboolib.sign;
import com.google.common.annotations.Beta;
import me.skymc.taboolib.Main; import me.skymc.taboolib.Main;
import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.listener.TListener; import me.skymc.taboolib.listener.TListener;
@ -24,6 +25,7 @@ import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
@Deprecated @Deprecated
@Beta
@TListener(condition = "check") @TListener(condition = "check")
public class SignUtils implements Listener { public class SignUtils implements Listener {

View File

@ -1,14 +1,16 @@
package me.skymc.taboolib.skull; package me.skymc.taboolib.skull;
import com.google.common.annotations.Beta;
import me.skymc.taboolib.inventory.builder.ItemBuilder; import me.skymc.taboolib.inventory.builder.ItemBuilder;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@Beta
@Deprecated @Deprecated
public class SkullUtils { public class SkullUtils {
public static ItemStack getItme(OfflinePlayer p) { public static ItemStack getItem(OfflinePlayer p) {
return new ItemBuilder(p).build(); return new ItemBuilder(p).build();
} }

View File

@ -0,0 +1,19 @@
package com.ilummc.tlib.scala
import org.bukkit.plugin.Plugin
object AsyncTask {
def apply(task: => Unit)(implicit plugin: Plugin): Int = {
ScalaTaskExecutor(task).runTaskAsynchronously(plugin).getTaskId
}
def apply(delay: Long)(task: => Unit)(implicit plugin: Plugin): Int = {
ScalaTaskExecutor(task).runTaskLaterAsynchronously(plugin, delay).getTaskId
}
def apply(init: Long, period: Long)(task: => Unit)(implicit plugin: Plugin): Int = {
ScalaTaskExecutor(task).runTaskTimerAsynchronously(plugin, init, period).getTaskId
}
}

View File

@ -0,0 +1,28 @@
package com.ilummc.tlib.scala
import com.ilummc.tlib.scala.Implicits._
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
object Example extends JavaPlugin with Listener {
@EventHandler
def onJoin(event: PlayerJoinEvent): Unit = {
event.getPlayer.sendActionBar("2333")
val tick = event.getPlayer.getFishTicks
event.getPlayer.setFishTicks(tick + 10)
event.getPlayer.displaySidebar("标题", Map("2333" -> 1))
event.getPlayer.displaySidebarUnranked("", "", "")
event.getPlayer.openSign(event.getPlayer.getWorld.getBlockAt(0, 0, 0))
event.getPlayer.setVelocity(1.0, 2.0, 3.0)
if (event.getPlayer.withdraw(100))
event.getPlayer.getInventory.addItem(new ItemStack(Material.DIAMOND))
event.getPlayer.openAnvil()
event.getPlayer << "locale.node" << "node.2"
event.getPlayer.teleport(event.getPlayer.getLocation + (1, 2, 3))
}
}

View File

@ -0,0 +1,26 @@
package com.ilummc.tlib.scala
import com.ilummc.tlib.scala.runtime.{RichLocation, RichOfflinePlayer, RichPlayer, RichVector}
import org.bukkit.entity.Player
import org.bukkit.util.Vector
import org.bukkit.{Location, OfflinePlayer, World, util}
object 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
}

View File

@ -0,0 +1,22 @@
package com.ilummc.tlib.scala
import org.bukkit.scheduler.BukkitRunnable
private[scala] class ScalaTaskExecutor(task: => Unit) extends BukkitRunnable {
override def run(): Unit = {
try task catch {
case _: CancelException => cancel()
case e: Throwable => throw e
}
}
}
object ScalaTaskExecutor {
def apply(task: => Unit): 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."
}

View File

@ -0,0 +1,21 @@
package com.ilummc.tlib.scala
import org.bukkit.plugin.Plugin
object Task {
def apply(task: => Unit)(implicit plugin: Plugin): Int = {
ScalaTaskExecutor(task).runTask(plugin).getTaskId
}
def apply(delay: Long)(task: => Unit)(implicit plugin: Plugin): Int = {
ScalaTaskExecutor(task).runTaskLater(plugin, delay).getTaskId
}
def apply(init: Long, period: Long)(task: => Unit)(implicit plugin: Plugin): Int = {
ScalaTaskExecutor(task).runTaskTimer(plugin, init, period).getTaskId
}
def cancel(): Nothing = throw new CancelException
}

View File

@ -0,0 +1,23 @@
package com.ilummc.tlib.scala.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
}

View File

@ -0,0 +1,30 @@
package com.ilummc.tlib.scala.runtime
import me.skymc.taboolib.Main
import me.skymc.taboolib.economy.EcoUtils
import me.skymc.taboolib.inventory.builder.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 = EcoUtils.get(offlinePlayer)
def withdraw(x: Double): Boolean = Main.getEconomy.withdrawPlayer(offlinePlayer, x).transactionSuccess
def deposit(x: Double): Boolean = Main.getEconomy.depositPlayer(offlinePlayer, x).transactionSuccess
def hasMoney(x: Double): Boolean = Main.getEconomy.has(offlinePlayer, x)
}
object RichOfflinePlayer {
implicit def player2rich(player: OfflinePlayer): RichOfflinePlayer = new RichOfflinePlayer(player)
implicit def rich2player(player: RichOfflinePlayer): OfflinePlayer = player.offlinePlayer
}

View File

@ -0,0 +1,70 @@
package com.ilummc.tlib.scala.runtime
import com.ilummc.tlib.resources.TLocale
import me.skymc.taboolib.anvil.AnvilContainerAPI
import me.skymc.taboolib.display.{ActionUtils, TitleUtils}
import me.skymc.taboolib.permission.PermissionUtils
import me.skymc.taboolib.player.PlayerUtils
import me.skymc.taboolib.scoreboard.ScoreboardUtil
import me.skymc.taboolib.sign.SignUtils
import org.bukkit.block.Block
import org.bukkit.entity.Player
import scala.collection.JavaConverters._
class RichPlayer(private val player: Player) extends RichOfflinePlayer(player) {
def sendActionBar(x: String): Unit = ActionUtils.send(player, x)
def getFishTicks: Int = PlayerUtils.getFishingTicks(PlayerUtils.getPlayerHookedFish(player))
def setFishTicks(x: Int): Unit = PlayerUtils.setFishingTicks(PlayerUtils.getPlayerHookedFish(player), x)
def resetData(): Unit = PlayerUtils.resetData(player, false)
def resetData(scoreboard: Boolean): Unit = PlayerUtils.resetData(player, scoreboard)
def displaySidebar(title: String, elements: Map[String, Integer]): Unit = ScoreboardUtil.rankedSidebarDisplay(player, title, mapAsJavaMap(elements))
def displaySidebarUnranked(title: String, elements: Array[String]): Unit = ScoreboardUtil.unrankedSidebarDisplay(player, elements: _*)
def displaySidebarUnranked(title: String, elements: List[String]): Unit = ScoreboardUtil.unrankedSidebarDisplay(player, elements: _*)
def displaySidebarUnranked(title: String, elements: String*): Unit = ScoreboardUtil.unrankedSidebarDisplay(player, elements: _*)
@deprecated def openSign(block: Block): Unit = SignUtils.openSign(player, block)
@deprecated def openSign(lines: Array[String], id: String): Unit = SignUtils.openSign(player, lines, id)
//todo TagDataHandler
def addPermission(perm: String): Unit = PermissionUtils.addPermission(player, perm)
def removePermission(perm: String): Unit = PermissionUtils.removePermission(player, perm)
def sendTitle(title: String, subtitle: String, fadein: Int, stay: Int, fadeout: Int): Unit =
TitleUtils.sendTitle(player, title, subtitle, fadein, stay, fadeout)
def sendTitle(p: Player, title: String, fadeint: Int, stayt: Int, fadeoutt: Int, subtitle: String, fadeinst: Int, stayst: Int, fadeoutst: Int): Unit =
TitleUtils.sendTitle(p, title, fadeint, stayt, fadeoutt, subtitle, fadeinst, stayst, fadeoutst)
def openAnvil(): Unit = AnvilContainerAPI.openAnvil(player)
def sendLocalizedMessage(node: String, params: String*): Unit = TLocale.sendTo(player, node, params: _*)
def locale(node: String, params: String*): Unit = sendLocalizedMessage(node, params: _*)
def <<(node: String): RichPlayer = {
TLocale.sendTo(player, node)
this
}
}
object RichPlayer {
implicit def player2rich(player: Player): RichPlayer = new RichPlayer(player)
implicit def rich2player(player: RichPlayer): Player = player.player
}

View File

@ -0,0 +1,25 @@
package com.ilummc.tlib.scala.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
}