Added Simple Command Completion

This commit is contained in:
Sn0wStorm 2018-10-30 18:22:04 +01:00
parent 6da8bbcb48
commit db61e27fa3
3 changed files with 48 additions and 1 deletions

View File

@ -108,6 +108,7 @@ public class P extends JavaPlugin {
inventoryListener = new InventoryListener();
worldListener = new WorldListener();
getCommand("Brewery").setExecutor(new CommandListener());
getCommand("Brewery").setTabCompleter(new TabListener());
p.getServer().getPluginManager().registerEvents(blockListener, p);
p.getServer().getPluginManager().registerEvents(playerListener, p);

View File

@ -20,7 +20,6 @@ public class CommandListener implements CommandExecutor {
public P p = P.p;
// TODO add command complete
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

View File

@ -0,0 +1,47 @@
package com.dre.brewery.listeners;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TabListener implements TabCompleter {
private static final Map<String, List<String>> completions = new HashMap<>();
private static final List<String> topCompletions = new ArrayList<>(2);
static {
completions.put("", Arrays.asList("info", "unlabel"));
topCompletions.add("brew");
//topCompletions.add("brewery");
}
@Override
public List<String> onTabComplete(CommandSender commandSender, Command cmd, String command, String[] args) {
if (args.length == 0) {
return topCompletions;
} else if (args.length == 1) {
for (Map.Entry<String, List<String>> entry : completions.entrySet()) {
List<String> list = new ArrayList<>();
for (String comp : entry.getValue()) {
if (comp.startsWith(args[0])) {
list.add(comp);
}
}
if (list.isEmpty()) {
return null;
}
return list;
}
}
return null;
}
}