add Message spam

optimize Converter
fix Error while shutdown
This commit is contained in:
TONY_All 2022-02-09 13:56:54 +08:00
parent 363457deba
commit c3464cbd99
6 changed files with 93 additions and 32 deletions

View File

@ -20,6 +20,7 @@ taboolib {
install("common") install("common")
install("platform-bukkit") install("platform-bukkit")
install("module-chat")
version = "6.0.7-26" version = "6.0.7-26"
} }
@ -39,5 +40,5 @@ dependencies {
} }
tasks.withType<KotlinCompile> { tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "16" kotlinOptions.jvmTarget = "11"
} }

View File

@ -3,15 +3,14 @@ package cc.maxmc.invite
import cc.maxmc.invite.command.InviteCodeCommands import cc.maxmc.invite.command.InviteCodeCommands
import cc.maxmc.invite.data.convert import cc.maxmc.invite.data.convert
import cc.maxmc.invite.data.initDatabase import cc.maxmc.invite.data.initDatabase
import kotlinx.coroutines.CoroutineExceptionHandler import cc.maxmc.invite.listener.InvitedListener
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.*
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import org.bukkit.Bukkit import org.bukkit.Bukkit
import taboolib.common.env.RuntimeDependencies import taboolib.common.env.RuntimeDependencies
import taboolib.common.env.RuntimeDependency import taboolib.common.env.RuntimeDependency
import taboolib.common.platform.Plugin import taboolib.common.platform.Plugin
import taboolib.common.platform.function.console import taboolib.common.platform.function.console
import taboolib.common.platform.function.info
import taboolib.platform.BukkitPlugin import taboolib.platform.BukkitPlugin
import java.util.concurrent.Executor import java.util.concurrent.Executor
@ -27,16 +26,22 @@ object BiliInviteCode : Plugin() {
override fun onEnable() { override fun onEnable() {
initDatabase() initDatabase()
InviteCodeCommands InviteCodeCommands
convert()
// cc.maxmc.invite.command.DebugCommands.debug() // cc.maxmc.invite.command.DebugCommands.debug()
} }
override fun onDisable() {
PluginScope.cancel("Server Shutdown")
info("Plugin Scope Down")
}
} }
val bukkitSyncContext = Executor { val bukkitSyncContext = Executor {
if (!BukkitPlugin.getInstance().isEnabled) return@Executor
Bukkit.getScheduler().runTask(BukkitPlugin.getInstance(), it) Bukkit.getScheduler().runTask(BukkitPlugin.getInstance(), it)
}.asCoroutineDispatcher() }.asCoroutineDispatcher()
val bukkitAsyncContext = Executor { val bukkitAsyncContext = Executor {
if (!BukkitPlugin.getInstance().isEnabled) return@Executor
Bukkit.getScheduler().runTaskAsynchronously(BukkitPlugin.getInstance(), it) Bukkit.getScheduler().runTaskAsynchronously(BukkitPlugin.getInstance(), it)
}.asCoroutineDispatcher() }.asCoroutineDispatcher()
@ -46,4 +51,5 @@ val PluginScope = CoroutineScope(bukkitAsyncContext + SupervisorJob() + Coroutin
except.stackTrace.forEach { except.stackTrace.forEach {
console().sendMessage("§c位于 $it") console().sendMessage("§c位于 $it")
} }
except.printStackTrace()
}) })

View File

