Skip to content

Commit

Permalink
fix completers & move version to "global properties"
Browse files Browse the repository at this point in the history
  • Loading branch information
Badbird5907 committed Oct 4, 2022
1 parent 41b09a3 commit b4be17d
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 28 deletions.
1 change: 0 additions & 1 deletion Commander-Bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ plugins {
}

group = "net.octopvp"
version = "0.0.1-DEV"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.regex.Pattern;

public class BukkitCommandWrapper extends org.bukkit.command.Command {
private CommandInfo commandInfo;
private final CommandInfo commandInfo;

public BukkitCommandWrapper(CommandInfo command) {
super(command.getName(), command.getDescription(), command.getUsage(), Arrays.asList(command.getAliases()));
Expand All @@ -51,6 +51,6 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)

@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
return commandInfo.getCommander().getSuggestions(new BukkitCommandSenderImpl(sender), alias, args);
return commandInfo.getCommander().getSuggestions(new BukkitCommandSenderImpl(sender), alias, args, sender);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public interface Commander {

void executeCommand(CoreCommandSender sender, String label, String[] args) throws CommandException;

List<String> getSuggestions(CoreCommandSender sender, String command);
List<String> getSuggestions(CoreCommandSender sender, String command, Object senderWrapper);

List<String> getSuggestions(CoreCommandSender sender, String prefix, String[] args);
List<String> getSuggestions(CoreCommandSender sender, String prefix, String[] args, Object senderWrapper);
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,24 @@ private void registerCompleters() {
Method method = entry.getKey();
Completer completer = method.getAnnotation(Completer.class);
String targetCommand = completer.name();
boolean isInParentCommand = method.getDeclaringClass().isAnnotationPresent(Command.class);
Command parentCommand = method.getDeclaringClass().getAnnotation(Command.class);
int[] indexes = completer.index();

for (int index : indexes) {
if (index < 0) {
throw new IllegalArgumentException("Completer index must be greater than 0!");
if (index < -1) {
throw new IllegalArgumentException("Completer index must be greater than -1!"); // -1 is the catch all
}
}
if (targetCommand.equals("")) {
throw new IllegalArgumentException("Completer name cannot be empty!");
}
String[] split = targetCommand.toLowerCase().split(" ");
boolean isSubCommand = split.length > 1;
boolean isSubCommand = isInParentCommand || split.length > 1;

CommandInfo command = getCommand(split[0]), parent;
CommandInfo command, parent;
if (!isSubCommand) command = getCommand(split[0]);
else command = getCommand(parentCommand.name());
if (command == null) return;

if (isSubCommand && !command.isParentCommand())
Expand All @@ -269,7 +274,11 @@ private void registerCompleters() {
CompleterInfo completerInfo;
if (isSubCommand) {
parent = command;
command = parent.getSubCommand(split[1]);
if (split.length > 1) {
command = parent.getSubCommand(split[split.length - 1]);
} else if (split.length == 1) {
command = parent.getSubCommand(split[0]);
}
if (command == null)
throw new IllegalArgumentException("Completer references a subcommand, which does not exist!");
completerInfo = new CompleterInfo(
Expand Down Expand Up @@ -591,7 +600,7 @@ public CommandInfo getCommand(String label) {
}

@Override
public List<String> getSuggestions(CoreCommandSender sender, final String input) {
public List<String> getSuggestions(CoreCommandSender sender, final String input, Object senderWrapper) {
String[] split = input.split(" ");
if (split.length == 0) {
return null;
Expand Down Expand Up @@ -657,7 +666,7 @@ public List<String> getSuggestions(CoreCommandSender sender, final String input)
Collection<String> customReturn = null;
if (customCompleter != null) {
Method method = customCompleter.getMethod();
Object[] args = ArgumentParser.parseCompleterArguments(customCompleter, command, sender, method.getParameters(), input, label, lastArg, split, allParams ? -1 : index);
Object[] args = ArgumentParser.parseCompleterArguments(customCompleter, command, sender, method.getParameters(), input, label, lastArg, split, allParams ? -1 : index, senderWrapper);
if (args == null) return null;
try {
Object result = method.invoke(customCompleter.getInstance(), args);
Expand Down Expand Up @@ -773,9 +782,8 @@ public List<String> getSuggestions(CoreCommandSender sender, final String input)
}

@Override
public List<String> getSuggestions(CoreCommandSender sender, String prefix, String[] args) {
public List<String> getSuggestions(CoreCommandSender sender, String prefix, String[] args, Object wrapper) {
String full = prefix + " " + String.join(" ", args); //Avoiding String#split at all costs because it compiles regex
List<String> suggestions = getSuggestions(sender, full);
return suggestions;
return getSuggestions(sender, full, wrapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Completer {
String name();
int[] index();

int[] index() default {-1};
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static Object[] parseArguments(CommandContext ctx, CommandArgs cArgs) {
return null;
}

public static Object[] parseCompleterArguments(CompleterInfo completerInfo, CommandInfo commandInfo, CoreCommandSender sender, Parameter[] parameters, String input, String label, String lastArg, String[] split, int index) {
public static Object[] parseCompleterArguments(CompleterInfo completerInfo, CommandInfo commandInfo, CoreCommandSender sender, Parameter[] parameters, String input, String label, String lastArg, String[] split, int index, Object senderWrapper) {
Object[] arguments = new Object[parameters.length];
int paramIndex = 0;
for (int i = 0; i < parameters.length; i++) {
Expand All @@ -160,6 +160,11 @@ public static Object[] parseCompleterArguments(CompleterInfo completerInfo, Comm
arguments[i] = supplier.get();
continue;
}
//check if senderWrapper is an instance of the parameter, if so, return it
if (senderWrapper != null && parameter.getType().isAssignableFrom(senderWrapper.getClass())) {
arguments[i] = senderWrapper;
continue;
}

if (parameter.isAnnotationPresent(Sender.class) || parameter.getName().equalsIgnoreCase("sender")) {
arguments[i] = sender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

package net.octopvp.commander.sender;

import net.octopvp.commander.exception.CommandParseException;

import java.util.UUID;

public interface CoreCommandSender {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

Expand All @@ -58,23 +59,23 @@ public void testCompletions() {
.registerProvider(TestClassTwo.class, new TestProvider2())
.registerProvider(TestClassThree.class, new TestProvider3())
;
List<String> completions = commander.getSuggestions(new CommandSender(), "test ");
List<String> completions = commander.getSuggestions(new CommandSender(), "test ", null);
assertNotNull(completions);
assertFalse(completions.isEmpty());
assertTrue(completions.contains("Hello") && completions.contains("World"));
System.out.println("First passed.");
List<String> completions2 = commander.getSuggestions(new CommandSender(), "test abcd ");
List<String> completions2 = commander.getSuggestions(new CommandSender(), "test abcd ", null);
assertNotNull(completions2);
assertFalse(completions2.isEmpty());
assertTrue(completions2.contains("Yes") && completions2.contains("ABCDEFG"));
System.out.println("Second passed.");
List<String> completions3 = commander.getSuggestions(new CommandSender(), "test abcd def ");
List<String> completions3 = commander.getSuggestions(new CommandSender(), "test abcd def ", null);
assertNotNull(completions3);
assertFalse(completions3.isEmpty());
assertTrue(completions3.contains("It Works!") && completions3.contains(":)"));
System.out.println("Third passed.");

List<String> completions4 = commander.getSuggestions(new CommandSender(), "t a b ");
List<String> completions4 = commander.getSuggestions(new CommandSender(), "t a b ", null);
System.out.println(completions4);
assertNotNull(completions4);
assertFalse(completions4.isEmpty());
Expand All @@ -92,7 +93,7 @@ public void t(@Sender CommandSender sender, String s, String a, String b) {

@Completer(name = "t", index = 2)
public List<String> t2(@Sender CommandSender sender, String input, String lastArg) {
return Arrays.asList("b");
return Collections.singletonList("b");
}

private static class TestProvider implements Provider<TestClass> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public void test() {
commander.executeCommand(new CommandSender(), "test", new String[]{"hello_world!"});
assertTrue(passed3);

List<String> completions = commander.getSuggestions(new CommandSender(), "test completer ");
List<String> completions = commander.getSuggestions(new CommandSender(), "test completer ", null);
assertNotNull(completions);
assertTrue(completions.contains("Hello") && completions.contains("World"));

List<String> completions2 = commander.getSuggestions(new CommandSender(), "test completer abcd ");
List<String> completions2 = commander.getSuggestions(new CommandSender(), "test completer abcd ", null);
assertNotNull(completions2);
assertTrue(completions2.contains("Yes") && completions2.contains("ABCDEFG"));

List<String> completions3 = commander.getSuggestions(new CommandSender(), "test completer abcd def ");
List<String> completions3 = commander.getSuggestions(new CommandSender(), "test completer abcd def ", null);
assertNotNull(completions3);
assertTrue(completions3.contains("It Works!") && completions3.contains(":)"));
}
Expand Down
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ plugins {
}

group = "net.octopvp"
version = "0.0.1-DEV"
description = "A universal command parsing library"

repositories {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=0.0.1-DEV

0 comments on commit b4be17d

Please sign in to comment.