-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
SidebarCommands.java
104 lines (85 loc) · 3.31 KB
/
SidebarCommands.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.github.games647.scoreboardstats.commands;
import com.github.games647.scoreboardstats.ScoreboardStats;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.util.StringUtil;
/**
* This class forward all commands to the user commands for a better access
*/
public class SidebarCommands implements TabExecutor {
private final ScoreboardStats plugin;
private final Map<String, CommandHandler> commands = Maps.newHashMap();
private final List<String> subCommands = Lists.newArrayList();
public SidebarCommands(ScoreboardStats plugin) {
this.plugin = plugin;
registerSubCommands();
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
//default subcommand
String subCommand = "toggle";
String[] newArgs = args;
if (args.length != 0) {
subCommand = args[0];
//remove the subcommand from the args list
newArgs = Arrays.copyOfRange(args, 1, args.length);
}
CommandHandler commandHandler = commands.get(subCommand);
if (commandHandler == null) {
sender.sendMessage(ChatColor.DARK_RED + "Command not found");
} else {
if (commandHandler.hasPermission(sender)) {
commandHandler.onCommand(sender, subCommand, newArgs);
}
}
return true;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (args.length == 0) {
return ImmutableList.of();
}
String lastWord = args[args.length - 1];
List<String> suggestion;
if (args.length == 1) {
return subCommands.stream()
.filter(subCommand -> StringUtil.startsWithIgnoreCase(subCommand, lastWord))
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(Collectors.toList());
}
String subCommand = args[0];
CommandHandler commandHandler = commands.get(subCommand);
if (commandHandler != null) {
//remove the subcommand from the args list
suggestion = commandHandler.onTabComplete(sender, subCommand, Arrays.copyOfRange(args, 1, args.length));
if (suggestion != null) {
//Prevent NPEs and the usage of this method in nearly every handler
suggestion.sort(String.CASE_INSENSITIVE_ORDER);
}
return suggestion;
}
return null;
}
private void registerSubCommands() {
register(new ToggleCommand(plugin));
register(new ReloadCommand(plugin));
}
private void register(CommandHandler handler) {
for (String alias : handler.getAliases()) {
commands.put(alias, handler);
subCommands.add(alias);
}
String subCommand = handler.getSubCommand();
commands.put(subCommand, handler);
subCommands.add(subCommand);
}
}