From 420a2da064b087b72f9647fe5ed67fa249aed99e Mon Sep 17 00:00:00 2001 From: TONY_All Date: Sat, 5 Feb 2022 16:32:11 +0800 Subject: [PATCH] add autokick --- .../maxmc/invite/command/InviteCodeCommands.kt | 16 +++++++++++++++- .../cc/maxmc/invite/data/InviteCodeData.kt | 9 +++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt b/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt index 134d887..844f8fb 100644 --- a/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt +++ b/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt @@ -4,6 +4,8 @@ import cc.maxmc.invite.PluginScope import cc.maxmc.invite.concurrent.chatInput import cc.maxmc.invite.data.InviteCode import cc.maxmc.invite.data.InviteCodes +import cc.maxmc.invite.data.UserTrial +import cc.maxmc.invite.data.userTrials import cc.maxmc.invite.listener.InvitedListener import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -49,7 +51,19 @@ object InviteCodeCommands { PluginScope.launch { val inviteCode = transaction { val inviteCode = InviteCode.find { InviteCodes.inviteCode eq argument }.firstOrNull() - ?: return@transaction let { sender.sendMessage("§c| §7该激活码不存在, 请重试."); null } + ?: return@transaction let { + val trial = userTrials.computeIfAbsent(sender.uniqueId) { + UserTrial(it) + } + trial.tryOnce() + if (trial.isLimited()) { + userTrials.remove(sender.uniqueId) + sender.kickPlayer("§c| §7您的尝试次数过多.") + return@let null + } + sender.sendMessage("§c| §7该激活码不存在, 请重试.") + null + } if (inviteCode.name != null) return@transaction sender.sendMessage("§c| §7该邀请码已被激活, 请重试.") .let { null } return@transaction inviteCode diff --git a/src/main/kotlin/cc/maxmc/invite/data/InviteCodeData.kt b/src/main/kotlin/cc/maxmc/invite/data/InviteCodeData.kt index 37e2fdc..cfcad09 100644 --- a/src/main/kotlin/cc/maxmc/invite/data/InviteCodeData.kt +++ b/src/main/kotlin/cc/maxmc/invite/data/InviteCodeData.kt @@ -15,6 +15,8 @@ import taboolib.common.platform.function.info import taboolib.common.platform.function.warning import taboolib.platform.BukkitPlugin import java.io.File +import java.util.* +import kotlin.collections.HashMap fun initDatabase() { try { @@ -48,5 +50,12 @@ class InviteCode(id: EntityID) : IntEntity(id) { var name by InviteCodes.name var uid by InviteCodes.uid var qq by InviteCodes.qq +} +val userTrials = HashMap() + +data class UserTrial(val uid: UUID, private var attempts: Int = 0) { + fun tryOnce() = attempts++ + + fun isLimited() = attempts == 3 } \ No newline at end of file