PaperMC / PaperMC/docs

[Brigadier] Add `/execute` extension example

Open
#534 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
MDX
Stars
73
Forks
280
Avg merge
20h 52m
Merged PRs (30d)
4

Description

[!NOTE]
This issue is part of a series of issues regarding the extension of the Paper documentation regarding Paper's Brigadier API.

Bringing together #533 and #532, the concepts explained in both of those pages can be used to provide an example on how to extend the most complicated command vanilla offers: /execute.

This is to serve as a technical example in order for people, who seek to actually use this kind of command modification, to have a reference to go back to, but also serve as a great learning point for people who wish to further deepen their knowledge about Brigadier.

A possible extension to the command could be the addition of /execute permission in order to only execute a command if the executor actually has a permission. If you have a better idea for an extension, be sure to suggest it!

The code for such an extension might look like this:

private void registerExecuteExtension(final @NotNull Commands commands) {
    final CommandDispatcher<CommandSourceStack> dispatcher = commands.getDispatcher();
    if (!(dispatcher.getRoot().getChild("execute") instanceof LiteralCommandNode<CommandSourceStack> executeNode)) {
        return;
    }

    dispatcher.register(executeNode.createBuilder().then(Commands.literal("permission")
        .then(Commands.argument("permission", StringArgumentType.word())
            .fork(executeNode, ctx -> ctx.getSource().getExecutor() instanceof Player player
                && player.hasPermission(StringArgumentType.getString(ctx, "permission"))
                ? List.of(ctx.getSource())
                : List.of()))
    ));
}

And it would result in the following usage:

execute as @a permission minecraft.broadcast run say wassup
[16:39:03 INFO]: [Not Secure] [Strokkur24] wassup

deop Strokkur24
[16:39:10 INFO]: Made Strokkur24 no longer a server operator
execute as @a permission minecraft.broadcast run say wassup

op Strokkur24
[16:39:20 INFO]: Made Strokkur24 a server operator
execute as @a permission minecraft.broadcast run say wassup
[16:39:22 INFO]: [Not Secure] [Strokkur24] wassup

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the related documentation work in issues #532 and #533, then study the /execute entry point and the provided registerExecuteExtension example. Done means the Brigadier documentation includes a technical extension example and shows its usage and permission-dependent output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.