Skip to content

Commit bb64181

Browse files
committed
add command tab completer
1 parent b525dd6 commit bb64181

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.astrainteractive.astralibs.command.api.command
2+
3+
import ru.astrainteractive.astralibs.command.api.context.BukkitCommandContext
4+
5+
/**
6+
* Can be used for testing purposes
7+
*/
8+
fun interface BukkitTabCompleter {
9+
fun onTabComplete(ctx: BukkitCommandContext): List<String>
10+
}
Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,64 @@
11
package ru.astrainteractive.astralibs.command.api.util
22

33
import org.bukkit.plugin.java.JavaPlugin
4+
import ru.astrainteractive.astralibs.command.api.command.BukkitTabCompleter
45
import ru.astrainteractive.astralibs.command.api.context.BukkitCommandContext
56
import ru.astrainteractive.astralibs.command.api.error.ErrorHandler
67
import ru.astrainteractive.astralibs.command.api.executor.CommandExecutor
78
import ru.astrainteractive.astralibs.command.api.parser.CommandParser
89

910
object PluginExt {
10-
fun <T : Any> JavaPlugin.registerCommand(
11+
fun <T : Any> JavaPlugin.setCommandExecutor(
1112
alias: String,
1213
commandParser: CommandParser<T, BukkitCommandContext>,
1314
commandExecutor: CommandExecutor<T>,
1415
errorHandler: ErrorHandler<BukkitCommandContext>
1516
) {
16-
val javaCommand = getCommand(alias) ?: error("Command with alias $alias not found")
17-
javaCommand.setExecutor { sender, bukkitCommand, label, args ->
18-
val commandContext = BukkitCommandContext(
17+
val pluginCommand = getCommand(alias) ?: error("Command with alias $alias not found")
18+
pluginCommand.setExecutor { sender, bukkitCommand, label, args ->
19+
val ctx = BukkitCommandContext(
1920
sender = sender,
2021
command = bukkitCommand,
2122
label = label,
2223
args = args
2324
)
24-
val parseResult = runCatching {
25-
commandParser.parse(commandContext)
26-
}
27-
parseResult.onFailure { throwable ->
28-
errorHandler.handle(commandContext, throwable)
29-
}
30-
parseResult.onSuccess(commandExecutor::execute)
25+
runCatching { commandParser.parse(ctx) }
26+
.onFailure { throwable -> errorHandler.handle(ctx, throwable) }
27+
.onSuccess(commandExecutor::execute)
3128
true
3229
}
3330
}
3431

3532
fun JavaPlugin.setCommandExecutor(
3633
alias: String,
3734
commandExecutor: CommandExecutor<BukkitCommandContext>,
35+
errorHandler: ErrorHandler<BukkitCommandContext>
3836
) {
3937
val javaCommand = getCommand(alias) ?: error("Command with alias $alias not found")
4038
javaCommand.setExecutor { sender, bukkitCommand, label, args ->
41-
val commandContext = BukkitCommandContext(
39+
val ctx = BukkitCommandContext(
4240
sender = sender,
4341
command = bukkitCommand,
4442
label = label,
4543
args = args
4644
)
47-
commandExecutor.execute(commandContext)
45+
runCatching { commandExecutor.execute(ctx) }
46+
.onFailure { errorHandler.handle(ctx, it) }
4847
true
4948
}
5049
}
50+
51+
@Suppress("UnusedPrivateMember")
52+
private fun JavaPlugin.setCommandTabCompleter(alias: String, tabCompleter: BukkitTabCompleter) {
53+
val pluginCommand = getCommand(alias) ?: error("Command $alias not found!")
54+
pluginCommand.setTabCompleter { commandSender, command, label, args ->
55+
val ctx = BukkitCommandContext(
56+
sender = commandSender,
57+
command = command,
58+
label = label,
59+
args = args
60+
)
61+
tabCompleter.onTabComplete(ctx)
62+
}
63+
}
5164
}

command/src/main/kotlin/ru/astrainteractive/astralibs/command/api/error/ErrorHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package ru.astrainteractive.astralibs.command.api.error
33
import ru.astrainteractive.astralibs.command.api.context.CommandContext
44

55
fun interface ErrorHandler<CC : CommandContext> {
6-
fun handle(commandContext: CC, throwable: Throwable)
6+
fun handle(ctx: CC, throwable: Throwable)
77
}

command/src/main/kotlin/ru/astrainteractive/astralibs/command/api/parser/CommandParser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fun interface CommandParser<R : Any, CC : CommandContext> {
1212
* Parse arguments into Result [R]
1313
*/
1414
@Throws(CommandException::class)
15-
fun parse(commandContext: CC): R
15+
fun parse(ctx: CC): R
1616
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ makeevrserg.java.ktarget=21
99
# Project
1010
makeevrserg.project.name=AstraLibs
1111
makeevrserg.project.group=ru.astrainteractive.astralibs
12-
makeevrserg.project.version.string=3.18.2
12+
makeevrserg.project.version.string=3.19.0
1313
makeevrserg.project.description=Core utilities for spigot development
1414
makeevrserg.project.developers=makeevrserg|Makeev Roman|makeevrserg@gmail.com
1515
makeevrserg.project.url=https://empireprojekt.ru

0 commit comments

Comments
 (0)