Skip to content

Commit

Permalink
Can now register commands to be ran by Bukkit
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopyl committed Dec 12, 2020
1 parent e47cd61 commit 85290da
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, CommandNode> rootCommands = new HashMap<>();
private final TypeCollection typeCollection = new TypeCollection();

public <T> void addCommand(String name, Class<T> commandType, Supplier<T> commandClassSupplier) {
CommandGroup group = new CommandGroup(commandType, commandClassSupplier, name, typeCollection);
rootCommands.put(name, group);
}

public <T> void registerCommand(String name, Class<T> commandType, Supplier<T> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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");
Expand All @@ -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);
}
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 85290da

Please sign in to comment.