-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommandService.java
More file actions
58 lines (48 loc) · 1.81 KB
/
CommandService.java
File metadata and controls
58 lines (48 loc) · 1.81 KB
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
package dansplugins.wildpets.commands;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.Set;
/**
* Service for managing and executing plugin commands.
*/
public class CommandService {
private ArrayList<AbstractPluginCommand> commands = new ArrayList<>();
private final Set<String> coreCommands;
private String notFoundMessage;
private final ArgumentParser parser = new ArgumentParser();
private final PermissionChecker permissionChecker = new PermissionChecker();
public CommandService(JavaPlugin plugin) {
coreCommands = plugin.getDescription().getCommands().keySet();
}
public void initialize(ArrayList<AbstractPluginCommand> commands, String notFoundMessage) {
this.commands = commands;
this.notFoundMessage = notFoundMessage;
}
public boolean interpretAndExecuteCommand(CommandSender sender, String label, String[] args) {
if (!coreCommands.contains(label)) {
return false;
}
if (args.length == 0) {
return false;
}
String subCommand = args[0];
String[] arguments = parser.dropFirstArgument(args);
for (AbstractPluginCommand command : commands) {
if (command.getNames().contains(subCommand)) {
if (!permissionChecker.checkPermission(sender, command.getPermissions())) {
return false;
}
if (arguments.length == 0) {
return command.execute(sender);
}
else {
return command.execute(sender, arguments);
}
}
}
sender.sendMessage(ChatColor.RED + notFoundMessage);
return false;
}
}