|
1 | 1 | package xyz.galaxyy.lualink |
2 | 2 |
|
3 | | -import cloud.commandframework.annotations.AnnotationParser |
4 | | -import cloud.commandframework.arguments.parser.ParserParameters |
5 | | -import cloud.commandframework.arguments.parser.StandardParameters |
6 | | -import cloud.commandframework.bukkit.CloudBukkitCapabilities |
7 | | -import cloud.commandframework.execution.CommandExecutionCoordinator |
8 | | -import cloud.commandframework.execution.FilteringCommandSuggestionProcessor |
9 | | -import cloud.commandframework.meta.CommandMeta |
10 | | -import cloud.commandframework.paper.PaperCommandManager |
11 | | -import io.leangen.geantyref.TypeToken |
12 | 3 | import org.bukkit.command.CommandSender |
13 | 4 | import org.bukkit.plugin.java.JavaPlugin |
14 | | -import xyz.galaxyy.lualink.commands.AvailableScriptParser |
15 | | -import xyz.galaxyy.lualink.commands.LoadedScriptParser |
| 5 | +import org.incendo.cloud.annotations.AnnotationParser |
| 6 | +import org.incendo.cloud.bukkit.CloudBukkitCapabilities |
| 7 | +import org.incendo.cloud.exception.ArgumentParseException |
| 8 | +import org.incendo.cloud.exception.handling.ExceptionHandler.unwrappingHandler |
| 9 | +import org.incendo.cloud.execution.ExecutionCoordinator |
| 10 | +import org.incendo.cloud.paper.LegacyPaperCommandManager |
| 11 | +import xyz.galaxyy.lualink.commands.arguments.AvailableScriptParser |
| 12 | +import xyz.galaxyy.lualink.commands.arguments.LoadedScriptParser |
16 | 13 | import xyz.galaxyy.lualink.commands.LuaLinkCommands |
| 14 | +import xyz.galaxyy.lualink.commands.exceptions.ReplyingParseException |
17 | 15 | import xyz.galaxyy.lualink.listeners.ServerLoadListener |
18 | | -import xyz.galaxyy.lualink.lua.LuaScript |
19 | 16 | import xyz.galaxyy.lualink.lua.LuaScriptManager |
20 | | -import java.io.File |
21 | | -import java.util.function.Function |
22 | 17 |
|
23 | 18 | class LuaLink : JavaPlugin() { |
24 | | - private lateinit var manager: PaperCommandManager<CommandSender> |
| 19 | + private lateinit var manager: LegacyPaperCommandManager<CommandSender> |
25 | 20 | private lateinit var annotationParser: AnnotationParser<CommandSender> |
26 | 21 | private val scriptManager: LuaScriptManager = LuaScriptManager(this) |
| 22 | + |
27 | 23 | override fun onEnable() { |
28 | 24 | this.server.pluginManager.registerEvents(ServerLoadListener(this.scriptManager), this) |
29 | | - this.setupCloud() |
30 | | - this.registerCommands() |
| 25 | + this.setupCommands() |
31 | 26 | } |
32 | 27 |
|
33 | 28 | override fun onDisable() { |
34 | 29 | val scripts = this.scriptManager.getLoadedScripts() |
35 | 30 | scripts.forEach(this.scriptManager::unLoadScript) |
36 | 31 | } |
37 | | - private fun registerCommands() { |
| 32 | + |
| 33 | + private fun setupCommands() { |
| 34 | + // Creating basic LegacyPaperCommandManager instance. We're not using PaperCommandManager here because it only supports 1.20.6 and higher. |
| 35 | + this.manager = LegacyPaperCommandManager.createNative(this, ExecutionCoordinator.simpleCoordinator()) |
| 36 | + // Registering Brigadier integration if capable, or asynchronous completions otherwise. It's not recommended to have both capabilities registered. |
| 37 | + if (this.manager.hasCapability(CloudBukkitCapabilities.BRIGADIER)) |
| 38 | + this.manager.registerBrigadier() |
| 39 | + else this.manager.hasCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION) |
| 40 | + // Creating AnnotationParser instance. |
| 41 | + this.annotationParser = AnnotationParser(this.manager, CommandSender::class.java) |
| 42 | + // Registering handler(s)... |
| 43 | + this.manager.exceptionController().registerHandler(ArgumentParseException::class.java, unwrappingHandler(ReplyingParseException::class.java)) |
| 44 | + this.manager.exceptionController().registerHandler(ReplyingParseException::class.java) { it.exception().runnable.run() } |
| 45 | + // Registering parser(s)... |
| 46 | + this.annotationParser.parse(AvailableScriptParser(this, this.scriptManager)) |
| 47 | + this.annotationParser.parse(LoadedScriptParser(this.scriptManager)) |
| 48 | + // Registering command(s)... |
38 | 49 | this.annotationParser.parse(LuaLinkCommands(this, this.scriptManager)) |
39 | 50 | } |
40 | | - private fun setupCloud() { |
41 | | - |
42 | | - val executionCoordinatorFunction = CommandExecutionCoordinator.simpleCoordinator<CommandSender>() |
43 | | - |
44 | | - val mapperFunction: Function<CommandSender, CommandSender> = Function.identity() |
45 | | - try { |
46 | | - this.manager = PaperCommandManager( /* Owning plugin */ |
47 | | - this, /* Coordinator function */ |
48 | | - executionCoordinatorFunction, /* Command Sender -> C */ |
49 | | - mapperFunction, /* C -> Command Sender */ |
50 | | - mapperFunction |
51 | | - ) |
52 | | - } catch (e: Exception) { |
53 | | - logger.severe("Failed to initialize the command this.manager") |
54 | | - /* Disable the plugin */server.pluginManager.disablePlugin(this) |
55 | | - return |
56 | | - } |
57 | | - |
58 | | - // Use contains to filter suggestions instead of default startsWith |
59 | | - |
60 | | - // Use contains to filter suggestions instead of default startsWith |
61 | | - manager.commandSuggestionProcessor( |
62 | | - FilteringCommandSuggestionProcessor( |
63 | | - FilteringCommandSuggestionProcessor.Filter.contains<CommandSender>(true).andTrimBeforeLastSpace() |
64 | | - ) |
65 | | - ) |
66 | | - |
67 | | - if (this.manager.hasCapability(CloudBukkitCapabilities.BRIGADIER)) { |
68 | | - this.manager.registerBrigadier() |
69 | | - } |
70 | 51 |
|
71 | | - if (this.manager.hasCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) { |
72 | | - this.manager.registerAsynchronousCompletions() |
73 | | - } |
74 | | - |
75 | | - |
76 | | - |
77 | | - val commandMetaFunction: Function<ParserParameters, CommandMeta> = |
78 | | - Function<ParserParameters, CommandMeta> { p -> |
79 | | - CommandMeta.simple() // This will allow you to decorate commands with descriptions |
80 | | - .with(CommandMeta.DESCRIPTION, p.get(StandardParameters.DESCRIPTION, "No description")) |
81 | | - .build() |
82 | | - } |
83 | | - this.annotationParser = AnnotationParser( /* Manager */ |
84 | | - this.manager, /* Command sender type */ |
85 | | - CommandSender::class.java, /* Mapper for command meta instances */ |
86 | | - commandMetaFunction |
87 | | - ) |
88 | | - |
89 | | - this.manager.parserRegistry().registerParserSupplier( |
90 | | - TypeToken.get(LuaScript::class.java) |
91 | | - ) { LoadedScriptParser(this.scriptManager) } |
92 | | - |
93 | | - this.manager.parserRegistry().registerParserSupplier( |
94 | | - TypeToken.get(File::class.java) |
95 | | - ) { AvailableScriptParser(this, this.scriptManager) } |
96 | | - } |
97 | 52 | } |
0 commit comments