-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandHandler.java
More file actions
139 lines (108 loc) · 4.74 KB
/
CommandHandler.java
File metadata and controls
139 lines (108 loc) · 4.74 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package singularity.command;
import lombok.Getter;
import lombok.Setter;
import singularity.Singularity;
import singularity.interfaces.IProperCommand;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.atomic.AtomicReference;
public class CommandHandler {
@Getter @Setter
private static ConcurrentSkipListMap<String, ModuleCommand> loadedModuleCommands = new ConcurrentSkipListMap<>();
@Getter @Setter
private static ConcurrentSkipListMap<String, CosmicCommand> loadedStreamlineCommands = new ConcurrentSkipListMap<>();
@Getter @Setter
private static ConcurrentSkipListMap<String, IProperCommand> properlyRegisteredCommands = new ConcurrentSkipListMap<>();
private static void registerCommandRaw(CosmicCommand command) {
if (isProperCommandRegistered(command.getIdentifier())) {
unregisterCommandRaw(command.getIdentifier());
}
IProperCommand properCommand = Singularity.getInstance().getPlatform().createCommand(command);
properCommand.register();
getProperlyRegisteredCommands().put(command.getIdentifier(), properCommand);
getLoadedStreamlineCommands().put(command.getBase(), command);
}
private static void unregisterCommandRaw(String identifier) {
IProperCommand c = getProperlyRegisteredCommands().get(identifier);
if (c == null) return;
c.unregister();
getProperlyRegisteredCommands().remove(identifier);
getLoadedStreamlineCommands().remove(identifier);
}
public static void registerStreamlineCommand(CosmicCommand command) {
registerCommandRaw(command);
}
public static void unregisterStreamlineCommand(CosmicCommand command) {
unregisterCommandRaw(command.getIdentifier());
}
public static void registerModuleCommand(ModuleCommand command) {
registerCommandRaw(command);
getLoadedModuleCommands().put(command.getBase(), command);
}
public static void unregisterModuleCommand(ModuleCommand command) {
unregisterCommandRaw(command.getIdentifier());
getLoadedModuleCommands().remove(command.getIdentifier());
}
public static IProperCommand getProperCommand(String identifier) {
return getProperlyRegisteredCommands().get(identifier);
}
public static CosmicCommand getStreamlineCommand(String identifier) {
return getLoadedStreamlineCommands().get(identifier);
}
public static ModuleCommand getModuleCommand(String identifier) {
return getLoadedModuleCommands().get(identifier);
}
public static boolean isProperCommandRegistered(String identifier) {
return getProperlyRegisteredCommands().containsKey(identifier);
}
public static boolean isStreamlineCommandRegistered(String identifier) {
return getLoadedStreamlineCommands().containsKey(identifier);
}
public static boolean isModuleCommandRegistered(String identifier) {
return getLoadedModuleCommands().containsKey(identifier);
}
public static void flushProperCommands() {
getProperlyRegisteredCommands().forEach((s, iProperCommand) -> {
iProperCommand.unregister();
});
}
public static ConcurrentSkipListSet<String> getAllAliases() {
ConcurrentSkipListSet<String> r = new ConcurrentSkipListSet<>();
getLoadedStreamlineCommands().forEach((s, command) -> {
r.addAll(Arrays.asList(command.getAliases()));
r.add(command.getBase());
});
getLoadedModuleCommands().forEach((s, command) -> {
r.addAll(Arrays.asList(command.getAliases()));
r.add(command.getBase());
});
return r;
}
public static CosmicCommand getCommandByAlias(String alias) {
CosmicCommand command = getStreamlineCommand(alias);
if (command != null) return command;
AtomicReference<CosmicCommand> commandRef = new AtomicReference<>(null);
getLoadedStreamlineCommands().forEach((s, c) -> {
if (commandRef.get() != null) return;
for (String a : c.getAliases()) {
if (a.equalsIgnoreCase(alias)) {
commandRef.set(c);
break;
}
}
});
if (commandRef.get() != null) return commandRef.get();
getLoadedModuleCommands().forEach((s, c) -> {
if (commandRef.get() != null) return;
for (String a : c.getAliases()) {
if (a.equalsIgnoreCase(alias)) {
commandRef.set(c);
break;
}
}
});
return commandRef.get();
}
}