|
| 1 | +namespace Source2Framework.Services.Chat |
| 2 | +{ |
| 3 | + using CounterStrikeSharp.API.Core; |
| 4 | + using CounterStrikeSharp.API.Core.Plugin; |
| 5 | + using CounterStrikeSharp.API.Modules.Utils; |
| 6 | + using Microsoft.Extensions.Logging; |
| 7 | + |
| 8 | + using Source2Framework.Extensions; |
| 9 | + using Source2Framework.Models; |
| 10 | + using Source2Framework.Services.Command; |
| 11 | + |
| 12 | + public sealed partial class ChatService : SharedService<ChatService>, IChatService |
| 13 | + { |
| 14 | + public required ICommandService CommandService; |
| 15 | + |
| 16 | + public static string ChatPrefix { get; set; } = string.Empty; |
| 17 | + |
| 18 | + public ChatService(ILogger<ChatService> logger, IPluginContext pluginContext) : base(logger, pluginContext) |
| 19 | + { } |
| 20 | + |
| 21 | + public override void Initialize(bool hotReload) |
| 22 | + { |
| 23 | + this.Plugin.AddCommandListener("say", this.OnSay); |
| 24 | + this.Plugin.AddCommandListener("say_team", this.OnSayTeam); |
| 25 | + |
| 26 | + ChatPrefix = ((this.Plugin as ChatServicePlugin)!.Config.ChatPrefix); |
| 27 | + } |
| 28 | + |
| 29 | + public override void OnServiceInitialized(IService service) |
| 30 | + { |
| 31 | + if (service is ICommandService commandService) |
| 32 | + { |
| 33 | + this.CommandService = commandService; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public override void Shutdown(bool hotReload) |
| 38 | + { |
| 39 | + this.Plugin.RemoveCommandListener("say", this.OnSay, HookMode.Pre); |
| 40 | + this.Plugin.RemoveCommandListener("say_team", this.OnSayTeam, HookMode.Pre); |
| 41 | + } |
| 42 | + |
| 43 | + public void PrintToChat(CCSPlayerController player, string message) |
| 44 | + { |
| 45 | + player.CPrintToChat(message); |
| 46 | + } |
| 47 | + |
| 48 | + public void PrintToChatAll(string message, Func<CCSPlayerController, bool>? predicate = null) |
| 49 | + { |
| 50 | + SDK.CPrintToChatAll(message, predicate); |
| 51 | + } |
| 52 | + |
| 53 | + public void PrintToTeam(string message, CsTeam team) |
| 54 | + { |
| 55 | + PrintToChatAll(message, (player) => player.Team == team); |
| 56 | + } |
| 57 | + |
| 58 | + public string GetPrefix() |
| 59 | + { |
| 60 | + return ChatPrefix; |
| 61 | + } |
| 62 | + |
| 63 | + private bool ShouldIgnore(IEnumerable<string> triggers, string command) |
| 64 | + { |
| 65 | + if (command.Contains(' ')) |
| 66 | + { |
| 67 | + command = command.Split(' ').First(); |
| 68 | + } |
| 69 | + |
| 70 | + string? prefix = triggers.FirstOrDefault((t) => command.StartsWith(t)); |
| 71 | + |
| 72 | + if (prefix != null) |
| 73 | + { |
| 74 | + command = command.ReplaceFirst(prefix, command.StartsWith($"{prefix}css") ? string.Empty : "css_"); |
| 75 | + |
| 76 | + if (this.CommandService.IsCommandRegistered(command)) |
| 77 | + { |
| 78 | + return true; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments