Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brigadier DSL: Add a way to have command redirects #91

Open
roridev opened this issue Mar 31, 2024 · 2 comments
Open

Brigadier DSL: Add a way to have command redirects #91

roridev opened this issue Mar 31, 2024 · 2 comments

Comments

@roridev
Copy link

roridev commented Mar 31, 2024

Currently, it is possible to register commands using CommandDispatcher<S>.register but since that is an Unit function, there isn't a way to pass the LiteralCommandNode to brigadier's redirect function. Which leads to either duplicating the command code or not using the kotlin dsl in favor of the original java api.

Ideas

1- Keeping with the idea of sugaring the literal node, there could be an overload which takes a redirects: List<String> parameter.

// Overload
public fun <S> CommandDispatcher<S>.register(
    command: String,
    redirects: List<String>,
    action: LiteralArgumentBuilder<S>.() -> Unit
);

// Usage:
fun messageComand(dispatcher: CommandDispatcher<ServerCommandSource> ) {
   dispatcher.register("msg", redirects: listOf("w", "tell")) {
      // The vanilla /msg command
   }
}

2 - Add a way to specify inside the LiteralArgumentBuilder redirects for the command being registered.

// Function Definition
fun <S> LiteralArgumentBuilder<S>.aliases(aliases : vararg String);

// Usage
dispatcher.register("msg") {
    aliases("w", "tell") {
        // The vanilla /msg command
    }
}

3 - Just make register return the LiteralCommandNode

@BrigadierDsl
public fun <S> CommandDispatcher<S>.register(
    command: String,
    action: LiteralArgumentBuilder<S>.() -> Unit
--- ) {
+++ ) : LiteralCommandNode<S>
    val argument = LiteralArgumentBuilder.literal<S>(command)
    argument.apply(action)
--- register(argument)
+++ return register(argument)
}

See Also

https://fabricmc.net/wiki/tutorial:command_redirects

@mky2
Copy link

mky2 commented Apr 15, 2024

I suggest simply make register exposes LiteralCommandNode and let others decide how they want to use it. Aliasing (depending how you define it) and redirection are two different concepts (aliasing can sometimes be achived by redirection but not always). The method node2.redirect(node1) simply add edges from node2 to all forward neighbours of node1, it does not copy all 'properties' of node1 to node2.

Suppose one writes

register("foo", listOf("foo2", "foo3")) {
    executes {
        println("foo")
    }
}

then it is natural to think running /foo, /foo2, and /foo3 all print "foo", but this is not what redirect does. So, care has to be taken when implementing such method.

@SilverAndro
Copy link
Contributor

SilverAndro commented Apr 24, 2024

something to note is that you can redirect to the middle of a command, e.g execute recursively redirects to it arguments to support all of them without creating a massive tree

its likely much better to implement a proper solution to this rather than assuming that people only ever want to redirect to a root command literal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants