-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommandManager.java
More file actions
92 lines (77 loc) · 3.28 KB
/
Copy pathCommandManager.java
File metadata and controls
92 lines (77 loc) · 3.28 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
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
package com.wizardlybump17.wlib.command;
import com.wizardlybump17.wlib.command.data.CommandData;
import com.wizardlybump17.wlib.command.exception.CommandException;
import com.wizardlybump17.wlib.command.extractor.CommandExtractor;
import com.wizardlybump17.wlib.command.extractor.DirectCommandExtractor;
import com.wizardlybump17.wlib.command.extractor.MethodCommandExtractor;
import com.wizardlybump17.wlib.command.holder.CommandHolder;
import com.wizardlybump17.wlib.command.registered.RegisteredCommand;
import lombok.Getter;
import lombok.NonNull;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.util.*;
@Getter
public class CommandManager {
private final List<RegisteredCommand> commands = new ArrayList<>();
protected final CommandHolder<?> holder;
private final @NonNull Map<Class<?>, Map<String, Field>> fieldCache = new HashMap<>();
private final @NotNull Set<CommandExtractor> commandExtractors = new HashSet<>();
public CommandManager(@NotNull CommandHolder<?> holder) {
this.holder = holder;
commandExtractors.add(new MethodCommandExtractor());
commandExtractors.add(new DirectCommandExtractor());
}
public void registerCommands(Object... objects) {
for (Object object : objects)
for (CommandExtractor extractor : commandExtractors)
commands.addAll(extractor.extract(this, holder, object));
commands.sort(null);
}
public void unregisterCommands() {
commands.clear();
}
public void execute(CommandSender<?> sender, String string) throws CommandException {
if (commands.isEmpty())
return;
for (RegisteredCommand registeredCommand : commands) {
CommandData command = registeredCommand.getCommand();
CommandResult result = registeredCommand.execute(sender, string);
switch (result) {
case PERMISSION_FAIL -> {
String message = command.getPermissionMessage();
if (message != null)
sender.sendMessage(message);
return;
}
case INVALID_SENDER -> {
String message = command.getInvalidSenderMessage();
if (message != null)
sender.sendMessage(message);
return;
}
case SUCCESS, EXCEPTION -> {
return;
}
}
}
}
@NonNull
public List<@NonNull String> autoComplete(@NonNull CommandSender<?> sender, @NonNull String string) {
return List.of();
}
public List<RegisteredCommand> getCommand(String name) {
List<RegisteredCommand> commands = new ArrayList<>(this.commands.size());
for (RegisteredCommand command : this.commands)
if (command.getName().equalsIgnoreCase(name))
commands.add(command);
return commands;
}
public List<RegisteredCommand> getCommands(@NotNull Object object) {
List<RegisteredCommand> commands = new ArrayList<>(this.commands.size());
for (RegisteredCommand command : this.commands)
if (command.isOwnedBy(object))
commands.add(command);
return commands;
}
}