From 363457debaa72fe9471b0d291a469e4a6bf9b35d Mon Sep 17 00:00:00 2001 From: TONY_All Date: Sat, 5 Feb 2022 23:01:35 +0800 Subject: [PATCH] all bugs free --- .../invite/command/InviteCodeCommands.kt | 53 ++++++++++--------- .../kotlin/cc/maxmc/invite/data/Converter.kt | 2 +- .../cc/maxmc/invite/data/InviteCodeData.kt | 2 +- .../maxmc/invite/listener/InvitedListener.kt | 16 +++++- 4 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt b/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt index 64272ae..529290b 100644 --- a/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt +++ b/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt @@ -1,6 +1,7 @@ package cc.maxmc.invite.command import cc.maxmc.invite.PluginScope +import cc.maxmc.invite.bukkitSyncContext import cc.maxmc.invite.concurrent.chatInput import cc.maxmc.invite.data.InviteCode import cc.maxmc.invite.data.InviteCodes @@ -34,6 +35,7 @@ object InviteCodeCommands { cmdAdd() cmdRemove() cmdCurrent() + cmdInspect() } } @@ -51,24 +53,26 @@ object InviteCodeCommands { if (!InvitedListener.cache.containsKey(sender.uniqueId)) return@execute sender.sendMessage("§e| §7您的账号已激活, 无需使用邀请码.") sender.sendMessage("§b| §7正在激活邀请码...") PluginScope.launch { - val inviteCode = transaction { + val inviteCode = newSuspendedTransaction { val inviteCode = InviteCode.find { InviteCodes.inviteCode eq argument }.firstOrNull() - ?: return@transaction let { + ?: return@newSuspendedTransaction let { val trial = userTrials.computeIfAbsent(sender.uniqueId) { UserTrial(it) } trial.tryOnce() if (trial.isLimited()) { userTrials.remove(sender.uniqueId) - sender.kickPlayer("§c| §7您的尝试次数过多.") + withContext(bukkitSyncContext) { + sender.kickPlayer("§c| §7您的尝试次数过多.") + } return@let null } sender.sendMessage("§c| §7该激活码不存在, 请重试.") null } - if (inviteCode.name != null) return@transaction sender.sendMessage("§c| §7该邀请码已被激活, 请重试.") + if (inviteCode.name != null) return@newSuspendedTransaction sender.sendMessage("§c| §7该邀请码已被激活, 请重试.") .let { null } - return@transaction inviteCode + return@newSuspendedTransaction inviteCode } ?: return@launch val qqNum = requireQQNumber(sender) ?: return@launch sender.sendMessage("§e| §7已取消激活验证码.") sender.sendMessage("§a| §7该激活码成功绑定至QQ: §a${qqNum}") @@ -141,33 +145,34 @@ object InviteCodeCommands { } } - private fun CommandBuilder.CommandComponent.cmdEdit() = literal("edit", permission = "inviteCode.edit".lowercase()) { - dynamic { - restrict { _, _, arg -> - transaction { - !InviteCode.find { InviteCodes.name eq arg }.empty() - } - } - suggestion(uncheck = true) { _, _ -> - return@suggestion Bukkit.getOnlinePlayers().map { it.name } - } + private fun CommandBuilder.CommandComponent.cmdEdit() = + literal("edit", permission = "inviteCode.edit".lowercase()) { dynamic { restrict { _, _, arg -> - arg.length <= 10 && arg.toLongOrNull() != null + transaction { + !InviteCode.find { InviteCodes.name eq arg }.empty() + } } - execute { sender, ctx, arg -> - PluginScope.launch { - transaction { - InviteCode.find { InviteCodes.name eq ctx.argument(-1) }.first().run { - qq = arg + suggestion(uncheck = true) { _, _ -> + return@suggestion Bukkit.getOnlinePlayers().map { it.name } + } + dynamic { + restrict { _, _, arg -> + arg.length <= 10 && arg.toLongOrNull() != null + } + execute { sender, ctx, arg -> + PluginScope.launch { + transaction { + InviteCode.find { InviteCodes.name eq ctx.argument(-1) }.first().run { + qq = arg + } + sender.sendMessage("§a| §7修改成功") } - sender.sendMessage("§a| §7修改成功") } } } } } - } private fun CommandBuilder.CommandComponent.cmdAdd() = literal("add") { dynamic { @@ -220,7 +225,7 @@ object InviteCodeCommands { output.createNewFile() output.writeText(result) } - sender.sendMessage("§a| §7现存可用邀请码已保存至plugins/BiliInviteCode/current.txt") + sender.sendMessage("§a| §7现存可用邀请码已保存至 §aplugins/BiliInviteCode/current.txt") } } } diff --git a/src/main/kotlin/cc/maxmc/invite/data/Converter.kt b/src/main/kotlin/cc/maxmc/invite/data/Converter.kt index 23a71ac..5f13d88 100644 --- a/src/main/kotlin/cc/maxmc/invite/data/Converter.kt +++ b/src/main/kotlin/cc/maxmc/invite/data/Converter.kt @@ -14,8 +14,8 @@ import java.nio.file.Files fun convert() { val file = File("./plugins/InviteCode/config.yml") - info("§b| §7找到旧版配置文件, 正在转换...") if (!file.exists()) return + info("§b| §7找到旧版配置文件, 正在转换...") val read = readFile(file) val usable = read.filter { it.value != null } .filter { it.value != "null" } diff --git a/src/main/kotlin/cc/maxmc/invite/data/InviteCodeData.kt b/src/main/kotlin/cc/maxmc/invite/data/InviteCodeData.kt index 494fda5..2bd81b4 100644 --- a/src/main/kotlin/cc/maxmc/invite/data/InviteCodeData.kt +++ b/src/main/kotlin/cc/maxmc/invite/data/InviteCodeData.kt @@ -32,7 +32,7 @@ fun initDatabase() { PluginScope.launch { transaction { info("§b| §7正在读取数据库信息...") - SchemaUtils.create(InviteCodes) + SchemaUtils.create(InviteCodes, TimeTable) info("§a| §7成功读取数据库信息.") } } diff --git a/src/main/kotlin/cc/maxmc/invite/listener/InvitedListener.kt b/src/main/kotlin/cc/maxmc/invite/listener/InvitedListener.kt index ca9efff..ece2e01 100644 --- a/src/main/kotlin/cc/maxmc/invite/listener/InvitedListener.kt +++ b/src/main/kotlin/cc/maxmc/invite/listener/InvitedListener.kt @@ -1,11 +1,13 @@ package cc.maxmc.invite.listener import cc.maxmc.invite.PluginScope +import cc.maxmc.invite.bukkitSyncContext import cc.maxmc.invite.concurrent.chatInput import cc.maxmc.invite.data.InviteCode import cc.maxmc.invite.data.InviteCodes import cc.maxmc.invite.data.LastLogout import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.bukkit.Location import org.bukkit.entity.Player import org.bukkit.event.player.* @@ -66,10 +68,14 @@ object InvitedListener { return input } + /** + * @return if player passed time check + */ suspend fun checkTime(uid: UUID): Boolean { return newSuspendedTransaction { + // pass if not exists. val lastLogout = LastLogout.findById(uid) ?: return@newSuspendedTransaction true - lastLogout.last.plusMonths(3).isBefore(LocalDate.now()) + lastLogout.last.plusMonths(3).isAfter(LocalDate.now()) } } @@ -81,7 +87,9 @@ object InvitedListener { .firstOrNull() ?: return@newSuspendedTransaction e.player.sendMessage("§c| §7您的账号未激活, 请输入 §c/ic use <激活码> §7来进行激活.") if (!checkTime(e.player.uniqueId)) { - e.player.kickPlayer("§c| §7由于超过3个月未登录游戏, 您的邀请码已失效.") + withContext(bukkitSyncContext) { + e.player.kickPlayer("§c| §7由于超过3个月未登录游戏, 您的邀请码已失效.") + } playerInviteCode.delete() return@newSuspendedTransaction } @@ -101,6 +109,10 @@ object InvitedListener { @SubscribeEvent fun onQuit(e: PlayerQuitEvent) { + if (cache.containsKey(e.player.uniqueId)) { + e.player.teleport(cache[e.player.uniqueId]!!) + return + } PluginScope.launch { newSuspendedTransaction { val lastLogout = LastLogout.findById(e.player.uniqueId)