|
1 | 1 | package ru.astrainteractive.astralibs.command.api.util
|
2 | 2 |
|
3 | 3 | import org.bukkit.plugin.java.JavaPlugin
|
| 4 | +import ru.astrainteractive.astralibs.command.api.command.BukkitTabCompleter |
4 | 5 | import ru.astrainteractive.astralibs.command.api.context.BukkitCommandContext
|
5 | 6 | import ru.astrainteractive.astralibs.command.api.error.ErrorHandler
|
6 | 7 | import ru.astrainteractive.astralibs.command.api.executor.CommandExecutor
|
7 | 8 | import ru.astrainteractive.astralibs.command.api.parser.CommandParser
|
8 | 9 |
|
9 | 10 | object PluginExt {
|
10 |
| - fun <T : Any> JavaPlugin.registerCommand( |
| 11 | + fun <T : Any> JavaPlugin.setCommandExecutor( |
11 | 12 | alias: String,
|
12 | 13 | commandParser: CommandParser<T, BukkitCommandContext>,
|
13 | 14 | commandExecutor: CommandExecutor<T>,
|
14 | 15 | errorHandler: ErrorHandler<BukkitCommandContext>
|
15 | 16 | ) {
|
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( |
19 | 20 | sender = sender,
|
20 | 21 | command = bukkitCommand,
|
21 | 22 | label = label,
|
22 | 23 | args = args
|
23 | 24 | )
|
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) |
31 | 28 | true
|
32 | 29 | }
|
33 | 30 | }
|
34 | 31 |
|
35 | 32 | fun JavaPlugin.setCommandExecutor(
|
36 | 33 | alias: String,
|
37 | 34 | commandExecutor: CommandExecutor<BukkitCommandContext>,
|
| 35 | + errorHandler: ErrorHandler<BukkitCommandContext> |
38 | 36 | ) {
|
39 | 37 | val javaCommand = getCommand(alias) ?: error("Command with alias $alias not found")
|
40 | 38 | javaCommand.setExecutor { sender, bukkitCommand, label, args ->
|
41 |
| - val commandContext = BukkitCommandContext( |
| 39 | + val ctx = BukkitCommandContext( |
42 | 40 | sender = sender,
|
43 | 41 | command = bukkitCommand,
|
44 | 42 | label = label,
|
45 | 43 | args = args
|
46 | 44 | )
|
47 |
| - commandExecutor.execute(commandContext) |
| 45 | + runCatching { commandExecutor.execute(ctx) } |
| 46 | + .onFailure { errorHandler.handle(ctx, it) } |
48 | 47 | true
|
49 | 48 | }
|
50 | 49 | }
|
| 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 | + } |
51 | 64 | }
|
0 commit comments