Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/paper-command-composition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Paper command composition (LumaGuilds migration prerequisites)

The `nexus-paper` command adapter groups classes with the same `@Command(name = "...")` into one Brigadier root tree. This supports splitting a large command into independently injectable, independently reviewable command classes without duplicating registration for the same literal.

## Example

```kotlin
@Command(name = "guild", aliases = ["g"])
class GuildRoot(private val menus: GuildMenus) {
@Subcommand("") // /guild and /g
fun menu(@Context player: Player) = menus.open(player)
}

@Command(name = "guild", aliases = ["g"])
class GuildHomes(private val homes: GuildHomeService) {
@Subcommand("home") // /guild home [name]
@Permission("guild.home")
fun home(
@Context player: Player,
@Arg("name", required = false) name: String = "main"
) = homes.teleport(player, name)

@Subcommand("announce")
fun announce(@Context player: Player, @Arg("message") @Greedy message: String) {
// `message` includes whitespace through end of input.
}
}
```

## Rules

- Repeated root names are composed into one registration and aliases are deduplicated; different root permissions or descriptions are rejected.
- Two handlers cannot claim the same literal path, including the empty root/default path; errors occur before the Paper lifecycle registration event.
- Nested literals share parents; child and parent permissions remain separate. Prefix visibility is permitted if any descendant is permitted; executing a route always checks its own permission and player-only requirement.
- `@Subcommand("")` denotes the root/default handler (not ACF's `@Default`).
- `@Arg(required = false)` and `@Arg(defaultValue = "...")` produce executable shorter Brigadier paths. Optional args must be trailing. Omitted arguments use the annotation default, the Kotlin parameter default, or `null` if nullable. Non-null optional parameters without either default are rejected by scanning.
- `@Greedy` is valid only on the final String `@Arg`; useful for guild chat and announcements.
- `@Suggests("providerName")` still resolves from the `suggestionProviders` map supplied to `registerPaperCommands()`.
- `@Arg("name")` supplies the Brigadier argument name even when the Kotlin parameter identifier differs.
- These are Nexus annotations, not drop-in ACF annotations. Migrate source deliberately, retaining command aliases, permissions and completion contracts.

## Boundaries

This is **command infrastructure only**, not the LumaGuilds migration. The existing NexusContext DI and lifecycle APIs remain unchanged. Large ACF command suites need a command-by-command parity inventory: syntax, optional/greedy arguments, async/main-thread constraints, player-only behavior, command help, and completions. Remaining ACF features (such as `@Conditions`, custom contexts, or automated help) must be implemented or deliberately rewritten before removal.

Run `./gradlew :nexus-paper:test` on JDK 25. Do not publish a Nexus version until CI and the LumaGuilds command parity review pass.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ import net.badgersmc.nexus.commands.annotations.Command
import kotlin.reflect.KClass
import kotlin.reflect.KFunction

/** Full metadata for a root command discovered via classpath scanning. */
/** Full metadata for a command handler discovered via classpath scanning. */
data class PaperCommandDefinition(
val commandClass: KClass<*>,
val annotation: Command,
val subcommands: List<PaperSubcommandDefinition>
)

/** Metadata for a single @Subcommand method. */
/** Metadata for one @Subcommand method. An empty path is the root/default handler. */
data class PaperSubcommandDefinition(
/** Path segments — e.g., ["admin", "setup"] for @Subcommand("admin setup") */
val path: List<String>,
val method: KFunction<*>,
val parameters: List<CommandParameter>,
val permission: String?,
val isPlayerOnly: Boolean,
val isAsync: Boolean,
/** Maps parameter names to suggestion provider names (from @Suggests). */
val suggestions: Map<String, String> = emptyMap()
val suggestions: Map<String, String> = emptyMap(),
val greedyParameters: Set<String> = emptySet()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.badgersmc.nexus.paper.commands

import net.badgersmc.nexus.commands.CommandException

/**
* Groups command classes by their shared Brigadier root, without changing the public
* @Command API. An ACF-style split can therefore define /guild subcommands in
* independent classes without registering competing root command trees.
*/
internal object PaperCommandPlanner {
data class Root(
val name: String,
val description: String,
val permission: String,
val aliases: List<String>,
val definitions: List<PaperCommandDefinition>
)

// Root validation rejects distinct configuration errors rather than masking conflicts.
@Suppress("ThrowsCount")
fun plan(definitions: List<PaperCommandDefinition>): List<Root> {

Check notice on line 21 in nexus-paper/src/main/kotlin/net/badgersmc/nexus/paper/commands/PaperCommandPlanner.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

nexus-paper/src/main/kotlin/net/badgersmc/nexus/paper/commands/PaperCommandPlanner.kt#L21

Method plan has 22 lines of code (limit is 20)
val roots = definitions.groupBy { it.annotation.name.lowercase() }
val occupiedNames = mutableMapOf<String, String>()
return roots.map { (name, group) ->
val permissions = group.map { it.annotation.permission }.distinct()
if (permissions.size != 1) {
throw CommandException("Conflicting root permissions for /$name: $permissions")
}
val descriptions = group.map { it.annotation.description }.filter { it.isNotBlank() }.distinct()
if (descriptions.size > 1) {
throw CommandException("Conflicting root descriptions for /$name: $descriptions")
}
val paths = mutableMapOf<String, String>()
group.forEach { definition ->
definition.subcommands.forEach { sub ->
val path = sub.path.joinToString(" ").lowercase()
val owner = "${definition.commandClass.simpleName}::${sub.method.name}"
val previous = paths.putIfAbsent(path, owner)
if (previous != null) {
throw CommandException("Duplicate /$name $path handlers: $previous and $owner")
}
}
}
val aliases = group.flatMap { it.annotation.aliases.toList() }
.filterNot { it.equals(name, ignoreCase = true) }
.distinctBy { it.lowercase() }
(listOf(name) + aliases).forEach { alias ->
val previous = occupiedNames.putIfAbsent(alias.lowercase(), name)
if (previous != null && previous != name) {
throw CommandException("Command alias '$alias' conflicts between /$previous and /$name")
}
}
Root(name, descriptions.firstOrNull() ?: "", permissions.single(), aliases, group)
}
}
}
Loading
Loading