Skip to content

Commit 216b637

Browse files
author
damt
committed
[+] Added tab completer
1 parent 111059f commit 216b637

File tree

8 files changed

+95
-13
lines changed

8 files changed

+95
-13
lines changed

src/main/java/xyz/damt/command/CommandHandler.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import xyz.damt.command.command.Command;
66
import xyz.damt.command.command.CommandFinder;
77
import xyz.damt.command.command.CommandRegistery;
8+
import xyz.damt.command.complete.TabComplete;
89
import xyz.damt.command.help.HelpCommandService;
910
import xyz.damt.command.provider.CommandProvider;
1011
import xyz.damt.command.provider.CommandProviderHandler;
@@ -23,6 +24,7 @@ public class CommandHandler {
2324
private final CommandRegistery commandRegistery;
2425

2526
private HelpCommandService helpCommandService;
27+
private TabComplete tabComplete;
2628

2729
/**
2830
* Command Handler that handles the whole
@@ -72,7 +74,7 @@ public final Command getCommand(String name) {
7274
*/
7375

7476
public final CommandHandler register(Object object, String name) {
75-
commandMap.put(name, new CommandFinder(object, commandProviderHandler, helpCommandService).getCommand(name));
77+
commandMap.put(name, new CommandFinder(object, commandProviderHandler, helpCommandService, tabComplete).getCommand(name));
7678
return this;
7779
}
7880

@@ -86,7 +88,7 @@ public final CommandHandler register(Object object, String name) {
8688

8789
public final CommandHandler register(Object... objects) {
8890
for (Object object : objects) {
89-
CommandFinder commandFinder = new CommandFinder(object, commandProviderHandler, helpCommandService);
91+
CommandFinder commandFinder = new CommandFinder(object, commandProviderHandler, helpCommandService, tabComplete);
9092
commandFinder.getCommands().forEach(command -> commandMap.put(command.getName(), command));
9193
}
9294
return this;
@@ -104,6 +106,18 @@ public final CommandHandler helpService(HelpCommandService helpCommandService) {
104106
return this;
105107
}
106108

109+
/**
110+
* Sets the tab complete of a command
111+
*
112+
* @param tabComplete tab completer to set to
113+
* @return {@link CommandHandler}
114+
*/
115+
116+
public final CommandHandler tabComplete(TabComplete tabComplete) {
117+
this.tabComplete = tabComplete;
118+
return this;
119+
}
120+
107121
/**
108122
* Inputs and registers all commands
109123
* That are added to the list
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package xyz.damt.command.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.PARAMETER)
10+
public @interface Optional {
11+
12+
String value();
13+
14+
}

src/main/java/xyz/damt/command/command/Command.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bukkit.command.CommandSender;
99
import org.bukkit.entity.Player;
1010
import xyz.damt.command.CommandHandler;
11+
import xyz.damt.command.complete.TabComplete;
1112
import xyz.damt.command.exception.CommandProviderNullException;
1213
import xyz.damt.command.executor.CommandExecutor;
1314
import xyz.damt.command.help.HelpCommandService;
@@ -30,6 +31,7 @@ public class Command {
3031

3132
private final CommandExecutor commandExecutor;
3233
private final HelpCommandService helpCommandService;
34+
private final TabComplete tabComplete;
3335

3436
private final List<Command> subCommands = new ArrayList<>();
3537
private final List<CommandParameter> commandParameters = new ArrayList<>();
@@ -47,7 +49,7 @@ public class Command {
4749
* @param async if the command should be ran async or not
4850
*/
4951

50-
public Command(Object object, String name, String[] aliases, Method method, String description, String usage, HelpCommandService helpCommandService, boolean async) {
52+
public Command(Object object, String name, String[] aliases, Method method, String description, String usage, HelpCommandService helpCommandService, TabComplete tabComplete, boolean async) {
5153
this.object = object;
5254

5355
this.name = name;
@@ -58,6 +60,7 @@ public Command(Object object, String name, String[] aliases, Method method, Stri
5860
this.usage = usage;
5961

6062
this.helpCommandService = helpCommandService;
63+
this.tabComplete = tabComplete;
6164
this.commandExecutor = new CommandExecutor(this);
6265
}
6366

@@ -103,12 +106,15 @@ public void execute(CommandSender commandSender, String[] args) {
103106
try {
104107
objects.add(commandParameter.getCommandProvider().provide(args[commandParameters.indexOf(commandParameter)]));
105108
} catch (CommandProviderNullException e) {
106-
if (e.getMessage() == null) {
107-
commandSender.sendMessage(ChatColor.RED + "The argument '" + commandParameter.getName() + "' is null!");
109+
if (!commandParameter.isOptional()) {
110+
if (e.getMessage() == null) {
111+
commandSender.sendMessage(ChatColor.RED + "The argument '" + commandParameter.getName() + "' is null!");
112+
return;
113+
}
114+
115+
commandSender.sendMessage(e.getMessage());
108116
return;
109117
}
110-
commandSender.sendMessage(e.getMessage());
111-
return;
112118
}
113119
}
114120

src/main/java/xyz/damt/command/command/CommandFinder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.bukkit.entity.Player;
44
import xyz.damt.command.annotation.Permission;
55
import xyz.damt.command.annotation.Sender;
6+
import xyz.damt.command.complete.TabComplete;
67
import xyz.damt.command.help.HelpCommandService;
78
import xyz.damt.command.provider.CommandProviderHandler;
89

@@ -18,6 +19,7 @@ public class CommandFinder {
1819

1920
private final CommandProviderHandler commandProviderHandler;
2021
private final HelpCommandService helpCommandService;
22+
private final TabComplete tabComplete;
2123

2224
private final List<Command> commands = new ArrayList<>();
2325

@@ -28,11 +30,12 @@ public class CommandFinder {
2830
* @param object {@link Object}
2931
*/
3032

31-
public CommandFinder(Object object, CommandProviderHandler commandProviderHandler, HelpCommandService helpCommandService) {
33+
public CommandFinder(Object object, CommandProviderHandler commandProviderHandler, HelpCommandService helpCommandService, TabComplete tabComplete) {
3234
this.object = object;
3335

3436
this.commandProviderHandler = commandProviderHandler;
3537
this.helpCommandService = helpCommandService;
38+
this.tabComplete = tabComplete;
3639

3740
this.init();
3841
this.edit();
@@ -101,7 +104,7 @@ private final void init() {
101104

102105
if (commandAnnotation != null) {
103106
Permission permissionAnnotation = method.getAnnotation(Permission.class);
104-
Command command = new Command(object, commandAnnotation.value(), commandAnnotation.aliases(), method, commandAnnotation.description(), commandAnnotation.usage(), helpCommandService, commandAnnotation.async());
107+
Command command = new Command(object, commandAnnotation.value(), commandAnnotation.aliases(), method, commandAnnotation.description(), commandAnnotation.usage(), helpCommandService, tabComplete, commandAnnotation.async());
105108

106109
if (permissionAnnotation != null) {
107110
command.setPermission(permissionAnnotation.permission());

src/main/java/xyz/damt/command/command/CommandParameter.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.Getter;
44
import lombok.SneakyThrows;
55
import xyz.damt.command.annotation.Name;
6+
import xyz.damt.command.annotation.Optional;
67
import xyz.damt.command.exception.CommandParameterException;
78
import xyz.damt.command.provider.CommandProvider;
89
import xyz.damt.command.provider.CommandProviderHandler;
@@ -17,6 +18,7 @@ public class CommandParameter {
1718
private final CommandProviderHandler commandProviderHandler;
1819

1920
private CommandProvider<?> commandProvider;
21+
private boolean optional, text;
2022
private String name;
2123

2224
@SneakyThrows
@@ -35,11 +37,18 @@ private final void init() throws CommandParameterException {
3537
this.name = parameter.getName();
3638
else this.name = name.value();
3739

40+
Optional optional = parameter.getAnnotation(Optional.class);
41+
42+
if (optional != null) {
43+
this.optional = true;
44+
this.name = optional.value();
45+
}
46+
3847
this.commandProvider = commandProviderHandler.getProvider(parameter);
3948

4049
if (commandProvider == null)
4150
throw new CommandParameterException("The command parameter '" + parameter.getType().getSimpleName() + "' for command " + command
42-
.getName() + " does not exist!");
51+
.getName() + " does not exist!");
4352
}
4453

4554
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package xyz.damt.command.complete;
2+
3+
import xyz.damt.command.command.Command;
4+
5+
import java.util.List;
6+
7+
public interface TabComplete {
8+
9+
List<String> get(Command command, List<Command> subCommands, String[] args);
10+
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package xyz.damt.command.complete.impl;
2+
3+
import xyz.damt.command.command.Command;
4+
import xyz.damt.command.complete.TabComplete;
5+
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class DefaultTabCompleter implements TabComplete {
10+
11+
@Override
12+
public List<String> get(Command command, List<Command> subCommands, String[] args) {
13+
final List<String> strings = subCommands.stream().map(Command::getName).collect(Collectors.toList());
14+
15+
for (String string : strings) {
16+
final String[] stringArgs = string.split("\\s+");
17+
strings.remove(string);
18+
19+
for (int i = args.length; i < stringArgs.length; i++) {
20+
strings.add(stringArgs[i]);
21+
}
22+
}
23+
24+
return strings;
25+
}
26+
27+
}

src/main/java/xyz/damt/command/executor/CommandExecutor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package xyz.damt.command.executor;
22

3-
import com.google.common.collect.Lists;
43
import org.bukkit.command.CommandSender;
54
import org.bukkit.command.defaults.BukkitCommand;
65
import xyz.damt.command.command.Command;
76
import xyz.damt.command.command.CommandParameter;
87

98
import java.util.Arrays;
10-
import java.util.Collections;
119
import java.util.List;
1210

1311
public class CommandExecutor extends BukkitCommand {
@@ -63,7 +61,7 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
6361
CommandParameter commandParameter = command.getCommandParameters().get(args.length);
6462

6563
if (commandParameter == null)
66-
return Collections.emptyList();
64+
return command.getTabComplete().get(command, command.getSubCommands(), args);
6765

6866
return commandParameter.getCommandProvider().suggestions(commandParameter);
6967
}

0 commit comments

Comments
 (0)