- Restructured everything

This commit is contained in:
2025-09-29 14:21:10 +02:00
parent be602a35e0
commit 35989d9767
80 changed files with 538 additions and 1350 deletions

View File

@@ -1,107 +1,94 @@
/*
* Copyright (C) 2025 UnlegitDqrk - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
* See LICENSE-File if exists
*/
/*
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
* See LICENSE-File if exists
*/
package dev.unlegitdqrk.unlegitlibrary.command;
import dev.unlegitdqrk.unlegitlibrary.command.events.*;
import dev.unlegitdqrk.unlegitlibrary.event.EventManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
public class CommandManager {
public final class CommandManager {
private final List<Command> commands;
private final Map<Class<? extends Command>, Command> commandInstances = new HashMap<>();
private final EventManager eventManager;
public CommandManager(EventManager eventManager) {
this.commands = new ArrayList<>();
this.eventManager = eventManager;
}
public final void registerCommand(Command command) {
if (this.commands.contains(command)) return;
this.commands.add(command);
// 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(Command command) {
if (!this.commands.contains(command)) return;
this.commands.remove(command);
public final void unregisterCommand(Class<? extends Command> commandClass) {
commandInstances.remove(commandClass);
}
public List<Command> getCommands() {
return new ArrayList<>(commands);
return new ArrayList<>(commandInstances.values());
}
public Command getCommandByName(String name) {
for (Command command : commands) if (command.name.equals(name)) return command;
return null;
}
public Command getCommandByAliases(List<String> aliases) {
for (String alias : aliases) {
for (Command command : commands) {
List<String> aliasesCommand = new ArrayList<>();
for (String registeredAlias : command.getAliases()) aliasesCommand.add(registeredAlias.toLowerCase());
if (aliasesCommand.contains(alias.toLowerCase())) return command;
}
for (Command cmd : commandInstances.values()) {
if (cmd.getName().equalsIgnoreCase(name)) return cmd;
}
return null;
}
public Command getCommandByAlias(String alias) {
for (Command command : commands) {
List<String> aliasesCommand = new ArrayList<>();
for (String registeredAlias : command.getAliases()) aliasesCommand.add(registeredAlias.toLowerCase());
if (aliasesCommand.contains(alias.toLowerCase())) return command;
for (Command cmd : commandInstances.values()) {
for (String a : cmd.getAliases()) {
if (a.equalsIgnoreCase(alias)) return cmd;
}
}
return null;
}
public Command getCommand(String input) {
if (getCommandByName(input) != null) return getCommandByName(input);
if (getCommandByAlias(input) != null) return getCommandByAlias(input);
return null;
Command cmd = getCommandByName(input);
return (cmd != null) ? cmd : getCommandByAlias(input);
}
public void execute(CommandExecutor commandExecutor, String line) {
String[] split = line.split(" ");
String command = split[0];
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);
if (getCommand(command) != null) {
Command cmd = getCommand(command);
PreCommandExecuteEvent preEvent = new PreCommandExecuteEvent(this, commandExecutor, cmd);
eventManager.executeEvent(preEvent);
Command command = getCommand(commandLabel);
if (command == null) {
eventManager.executeEvent(new CommandNotFoundEvent(this, executor, commandLabel, args));
return;
}
if (preEvent.isCancelled()) return;
// Pre-execute event
PreCommandExecuteEvent preEvent = new PreCommandExecuteEvent(this, executor, command);
eventManager.executeEvent(preEvent);
if (preEvent.isCancelled()) return;
if (commandExecutor.hasPermissions(cmd.getPermissions())) {
CommandExecuteEvent event = new CommandExecuteEvent(this, commandExecutor, cmd);
eventManager.executeEvent(event);
// Check permissions
if (!executor.hasPermissions(command.getPermissions())) {
eventManager.executeEvent(new CommandExecutorMissingPermissionEvent(this, executor, command));
return;
}
if (event.isCancelled()) return;
// Execute event
CommandExecuteEvent execEvent = new CommandExecuteEvent(this, executor, command);
eventManager.executeEvent(execEvent);
if (execEvent.isCancelled()) return;
cmd.execute(commandExecutor, command, args);
eventManager.executeEvent(new CommandExecutedEvent(this, commandExecutor, cmd));
} else eventManager.executeEvent(new CommandExecutorMissingPermissionEvent(this, commandExecutor, cmd));
} else eventManager.executeEvent(new CommandNotFoundEvent(this, commandExecutor, command, args));
// Execute command
command.execute(executor, commandLabel, args);
// Post-execute event
eventManager.executeEvent(new CommandExecutedEvent(this, executor, command));
}
}