-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCallAdminSystem.cs
More file actions
226 lines (192 loc) · 7.44 KB
/
CallAdminSystem.cs
File metadata and controls
226 lines (192 loc) · 7.44 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Capabilities;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using MenuManager;
using CallAdminSystem.Configs;
using CallAdminSystem.Commands;
using CallAdminSystem.Services;
using CallAdminSystem.Models;
namespace CallAdminSystem;
[MinimumApiVersion(363)]
public class CallAdminSystem : BasePlugin, IPluginConfig<BaseConfigs>
{
public override string ModuleAuthor => "luca.uy";
public override string ModuleVersion => "2.1.2";
public override string ModuleName => "CallAdminSystem";
public override string ModuleDescription => "Allows players to report users with Discord integration, MenuManager support and MySQL database";
public required BaseConfigs Config { get; set; }
// MenuManager capability
private IMenuApi? _menuApi;
private readonly PluginCapability<IMenuApi?> _menuCapability = new("menu:nfcore");
// Services
private CooldownService? _cooldownService;
private DiscordService? _discordService;
private DatabaseService? _databaseService;
private ReportService? _reportService;
// Commands
private ReportCommands? _reportCommands;
private ClaimCommands? _claimCommands;
// Data storage
private PersonTargetData?[] _selectedReason = new PersonTargetData?[65];
private string? _cachedIPandPort;
private string? _hostname;
public override void Load(bool hotReload)
{
InitializeServices();
InitializeCommands();
SetupListeners();
InitializeServerInfo();
CreateReasonsFileIfNotExists();
_ = InitializeDatabaseAsync();
}
public override void OnAllPluginsLoaded(bool hotReload)
{
_menuApi = _menuCapability.Get();
if (_menuApi == null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[CallAdminSystem] CRITICAL ERROR: MenuManager API not found!");
Console.WriteLine("[CallAdminSystem] MenuManager is a required dependency for this plugin to function.");
Console.WriteLine("[CallAdminSystem] Please install MenuManagerCS2 from: https://github.com/NickFox007/MenuManagerCS2");
Console.WriteLine("[CallAdminSystem] Plugin will now unload automatically.");
Console.ResetColor();
Server.NextFrame(() =>
{
try
{
Server.ExecuteCommand($"css_plugins unload {ModuleName}");
}
catch (Exception ex)
{
Console.WriteLine($"[CallAdminSystem] Error during auto-unload: {ex.Message}");
}
});
return;
}
else
{
if (_reportCommands != null)
{
_reportCommands.SetMenuApi(_menuApi);
}
}
}
public void OnConfigParsed(BaseConfigs config)
{
Config = config;
if (_discordService != null)
{
_discordService.UpdateConfig(config);
}
if (_cooldownService != null)
{
_cooldownService.UpdateConfig(config);
}
if (_reportService != null)
{
_reportService.UpdateConfig(config);
}
InitializeServerInfo();
_ = InitializeDatabaseAsync();
}
private async Task InitializeDatabaseAsync()
{
try
{
if (_databaseService != null && Config.Database.Enabled)
{
await _databaseService.InitializeDatabase();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[CallAdminSystem] Database initialized successfully!");
Console.ResetColor();
}
else if (_databaseService != null && !Config.Database.Enabled)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("[CallAdminSystem] Database is disabled in configuration");
Console.ResetColor();
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[CallAdminSystem] Failed to initialize database: {ex.Message}");
Console.ResetColor();
}
}
private void InitializeServices()
{
_cooldownService = new CooldownService(Config);
_discordService = new DiscordService(Config, Localizer);
_databaseService = new DatabaseService(Config.Database);
_reportService = new ReportService(Config, _discordService, _databaseService, Localizer);
}
private void InitializeCommands()
{
if (_cooldownService == null || _reportService == null) return;
_reportCommands = new ReportCommands(Config, _cooldownService, _reportService, _selectedReason, Localizer);
_claimCommands = new ClaimCommands(Config, _discordService!, () => _cachedIPandPort ?? "", () => _hostname ?? "", Localizer);
foreach (var command in Config.Commands.ReportCommands)
{
AddCommand(command, "Report a player", _reportCommands.HandleReportCommand);
}
foreach (var command in Config.Commands.ClaimCommands)
{
AddCommand(command, "Claim admin presence", _claimCommands.HandleClaimCommand);
}
}
private void SetupListeners()
{
RegisterListener<Listeners.OnClientConnected>(slot => _selectedReason[(int)slot + 1] = new PersonTargetData { Target = -1, IsSelectedReason = false });
RegisterListener<Listeners.OnClientDisconnectPost>(slot => _selectedReason[(int)slot + 1] = null);
AddCommandListener("say", OnPlayerSay);
AddCommandListener("say_team", OnPlayerSay);
}
private void InitializeServerInfo()
{
if (Config.Server.GetIPandPORTautomatic)
{
Server.NextWorldUpdate(() =>
{
string? ip = ConVar.Find("ip")?.StringValue;
string? port = ConVar.Find("hostport")?.GetPrimitiveValue<int>().ToString();
_cachedIPandPort = !string.IsNullOrEmpty(ip) && !string.IsNullOrEmpty(port) ? $"{ip}:{port}" : Config.Server.IPandPORT;
});
}
else
{
_cachedIPandPort = Config.Server.IPandPORT;
}
_hostname = Config.Server.UseHostname ? (ConVar.Find("hostname")?.StringValue ?? Localizer["NewReport"]) : Localizer["NewReport"];
}
private void CreateReasonsFileIfNotExists()
{
var reasonsFilePath = Path.Combine(ModuleDirectory, "reasons.txt");
if (!File.Exists(reasonsFilePath))
{
File.WriteAllText(reasonsFilePath, "Cheating\nToxic Behavior\nTeam Killing\nGriefing\n");
}
}
private HookResult OnPlayerSay(CCSPlayerController? player, CommandInfo commandinfo)
{
if (player == null || _reportCommands == null) return HookResult.Continue;
return _reportCommands.HandlePlayerSay(player, commandinfo);
}
public override void Unload(bool hotReload)
{
if (_menuApi != null)
{
foreach (var player in Utilities.GetPlayers().Where(p => p.IsValid))
{
_menuApi.CloseMenu(player);
}
}
_menuApi = null;
_cooldownService?.Dispose();
_reportService?.Dispose();
_discordService?.Dispose();
}
}