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

Feat: Add 'with' methods to CommandSourceStack #11868

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Strokkur424
Copy link
Contributor

fixes #11866

The following methods have been added:

  • CommandSourceStack#withExecutor(@Nullable Entity)
  • CommandSourceStack#withLocation(Location location)

JavaDocs hasn't been added yet, but that is something I will be doing very soon.

Testing the code can be done via the following:

this.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, event -> {
    final LiteralCommandNode<CommandSourceStack> node = Commands.literal("test")
        .then(Commands.literal("run")
            .executes(ctx -> {
                Bukkit.broadcast(MiniMessage.miniMessage().deserialize("<name>: I am at <x> <y> <z>",
                    Placeholder.component("name", ctx.getSource().getExecutor().name()),
                    Placeholder.unparsed("x", String.valueOf(ctx.getSource().getLocation().x())),
                    Placeholder.unparsed("y", String.valueOf(ctx.getSource().getLocation().y())),
                    Placeholder.unparsed("z", String.valueOf(ctx.getSource().getLocation().z()))
                ));

                return 1;
            })).build();

    final LiteralCommandNode<CommandSourceStack> withNode = Commands.literal("testwith")
        .then(Commands.argument("entities", ArgumentTypes.entities())
            .then(Commands.literal("run")
                .fork(node, ctx -> {
                    final EntitySelectorArgumentResolver entities = ctx.getArgument("entities", EntitySelectorArgumentResolver.class);
                    final List<Entity> resolved = entities.resolve(ctx.getSource());

                    return resolved.stream()
                        .map(entity -> ctx.getSource().withExecutor(entity).withLocation(entity.getLocation()))
                        .toList();
                }))).build();

    event.registrar().register(node);
    event.registrar().register(withNode);
});

Running /test run works as expected, printing out the executing player's location.
Running /testwith @e[limit=2,type=!player] run run also works, printing out two entities names and locations.

testwith-command

@Strokkur424 Strokkur424 requested a review from a team as a code owner December 30, 2024 13:36
@emilyy-dev
Copy link
Member

emilyy-dev commented Jan 2, 2025 via email

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

Successfully merging this pull request may close these issues.

Expand the CommandSourceStack interface by exposing mutators
3 participants