From 95f73781e9d082d4d97e0268524187140c60ecb5 Mon Sep 17 00:00:00 2001 From: abnerfs Date: Tue, 21 Nov 2023 22:17:32 -0300 Subject: [PATCH] Add optional admin restriction and extra configs --- Config.cs | 20 +++++++++++++ README.md | 14 +++++++++ ShowDamage.cs | 76 +++++++++++++++++++++++++++++------------------ ShowDamage.csproj | 1 + 4 files changed, 82 insertions(+), 29 deletions(-) create mode 100644 Config.cs diff --git a/Config.cs b/Config.cs new file mode 100644 index 0000000..613907d --- /dev/null +++ b/Config.cs @@ -0,0 +1,20 @@ +using CounterStrikeSharp.API.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShowDamage +{ + public class Config : IBasePluginConfig + { + public string AdminGroup { get; set; } = ""; + + public bool HideDamage { get; set; } = false; + + public bool ShowArmorDmg { get; set; } = true; + + public int Version { get; set; } = 1; + } +} diff --git a/README.md b/README.md index 714bd19..e24164d 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,17 @@ Simple showdamage plugin for CS2 - Download the latest release from https://github.com/abnerfs/cs2-showdamage/releases - Extract the .zip file into `addons/counterstrikesharp/plugins` - Enjoy + +# Config +- A config file will be created in `addons/counterstrikesharp/configs/plugins/ShowDamage` the first time you load the plugin. +- Changes in the config file will require you to reload the plugin or restart the server (change the map won't work). + +```json +// This configuration was automatically generated by CounterStrikeSharp for plugin 'ShowDamage', at 2023/11/21 10:06:01 +{ + "AdminGroup": "", // In case you want restrict the plugin functionality to specific admin groups, see https://docs.cssharp.dev/admin-framework/defining-groups/ + "HideDamage": false, // Enable this in case you want the player to know it hits but not to know the amount of damage + "ShowArmorDmg": true, // Whether or not armor damage should be shown + "Version": 1 // Don't change this +} +``` diff --git a/ShowDamage.cs b/ShowDamage.cs index 2ba239c..c27bd2f 100644 --- a/ShowDamage.cs +++ b/ShowDamage.cs @@ -1,9 +1,11 @@ using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes.Registration; +using CounterStrikeSharp.API.Modules.Admin; using CounterStrikeSharp.API.Modules.Cvars; using CounterStrikeSharp.API.Modules.Entities; using CounterStrikeSharp.API.Modules.Utils; +using System.Numerics; using System.Text; namespace ShowDamage @@ -14,14 +16,14 @@ class DamageDone public float Armor { get; set; } } - public class ShowDamage : BasePlugin + public class ShowDamage : BasePlugin, IPluginConfig { public override string ModuleName => "AbNeR ShowDamage"; - public override string ModuleVersion => "1.0.1"; + public override string ModuleVersion => "1.1.0"; public override string ModuleAuthor => "AbNeR_CSS"; - + public override string ModuleDescription => "Shows damage dealt to enemies in the center text"; Dictionary damageDone = new(); @@ -39,13 +41,42 @@ public bool FFAEnabled } } + public required Config Config { get; set; } + + public void OnConfigParsed(Config config) + { + Config = config; + } + public override void Load(bool hotReload) { Console.WriteLine("Showdamage loaded"); - ffaEnabledConVar = ConVar.Find("mp_teammates_are_enemies"); } + Action BuildCallback(int attackerUserId) => + () => + { + if (damageDone.ContainsKey(attackerUserId)) + { + var player = Utilities.GetPlayerFromUserid(attackerUserId); + if (player is not null && player.IsValid) + { + var dmg = damageDone[attackerUserId]; + if (dmg is not null) + { + StringBuilder builder = new(); + builder.Append($"-{dmg.Health} HP"); + if (dmg.Armor > 0 && Config.ShowArmorDmg) + builder.Append($"\n-{dmg.Armor} Armor"); + + player.PrintToCenter(builder.ToString()); + } + } + damageDone.Remove(attackerUserId); + } + }; + [GameEventHandler] public HookResult EventPlayerHurt(EventPlayerHurt ev, GameEventInfo info) { @@ -56,10 +87,19 @@ ev.Userid is null || return HookResult.Continue; int attackerUserId = ev.Attacker.UserId!.Value; + if (!string.IsNullOrEmpty(Config.AdminGroup) && !AdminManager.PlayerInGroup(ev.Attacker, Config.AdminGroup)) + return HookResult.Continue; + + if (Config.HideDamage) + { + ev.Attacker.PrintToCenter("*"); + return HookResult.Continue; + } + if (damageDone.ContainsKey(attackerUserId)) { - var dmg = damageDone[attackerUserId]; - if(dmg is not null) + DamageDone? dmg = damageDone[attackerUserId]; + if (dmg is not null) { dmg.Health += ev.DmgHealth; dmg.Armor += ev.DmgArmor; @@ -68,29 +108,7 @@ ev.Userid is null || else { damageDone.Add(attackerUserId, new DamageDone { Armor = ev.DmgArmor, Health = ev.DmgHealth }); - Action callback = () => - { - if (damageDone.ContainsKey(attackerUserId)) - { - var player = Utilities.GetPlayerFromUserid(attackerUserId); - if (player is not null && player.IsValid) - { - var dmg = damageDone[attackerUserId]; - if(dmg is not null) - { - StringBuilder builder = new(); - builder.Append($"-{dmg.Health} HP"); - if (dmg.Armor > 0) - builder.Append($"\n-{dmg.Armor} Armor"); - - player.PrintToCenter(builder.ToString()); - } - } - damageDone.Remove(attackerUserId); - } - }; - - NativeAPI.CreateTimer(0.1F, callback, 0); + AddTimer(0.1F, BuildCallback(attackerUserId), 0); } return HookResult.Continue; } diff --git a/ShowDamage.csproj b/ShowDamage.csproj index f3748dc..ddd22d4 100644 --- a/ShowDamage.csproj +++ b/ShowDamage.csproj @@ -9,6 +9,7 @@ ..\..\..\..\..\CSSHARP\CounterStrikeSharp.API.dll + False