Skip to content

Commit

Permalink
refactor: swap to Brigadier command-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jun 12, 2024
1 parent 0b57d2b commit b884914
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 90 deletions.
117 changes: 28 additions & 89 deletions src/main/kotlin/com/mineinabyss/packy/PackyCommands.kt
Original file line number Diff line number Diff line change
@@ -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<out String>
): List<String> {
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()
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PackyPlugin : JavaPlugin() {

override fun onEnable() {
createPackyContext()
PackyCommands()
PackyCommands.registerCommands()
PackyServer.startServer()

listeners(PlayerListener())
Expand Down

0 comments on commit b884914

Please sign in to comment.