forked from UnlegitDqrk/unlegitlibrary
95 lines
3.2 KiB
Java
95 lines
3.2 KiB
Java
package dev.unlegitdqrk.unlegitlibrary.command;
|
|
|
|
import dev.unlegitdqrk.unlegitlibrary.command.events.*;
|
|
import dev.unlegitdqrk.unlegitlibrary.event.EventManager;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.util.*;
|
|
|
|
public final class CommandManager {
|
|
|
|
private final Map<Class<? extends Command>, Command> commandInstances = new HashMap<>();
|
|
private final EventManager eventManager;
|
|
|
|
public CommandManager(EventManager eventManager) {
|
|
this.eventManager = eventManager;
|
|
}
|
|
|
|
// Registriere die Klasse, instanziiere sie einmalig
|
|
public final void registerCommand(Class<? extends Command> commandClass) {
|
|
if (commandInstances.containsKey(commandClass)) return;
|
|
|
|
try {
|
|
Command instance = commandClass.getDeclaredConstructor().newInstance();
|
|
commandInstances.put(commandClass, instance);
|
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
|
e.printStackTrace(); // Oder Logger verwenden
|
|
}
|
|
}
|
|
|
|
public final void unregisterCommand(Class<? extends Command> commandClass) {
|
|
commandInstances.remove(commandClass);
|
|
}
|
|
|
|
public List<Command> getCommands() {
|
|
return new ArrayList<>(commandInstances.values());
|
|
}
|
|
|
|
public Command getCommandByName(String name) {
|
|
for (Command cmd : commandInstances.values()) {
|
|
if (cmd.getName().equalsIgnoreCase(name)) return cmd;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public Command getCommandByAlias(String alias) {
|
|
for (Command cmd : commandInstances.values()) {
|
|
for (String a : cmd.getAliases()) {
|
|
if (a.equalsIgnoreCase(alias)) return cmd;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public Command getCommand(String input) {
|
|
Command cmd = getCommandByName(input);
|
|
return (cmd != null) ? cmd : getCommandByAlias(input);
|
|
}
|
|
|
|
public void execute(CommandExecutor executor, String line) {
|
|
String[] split = line.trim().split("\\s+");
|
|
if (split.length == 0) return;
|
|
|
|
String commandLabel = split[0];
|
|
String[] args = Arrays.copyOfRange(split, 1, split.length);
|
|
|
|
Command command = getCommand(commandLabel);
|
|
if (command == null) {
|
|
eventManager.executeEvent(new CommandNotFoundEvent(this, executor, commandLabel, args));
|
|
return;
|
|
}
|
|
|
|
// Pre-execute event
|
|
PreCommandExecuteEvent preEvent = new PreCommandExecuteEvent(this, executor, command);
|
|
eventManager.executeEvent(preEvent);
|
|
if (preEvent.isCancelled()) return;
|
|
|
|
// Check permissions
|
|
if (!executor.hasPermissions(command.getPermissions())) {
|
|
eventManager.executeEvent(new CommandExecutorMissingPermissionEvent(this, executor, command));
|
|
return;
|
|
}
|
|
|
|
// Execute event
|
|
CommandExecuteEvent execEvent = new CommandExecuteEvent(this, executor, command);
|
|
eventManager.executeEvent(execEvent);
|
|
if (execEvent.isCancelled()) return;
|
|
|
|
// Execute command
|
|
command.execute(executor, commandLabel, args);
|
|
|
|
// Post-execute event
|
|
eventManager.executeEvent(new CommandExecutedEvent(this, executor, command));
|
|
}
|
|
}
|