Skip to content

Commit

Permalink
feat: move the sender mapper example to compiled snippet and other im…
Browse files Browse the repository at this point in the history
…provements
  • Loading branch information
broccolai committed Aug 8, 2024
1 parent 7a1798a commit d699cd3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion code/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
indra = "3.1.3"

cloud = "2.0.0-rc.2"
cloudMinecraft = "2.0.0-beta.8"
cloudMinecraft = "2.0.0-SNAPSHOT"
cloudProcessors = "1.0.0-beta.3"

paper = "1.20.6-R0.1-SNAPSHOT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.SenderMapper;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.paper.LegacyPaperCommandManager;
import org.incendo.cloud.paper.PaperCommandManager;
import org.incendo.cloud.paper.util.sender.PaperSimpleSenderMapper;
import org.incendo.cloud.paper.util.sender.PlayerSource;
import org.incendo.cloud.paper.util.sender.Source;

public class PaperExample {

Expand Down Expand Up @@ -58,6 +62,28 @@ public void exampleModernCustom(
// --8<-- [end:modern_custom]
}

public void exampleModernSimpleSEnderMapper(
final @NonNull JavaPlugin javaPlugin
) {
final ExecutionCoordinator<Source> executionCoordinator = ExecutionCoordinator.simpleCoordinator();
// --8<-- [start:modern_simple_sender_mapper]
PaperCommandManager<Source> commandManager = PaperCommandManager
.builder(PaperSimpleSenderMapper.simpleSenderMapper())
.executionCoordinator(executionCoordinator)
.buildOnEnable(javaPlugin);
// or: .buildBootstrapped(bootstrapContext);

// this command will only be available to players, and the player type is directly available.
commandManager.command(commandManager.commandBuilder("player_command")
.senderType(PlayerSource.class)
.handler(context -> {
Player player = context.sender().source();
player.sendMessage("Hello, player!");
})
);
// --8<-- [end:modern_simple_sender_mapper]
}

public record YourSenderType() {
}
}
10 changes: 5 additions & 5 deletions docs/minecraft/brigadier.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ per-argument validation, coloring and syntax hinting.
<figcaption>Accurate Command Structure</figcaption>
</figure>

!!! warning "Alias Registration"

Only aliases for root nodes will be registered when using Brigadier. Due to how it's command
tree works it can quickly become inflated when sub-commands have aliases.

## CloudBrigadierManager

You can get an instance of `CloudBrigadierManager` from the command manager for your platform of choice.
If the command manager is a `BrigadierManagerHolder`, then you can get the instance using `commandManager.brigadierManager()`.

The `CloudBrigadierManager` is how you interact with Brigadier to register mappings and configure settings.

!!! warning "Alias Registration"

Only aliases for root nodes will be registered when using Brigadier. Due to how it's command
tree works it can quickly become inflated when sub-commands have aliases.

### Settings

`CloudBrigadierManager` has settings that can be accessed using `CloudBrigadierManager.settings()`.
Expand Down
11 changes: 5 additions & 6 deletions docs/minecraft/paper.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ should be used.

!!! tip "Plugin Configuration Files"

Do not register your commands in your plugin.yml or paper-plugin.yml, Cloud handles the registration
Do not register your commands in your plugin.yml or paper-plugin.yml, Cloud handles the registration
itself and doing it yourself will cause issues.

### Legacy
Expand Down Expand Up @@ -140,10 +140,9 @@ if (commandManager.hasCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) {

## Provided Sender Mapper

Cloud includes a built-in sender mapper designed for the command manager. Due to the CommandSourceStack having no extensions it can be difficult to work, our sender mapper can be used like so:
Cloud includes a built-in sender mapper designed for the command manager. Due to the CommandSourceStack having no exposed implementations it can be difficult to work,
here's an example of creating a command manager with the sender mapper and using the provided mapped sender:

```java
PaperCommandManager.builder(PaperSimpleSenderMapper.simpleSenderMapper())
```
{{ snippet("minecraft/PaperExample.java", section = "modern_simple_sender_mapper", title = "") }}

This will give you access to Source with the included extensions: PlayerSource, ConsoleSource, EntitySource and GenericSource
This will give you access to Source with the included extensions: PlayerSource, ConsoleSource, EntitySource and GenericSource

0 comments on commit d699cd3

Please sign in to comment.