Skip to content

Commit 377e0b9

Browse files
author
damt
committed
[+] Fixed tab completion, and added more examples
1 parent 216b637 commit 377e0b9

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public boolean execute(CommandSender commandSender, String s, String[] strings)
5858

5959
@Override
6060
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
61-
CommandParameter commandParameter = command.getCommandParameters().get(args.length);
61+
CommandParameter commandParameter = command.getCommandParameters().get(args.length - 1);
6262

6363
if (commandParameter == null)
6464
return command.getTabComplete().get(command, command.getSubCommands(), args);

src/test/java/xyz/damt/example/Main.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import org.bukkit.inventory.ItemStack;
55
import org.bukkit.plugin.java.JavaPlugin;
66
import xyz.damt.command.CommandHandler;
7+
import xyz.damt.command.complete.impl.DefaultTabCompleter;
78
import xyz.damt.command.help.impl.DefaultHelpCommandService;
89
import xyz.damt.example.command.FlyCommand;
910
import xyz.damt.example.command.ItemStackCommand;
11+
import xyz.damt.example.completer.TabCompleter;
12+
import xyz.damt.example.help.HelpService;
1013
import xyz.damt.example.provider.ItemStackProvider;
1114

1215
@Getter
@@ -16,14 +19,12 @@ public class Main extends JavaPlugin {
1619
private static Main instance;
1720

1821
@Override
19-
public void onLoad() {
22+
public void onEnable() {
2023
instance = this;
21-
}
2224

23-
@Override
24-
public void onEnable() {
2525
new CommandHandler(this).bind(ItemStack.class, new ItemStackProvider())
26-
.register(new FlyCommand(), new ItemStackCommand()).helpService(new DefaultHelpCommandService("&c", "&7")).registerCommands();
26+
.register(new FlyCommand(), new ItemStackCommand()).helpService(new HelpService())
27+
.tabComplete(new TabCompleter()).registerCommands();
2728
}
2829

2930
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package xyz.damt.example.completer;
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 TabCompleter implements TabComplete {
10+
11+
@Override
12+
public List<String> get(Command command, List<Command> subCommands, String[] args) {
13+
return subCommands.stream().map(subCommand -> subCommand.getName() + "\n").collect(Collectors.toList());
14+
}
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package xyz.damt.example.help;
2+
3+
import xyz.damt.command.command.Command;
4+
import xyz.damt.command.help.HelpCommandService;
5+
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class HelpService implements HelpCommandService {
10+
11+
@Override
12+
public List<String> get(Command command, List<Command> subCommands) {
13+
return subCommands.stream().map(command1 -> command.getUsage() + ", ").collect(Collectors.toList());
14+
}
15+
16+
}

0 commit comments

Comments
 (0)