forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminCommandHandler.cs
More file actions
72 lines (63 loc) · 2.2 KB
/
Copy pathAdminCommandHandler.cs
File metadata and controls
72 lines (63 loc) · 2.2 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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using L2dotNET.Attributes;
using L2dotNET.Commands;
using L2dotNET.Models.Player;
using L2dotNET.Utility;
using NLog;
namespace L2dotNET.Handlers
{
public class AdminCommandHandler : IAdminCommandHandler
{
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
private readonly SortedList<string, AAdminCommand> _commands = new SortedList<string, AAdminCommand>();
public bool Initialised { get; private set; }
private readonly IServiceProvider _serviceProvider;
public AdminCommandHandler(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public async Task Initialise()
{
if (Initialised)
{
return;
}
IEnumerable<Type> typelist = Utilz.GetTypesInNamespace(Assembly.GetExecutingAssembly(), "L2dotNET.Commands.Admin");
foreach (Type t in typelist)
Register(Activator.CreateInstance(t, _serviceProvider));
Log.Info($"Loaded {_commands.Count} commands.");
Initialised = true;
}
public void Request(L2Player admin, string alias)
{
string cmd = alias;
if (alias.Contains(" "))
cmd = alias.Split(' ')[0];
if (!_commands.ContainsKey(cmd))
{
admin.SendMessageAsync($"Command {cmd} not exists.");
admin.SendActionFailedAsync();
return;
}
AAdminCommand processor = _commands[cmd];
try
{
processor.UseAsync(admin, alias);
}
catch (Exception sss)
{
admin.SendMessageAsync("Probably syntax eror.");
Log.Error(sss);
}
}
public void Register(object processor)
{
CommandAttribute attribute =
(CommandAttribute) processor.GetType().GetCustomAttribute(typeof(CommandAttribute));
_commands.Add(attribute.CommandName,(AAdminCommand) processor);
}
}
}