|
1 | | -# Commands |
| 1 | +# Commands |
| 2 | +Brigadier ripoff but technically done with brigadier (this uses Bukkit's command system and then Bukkit internally uses Brigadier) [Commands -> Bukkit -> Brigadier] |
| 3 | + |
| 4 | +Slight inspiration from CommandAPI by Jorel |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +https://jitpack.io/#Manered/Commands/v1.0.0 |
| 9 | + |
| 10 | +Example: |
| 11 | + |
| 12 | +```java |
| 13 | +public class TestPlugin extends JavaPlugin { |
| 14 | + @Override |
| 15 | + public void onEnable() { |
| 16 | + final CommandsAPI api = CommandsAPI.api(() -> this); |
| 17 | + final CommandManager manager = api.manager(); |
| 18 | + |
| 19 | + final CommandNode command = CommandNode.builder() |
| 20 | + .literal("broadcast") |
| 21 | + .info(info -> info |
| 22 | + .aliases("bc") |
| 23 | + .description("Broadcast Command") |
| 24 | + .permission("broadcast.use", Component.text( |
| 25 | + "Insufficient permissions.", NamedTextColor.RED |
| 26 | + )) |
| 27 | + ) |
| 28 | + .build() |
| 29 | + .handler(context -> { |
| 30 | + context.source().sendRichMessage("<red>Usage:"); |
| 31 | + context.source().sendRichMessage("<red>/<command> global <minimessage text>" |
| 32 | + .replaceAll("<command>", context.rootAlias()) |
| 33 | + ); |
| 34 | + context.source().sendRichMessage("<red>/<command> world <world> <minimessage text>" |
| 35 | + .replaceAll("<command>", context.rootAlias()) |
| 36 | + ); |
| 37 | + }) |
| 38 | + .subcommand(CommandNode.literal("global") |
| 39 | + .argument(CommandArgument.argument(GreedyTextArgument.class, "text", (SyncSuggestionHandler) context -> List.of( |
| 40 | + Suggestion.suggestion("Restarting in 1 minute..."), |
| 41 | + Suggestion.suggestion("Restarting in 30 seconds..."), |
| 42 | + Suggestion.suggestion("Restarting in 15 seconds..."), |
| 43 | + Suggestion.suggestion("Restarting in 10 seconds..."), |
| 44 | + Suggestion.suggestion("Restarting in 5 seconds...") |
| 45 | + Suggestion.suggestion("Restarting in 3 seconds..."), |
| 46 | + Suggestion.suggestion("Restarting in 2 seconds..."), |
| 47 | + Suggestion.suggestion("Restarting in 1 second..."), |
| 48 | + Suggestion.suggestion("Restarting...") |
| 49 | + ))) |
| 50 | + .handler(context -> { |
| 51 | + final Component text = context.arguments().argumentOr(Component.class, "text", () -> { |
| 52 | + context.source().sendRichMessage("<red>Usage: /<command> global <minimessage text>" |
| 53 | + .replaceAll("<command>", context.rootAlias()) |
| 54 | + ); |
| 55 | + return null; |
| 56 | + }); |
| 57 | + |
| 58 | + Bukkit.broadcast(Component.text() |
| 59 | + .append(Component.text("[", NamedTextColor.GRAY)) |
| 60 | + .append(Component.text("Broadcast", NamedTextColor.DARK_RED)) |
| 61 | + .append(Component.text("]", NamedTextColor.GRAY)) |
| 62 | + .append(Component.text(" ", NamedTextColor.WHITE)) |
| 63 | + .append(text) |
| 64 | + .build() |
| 65 | + ); |
| 66 | + }) |
| 67 | + ) |
| 68 | + .subcommand(CommandNode.literal("world") |
| 69 | + .argument(CommandArgument.argument(WorldArgument.class, "world")) |
| 70 | + .argument(CommandArgument.argument(GreedyTextArgument.class, "text", (SyncSuggestionHandler) context -> List.of( |
| 71 | + Suggestion.suggestion("Restarting in 1 minute..."), |
| 72 | + Suggestion.suggestion("Restarting in 30 seconds..."), |
| 73 | + Suggestion.suggestion("Restarting in 15 seconds..."), |
| 74 | + Suggestion.suggestion("Restarting in 10 seconds..."), |
| 75 | + Suggestion.suggestion("Restarting in 5 seconds...") |
| 76 | + Suggestion.suggestion("Restarting in 3 seconds..."), |
| 77 | + Suggestion.suggestion("Restarting in 2 seconds..."), |
| 78 | + Suggestion.suggestion("Restarting in 1 second..."), |
| 79 | + Suggestion.suggestion("Restarting...") |
| 80 | + ))) |
| 81 | + .handler(context -> { |
| 82 | + final World world = context.arguments().argumentOr(World.class, "world", () -> Objects.requireNonNull(context.source().asPlayer()).getWorld()); |
| 83 | + |
| 84 | + final Component text = context.arguments().argumentOr(Component.class, "text", () -> { |
| 85 | + context.source().sendRichMessage("<red>Usage: /<command> world <world> <minimessage text>" |
| 86 | + .replaceAll("<command>", context.rootAlias()) |
| 87 | + ); |
| 88 | + return null; |
| 89 | + }); |
| 90 | + |
| 91 | + world.sendMessage(Component.text() |
| 92 | + .append(Component.text("[", NamedTextColor.GRAY)) |
| 93 | + .append(Component.text("Broadcast", NamedTextColor.DARK_RED)) |
| 94 | + .append(Component.text("]", NamedTextColor.GRAY)) |
| 95 | + .append(Component.text(" ")) |
| 96 | + .append(text) |
| 97 | + .build() |
| 98 | + ); |
| 99 | + }) |
| 100 | + ); |
| 101 | + |
| 102 | + manager.register(command); |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
0 commit comments