Skip to content

Commit b3f2aa4

Browse files
committed
v1.1.1 - (Hopefully) Final Version
1 parent e5ce64d commit b3f2aa4

File tree

5 files changed

+70
-92
lines changed

5 files changed

+70
-92
lines changed

README.md

Lines changed: 42 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,65 @@
11
# 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-
42
Slight inspiration from CommandAPI by Jorel
53

64
---
75

8-
https://jitpack.io/#Manered/Commands/v1.0.0
6+
https://jitpack.io/#Manered/Commands/v1.1.0
97

108
Example:
119

1210
```java
1311
public class TestPlugin extends JavaPlugin {
12+
private static CommandAPI API = null;
13+
1414
@Override
1515
public void onEnable() {
16-
final CommandsAPI api = CommandsAPI.api(() -> this);
17-
final CommandManager manager = api.manager();
16+
API = CommandAPI.init(this);
1817

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>");
3725
})
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")
6536
);
37+
38+
player.getWorld().sendMessage(text);
39+
player.getWorld().sendActionBar(text);
6640
})
6741
)
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")
9851
);
52+
53+
player.getServer().sendMessage(text);
54+
player.getServer().sendActionBar(text);
9955
})
100-
);
56+
)
57+
);
58+
}
10159

102-
manager.register(command);
60+
@NotNull
61+
public static CommandAPI getCommandAPI() {
62+
return API;
10363
}
10464
}
10565
```

src/main/java/dev/manere/commands/CommandNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ public CommandNode root() {
218218
return current;
219219
}
220220

221+
public boolean isRoot() {
222+
return parent().isEmpty();
223+
}
224+
221225
@Override
222226
public boolean equals(Object obj) {
223227
if (!(obj instanceof CommandNode other)) return false;

src/main/java/dev/manere/commands/CommandRequirements.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,30 @@
44
import org.bukkit.entity.Player;
55
import org.jetbrains.annotations.NotNull;
66

7+
import java.util.function.Predicate;
8+
79
public final class CommandRequirements {
8-
public static <S> boolean requireSender(final @NotNull Class<S> senderRequired, final @NotNull CommandContext<? extends CommandSender> context) {
9-
return !senderRequired.isInstance(context.getSource());
10+
public static <S> boolean requireSender(final @NotNull Class<S> senderRequired, final @NotNull CommandSender source) {
11+
return !senderRequired.isInstance(source);
12+
}
13+
14+
public static boolean requirePlayer(final @NotNull CommandSender source) {
15+
return requireSender(Player.class, source);
1016
}
1117

12-
public static boolean requirePlayer(final @NotNull CommandContext<? extends CommandSender> context) {
13-
return requireSender(Player.class, context);
18+
public static boolean requirePlayer(final @NotNull CommandSender source, final @NotNull Predicate<Player> then) {
19+
return requireSender(Player.class, source, then);
20+
}
21+
22+
public static <S> boolean requireSender(final @NotNull Class<S> senderRequired, final @NotNull CommandSender source, final @NotNull Predicate<S> then) {
23+
if (!requireSender(senderRequired, source)) {
24+
try {
25+
return then.test(senderRequired.cast(source));
26+
} catch (final ClassCastException e) {
27+
return true;
28+
}
29+
}
30+
31+
return true;
1432
}
1533
}

src/main/java/dev/manere/commands/api/CommandAPIBrigadier.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import org.jetbrains.annotations.ApiStatus;
2323
import org.jetbrains.annotations.NotNull;
2424

25-
import java.util.List;
26-
import java.util.Map;
27-
import java.util.Optional;
25+
import java.util.*;
2826
import java.util.concurrent.CompletableFuture;
2927
import java.util.function.Consumer;
3028
import java.util.function.Predicate;
@@ -44,8 +42,6 @@ public static <S extends CommandSender> CommandContext<S> buildContext(final @No
4442
@NotNull
4543
@SuppressWarnings("UnstableApiUsage")
4644
public static LiteralCommandNode<CommandSourceStack> convert(final @NotNull CommandNode node) {
47-
System.out.println("Started converting...");
48-
4945
LiteralArgumentBuilder<CommandSourceStack> builder = Commands.literal(node.literal())
5046
.executes(cmd -> {
5147
final CommandSender cmdSender = cmd.getSource().getSender();

src/main/java/dev/manere/commands/completion/CompletionProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface CompletionProvider<C> {
1313
C completes(final @NotNull CommandContext<? extends CommandSender> context);
1414

1515
@NotNull
16-
static AsyncCompletionProvider async(final @NotNull AsyncCompletionProvider lambda) {
16+
static AsyncCompletionProvider future(final @NotNull AsyncCompletionProvider lambda) {
1717
return lambda;
1818
}
1919

0 commit comments

Comments
 (0)