FireworkCalculate - 2022 .ver
This commit is contained in:
parent
0719ae092f
commit
e0a9853e9c
|
@ -1,5 +1,6 @@
|
|||
package cc.maxmc.fwc
|
||||
|
||||
import cc.maxmc.fwc.command.Commands
|
||||
import cc.maxmc.fwc.database.connect
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
@ -23,23 +24,24 @@ import java.util.concurrent.Executor
|
|||
object FireworkCounter: Plugin() {
|
||||
|
||||
override fun onEnable() {
|
||||
Commands
|
||||
connect()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
val PluginScope = CoroutineScope(SupervisorJob() + CoroutineExceptionHandler { _, except ->
|
||||
console().sendMessage("§e执行异步操作时出现异常 ${except.message}")
|
||||
console().sendMessage("§c栈追踪: ")
|
||||
except.stackTrace.forEach {
|
||||
console().sendMessage("§c位于 $it")
|
||||
}
|
||||
})
|
||||
|
||||
val bukkitSyncContext = Executor {
|
||||
Bukkit.getScheduler().runTask(BukkitPlugin.getInstance(), it)
|
||||
}.asCoroutineDispatcher()
|
||||
|
||||
val bukkitAsyncContext = Executor {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(BukkitPlugin.getInstance(), it)
|
||||
}.asCoroutineDispatcher()
|
||||
}.asCoroutineDispatcher()
|
||||
|
||||
val PluginScope = CoroutineScope(bukkitAsyncContext + SupervisorJob() + CoroutineExceptionHandler { _, except ->
|
||||
console().sendMessage("§e执行异步操作时出现异常 ${except.message}")
|
||||
console().sendMessage("§c栈追踪: ")
|
||||
except.stackTrace.forEach {
|
||||
console().sendMessage("§c位于 $it")
|
||||
}
|
||||
})
|
||||
|
|
|
@ -0,0 +1,134 @@
|
|||
package cc.maxmc.fwc.command
|
||||
|
||||
import cc.maxmc.fwc.PluginScope
|
||||
import cc.maxmc.fwc.database.FireworkTable
|
||||
import cc.maxmc.fwc.database.PlayerFireworkScore
|
||||
import kotlinx.coroutines.launch
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.entity.Player
|
||||
import org.jetbrains.exposed.sql.exposedLogger
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import taboolib.common.platform.command.CommandBuilder
|
||||
import taboolib.common.platform.command.command
|
||||
|
||||
object Commands {
|
||||
init {
|
||||
register()
|
||||
}
|
||||
|
||||
private fun register() = command("firework", description = "花火活动相关命令") {
|
||||
topCommand()
|
||||
addCommand()
|
||||
setCommand()
|
||||
showCommand()
|
||||
}
|
||||
|
||||
private fun CommandBuilder.CommandComponent.topCommand() = literal("top") {
|
||||
execute<Player> { sender, _, _ ->
|
||||
PluginScope.launch {
|
||||
sender.sendMessage("§6| §7花火点排行:")
|
||||
transaction {
|
||||
val scores = PlayerFireworkScore.all().sortedByDescending { it.score }
|
||||
scores.forEachIndexed { rank, score ->
|
||||
sender.sendMessage("§e| §7${rank + 1}.${score.name} - §e${score.score} 花火点")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO
|
||||
dynamic {
|
||||
restrict<Player> { _, _, argument ->
|
||||
return@restrict argument.toIntOrNull() != null
|
||||
}
|
||||
|
||||
execute<Player> { sender, _, argument ->
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
private fun CommandBuilder.CommandComponent.addCommand() = literal("add", permission = "fireworks.add") {
|
||||
dynamic {
|
||||
suggestion<Player> { _, _ ->
|
||||
Bukkit.getOnlinePlayers().map { it.name }
|
||||
}
|
||||
restrict<Player> { _, _, argument ->
|
||||
return@restrict Bukkit.getPlayerExact(argument) != null
|
||||
}
|
||||
dynamic {
|
||||
restrict<Player> { _, _, argument ->
|
||||
argument.toIntOrNull() != null
|
||||
}
|
||||
execute<Player> { sender, context, argument ->
|
||||
val target = Bukkit.getPlayerExact(context.argument(-1))!!
|
||||
val scoreAdded = argument.toInt()
|
||||
PluginScope.launch {
|
||||
transaction {
|
||||
val scoreObj = PlayerFireworkScore.findById(target.uniqueId) ?: PlayerFireworkScore.new(target.uniqueId) {
|
||||
name = target.name
|
||||
score = 0
|
||||
}
|
||||
scoreObj.score += scoreAdded
|
||||
sender.sendMessage("§6| §7成功给予玩家 §a${target.name} §6$argument 花火点")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun CommandBuilder.CommandComponent.setCommand() = literal("set", permission = "fireworks.set") {
|
||||
dynamic {
|
||||
suggestion<Player> { _, _ ->
|
||||
Bukkit.getOnlinePlayers().map { it.name }
|
||||
}
|
||||
restrict<Player> { _, _, argument ->
|
||||
return@restrict Bukkit.getPlayerExact(argument) != null
|
||||
}
|
||||
dynamic {
|
||||
restrict<Player> { _, _, argument ->
|
||||
argument.toIntOrNull() != null
|
||||
}
|
||||
execute<Player> { sender, context, argument ->
|
||||
val target = Bukkit.getPlayerExact(context.argument(-1))!!
|
||||
val scoreAdded = argument.toInt()
|
||||
PluginScope.launch {
|
||||
transaction {
|
||||
val scoreObj = PlayerFireworkScore.findById(target.uniqueId) ?: PlayerFireworkScore.new(target.uniqueId) {
|
||||
name = target.name
|
||||
score = 0
|
||||
}
|
||||
scoreObj.score = scoreAdded
|
||||
sender.sendMessage("§6| §7成功将玩家 §a${target.name} §7的花火点设置为 §6$argument.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun CommandBuilder.CommandComponent.showCommand() = literal("show", permission = "fireworks.show") {
|
||||
dynamic {
|
||||
suggestion<Player> { _, _ ->
|
||||
Bukkit.getOnlinePlayers().map { it.name }
|
||||
}
|
||||
restrict<Player> { _, _, argument ->
|
||||
return@restrict Bukkit.getPlayerExact(argument) != null
|
||||
}
|
||||
execute<Player> { sender, _, argument ->
|
||||
val target = Bukkit.getPlayerExact(argument)!!
|
||||
PluginScope.launch {
|
||||
transaction {
|
||||
val scoreObj = PlayerFireworkScore.findById(target.uniqueId) ?: PlayerFireworkScore.new(target.uniqueId) {
|
||||
name = target.name
|
||||
score = 0
|
||||
}
|
||||
sender.sendMessage("§6| §7玩家 §a${target.name} §7的花火点为 §6${scoreObj.score}.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package cc.maxmc.fwc.listener
|
||||
|
||||
import cc.maxmc.fwc.database.PlayerFireworkScore
|
||||
import cc.maxmc.fwc.utils.Calculator
|
||||
import cc.maxmc.fwc.misc.Calculator
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.event.entity.FireworkExplodeEvent
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
@ -12,13 +12,13 @@ object FireworkListener {
|
|||
fun onFirework(e: FireworkExplodeEvent) {
|
||||
val shooter = e.entity.shooter
|
||||
if (shooter !is Player) return
|
||||
var score = Calculator.calculateScore(e.entity.fireworkMeta)
|
||||
val score = Calculator.calculateScore(e.entity.fireworkMeta)
|
||||
shooter.sendMessage("§6| §7恭喜您获得 §6$score 花火点")
|
||||
transaction {
|
||||
val current: PlayerFireworkScore =
|
||||
PlayerFireworkScore.findById(shooter.uniqueId) ?: PlayerFireworkScore.new(shooter.uniqueId) {
|
||||
name = shooter.name
|
||||
score = 0
|
||||
this.score = 0
|
||||
}
|
||||
current.score = current.score + score
|
||||
shooter.sendMessage("§6| §7您目前的花火点为: §6${PlayerFireworkScore[shooter.uniqueId].score}")
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package cc.maxmc.fwc.utils
|
||||
package cc.maxmc.fwc.misc
|
||||
|
||||
import org.bukkit.FireworkEffect
|
||||
import org.bukkit.FireworkEffect.Type.*
|
|
@ -1,4 +1,4 @@
|
|||
package cc.maxmc.fwc.utils
|
||||
package cc.maxmc.fwc.misc
|
||||
|
||||
import cc.maxmc.fwc.database.PlayerFireworkScore
|
||||
import org.bukkit.entity.Player
|
Loading…
Reference in New Issue