From 158bc916cb3e585255df1360373a5ac0241a3254 Mon Sep 17 00:00:00 2001 From: Gold KingZ <48490385+oqyh@users.noreply.github.com> Date: Wed, 1 Jan 2025 18:54:18 +0400 Subject: [PATCH] 1.0.0 --- Config/Configs.cs | 103 ++++++++++++++++++++++ Config/Helper.cs | 174 +++++++++++++++++++++++++++++++++++++ HealthBar-GoldKingZ.cs | 69 +++++++++++++++ HealthBar-GoldKingZ.csproj | 21 +++++ 4 files changed, 367 insertions(+) create mode 100644 Config/Configs.cs create mode 100644 Config/Helper.cs create mode 100644 HealthBar-GoldKingZ.cs create mode 100644 HealthBar-GoldKingZ.csproj diff --git a/Config/Configs.cs b/Config/Configs.cs new file mode 100644 index 0000000..29ad853 --- /dev/null +++ b/Config/Configs.cs @@ -0,0 +1,103 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace HealthBar_GoldKingZ.Config +{ + public static class Configs + { + public static class Shared { + public static string? CookiesModule { get; set; } + } + + private static readonly string ConfigDirectoryName = "config"; + private static readonly string ConfigFileName = "config.json"; + private static string? _configFilePath; + private static ConfigData? _configData; + + private static readonly JsonSerializerOptions SerializationOptions = new() + { + Converters = + { + new JsonStringEnumConverter() + }, + WriteIndented = true, + AllowTrailingCommas = true, + ReadCommentHandling = JsonCommentHandling.Skip, + }; + + public static bool IsLoaded() + { + return _configData is not null; + } + + public static ConfigData GetConfigData() + { + if (_configData is null) + { + throw new Exception("Config not yet loaded."); + } + + return _configData; + } + + public static ConfigData Load(string modulePath) + { + var configFileDirectory = Path.Combine(modulePath, ConfigDirectoryName); + if(!Directory.Exists(configFileDirectory)) + { + Directory.CreateDirectory(configFileDirectory); + } + + _configFilePath = Path.Combine(configFileDirectory, ConfigFileName); + if (File.Exists(_configFilePath)) + { + _configData = JsonSerializer.Deserialize(File.ReadAllText(_configFilePath), SerializationOptions); + } + else + { + _configData = new ConfigData(); + } + + if (_configData is null) + { + throw new Exception("Failed to load configs."); + } + + SaveConfigData(_configData); + + return _configData; + } + + private static void SaveConfigData(ConfigData configData) + { + if (_configFilePath is null) + { + throw new Exception("Config not yet loaded."); + } + string json = JsonSerializer.Serialize(configData, SerializationOptions); + + + json = System.Text.RegularExpressions.Regex.Unescape(json); + File.WriteAllText(_configFilePath, json, System.Text.Encoding.UTF8); + } + + public class ConfigData + { + public bool DisableOnWarmUp { get; set; } + public int DisplayHealthBarStyle { get; set; } + public bool ShowHealthBarToAll { get; set; } + + public string empty { get; set; } + public string Information_For_You_Dont_Delete_it { get; set; } + + public ConfigData() + { + DisableOnWarmUp = false; + DisplayHealthBarStyle = 0; + ShowHealthBarToAll = false; + empty = "----------------------------[ ↓ Info For All Configs Above ↓ ]----------------------------"; + Information_For_You_Dont_Delete_it = " Vist [https://github.com/oqyh/cs2-HealthBar-GoldKingZ/tree/main?tab=readme-ov-file#-configuration-] To Understand All Above"; + } + } + } +} \ No newline at end of file diff --git a/Config/Helper.cs b/Config/Helper.cs new file mode 100644 index 0000000..e669e02 --- /dev/null +++ b/Config/Helper.cs @@ -0,0 +1,174 @@ +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Modules.Admin; +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Modules.Utils; +using System.Text.RegularExpressions; +using System.Text.Json; +using HealthBar_GoldKingZ.Config; +using System.Text.Encodings.Web; + +namespace HealthBar_GoldKingZ; + +public class Helper +{ + public static void AdvancedPlayerPrintToChat(CCSPlayerController player, string message, params object[] args) + { + if (string.IsNullOrEmpty(message))return; + + for (int i = 0; i < args.Length; i++) + { + message = message.Replace($"{{{i}}}", args[i].ToString()); + } + if (Regex.IsMatch(message, "{nextline}", RegexOptions.IgnoreCase)) + { + string[] parts = Regex.Split(message, "{nextline}", RegexOptions.IgnoreCase); + foreach (string part in parts) + { + string messages = part.Trim(); + player.PrintToChat(" " + messages); + } + }else + { + player.PrintToChat(message); + } + } + public static void AdvancedServerPrintToChatAll(string message, params object[] args) + { + if (string.IsNullOrEmpty(message))return; + + for (int i = 0; i < args.Length; i++) + { + message = message.Replace($"{{{i}}}", args[i].ToString()); + } + if (Regex.IsMatch(message, "{nextline}", RegexOptions.IgnoreCase)) + { + string[] parts = Regex.Split(message, "{nextline}", RegexOptions.IgnoreCase); + foreach (string part in parts) + { + string messages = part.Trim(); + Server.PrintToChatAll(" " + messages); + } + }else + { + Server.PrintToChatAll(message); + } + } + public static void AdvancedPlayerPrintToConsole(CCSPlayerController player, string message, params object[] args) + { + if (string.IsNullOrEmpty(message))return; + + for (int i = 0; i < args.Length; i++) + { + message = message.Replace($"{{{i}}}", args[i].ToString()); + } + if (Regex.IsMatch(message, "{nextline}", RegexOptions.IgnoreCase)) + { + string[] parts = Regex.Split(message, "{nextline}", RegexOptions.IgnoreCase); + foreach (string part in parts) + { + string messages = part.Trim(); + player.PrintToConsole(" " + messages); + } + }else + { + player.PrintToConsole(message); + } + } + + public static bool IsPlayerInGroupPermission(CCSPlayerController player, string groups) + { + var excludedGroups = groups.Split(','); + foreach (var group in excludedGroups) + { + if(group.StartsWith("#")) + { + if (AdminManager.PlayerInGroup(player, group)) + { + return true; + } + + }else if(group.StartsWith("@")) + { + if (AdminManager.PlayerHasPermissions(player, group)) + { + return true; + } + }else + { + if (AdminManager.PlayerInGroup(player, group)) + { + return true; + } + } + } + return false; + } + public static List GetPlayersController(bool IncludeBots = false, bool IncludeSPEC = true, bool IncludeCT = true, bool IncludeT = true) + { + var playerList = Utilities + .FindAllEntitiesByDesignerName("cs_player_controller") + .Where(p => p != null && p.IsValid && + (IncludeBots || (!p.IsBot && !p.IsHLTV)) && + p.Connected == PlayerConnectedState.PlayerConnected && + ((IncludeCT && p.TeamNum == (byte)CsTeam.CounterTerrorist) || + (IncludeT && p.TeamNum == (byte)CsTeam.Terrorist) || + (IncludeSPEC && p.TeamNum == (byte)CsTeam.Spectator))) + .ToList(); + + return playerList; + } + public static int GetPlayersCount(bool IncludeBots = false, bool IncludeSPEC = true, bool IncludeCT = true, bool IncludeT = true) + { + return Utilities.GetPlayers().Count(p => + p != null && + p.IsValid && + p.Connected == PlayerConnectedState.PlayerConnected && + (IncludeBots || (!p.IsBot && !p.IsHLTV)) && + ((IncludeCT && p.TeamNum == (byte)CsTeam.CounterTerrorist) || + (IncludeT && p.TeamNum == (byte)CsTeam.Terrorist) || + (IncludeSPEC && p.TeamNum == (byte)CsTeam.Spectator)) + ); + } + + public static void ClearVariables() + { + + } + + public static string ReplaceMessages(string Message, string date, string time, string PlayerName, string SteamId, string ipAddress, string reason) + { + var replacedMessage = Message + .Replace("{TIME}", time) + .Replace("{DATE}", date) + .Replace("{PLAYERNAME}", PlayerName.ToString()) + .Replace("{STEAMID}", SteamId.ToString()) + .Replace("{IP}", ipAddress.ToString()) + .Replace("{REASON}", reason); + return replacedMessage; + } + public static string RemoveLeadingSpaces(string content) + { + string[] lines = content.Split('\n'); + for (int i = 0; i < lines.Length; i++) + { + lines[i] = lines[i].TrimStart(); + } + return string.Join("\n", lines); + } + public static CCSGameRules? GetGameRules() + { + try + { + var gameRulesEntities = Utilities.FindAllEntitiesByDesignerName("cs_gamerules"); + return gameRulesEntities.First().GameRules; + } + catch + { + return null; + } + } + public static bool IsWarmup() + { + return GetGameRules()?.WarmupPeriod ?? false; + } +} \ No newline at end of file diff --git a/HealthBar-GoldKingZ.cs b/HealthBar-GoldKingZ.cs new file mode 100644 index 0000000..c26e964 --- /dev/null +++ b/HealthBar-GoldKingZ.cs @@ -0,0 +1,69 @@ +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Modules.Commands; +using System.Text.Json.Serialization; +using Microsoft.Extensions.Localization; +using CounterStrikeSharp.API.Modules.Admin; +using CounterStrikeSharp.API.Core.Attributes.Registration; +using CounterStrikeSharp.API.Core.Attributes; +using HealthBar_GoldKingZ.Config; +using CounterStrikeSharp.API.Modules.UserMessages; +using CounterStrikeSharp.API.Modules.Entities; + +namespace HealthBar_GoldKingZ; + + +public class HealthBarGoldKingZ : BasePlugin +{ + public override string ModuleName => "Show HealthBar To Attacker Or All Players"; + public override string ModuleVersion => "1.0.0"; + public override string ModuleAuthor => "Gold KingZ"; + public override string ModuleDescription => "https://github.com/oqyh"; + public static HealthBarGoldKingZ Instance { get; set; } = new(); + + public override void Load(bool hotReload) + { + Instance = this; + Configs.Load(ModuleDirectory); + Configs.Shared.CookiesModule = ModuleDirectory; + + RegisterEventHandler(OnEventPlayerHurt); + } + + public HookResult OnEventPlayerHurt(EventPlayerHurt @event, GameEventInfo info) + { + if (@event == null || Configs.GetConfigData().DisableOnWarmUp && Helper.IsWarmup()) return HookResult.Continue; + + var victim = @event.Userid; + var dmgHealth = @event.DmgHealth; + var health = @event.Health; + + if (victim == null || !victim.IsValid) return HookResult.Continue; + var victimHealth = victim.PlayerPawn.Value!.MaxHealth; + + var attacker = @event.Attacker; + if (attacker == null || !attacker.IsValid) return HookResult.Continue; + + float oldHealth = health + dmgHealth; + if (oldHealth == health) return HookResult.Continue; + + float oldHealthRatio = oldHealth / victimHealth; + float newHealthRatio = (float)health / victimHealth; + + var message = UserMessage.FromPartialName("UpdateScreenHealthBar"); + message.SetInt("entidx", (int)victim.PlayerPawn.Index); + message.SetFloat("healthratio_old", oldHealthRatio); + message.SetFloat("healthratio_new", newHealthRatio); + message.SetInt("style", Configs.GetConfigData().DisplayHealthBarStyle); + if(Configs.GetConfigData().ShowHealthBarToAll) + { + message.Recipients.AddAllPlayers(); + message.Send(); + }else + { + message.Send(attacker); + } + + return HookResult.Continue; + } +} \ No newline at end of file diff --git a/HealthBar-GoldKingZ.csproj b/HealthBar-GoldKingZ.csproj new file mode 100644 index 0000000..bcabef2 --- /dev/null +++ b/HealthBar-GoldKingZ.csproj @@ -0,0 +1,21 @@ + + + net8.0 + enable + enable + false + false + bin\Release\addons\counterstrikesharp\plugins\HealthBar-GoldKingZ\ + false + Release + none + + + + + + + + + + \ No newline at end of file