diff --git a/src/main/kotlin/cc/maxmc/invite/BiliInviteCode.kt b/src/main/kotlin/cc/maxmc/invite/BiliInviteCode.kt index 8defd5e..1df5279 100644 --- a/src/main/kotlin/cc/maxmc/invite/BiliInviteCode.kt +++ b/src/main/kotlin/cc/maxmc/invite/BiliInviteCode.kt @@ -1,6 +1,7 @@ package cc.maxmc.invite import cc.maxmc.invite.command.InviteCodeCommands +import cc.maxmc.invite.data.convert import cc.maxmc.invite.data.initDatabase import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope @@ -25,6 +26,7 @@ object BiliInviteCode : Plugin() { override fun onEnable() { initDatabase() InviteCodeCommands + convert() // cc.maxmc.invite.command.DebugCommands.debug() } } diff --git a/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt b/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt index 1b560ca..f7b4cc3 100644 --- a/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt +++ b/src/main/kotlin/cc/maxmc/invite/command/InviteCodeCommands.kt @@ -125,12 +125,14 @@ object InviteCodeCommands { } } - private fun CommandBuilder.CommandComponent.cmdEdit() = literal("edit") { + private fun CommandBuilder.CommandComponent.cmdEdit() = literal("edit", permission = "invitecode.edit") { dynamic { restrict { _, _, arg -> - !InviteCode.find { InviteCodes.name eq arg }.empty() + transaction { + !InviteCode.find { InviteCodes.name eq arg }.empty() + } } - suggestion { sender, ctx -> + suggestion { _, _ -> return@suggestion Bukkit.getOnlinePlayers().map { it.name } } dynamic { @@ -139,7 +141,7 @@ object InviteCodeCommands { } execute { sender, ctx, arg -> PluginScope.launch { - InviteCode.find { InviteCodes.name eq ctx.get(-1) }.first().run { + InviteCode.find { InviteCodes.name eq ctx.argument(-1) }.first().run { qq = arg } sender.sendMessage("§a| §7修改成功") @@ -153,29 +155,33 @@ object InviteCodeCommands { dynamic { execute { sender, _, arg -> PluginScope.launch { - if (!InviteCode.find { InviteCodes.inviteCode eq arg }.empty()) { - sender.sendMessage("§c| §7该邀请码已存在, 无法添加.") - return@launch + transaction { + if (!InviteCode.find { InviteCodes.inviteCode eq arg }.empty()) { + sender.sendMessage("§c| §7该邀请码已存在, 无法添加.") + return@transaction + } + InviteCode.new { + inviteCode = arg + } + sender.sendMessage("§a| §7成功添加一个邀请码: §a$arg") } - InviteCode.new { - inviteCode = arg - } - sender.sendMessage("§a| §7成功添加一个邀请码: §a$arg") } } } } private fun CommandBuilder.CommandComponent.cmdRemove() = literal("remove") { - execute { sender, _, arg -> - PluginScope.launch { - transaction { - val findings = InviteCode.find { InviteCodes.inviteCode eq arg } - if (findings.empty()) { - sender.sendMessage("§e| §7该邀请码不存在, 无需删除.") + dynamic { + execute { sender, _, arg -> + PluginScope.launch { + transaction { + val findings = InviteCode.find { InviteCodes.inviteCode eq arg } + if (findings.empty()) { + return@transaction sender.sendMessage("§e| §7该邀请码不存在, 无需删除.") + } + findings.first().delete() + sender.sendMessage("§a| §7成功删除该邀请码.") } - findings.first().delete() - sender.sendMessage("§a| §7成功删除该邀请码.") } } } diff --git a/src/main/kotlin/cc/maxmc/invite/data/Converter.kt b/src/main/kotlin/cc/maxmc/invite/data/Converter.kt index 492cac6..23a71ac 100644 --- a/src/main/kotlin/cc/maxmc/invite/data/Converter.kt +++ b/src/main/kotlin/cc/maxmc/invite/data/Converter.kt @@ -39,14 +39,14 @@ fun convert() { } } -fun readFile(file: File): Map { +private fun readFile(file: File): Map { val config = YamlConfiguration.loadConfiguration(file) return config.getMapList("invite-code") .associate { try { - val player = it["player"] as String? + val player = it["player"]?.toString() it.remove("player") - val code = it.values.first() as String + val code = it.values.first().toString() code to player } catch (e: Exception) { println("§e| §7一条数据转换失败, 正在忽略...")