|
1 | 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 | 2 | Slight inspiration from CommandAPI by Jorel |
5 | 3 |
|
6 | 4 | --- |
7 | 5 |
|
8 | | -https://jitpack.io/#Manered/Commands/v1.0.0 |
| 6 | +https://jitpack.io/#Manered/Commands/v1.1.0 |
9 | 7 |
|
10 | 8 | Example: |
11 | 9 |
|
12 | 10 | ```java |
13 | 11 | public class TestPlugin extends JavaPlugin { |
| 12 | + private static CommandAPI API = null; |
| 13 | + |
14 | 14 | @Override |
15 | 15 | public void onEnable() { |
16 | | - final CommandsAPI api = CommandsAPI.api(() -> this); |
17 | | - final CommandManager manager = api.manager(); |
| 16 | + API = CommandAPI.init(this); |
18 | 17 |
|
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 | | - ); |
| 18 | + API.register(new CommandNode("broadcast") |
| 19 | + .permission("broadcast.use") |
| 20 | + .aliases("bc", "announce") |
| 21 | + .executes((ctx) -> { |
| 22 | + ctx.getSource().sendRichMessage("<red>Usage:"); |
| 23 | + ctx.getSource().sendRichMessage("<red>/broadcast world <world> <text>"); |
| 24 | + ctx.getSource().sendRichMessage("<red>/broadcast everyone <text>"); |
37 | 25 | }) |
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() |
| 26 | + .subcommand(new CommandNode("world") |
| 27 | + .permission("broadcast.world") |
| 28 | + .filter(sender -> sender instanceof Player) |
| 29 | + .argument(CommandArgument.required( |
| 30 | + TextArgument::new, "text", |
| 31 | + CompletionProvider.sync(context -> List.of(new Completion("Hello, World!", Component.text("Test"))))) |
| 32 | + ) |
| 33 | + .executes(Player.class, (player, ctx) -> { |
| 34 | + final Component text = MiniMessage.miniMessage().deserialize( |
| 35 | + "<gold><bold>BROADCAST</bold></gold> " + ctx.getArgument(String.class, "text").orElse("Error") |
65 | 36 | ); |
| 37 | + |
| 38 | + player.getWorld().sendMessage(text); |
| 39 | + player.getWorld().sendActionBar(text); |
66 | 40 | }) |
67 | 41 | ) |
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() |
| 42 | + .subcommand(new CommandNode("everyone") |
| 43 | + .permission("broadcast.everyone") |
| 44 | + .argument(CommandArgument.required( |
| 45 | + TextArgument::new, "text", |
| 46 | + CompletionProvider.sync(context -> List.of(new Completion("Hello, World!", Component.text("Test"))))) |
| 47 | + ) |
| 48 | + .executes(Player.class, (player, ctx) -> { |
| 49 | + final Component text = MiniMessage.miniMessage().deserialize( |
| 50 | + "<gold><bold>BROADCAST</bold></gold> " + ctx.getArgument(String.class, "text").orElse("Error") |
98 | 51 | ); |
| 52 | + |
| 53 | + player.getServer().sendMessage(text); |
| 54 | + player.getServer().sendActionBar(text); |
99 | 55 | }) |
100 | | - ); |
| 56 | + ) |
| 57 | + ); |
| 58 | + } |
101 | 59 |
|
102 | | - manager.register(command); |
| 60 | + @NotNull |
| 61 | + public static CommandAPI getCommandAPI() { |
| 62 | + return API; |
103 | 63 | } |
104 | 64 | } |
105 | 65 | ``` |
0 commit comments