From b884914052426e17d7690d1e162a5888e40ce34c Mon Sep 17 00:00:00 2001 From: Boy Date: Wed, 12 Jun 2024 19:47:58 +0200 Subject: [PATCH] refactor: swap to Brigadier command-logic --- .../com/mineinabyss/packy/PackyCommands.kt | 117 +++++------------- .../com/mineinabyss/packy/PackyPlugin.kt | 2 +- 2 files changed, 29 insertions(+), 90 deletions(-) diff --git a/src/main/kotlin/com/mineinabyss/packy/PackyCommands.kt b/src/main/kotlin/com/mineinabyss/packy/PackyCommands.kt index e283bb1..018101e 100644 --- a/src/main/kotlin/com/mineinabyss/packy/PackyCommands.kt +++ b/src/main/kotlin/com/mineinabyss/packy/PackyCommands.kt @@ -1,112 +1,51 @@ package com.mineinabyss.packy -import com.github.shynixn.mccoroutine.bukkit.asyncDispatcher import com.github.shynixn.mccoroutine.bukkit.launch import com.mineinabyss.guiy.inventory.guiy -import com.mineinabyss.idofront.commands.arguments.genericArg -import com.mineinabyss.idofront.commands.arguments.optionArg -import com.mineinabyss.idofront.commands.execution.IdofrontCommandExecutor -import com.mineinabyss.idofront.commands.extensions.actions.ensureSenderIsPlayer -import com.mineinabyss.idofront.commands.extensions.actions.playerAction +import com.mineinabyss.idofront.commands.brigadier.commands import com.mineinabyss.idofront.messaging.error -import com.mineinabyss.idofront.messaging.info import com.mineinabyss.idofront.messaging.success import com.mineinabyss.packy.components.packyData -import com.mineinabyss.packy.config.PackyTemplate import com.mineinabyss.packy.config.packy import com.mineinabyss.packy.menus.picker.PackyMainMenu -import org.bukkit.command.Command -import org.bukkit.command.CommandSender -import org.bukkit.command.TabCompleter -import org.bukkit.entity.Player +import io.papermc.paper.command.brigadier.argument.ArgumentTypes -class PackyCommands : IdofrontCommandExecutor(), TabCompleter { - override val commands = commands(packy.plugin) { - "packy" { - "github" { - "fetch" { - val template: PackyTemplate by genericArg(parseFunction = { passed -> - packy.templates.filter { it.value.githubDownload != null }[passed]!! - }) - action { - packy.plugin.launch(packy.plugin.asyncDispatcher) { - val templates = packy.templates.filter { it.value.githubDownload?.key() == template.githubDownload!!.key() }.values.sortedBy { it.id }.toTypedArray() - sender.info("Downloading template ${template.id}...") - PackyDownloader.updateGithubTemplate(*templates) - sender.success("Downloaded template ${template.id}!") - } +object PackyCommands { + fun registerCommands() { + packy.plugin.commands { + "packy" { + "reload" { + executes { + packy.plugin.createPackyContext() + sender.success("Packy has been reloaded!") } } - } - "reload" { - action { - packy.plugin.createPackyContext() - sender.success("Packy has been reloaded!") - } - } - "send" { - playerAction { - packy.plugin.launch { - PackyServer.sendPack(player) - sender.success("Sent pack to ${player.name}") + "menu" { + playerExecutes { + guiy { PackyMainMenu(player) } } } - } - "gui" { - playerAction { - guiy { PackyMainMenu(player) } - } - } - "server" { - val state: String by optionArg(listOf("start", "stop")) - action { - when { - state == "start" && PackyServer.packServer != null -> { - PackyServer.startServer() - sender.success("PackyServer started on ${packy.config.server.ip}:${packy.config.server.port}...") - } - else -> { - PackyServer.stopServer() - sender.error("PackyServer stopped...") + "send" { + val players by ArgumentTypes.players() + executes { + packy.plugin.launch { + players().forEach { + PackyServer.sendPack(it) + sender.success("Sent pack to ${it.name}") + } } } } - } - "bypass" { - ensureSenderIsPlayer() - action { - val player = sender as Player - player.packyData.bypassForced = !player.packyData.bypassForced - when (player.packyData.bypassForced) { - true -> sender.success("Bypassing forced pack") - else -> sender.error("No longer bypassing forced pack") + "bypass" { + playerExecutes { + player.packyData.bypassForced = !player.packyData.bypassForced + when (player.packyData.bypassForced) { + true -> sender.success("Bypassing forced pack") + else -> sender.error("No longer bypassing forced pack") + } } } } } } - - override fun onTabComplete( - sender: CommandSender, - command: Command, - alias: String, - args: Array - ): List { - return if (command.name == "packy") { - when (args.size) { - 1 -> listOf("reload", "gui", "server", "send", "github").filter { it.startsWith(args[0]) } - 2 -> when(args[0]) { - "server" -> listOf("start", "stop") - "send", "gui", "bypass" -> packy.plugin.server.onlinePlayers.map { it.name } - "github" -> listOf("fetch") - else -> listOf() - }.filter { it.startsWith(args[1]) } - 3 -> when(args[0]) { - "github" -> packy.templates.entries.filter { it.value.githubDownload != null }.map { it.key } - else -> listOf() - }.filter { it.startsWith(args[2]) } - else -> listOf() - } - } else listOf() - } } diff --git a/src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt b/src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt index 45ec161..f27b18f 100644 --- a/src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt +++ b/src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt @@ -32,7 +32,7 @@ class PackyPlugin : JavaPlugin() { override fun onEnable() { createPackyContext() - PackyCommands() + PackyCommands.registerCommands() PackyServer.startServer() listeners(PlayerListener())