From 85290da26e1a32887de3dedee7b8196da1e3429d Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Sat, 12 Dec 2020 09:55:07 +0100 Subject: [PATCH] Can now register commands to be ran by Bukkit --- .../components/commands/CommandGroup.java | 1 + .../components/commands/CommandManager.java | 15 +++++++++ .../commands/QuartzCommandExecutor.java | 26 +++++++++++++++ .../commands/CommandGraphTests.java | 15 ++++----- .../commands/CommandRegistrationTests.java | 33 +++++++++++++++++++ 5 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 src/main/java/fr/zcraft/quartzlib/components/commands/QuartzCommandExecutor.java create mode 100644 src/test/java/fr/zcraft/quartzlib/components/commands/CommandRegistrationTests.java diff --git a/src/main/java/fr/zcraft/quartzlib/components/commands/CommandGroup.java b/src/main/java/fr/zcraft/quartzlib/components/commands/CommandGroup.java index a030a666..77db87b5 100644 --- a/src/main/java/fr/zcraft/quartzlib/components/commands/CommandGroup.java +++ b/src/main/java/fr/zcraft/quartzlib/components/commands/CommandGroup.java @@ -93,6 +93,7 @@ void run(Object parentInstance, CommandSender sender, String[] args) throws Comm private void runSelf(Object instance, CommandSender sender, String[] args) throws CommandException { String commandName = args[0]; CommandNode subCommand = subCommands.get(commandName); + // TODO: handle null subCommand.run(instance, sender, Arrays.copyOfRange(args, 1, args.length)); } diff --git a/src/main/java/fr/zcraft/quartzlib/components/commands/CommandManager.java b/src/main/java/fr/zcraft/quartzlib/components/commands/CommandManager.java index d496c506..6e7933f9 100644 --- a/src/main/java/fr/zcraft/quartzlib/components/commands/CommandManager.java +++ b/src/main/java/fr/zcraft/quartzlib/components/commands/CommandManager.java @@ -1,18 +1,33 @@ package fr.zcraft.quartzlib.components.commands; import fr.zcraft.quartzlib.components.commands.exceptions.CommandException; +import fr.zcraft.quartzlib.core.QuartzLib; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.function.Supplier; import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; public class CommandManager { private final Map rootCommands = new HashMap<>(); private final TypeCollection typeCollection = new TypeCollection(); + public void addCommand(String name, Class commandType, Supplier commandClassSupplier) { + CommandGroup group = new CommandGroup(commandType, commandClassSupplier, name, typeCollection); + rootCommands.put(name, group); + } + public void registerCommand(String name, Class commandType, Supplier commandClassSupplier) { CommandGroup group = new CommandGroup(commandType, commandClassSupplier, name, typeCollection); rootCommands.put(name, group); + registerCommand(group); + } + + private void registerCommand(CommandGroup group) { + PluginCommand command = QuartzLib.getPlugin().getCommand(group.getName()); + // TODO: handle null here + Objects.requireNonNull(command).setExecutor(new QuartzCommandExecutor(group)); } public void run(CommandSender sender, String commandName, String... args) throws CommandException { diff --git a/src/main/java/fr/zcraft/quartzlib/components/commands/QuartzCommandExecutor.java b/src/main/java/fr/zcraft/quartzlib/components/commands/QuartzCommandExecutor.java new file mode 100644 index 00000000..9c2fa779 --- /dev/null +++ b/src/main/java/fr/zcraft/quartzlib/components/commands/QuartzCommandExecutor.java @@ -0,0 +1,26 @@ +package fr.zcraft.quartzlib.components.commands; + +import fr.zcraft.quartzlib.components.commands.exceptions.CommandException; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +public class QuartzCommandExecutor implements CommandExecutor { + private final CommandGroup group; + + public QuartzCommandExecutor(CommandGroup group) { + this.group = group; + } + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, + @NotNull String[] args) { + try { + group.run(sender, args); + } catch (CommandException e) { + throw new RuntimeException(e); // TODO + } + return true; + } +} diff --git a/src/test/java/fr/zcraft/quartzlib/components/commands/CommandGraphTests.java b/src/test/java/fr/zcraft/quartzlib/components/commands/CommandGraphTests.java index e3fb96c2..1462f251 100644 --- a/src/test/java/fr/zcraft/quartzlib/components/commands/CommandGraphTests.java +++ b/src/test/java/fr/zcraft/quartzlib/components/commands/CommandGraphTests.java @@ -68,7 +68,7 @@ public void list() { } } - commands.registerCommand("foo", FooCommand.class, () -> new FooCommand()); + commands.addCommand("foo", FooCommand.class, () -> new FooCommand()); commands.run(server.addPlayer(), "foo", "get"); Assert.assertArrayEquals(new boolean[] {false, true, false}, ran); } @@ -83,7 +83,7 @@ public void add(String arg) { } } - commands.registerCommand("foo", FooCommand.class, () -> new FooCommand()); + commands.addCommand("foo", FooCommand.class, () -> new FooCommand()); commands.run(server.addPlayer(), "foo", "add", "pomf"); Assert.assertArrayEquals(new String[] {"pomf"}, argValue); } @@ -98,7 +98,7 @@ public void add(Integer arg) { } } - commands.registerCommand("foo", FooCommand.class, () -> new FooCommand()); + commands.addCommand("foo", FooCommand.class, () -> new FooCommand()); commands.run(server.addPlayer(), "foo", "add", "42"); Assert.assertArrayEquals(new int[] {42}, argValue); } @@ -113,7 +113,7 @@ public void add(FooEnum arg) { } } - commands.registerCommand("foo", FooCommand.class, () -> new FooCommand()); + commands.addCommand("foo", FooCommand.class, () -> new FooCommand()); commands.run(server.addPlayer(), "foo", "add", "foo"); Assert.assertArrayEquals(new FooEnum[] {FooEnum.FOO}, argValue); commands.run(server.addPlayer(), "foo", "add", "bar"); @@ -131,7 +131,7 @@ public void add(@Sender CommandSender sender) { } } - commands.registerCommand("foo", FooCommand.class, () -> new FooCommand()); + commands.addCommand("foo", FooCommand.class, () -> new FooCommand()); commands.run(player, "foo", "add"); Assert.assertArrayEquals(new CommandSender[] {player}, senders); } @@ -155,9 +155,8 @@ public void add() { } } - Player player = server.addPlayer(); - commands.registerCommand("foo", FooCommand.class, () -> new FooCommand()); - commands.run(player, "foo", "sub", "add"); + commands.addCommand("foo", FooCommand.class, () -> new FooCommand()); + commands.run(server.addPlayer(), "foo", "sub", "add"); Assert.assertArrayEquals(new boolean[] {true}, ran); } diff --git a/src/test/java/fr/zcraft/quartzlib/components/commands/CommandRegistrationTests.java b/src/test/java/fr/zcraft/quartzlib/components/commands/CommandRegistrationTests.java new file mode 100644 index 00000000..04adafde --- /dev/null +++ b/src/test/java/fr/zcraft/quartzlib/components/commands/CommandRegistrationTests.java @@ -0,0 +1,33 @@ +package fr.zcraft.quartzlib.components.commands; + +import fr.zcraft.quartzlib.MockedToasterTest; +import fr.zcraft.quartzlib.components.commands.exceptions.CommandException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class CommandRegistrationTests extends MockedToasterTest { + private CommandManager commands; + + @Before + public void beforeEach() { + commands = new CommandManager(); + } + + @Test + public void canRegisterAndRunCommand() throws CommandException { + + final boolean[] ran = {false}; + + class FooCommand { + public void get() { + ran[0] = true; + } + } + + commands.registerCommand("toaster", FooCommand.class, () -> new FooCommand()); + boolean success = server.dispatchCommand(server.addPlayer(), "toaster get"); + Assert.assertTrue(success); + Assert.assertArrayEquals(new boolean[] {true}, ran); + } +}