Skip to content

Commit fd54913

Browse files
committed
Can now register commands to be ran by Bukkit
1 parent c4f08b6 commit fd54913

File tree

5 files changed

+82
-8
lines changed

5 files changed

+82
-8
lines changed

src/main/java/fr/zcraft/quartzlib/components/commands/CommandGroup.java

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void run(Object parentInstance, CommandSender sender, String[] args) throws Comm
9393
private void runSelf(Object instance, CommandSender sender, String[] args) throws CommandException {
9494
String commandName = args[0];
9595
CommandNode subCommand = subCommands.get(commandName);
96+
// TODO: handle null
9697
subCommand.run(instance, sender, Arrays.copyOfRange(args, 1, args.length));
9798
}
9899

src/main/java/fr/zcraft/quartzlib/components/commands/CommandManager.java

+15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
package fr.zcraft.quartzlib.components.commands;
22

33
import fr.zcraft.quartzlib.components.commands.exceptions.CommandException;
4+
import fr.zcraft.quartzlib.core.QuartzLib;
45
import java.util.HashMap;
56
import java.util.Map;
7+
import java.util.Objects;
68
import java.util.function.Supplier;
79
import org.bukkit.command.CommandSender;
10+
import org.bukkit.command.PluginCommand;
811

912
public class CommandManager {
1013
private final Map<String, CommandNode> rootCommands = new HashMap<>();
1114
private final TypeCollection typeCollection = new TypeCollection();
1215

16+
public <T> void addCommand(String name, Class<T> commandType, Supplier<T> commandClassSupplier) {
17+
CommandGroup group = new CommandGroup(commandType, commandClassSupplier, name, typeCollection);
18+
rootCommands.put(name, group);
19+
}
20+
1321
public <T> void registerCommand(String name, Class<T> commandType, Supplier<T> commandClassSupplier) {
1422
CommandGroup group = new CommandGroup(commandType, commandClassSupplier, name, typeCollection);
1523
rootCommands.put(name, group);
24+
registerCommand(group);
25+
}
26+
27+
private void registerCommand(CommandGroup group) {
28+
PluginCommand command = QuartzLib.getPlugin().getCommand(group.getName());
29+
// TODO: handle null here
30+
Objects.requireNonNull(command).setExecutor(new QuartzCommandExecutor(group));
1631
}
1732

1833
public void run(CommandSender sender, String commandName, String... args) throws CommandException {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package fr.zcraft.quartzlib.components.commands;
2+
3+
import fr.zcraft.quartzlib.components.commands.exceptions.CommandException;
4+
import org.bukkit.command.Command;
5+
import org.bukkit.command.CommandExecutor;
6+
import org.bukkit.command.CommandSender;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
public class QuartzCommandExecutor implements CommandExecutor {
10+
private final CommandGroup group;
11+
12+
public QuartzCommandExecutor(CommandGroup group) {
13+
this.group = group;
14+
}
15+
16+
@Override
17+
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
18+
@NotNull String[] args) {
19+
try {
20+
group.run(sender, args);
21+
} catch (CommandException e) {
22+
throw new RuntimeException(e); // TODO
23+
}
24+
return true;
25+
}
26+
}

src/test/java/fr/zcraft/quartzlib/components/commands/CommandGraphTests.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void list() {
6868
}
6969
}
7070

71-
commands.registerCommand("foo", FooCommand.class, () -> new FooCommand());
71+
commands.addCommand("foo", FooCommand.class, () -> new FooCommand());
7272
commands.run(server.addPlayer(), "foo", "get");
7373
Assert.assertArrayEquals(new boolean[] {false, true, false}, ran);
7474
}
@@ -83,7 +83,7 @@ public void add(String arg) {
8383
}
8484
}
8585

86-
commands.registerCommand("foo", FooCommand.class, () -> new FooCommand());
86+
commands.addCommand("foo", FooCommand.class, () -> new FooCommand());
8787
commands.run(server.addPlayer(), "foo", "add", "pomf");
8888
Assert.assertArrayEquals(new String[] {"pomf"}, argValue);
8989
}
@@ -98,7 +98,7 @@ public void add(Integer arg) {
9898
}
9999
}
100100

101-
commands.registerCommand("foo", FooCommand.class, () -> new FooCommand());
101+
commands.addCommand("foo", FooCommand.class, () -> new FooCommand());
102102
commands.run(server.addPlayer(), "foo", "add", "42");
103103
Assert.assertArrayEquals(new int[] {42}, argValue);
104104
}
@@ -113,7 +113,7 @@ public void add(FooEnum arg) {
113113
}
114114
}
115115

116-
commands.registerCommand("foo", FooCommand.class, () -> new FooCommand());
116+
commands.addCommand("foo", FooCommand.class, () -> new FooCommand());
117117
commands.run(server.addPlayer(), "foo", "add", "foo");
118118
Assert.assertArrayEquals(new FooEnum[] {FooEnum.FOO}, argValue);
119119
commands.run(server.addPlayer(), "foo", "add", "bar");
@@ -131,7 +131,7 @@ public void add(@Sender CommandSender sender) {
131131
}
132132
}
133133

134-
commands.registerCommand("foo", FooCommand.class, () -> new FooCommand());
134+
commands.addCommand("foo", FooCommand.class, () -> new FooCommand());
135135
commands.run(player, "foo", "add");
136136
Assert.assertArrayEquals(new CommandSender[] {player}, senders);
137137
}
@@ -155,9 +155,8 @@ public void add() {
155155
}
156156
}
157157

158-
Player player = server.addPlayer();
159-
commands.registerCommand("foo", FooCommand.class, () -> new FooCommand());
160-
commands.run(player, "foo", "sub", "add");
158+
commands.addCommand("foo", FooCommand.class, () -> new FooCommand());
159+
commands.run(server.addPlayer(), "foo", "sub", "add");
161160
Assert.assertArrayEquals(new boolean[] {true}, ran);
162161
}
163162

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package fr.zcraft.quartzlib.components.commands;
2+
3+
import fr.zcraft.quartzlib.MockedToasterTest;
4+
import fr.zcraft.quartzlib.components.commands.exceptions.CommandException;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
public class CommandRegistrationTests extends MockedToasterTest {
10+
private CommandManager commands;
11+
12+
@Before
13+
public void beforeEach() {
14+
commands = new CommandManager();
15+
}
16+
17+
@Test
18+
public void canRegisterAndRunCommand() throws CommandException {
19+
20+
final boolean[] ran = {false};
21+
22+
class FooCommand {
23+
public void get() {
24+
ran[0] = true;
25+
}
26+
}
27+
28+
commands.registerCommand("toaster", FooCommand.class, () -> new FooCommand());
29+
boolean success = server.dispatchCommand(server.addPlayer(), "toaster get");
30+
Assert.assertTrue(success);
31+
Assert.assertArrayEquals(new boolean[] {true}, ran);
32+
}
33+
}

0 commit comments

Comments
 (0)