Skip to content

Commit

Permalink
feat: tab-complete warp names (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyksaw authored Oct 26, 2024
1 parent c2280d1 commit f96f9a0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.Location
import org.bukkit.Server
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.event.player.PlayerTeleportEvent
Expand All @@ -22,7 +23,7 @@ class WarpCommand(
) : SpigotCommand<WarpCommand.Args> {
override val label = "warp"

override val usage = CommandHelpBuilder(usage = "/warp <name>") // TODO
override val usage = CommandHelpBuilder(usage = "/warp <name>")

override suspend fun run(
sender: CommandSender,
Expand Down Expand Up @@ -66,6 +67,22 @@ class WarpCommand(
)
}

override suspend fun tabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: Array<out String>,
): List<String>? {
val warps = warpRepository.all()

if (args.isEmpty()) {
return warps.map { it.name }
}
return warps
.filter { it.name.lowercase().startsWith(args.first().lowercase()) }
.map { it.name }
}

data class Args(
val warpName: String,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.projectcitybuild.pcbridge.support.spigot.SpigotCommand
import com.projectcitybuild.pcbridge.support.spigot.UnauthorizedCommandException
import com.projectcitybuild.pcbridge.support.tryValueOf
import org.bukkit.Server
import org.bukkit.command.Command
import org.bukkit.command.CommandSender

class WarpsCommand(
Expand Down Expand Up @@ -117,6 +118,55 @@ class WarpsCommand(
}
}

override suspend fun tabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: Array<out String>,
): List<String>? {
if (args.isEmpty() || args.first().isEmpty()) {
return if (sender.hasPermission("pcbridge.warp.manage")) {
listOf("create", "delete", "list", "move", "rename")
} else {
listOf("list")
}
}
when (args.first()) {
"delete" -> {
if (args.size != 2) return null
val name = args[1]
if (name.isEmpty()) {
return warpRepository.all().map { it.name }
}
return warpRepository.all()
.filter { it.name.lowercase().startsWith(name.lowercase()) }
.map { it.name }
}
"move" -> {
if (args.size != 2) return null
val name = args[1]
if (name.isEmpty()) {
return warpRepository.all().map { it.name }
}
return warpRepository.all()
.filter { it.name.lowercase().startsWith(name.lowercase()) }
.map { it.name }
}
"rename" -> {
if (args.size != 2) return null
val name = args[1]
if (name.isEmpty()) {
return warpRepository.all().map { it.name }
}
return warpRepository.all()
.filter { it.name.lowercase().startsWith(name.lowercase()) }
.map { it.name }
}

else -> return null
}
}

data class Args(
val command: Command,
val remainingArgs: List<String>,
Expand All @@ -131,10 +181,9 @@ class WarpsCommand(

class Parser : CommandArgsParser<Args> {
override fun parse(args: List<String>): Args {
if (args.isEmpty()) {
throw BadCommandUsageException()
}
val command =
val command = if (args.isEmpty())
Command.List
else
tryValueOf<Command>(args[0].replaceFirstChar { it.uppercase() })
?: throw BadCommandUsageException()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.projectcitybuild.pcbridge.support.spigot

import com.projectcitybuild.pcbridge.support.messages.CommandHelpBuilder
import org.bukkit.command.Command
import org.bukkit.command.CommandSender

interface SpigotCommand<T> {
Expand All @@ -17,6 +18,13 @@ interface SpigotCommand<T> {
sender: CommandSender,
args: T,
)

suspend fun tabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: Array<out String>,
): List<String>? = emptyList()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ class SpigotCommandRegistry(
}
},
)
pluginCommand.setSuspendingTabCompleter(
object : SuspendingTabCompleter {
override suspend fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: Array<out String>,
): List<String>? = handler.tabComplete(
sender,
command,
alias,
args,
)
}
)
registered.add(label)

if (tabCompleter != null) {
Expand Down

0 comments on commit f96f9a0

Please sign in to comment.