all bugs free

This commit is contained in:
TONY_All 2022-02-05 23:01:35 +08:00
parent 585761ae9c
commit 363457deba
4 changed files with 45 additions and 28 deletions

View File

@ -1,6 +1,7 @@
package cc.maxmc.invite.command package cc.maxmc.invite.command
import cc.maxmc.invite.PluginScope import cc.maxmc.invite.PluginScope
import cc.maxmc.invite.bukkitSyncContext
import cc.maxmc.invite.concurrent.chatInput 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
@ -34,6 +35,7 @@ object InviteCodeCommands {
cmdAdd() cmdAdd()
cmdRemove() cmdRemove()
cmdCurrent() cmdCurrent()
cmdInspect()
} }
} }
@ -51,24 +53,26 @@ object InviteCodeCommands {
if (!InvitedListener.cache.containsKey(sender.uniqueId)) return@execute sender.sendMessage("§e| §7您的账号已激活, 无需使用邀请码.") if (!InvitedListener.cache.containsKey(sender.uniqueId)) return@execute sender.sendMessage("§e| §7您的账号已激活, 无需使用邀请码.")
sender.sendMessage("§b| §7正在激活邀请码...") sender.sendMessage("§b| §7正在激活邀请码...")
PluginScope.launch { PluginScope.launch {
val inviteCode = transaction { val inviteCode = newSuspendedTransaction {
val inviteCode = InviteCode.find { InviteCodes.inviteCode eq argument }.firstOrNull() val inviteCode = InviteCode.find { InviteCodes.inviteCode eq argument }.firstOrNull()
?: return@transaction let { ?: return@newSuspendedTransaction let {
val trial = userTrials.computeIfAbsent(sender.uniqueId) { val trial = userTrials.computeIfAbsent(sender.uniqueId) {
UserTrial(it) UserTrial(it)
} }
trial.tryOnce() trial.tryOnce()
if (trial.isLimited()) { if (trial.isLimited()) {
userTrials.remove(sender.uniqueId) userTrials.remove(sender.uniqueId)
sender.kickPlayer("§c| §7您的尝试次数过多.") withContext(bukkitSyncContext) {
sender.kickPlayer("§c| §7您的尝试次数过多.")
}
return@let null return@let null
} }
sender.sendMessage("§c| §7该激活码不存在, 请重试.") sender.sendMessage("§c| §7该激活码不存在, 请重试.")
null null
} }
if (inviteCode.name != null) return@transaction sender.sendMessage("§c| §7该邀请码已被激活, 请重试.") if (inviteCode.name != null) return@newSuspendedTransaction sender.sendMessage("§c| §7该邀请码已被激活, 请重试.")
.let { null } .let { null }
return@transaction inviteCode return@newSuspendedTransaction inviteCode
} ?: return@launch } ?: return@launch
val qqNum = requireQQNumber(sender) ?: return@launch sender.sendMessage("§e| §7已取消激活验证码.") val qqNum = requireQQNumber(sender) ?: return@launch sender.sendMessage("§e| §7已取消激活验证码.")
sender.sendMessage("§a| §7该激活码成功绑定至QQ: §a${qqNum}") sender.sendMessage("§a| §7该激活码成功绑定至QQ: §a${qqNum}")
@ -141,33 +145,34 @@ object InviteCodeCommands {
} }
} }
private fun CommandBuilder.CommandComponent.cmdEdit() = literal("edit", permission = "inviteCode.edit".lowercase()) { private fun CommandBuilder.CommandComponent.cmdEdit() =
dynamic { literal("edit", permission = "inviteCode.edit".lowercase()) {
restrict<ConsoleCommandSender> { _, _, arg ->
transaction {
!InviteCode.find { InviteCodes.name eq arg }.empty()
}
}
suggestion<ConsoleCommandSender>(uncheck = true) { _, _ ->
return@suggestion Bukkit.getOnlinePlayers().map { it.name }
}
dynamic { dynamic {
restrict<ConsoleCommandSender> { _, _, arg -> restrict<ConsoleCommandSender> { _, _, arg ->
arg.length <= 10 && arg.toLongOrNull() != null transaction {
!InviteCode.find { InviteCodes.name eq arg }.empty()
}
} }
execute<ConsoleCommandSender> { sender, ctx, arg -> suggestion<ConsoleCommandSender>(uncheck = true) { _, _ ->
PluginScope.launch { return@suggestion Bukkit.getOnlinePlayers().map { it.name }
transaction { }
InviteCode.find { InviteCodes.name eq ctx.argument(-1) }.first().run { dynamic {
qq = arg restrict<ConsoleCommandSender> { _, _, arg ->
arg.length <= 10 && arg.toLongOrNull() != null
}
execute<ConsoleCommandSender> { 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") { private fun CommandBuilder.CommandComponent.cmdAdd() = literal("add") {
dynamic { dynamic {
@ -220,7 +225,7 @@ object InviteCodeCommands {
output.createNewFile() output.createNewFile()
output.writeText(result) output.writeText(result)
} }
sender.sendMessage("§a| §7现存可用邀请码已保存至plugins/BiliInviteCode/current.txt") sender.sendMessage("§a| §7现存可用邀请码已保存至 §aplugins/BiliInviteCode/current.txt")
} }
} }
} }

View File

@ -14,8 +14,8 @@ import java.nio.file.Files
fun convert() { fun convert() {
val file = File("./plugins/InviteCode/config.yml") val file = File("./plugins/InviteCode/config.yml")
info("§b| §7找到旧版配置文件, 正在转换...")
if (!file.exists()) return if (!file.exists()) return
info("§b| §7找到旧版配置文件, 正在转换...")
val read = readFile(file) val read = readFile(file)
val usable = read.filter { it.value != null } val usable = read.filter { it.value != null }
.filter { it.value != "null" } .filter { it.value != "null" }

View File

@ -32,7 +32,7 @@ fun initDatabase() {
PluginScope.launch { PluginScope.launch {
transaction { transaction {
info("§b| §7正在读取数据库信息...") info("§b| §7正在读取数据库信息...")
SchemaUtils.create(InviteCodes) SchemaUtils.create(InviteCodes, TimeTable)
info("§a| §7成功读取数据库信息.") info("§a| §7成功读取数据库信息.")
} }
} }

View File

@ -1,11 +1,13 @@
package cc.maxmc.invite.listener package cc.maxmc.invite.listener
import cc.maxmc.invite.PluginScope import cc.maxmc.invite.PluginScope
import cc.maxmc.invite.bukkitSyncContext
import cc.maxmc.invite.concurrent.chatInput 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.launch
import kotlinx.coroutines.withContext
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.*
@ -66,10 +68,14 @@ object InvitedListener {
return input return input
} }
/**
* @return if player passed time check
*/
suspend fun checkTime(uid: UUID): Boolean { suspend fun checkTime(uid: UUID): Boolean {
return newSuspendedTransaction { return newSuspendedTransaction {
// pass if not exists.
val lastLogout = LastLogout.findById(uid) ?: return@newSuspendedTransaction true 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() .firstOrNull()
?: return@newSuspendedTransaction e.player.sendMessage("§c| §7您的账号未激活, 请输入 §c/ic use <激活码> §7来进行激活.") ?: return@newSuspendedTransaction e.player.sendMessage("§c| §7您的账号未激活, 请输入 §c/ic use <激活码> §7来进行激活.")
if (!checkTime(e.player.uniqueId)) { if (!checkTime(e.player.uniqueId)) {
e.player.kickPlayer("§c| §7由于超过3个月未登录游戏, 您的邀请码已失效.") withContext(bukkitSyncContext) {
e.player.kickPlayer("§c| §7由于超过3个月未登录游戏, 您的邀请码已失效.")
}
playerInviteCode.delete() playerInviteCode.delete()
return@newSuspendedTransaction return@newSuspendedTransaction
} }
@ -101,6 +109,10 @@ object InvitedListener {
@SubscribeEvent @SubscribeEvent
fun onQuit(e: PlayerQuitEvent) { fun onQuit(e: PlayerQuitEvent) {
if (cache.containsKey(e.player.uniqueId)) {
e.player.teleport(cache[e.player.uniqueId]!!)
return
}
PluginScope.launch { PluginScope.launch {
newSuspendedTransaction { newSuspendedTransaction {
val lastLogout = LastLogout.findById(e.player.uniqueId) val lastLogout = LastLogout.findById(e.player.uniqueId)