@ -17,10 +17,16 @@ import org.bukkit.command.ConsoleCommandSender
import org.bukkit.entity.Player import org.bukkit.entity.Player
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.command.CommandBuilder import taboolib.common.platform.command.CommandBuilder
import taboolib.common.platform.command.PermissionDefault
import taboolib.common.platform.command.command import taboolib.common.platform.command.command
import taboolib.common.platform.function.adaptCommandSender
import taboolib.common.platform.function.getDataFolder import taboolib.common.platform.function.getDataFolder
import taboolib.common.platform.function.info import taboolib.common.platform.function.info
import taboolib.module.chat.TellrawJson
import taboolib.platform.type.BukkitCommandSender
import taboolib.platform.type.BukkitPlayer
import java.io.File import java.io.File
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@ -28,7 +34,7 @@ import java.util.*
object InviteCodeCommands { object InviteCodeCommands {
init { init {
command("inviteCode", aliases = listOf("code", "icode", "ic")) { command("inviteCode", aliases = listOf("code", "icode", "ic"), permissionDefault = PermissionDefault.TRUE) {
cmdUse() cmdUse()
cmdGenerate() cmdGenerate()
cmdEdit() cmdEdit()
@ -43,7 +49,10 @@ object InviteCodeCommands {
tailrec suspend fun requireQQNumber(player: Player): String? { tailrec suspend fun requireQQNumber(player: Player): String? {
val input = chatInput(player, "§e| §7请输入您的QQ号进行绑定 (输入 §ecancel §7取消)") val input = chatInput(player, "§e| §7请输入您的QQ号进行绑定 (输入 §ecancel §7取消)")
if (input == "cancel") return null if (input == "cancel") return null
if (input.length > 13) return requireQQNumber(player) if (input.length > 10) {
player.sendMessage("§c| §7您的QQ号长度有误(最长10位),请重试.")
return requireQQNumber(player)
}
if (input.toLongOrNull() == null) return requireQQNumber(player) if (input.toLongOrNull() == null) return requireQQNumber(player)
return input return input
} }
@ -74,7 +83,10 @@ object InviteCodeCommands {
.let { null } .let { null }
return@newSuspendedTransaction inviteCode return@newSuspendedTransaction inviteCode
} ?: return@launch } ?: return@launch
val qqNum = requireQQNumber(sender) ?: return@launch sender.sendMessage("§e| §7已取消激活验证码.") InvitedListener.send.remove(sender.uniqueId)
val qqNum = runCatching {
requireQQNumber(sender) ?: return@launch sender.sendMessage("§e| §7已取消激活验证码.").also { InvitedListener.send.add(sender.uniqueId) }
}.getOrNull() ?: return@launch InvitedListener.cache.remove(sender.uniqueId).let { }
sender.sendMessage("§a| §7该激活码成功绑定至QQ: §a${qqNum}") sender.sendMessage("§a| §7该激活码成功绑定至QQ: §a${qqNum}")
InvitedListener.cache.remove(sender.uniqueId) InvitedListener.cache.remove(sender.uniqueId)
transaction { transaction {
@ -241,7 +253,13 @@ object InviteCodeCommands {
newSuspendedTransaction { newSuspendedTransaction {
val code = InviteCode.find { InviteCodes.name eq arg }.firstOrNull() val code = InviteCode.find { InviteCodes.name eq arg }.firstOrNull()
?: return@newSuspendedTransaction sender.sendMessage("§c| §7该玩家不存在") ?: return@newSuspendedTransaction sender.sendMessage("§c| §7该玩家不存在")
sender.sendMessage("§a| §7该玩家绑定的QQ为: §a${code.qq}") if (code.qq == null) {
return@newSuspendedTransaction sender.sendMessage("§e| §7该玩家暂未绑定QQ.")
}
TellrawJson().run {
append("§a| §7该玩家绑定的QQ为: ")
append("§a${code.qq}").hoverText("(§e点击复制§r)").copyToClipboard(code.qq!!)
}.sendTo(adaptCommandSender(sender))
} }
} }
} }

View File

@ -1,16 +1,25 @@
package cc.maxmc.invite.data package cc.maxmc.invite.data
import cc.maxmc.invite.PluginScope import cc.maxmc.invite.PluginScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.bukkit.Bukkit import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.configuration.file.YamlConfiguration import org.bukkit.configuration.file.YamlConfiguration
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.batchInsert
import org.jetbrains.exposed.sql.transactions.experimental.suspendedTransactionAsync
import taboolib.common.platform.function.getDataFolder import taboolib.common.platform.function.getDataFolder
import taboolib.common.platform.function.info import taboolib.common.platform.function.info
import java.io.File import java.io.File
import java.nio.file.Files import java.nio.file.Files
import java.time.Duration
import java.util.*
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.ExecutorService
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
val convertDispatcher = newFixedThreadPoolContext(200, "Convert")
fun convert() { fun convert() {
val file = File("./plugins/InviteCode/config.yml") val file = File("./plugins/InviteCode/config.yml")
@ -22,17 +31,25 @@ fun convert() {
.toList() .toList()
.associate { it.first to it.second!! } .associate { it.first to it.second!! }
PluginScope.launch { PluginScope.launch {
transaction { info("§a| §7开始转换")
usable.forEach { (code, player) -> val start = System.nanoTime()
val uuid = Bukkit.getOfflinePlayer(player).uniqueId val defData = CopyOnWriteArrayList<Deferred<Pair<OfflinePlayer, String>>>()
InviteCode.new { usable.forEach { (k, v) ->
inviteCode = code val deferred = async(convertDispatcher) { Bukkit.getOfflinePlayer(v).also { info("§b| §7在线程 ${Thread.currentThread().name} 加载 ${it.name} 的数据") } to k }
uid = uuid defData.add(deferred)
name = player }
} val data = defData.map { Triple(it.await().first.name, it.await().first.uniqueId, it.await().second) }
info("§a| §7UID转换成功! (耗时: ${TimeUnit.MILLISECONDS.convert(Duration.ofNanos(System.nanoTime() - start))} ms)")
val suspendedTransactionAsync = suspendedTransactionAsync {
InviteCodes.batchInsert(data) {
this[InviteCodes.name] = it.first
this[InviteCodes.uid] = it.second
this[InviteCodes.inviteCode] = it.third
} }
} }
info("§a| §7转换成功!") suspendedTransactionAsync.await()
info("§a| §7转换成功! (耗时: ${TimeUnit.MILLISECONDS.convert(Duration.ofNanos(System.nanoTime() - start))} ms)")
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
Files.move(file.toPath(), File(getDataFolder(), "legacy.yml.deprecated").toPath()) Files.move(file.toPath(), File(getDataFolder(), "legacy.yml.deprecated").toPath())
} }

View File

@ -35,6 +35,7 @@ fun initDatabase() {
SchemaUtils.create(InviteCodes, TimeTable) SchemaUtils.create(InviteCodes, TimeTable)
info("§a| §7成功读取数据库信息.") info("§a| §7成功读取数据库信息.")
} }
convert()
} }
} }

View File

@ -6,14 +6,15 @@ import cc.maxmc.invite.concurrent.chatInput
import cc.maxmc.invite.data.InviteCode import cc.maxmc.invite.data.InviteCode
import cc.maxmc.invite.data.InviteCodes import cc.maxmc.invite.data.InviteCodes
import cc.maxmc.invite.data.LastLogout import cc.maxmc.invite.data.LastLogout
import kotlinx.coroutines.launch import kotlinx.coroutines.*
import kotlinx.coroutines.withContext import org.bukkit.Bukkit
import org.bukkit.Location import org.bukkit.Location
import org.bukkit.entity.Player import org.bukkit.entity.Player
import org.bukkit.event.player.* import org.bukkit.event.player.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import taboolib.common.platform.event.SubscribeEvent import taboolib.common.platform.event.SubscribeEvent
import taboolib.common.platform.function.console import taboolib.common.platform.function.console
import taboolib.common.platform.function.info
import java.time.LocalDate import java.time.LocalDate
import java.util.* import java.util.*
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
@ -21,12 +22,21 @@ import java.util.concurrent.ConcurrentHashMap
object InvitedListener { object InvitedListener {
val cache = ConcurrentHashMap<UUID, Location>() val cache = ConcurrentHashMap<UUID, Location>()
val send = ArrayList<UUID>()
val job = PluginScope.launch {
while (isActive) {
delay(1500)
send.forEach { uid ->
val player = Bukkit.getPlayer(uid) ?: return@launch send.remove(uid).let { }
player.sendMessage("§c| §7您的账号未激活, 请输入 §c/ic use <邀请码> §7来进行激活.")
}
}
}
@SubscribeEvent @SubscribeEvent
fun onMove(e: PlayerMoveEvent) { fun onMove(e: PlayerMoveEvent) {
if (!cache.containsKey(e.player.uniqueId)) return cache[e.player.uniqueId]?.let { e.player.teleport(it) }
if (e.player.location.distance(cache[e.player.uniqueId]!!) > 5) {
e.player.teleport(cache[e.player.uniqueId]!!)
}
} }
@SubscribeEvent @SubscribeEvent
@ -63,7 +73,10 @@ object InvitedListener {
tailrec suspend fun requireQQNumber(player: Player): String? { tailrec suspend fun requireQQNumber(player: Player): String? {
val input = chatInput(player, "§e| §7请输入您的QQ号进行绑定 (输入 §ecancel §7取消)") val input = chatInput(player, "§e| §7请输入您的QQ号进行绑定 (输入 §ecancel §7取消)")
if (input == "cancel") return null if (input == "cancel") return null
if (input.length > 13) return requireQQNumber(player) if (input.length > 10) {
player.sendMessage("§c| §7您的QQ号长度有误(最长10位),请重试.")
return requireQQNumber(player)
}
if (input.toLongOrNull() == null) return requireQQNumber(player) if (input.toLongOrNull() == null) return requireQQNumber(player)
return input return input
} }
@ -85,7 +98,8 @@ object InvitedListener {
newSuspendedTransaction { newSuspendedTransaction {
val playerInviteCode = InviteCode.find { InviteCodes.uid eq e.player.uniqueId } val playerInviteCode = InviteCode.find { InviteCodes.uid eq e.player.uniqueId }
.firstOrNull() .firstOrNull()
?: return@newSuspendedTransaction e.player.sendMessage("§c| §7您的账号未激活, 请输入 §c/ic use <激活码> §7来进行激活.") ?: return@newSuspendedTransaction e.player.sendMessage("§c| §7您的账号未激活, 请输入 §c/ic use <邀请码> §7来进行激活.")
.also { send.add(e.player.uniqueId) }
if (!checkTime(e.player.uniqueId)) { if (!checkTime(e.player.uniqueId)) {
withContext(bukkitSyncContext) { withContext(bukkitSyncContext) {
e.player.kickPlayer("§c| §7由于超过3个月未登录游戏, 您的邀请码已失效.") e.player.kickPlayer("§c| §7由于超过3个月未登录游戏, 您的邀请码已失效.")
@ -94,7 +108,11 @@ object InvitedListener {
return@newSuspendedTransaction return@newSuspendedTransaction
} }
if (playerInviteCode.qq == null) { if (playerInviteCode.qq == null) {
playerInviteCode.qq = requireQQNumber(e.player) runCatching {
playerInviteCode.qq = requireQQNumber(e.player) ?: return@newSuspendedTransaction withContext(bukkitSyncContext) {
e.player.kickPlayer("§e| §7您需要绑定QQ号以正常进行游戏.")
}
}.exceptionOrNull()?.run { cache.remove(e.player.uniqueId) }
} }
e.player.sendMessage("§a| §7检查通过, 您可以正常进行游戏.") e.player.sendMessage("§a| §7检查通过, 您可以正常进行游戏.")
cache.remove(e.player.uniqueId) cache.remove(e.player.uniqueId)