mirror of
https://git.battle-of-pip.de/root/vpr-mitarbeiterverwaltung.git
synced 2025-12-13 22:31:38 +01:00
Initial Commit
This commit is contained in:
44
Server/CommandManager.cs
Normal file
44
Server/CommandManager.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Reflection;
|
||||
using Server.Commands;
|
||||
|
||||
namespace Server;
|
||||
|
||||
public class CommandManager
|
||||
{
|
||||
private readonly Dictionary<string, ICommand> _commands = new();
|
||||
|
||||
public CommandManager()
|
||||
{
|
||||
LoadCommands();
|
||||
}
|
||||
|
||||
private void LoadCommands()
|
||||
{
|
||||
// Get all types implementing ICommand
|
||||
var commandTypes = Assembly.GetExecutingAssembly()
|
||||
.GetTypes()
|
||||
.Where(t => typeof(ICommand).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
|
||||
|
||||
foreach (var type in commandTypes)
|
||||
{
|
||||
// Create an instance of the command
|
||||
if (Activator.CreateInstance(type) is ICommand command)
|
||||
{
|
||||
// Use the Executor property as the key
|
||||
_commands[command.Executor] = command;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ExecuteCommand(string executor, params string[] args)
|
||||
{
|
||||
if (_commands.TryGetValue(executor, out var command))
|
||||
{
|
||||
command.Exec(args);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Command '{executor}' not found.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user