Skip to content

Commit

Permalink
Add basic support for overriden methods
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopyl committed Dec 18, 2020
1 parent 9b35817 commit dd84229
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandMethod> methods = new ArrayList<>();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,7 +39,7 @@ class CommandMethod {
this.parameterCount = parameters.length;
}

public String getName() {
public @NotNull String getName() {
return name;
}

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

0 comments on commit dd84229

Please sign in to comment.