diff --git a/src/main/java/fr/zcraft/quartzlib/components/commands/CommandEndpoint.java b/src/main/java/fr/zcraft/quartzlib/components/commands/CommandEndpoint.java index 9ff6a167..cfad4eab 100644 --- a/src/main/java/fr/zcraft/quartzlib/components/commands/CommandEndpoint.java +++ b/src/main/java/fr/zcraft/quartzlib/components/commands/CommandEndpoint.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.Nullable; class CommandEndpoint extends CommandNode { private final List methods = new ArrayList<>(); @@ -14,7 +15,12 @@ class CommandEndpoint extends CommandNode { @Override void run(Object parentInstance, CommandSender sender, String[] args) throws CommandException { - this.methods.get(0).run(parentInstance, sender, args); + CommandMethod method = findMatchingMethod(args); + method.run(parentInstance, sender, args); + } + + @Nullable CommandMethod findMatchingMethod(String[] args) { + return this.methods.stream().filter(m -> m.getArguments().length == args.length).findFirst().orElse(null); } void addMethod(CommandMethod method) { diff --git a/src/main/java/fr/zcraft/quartzlib/components/commands/CommandMethod.java b/src/main/java/fr/zcraft/quartzlib/components/commands/CommandMethod.java index 6f0e52f9..d6f2abca 100644 --- a/src/main/java/fr/zcraft/quartzlib/components/commands/CommandMethod.java +++ b/src/main/java/fr/zcraft/quartzlib/components/commands/CommandMethod.java @@ -10,13 +10,15 @@ import java.util.ArrayList; import java.util.List; import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; class CommandMethod { - private final Method method; - private final String name; - private final CommandMethodArgument[] arguments; + @NotNull private final Method method; + @NotNull private final String name; + @NotNull private final CommandMethodArgument[] arguments; private final int parameterCount; - private CommandMethodSenderArgument senderArgument = null; + @Nullable private CommandMethodSenderArgument senderArgument = null; CommandMethod(Method method, TypeCollection typeCollection) { this.method = method; @@ -37,7 +39,7 @@ class CommandMethod { this.parameterCount = parameters.length; } - public String getName() { + public @NotNull String getName() { return name; } @@ -66,11 +68,11 @@ private Object[] parseArguments(CommandSender sender, String[] args) return parsed; } - public CommandMethodArgument[] getArguments() { + public @NotNull CommandMethodArgument[] getArguments() { return arguments; } - public Method getMethod() { + public @NotNull Method getMethod() { return method; } } 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 1462f251..5d069f91 100644 --- a/src/test/java/fr/zcraft/quartzlib/components/commands/CommandGraphTests.java +++ b/src/test/java/fr/zcraft/quartzlib/components/commands/CommandGraphTests.java @@ -160,6 +160,29 @@ public void add() { Assert.assertArrayEquals(new boolean[] {true}, ran); } + @Test + public void canHandleOverrides() throws CommandException { + final String[] argValue = {""}; + + class FooCommand { + public void add(String arg) { + argValue[0] = arg; + } + + public void add() { + argValue[0] = "bar"; + } + } + + commands.addCommand("foo", FooCommand.class, () -> new FooCommand()); + + commands.run(server.addPlayer(), "foo", "add", "pomf"); + Assert.assertArrayEquals(new String[] {"pomf"}, argValue); + + commands.run(server.addPlayer(), "foo", "add"); + Assert.assertArrayEquals(new String[] {"bar"}, argValue); + } + enum FooEnum { FOO, BAR }