Skip to content

Commit 2257ee4

Browse files
committed
fix: only register/remove listeners when dependency is available
1 parent f187de0 commit 2257ee4

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

Source2Framework.ChatService/Chat/ChatService.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ public ChatService(ILogger<ChatService> logger, IPluginContext pluginContext) :
2020

2121
public override void Initialize(bool hotReload)
2222
{
23-
this.Plugin.AddCommandListener("say", this.OnSay);
24-
this.Plugin.AddCommandListener("say_team", this.OnSayTeam);
25-
2623
ChatPrefix = ((this.Plugin as ChatServicePlugin)!.Config.ChatPrefix);
2724
}
2825

@@ -31,15 +28,34 @@ public override void OnServiceInitialized(IService service)
3128
if (service is ICommandService commandService)
3229
{
3330
this.CommandService = commandService;
31+
32+
this.Plugin.AddCommandListener("say", this.OnSay);
33+
this.Plugin.AddCommandListener("say_team", this.OnSayTeam);
3434
}
3535
}
3636

37-
public override void Shutdown(bool hotReload)
37+
private void RemoveListeners()
3838
{
3939
this.Plugin.RemoveCommandListener("say", this.OnSay, HookMode.Pre);
4040
this.Plugin.RemoveCommandListener("say_team", this.OnSayTeam, HookMode.Pre);
4141
}
4242

43+
public override void OnServiceShutdown(IService service)
44+
{
45+
if (service is ICommandService)
46+
{
47+
this.RemoveListeners();
48+
}
49+
}
50+
51+
public override void Shutdown(bool hotReload)
52+
{
53+
if (this.CommandService == null)
54+
return;
55+
56+
this.RemoveListeners();
57+
}
58+
4359
public void PrintToChat(CCSPlayerController player, string message)
4460
{
4561
player.CPrintToChat(message);

0 commit comments

Comments
 (0)