Skip to content

Commit c935e80

Browse files
committed
Adding Dependancy Injection
1 parent 2c8bcef commit c935e80

25 files changed

+537
-704
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
using CounterStrikeSharp.API;
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Core.Attributes.Registration;
5+
using CounterStrikeSharp.API.Core.Plugin;
6+
using CustomCommands.Services;
7+
8+
namespace CustomCommands.Controller;
9+
public class EventManager : IEventManager
10+
{
11+
private readonly IPluginGlobals PluginGlobals;
12+
private readonly CustomCommands PluginContext;
13+
private readonly IReplaceTagsFunctions ReplaceTagsFunctions;
14+
15+
public EventManager(IPluginGlobals PluginGlobals, IPluginContext PluginContext, IReplaceTagsFunctions ReplaceTagsFunctions)
16+
{
17+
this.PluginGlobals = PluginGlobals;
18+
this.PluginContext = ((PluginContext as PluginContext)!.Plugin as CustomCommands)!;
19+
this.ReplaceTagsFunctions = ReplaceTagsFunctions;
20+
}
21+
22+
[GameEventHandler]
23+
public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo _)
24+
{
25+
PluginGlobals.centerClientOn.RemoveAll(p => p.ClientId == @event.Userid.UserId);
26+
27+
return HookResult.Continue;
28+
}
29+
30+
public void RegisterListeners()
31+
{
32+
PluginContext.RegisterListener<Listeners.OnTick>(() =>
33+
{
34+
// Client Print To Center
35+
if(PluginGlobals.centerClientOn != null && PluginGlobals.centerClientOn.Count !> 0 )
36+
{
37+
foreach (var player in PluginGlobals.centerClientOn)
38+
{
39+
var targetPlayer = Utilities.GetPlayerFromUserid(player.ClientId);
40+
if (player != null && targetPlayer != null)
41+
{
42+
targetPlayer.PrintToCenterHtml(player.Message, 1);
43+
}
44+
}
45+
}
46+
47+
48+
// Server Print To Center
49+
if (PluginGlobals.centerServerOn.IsRunning)
50+
{
51+
Utilities.GetPlayers().ForEach(controller =>
52+
{
53+
if (controller == null || !controller.IsValid) return;
54+
55+
string message = ReplaceTagsFunctions.ReplaceMessageTags(PluginGlobals.centerServerOn.Message, controller);
56+
controller.PrintToCenterHtml(message, 1);
57+
});
58+
}
59+
});
60+
}
61+
}

CustomCommands/Controller/GeneralFunctions.cs

Lines changed: 0 additions & 196 deletions
This file was deleted.

CustomCommands/Controller/LoadJson.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Text.Json;
2+
using CustomCommands.Model;
3+
using CustomCommands.Services;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace CustomCommands.Controller;
7+
8+
public class LoadJson : ILoadJson
9+
{
10+
private readonly ILogger<CustomCommands> Logger;
11+
public LoadJson(ILogger<CustomCommands> Logger)
12+
{
13+
this.Logger = Logger;
14+
}
15+
16+
/// <summary>
17+
/// Load the commands from the JSON file
18+
/// </summary>
19+
/// <returns>Returns the Commands as a List</returns>
20+
public List<Commands>? LoadCommandsFromJson(string path)
21+
{
22+
string jsonPath = Path.Combine(path, "Commands.json");
23+
if (File.Exists(jsonPath))
24+
{
25+
var json = File.ReadAllText(jsonPath);
26+
return JsonSerializer.Deserialize<List<Commands>>(json);
27+
}
28+
else
29+
{
30+
Logger.LogWarning("No Config file found. Please create one");
31+
return null;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)