-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandManager.java
More file actions
60 lines (54 loc) · 3.19 KB
/
CommandManager.java
File metadata and controls
60 lines (54 loc) · 3.19 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
package com.github.pinont.singularitylib.api.manager;
import com.github.pinont.singularitylib.api.command.SimpleCommand;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import org.bukkit.ChatColor;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
import static com.github.pinont.singularitylib.plugin.CorePlugin.getStartTime;
import static com.github.pinont.singularitylib.plugin.CorePlugin.sendConsoleMessage;
public class CommandManager {
private int success = 0;
private int failure = 0;
private int target_amount = 0;
public void register(Plugin plugin, List<SimpleCommand> simpleCommands) {
final LifecycleEventManager<@NotNull Plugin> lifecycleManager = plugin.getLifecycleManager();
sendConsoleMessage(ChatColor.WHITE + "Registering Commands: " + Arrays.toString(simpleCommands.stream().map(SimpleCommand::getName).toArray()));
lifecycleManager.registerEventHandler(LifecycleEvents.COMMANDS, (event) -> {
for (SimpleCommand simpleCommand : simpleCommands) {
String[] aliases = simpleCommand.getName().toLowerCase().split(":");
sendConsoleMessage(ChatColor.WHITE + "Founded " + ChatColor.YELLOW + (aliases.length - 1) + ChatColor.WHITE + " aliases in " + ChatColor.YELLOW + aliases[0]);
target_amount++;
try {
event.registrar().register(aliases[0], simpleCommand);
sendConsoleMessage(ChatColor.WHITE + "Registered command: " + aliases[0]);
success++;
} catch (Exception e) {
failure++;
sendConsoleMessage(ChatColor.YELLOW + "Failed to register command: " + aliases[0] + "\n\nERROR TRACE: \n" + e.getMessage());
}
if (aliases.length > 1) {
for (int i = 1; i < aliases.length; i++) {
target_amount++;
try {
event.registrar().register(aliases[i], simpleCommand);
sendConsoleMessage(ChatColor.WHITE + "Registered alias: " + aliases[i]);
success++;
} catch (Exception e) {
failure++;
sendConsoleMessage(ChatColor.YELLOW + "Failed to register alias: " + aliases[i] + "\n\nERROR TRACE: \n" + e.getMessage());
}
}
}
}
if (failure > 0) {
sendConsoleMessage(ChatColor.GREEN + "Command Register: successfully registered " + success + "/" + target_amount + " commands, " + ChatColor.RED + failure + " command failures");
}
sendConsoleMessage(ChatColor.GREEN + "Command Register: successfully registered " + success + "/" + target_amount + " commands.");
Long startTime = System.currentTimeMillis() - getStartTime();
sendConsoleMessage(ChatColor.WHITE + "Plugin is Enabled (took " + ChatColor.YELLOW + startTime + ChatColor.WHITE + " ms)");
});
}
}