one possible ver

This commit is contained in:
TONY_All 2022-02-01 15:37:59 +08:00
parent 884ed6f510
commit 0719ae092f
7 changed files with 121 additions and 79 deletions

View File

@ -5,6 +5,8 @@ plugins {
id("io.izzel.taboolib") version "1.34" id("io.izzel.taboolib") version "1.34"
} }
val exposedVersion: String by project
group = "cc.maxmc.fwc" group = "cc.maxmc.fwc"
version = "1.0.0-SNAPSHOT" version = "1.0.0-SNAPSHOT"
@ -15,6 +17,7 @@ taboolib {
name("TONY_All") name("TONY_All")
} }
} }
options("skip-kotlin-relocate")
install("common") install("common")
install("platform-bukkit") install("platform-bukkit")
@ -29,6 +32,11 @@ dependencies {
compileOnly("ink.ptms.core:v11800:11800:mapped") compileOnly("ink.ptms.core:v11800:11800:mapped")
compileOnly("ink.ptms.core:v11800:11800:universal") compileOnly("ink.ptms.core:v11800:11800:universal")
compileOnly(kotlin("stdlib")) compileOnly(kotlin("stdlib"))
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
implementation("com.h2database:h2:2.1.210")
} }
tasks.withType<KotlinCompile> { tasks.withType<KotlinCompile> {

View File

@ -1 +1,2 @@
kotlin.code.style=official kotlin.code.style=official
exposedVersion=0.37.3

View File

@ -1,5 +1,45 @@
package cc.maxmc.fwc package cc.maxmc.fwc
import cc.maxmc.fwc.database.connect
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import org.bukkit.Bukkit
import taboolib.common.env.RuntimeDependencies
import taboolib.common.env.RuntimeDependency
import taboolib.common.platform.Plugin
import taboolib.common.platform.function.console
import taboolib.platform.BukkitPlugin import taboolib.platform.BukkitPlugin
import java.util.concurrent.Executor
object FireworkCounter: BukkitPlugin() @RuntimeDependencies(
RuntimeDependency("org.jetbrains.exposed:exposed-core:0.37.3"),
RuntimeDependency("org.jetbrains.exposed:exposed-dao:0.37.3"),
RuntimeDependency("org.jetbrains.exposed:exposed-jdbc:0.37.3"),
RuntimeDependency("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0"),
RuntimeDependency("com.h2database:h2:2.1.210")
)
object FireworkCounter: Plugin() {
override fun onEnable() {
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()

View File

@ -0,0 +1,38 @@
package cc.maxmc.fwc.database
import org.jetbrains.exposed.dao.UUIDEntity
import org.jetbrains.exposed.dao.UUIDEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction
import taboolib.common.platform.function.getDataFolder
import taboolib.common.platform.function.info
import java.util.*
fun connect() {
if (!getDataFolder().exists()) getDataFolder().mkdirs()
// jdbc:h2:file:data
Database.connect("jdbc:h2:file:./${getDataFolder().path}/data")
transaction {
SchemaUtils.create(FireworkTable)
}
info("§a| §7成功创建数据库.")
}
object FireworkTable : IdTable<UUID>("fireworkTable") {
override val id: Column<EntityID<UUID>> = uuid("uid").entityId()
val name = varchar("name", 255)
val score = integer("score")
override val primaryKey = PrimaryKey(arrayOf(id, name))
}
class PlayerFireworkScore(id: EntityID<UUID>) : UUIDEntity(id) {
companion object : UUIDEntityClass<PlayerFireworkScore>(FireworkTable)
val uid by FireworkTable.id
var name by FireworkTable.name
var score by FireworkTable.score
}

View File

@ -1,92 +1,27 @@
package cc.maxmc.fwc.listener package cc.maxmc.fwc.listener
import cc.maxmc.fwc.database.PlayerFireworkScore
import cc.maxmc.fwc.utils.Calculator import cc.maxmc.fwc.utils.Calculator
import org.bukkit.FireworkEffect.Type.*
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.entity.Player import org.bukkit.entity.Player
import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.event.block.BlockFromToEvent
import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.event.entity.ExplosionPrimeEvent
import org.bukkit.event.entity.FireworkExplodeEvent import org.bukkit.event.entity.FireworkExplodeEvent
import org.bukkit.event.player.PlayerInteractEvent import org.jetbrains.exposed.sql.transactions.transaction
import taboolib.common.platform.event.SubscribeEvent import taboolib.common.platform.event.SubscribeEvent
object FireworkListener { object FireworkListener {
val breakCache = ArrayList<Location>(10000000)
@SubscribeEvent @SubscribeEvent
fun onFirework(e: FireworkExplodeEvent) { fun onFirework(e: FireworkExplodeEvent) {
val shooter = e.entity.shooter val shooter = e.entity.shooter
if (shooter !is Player) return if (shooter !is Player) return
val fireworkData = e.entity.fireworkMeta var score = Calculator.calculateScore(e.entity.fireworkMeta)
shooter.sendMessage("§b|") shooter.sendMessage("§6| §7恭喜您获得 §6$score 花火点")
shooter.sendMessage("§b|") transaction {
if (fireworkData.effectsSize == 0) { val current: PlayerFireworkScore =
shooter.sendMessage("§b| §7效果: 无") PlayerFireworkScore.findById(shooter.uniqueId) ?: PlayerFireworkScore.new(shooter.uniqueId) {
} else { name = shooter.name
shooter.sendMessage("§b| §7效果: ") score = 0
fireworkData.effects.forEach {
val typeName = when (it.type) {
BALL -> "球状"
BALL_LARGE -> "大型球状(火焰弹)"
STAR -> "星型(金粒)"
BURST -> "爆裂型爆炸(羽毛)"
CREEPER -> "苦力怕(头颅)"
} }
shooter.sendMessage("§b| - §7种类: §b${typeName}") current.score = current.score + score
if (it.hasTrail()) shooter.sendMessage("§b| §7轨迹(钻石)") shooter.sendMessage("§6| §7您目前的花火点为: §6${PlayerFireworkScore[shooter.uniqueId].score}")
if (it.hasFlicker()) shooter.sendMessage("§b| §7闪烁(荧石粉)")
}
} }
shooter.sendMessage("§b| ")
shooter.sendMessage("§b| §7高度: §b${fireworkData.power}")
shooter.sendMessage("§6| §7花火点: §6${Calculator.calculateScore(e.entity.fireworkMeta)}")
shooter.sendMessage("§b|")
shooter.sendMessage("§b|")
}
@SubscribeEvent
fun onPlace(e: BlockPlaceEvent) {
if (e.blockPlaced.type == Material.TNT) {
e.player.sendMessage("§e| §7抱歉, 不能这么干呢.")
e.isCancelled = true
}
}
@SubscribeEvent
fun onInteract(e: PlayerInteractEvent) {
if (e.item?.type == Material.END_CRYSTAL) {
e.player.sendMessage("§e| §7抱歉, 不能这么干呢.")
e.isCancelled = true
}
}
@SubscribeEvent
fun onExplode(e: ExplosionPrimeEvent) {
e.isCancelled = true
}
@SubscribeEvent
fun onPlacePlayer(e: BlockPlaceEvent) {
if (e.player.isOp) return
breakCache.add(e.block.location)
}
@SubscribeEvent
fun onDestroyBlock(e: BlockBreakEvent) {
if (e.player.isOp) return
if (breakCache.contains(e.block.location)) {
breakCache.remove(e.block.location)
return
}
e.isCancelled = true
e.player.sendMessage("§e| §7抱歉, 不能这么干呢.")
}
@SubscribeEvent
fun onRedstone(e: BlockFromToEvent) {
e.isCancelled = true
} }
} }

View File

@ -1,6 +1,5 @@
package cc.maxmc.fwc.utils package cc.maxmc.fwc.utils
import org.bukkit.Bukkit
import org.bukkit.FireworkEffect import org.bukkit.FireworkEffect
import org.bukkit.FireworkEffect.Type.* import org.bukkit.FireworkEffect.Type.*
import org.bukkit.inventory.meta.FireworkMeta import org.bukkit.inventory.meta.FireworkMeta
@ -57,7 +56,7 @@ object Calculator {
* @param roundingMode 舍入规则 * @param roundingMode 舍入规则
* @return 结果 * @return 结果
*/ */
fun bigRoot(number: BigDecimal, n: Int, scale: Int, roundingMode: Int): BigDecimal { private fun bigRoot(number: BigDecimal, n: Int, scale: Int, roundingMode: Int): BigDecimal {
var number = number var number = number
var negate = false var negate = false
if (n < 0) throw ArithmeticException() if (n < 0) throw ArithmeticException()

View File

@ -0,0 +1,21 @@
package cc.maxmc.fwc.utils
import cc.maxmc.fwc.database.PlayerFireworkScore
import org.bukkit.entity.Player
import org.jetbrains.exposed.sql.transactions.transaction
import taboolib.platform.compat.PlaceholderExpansion
object PlaceholderExp: PlaceholderExpansion {
override val identifier: String = "firework"
override fun onPlaceholderRequest(player: Player?, args: String): String {
player?: return ""
if (args == "score") {
val score = transaction {
return@transaction PlayerFireworkScore.findById(player.uniqueId)?.score ?: 0
}
return score.toString()
}
return ""
}
}