diff --git a/ZombieSharp/Models/GameConfigs.cs b/ZombieSharp/Models/GameConfigs.cs index 44a913e..1ba1d3e 100644 --- a/ZombieSharp/Models/GameConfigs.cs +++ b/ZombieSharp/Models/GameConfigs.cs @@ -13,6 +13,6 @@ public class GameConfigs // weapons section public bool WeaponPurchaseEnable { get; set; } = false; - public bool WeaponRestrictEnable { get; set; } = true; + public bool WeaponRestrictEnable { get; set; } = true; public bool WeaponBuyZoneOnly { get; set; } = false; } \ No newline at end of file diff --git a/ZombieSharp/Models/ZombiePlayer.cs b/ZombieSharp/Models/ZombiePlayer.cs index 3d421ad..7e8aca8 100644 --- a/ZombieSharp/Models/ZombiePlayer.cs +++ b/ZombieSharp/Models/ZombiePlayer.cs @@ -15,6 +15,6 @@ public ZombiePlayer(MotherZombieStatus status = MotherZombieStatus.NONE, bool zo Zombie = zombie; } - public MotherZombieStatus MotherZombie { get; set; } = MotherZombieStatus.NONE; + public MotherZombieStatus MotherZombie { get; set; } = MotherZombieStatus.NONE; public bool Zombie { get; set; } = false; } diff --git a/ZombieSharp/Plugin/Classes.cs b/ZombieSharp/Plugin/Classes.cs index df46958..a4a6e10 100644 --- a/ZombieSharp/Plugin/Classes.cs +++ b/ZombieSharp/Plugin/Classes.cs @@ -32,7 +32,7 @@ public void ClassesOnMapStart() var configPath = Path.Combine(ZombieSharp.ConfigPath, "playerclasses.jsonc"); - if(!File.Exists(configPath)) + if (!File.Exists(configPath)) { _logger.LogCritical("[ClassesOnMapStart] Couldn't find a playerclasses.jsonc file!"); return; @@ -44,13 +44,13 @@ public void ClassesOnMapStart() ClassesConfig = JsonConvert.DeserializeObject>(File.ReadAllText(configPath)); // settings is loaded before classes so we can get default human and zombie. - if(GameSettings.Settings != null) + if (GameSettings.Settings != null) { - if(!string.IsNullOrEmpty(GameSettings.Settings.DefaultHumanBuffer)) + if (!string.IsNullOrEmpty(GameSettings.Settings.DefaultHumanBuffer)) { var uniqueName = GameSettings.Settings.DefaultHumanBuffer; - if(!ClassesConfig!.ContainsKey(uniqueName)) + if (!ClassesConfig!.ContainsKey(uniqueName)) { _logger.LogCritical("[ClassesOnMapStart] Couldn't get classes \"{0}\" from playerclasses.jsonc", uniqueName); } @@ -60,11 +60,11 @@ public void ClassesOnMapStart() _logger.LogInformation("[ClassesOnMapStart] Classes {0} is default class for human", DefaultHuman.Name); } - if(!string.IsNullOrEmpty(GameSettings.Settings.DefaultZombieBuffer)) + if (!string.IsNullOrEmpty(GameSettings.Settings.DefaultZombieBuffer)) { var uniqueName = GameSettings.Settings.DefaultZombieBuffer; - if(!ClassesConfig!.ContainsKey(uniqueName)) + if (!ClassesConfig!.ContainsKey(uniqueName)) { _logger.LogCritical("[ClassesOnMapStart] Couldn't get classes \"{0}\" from playerclasses.jsonc", uniqueName); } @@ -74,11 +74,11 @@ public void ClassesOnMapStart() _logger.LogInformation("[ClassesOnMapStart] Classes {0} is default class for human", DefaultZombie.Name); } - if(!string.IsNullOrEmpty(GameSettings.Settings.MotherZombieBuffer)) + if (!string.IsNullOrEmpty(GameSettings.Settings.MotherZombieBuffer)) { var uniqueName = GameSettings.Settings.MotherZombieBuffer; - if(!ClassesConfig!.ContainsKey(uniqueName)) + if (!ClassesConfig!.ContainsKey(uniqueName)) { _logger.LogCritical("[ClassesOnMapStart] Couldn't get classes \"{0}\" from playerclasses.jsonc", uniqueName); } @@ -88,31 +88,31 @@ public void ClassesOnMapStart() _logger.LogInformation("[ClassesOnMapStart] Classes {0} is mother zombie classes.", MotherZombie.Name); } - if(DefaultHuman == null) + if (DefaultHuman == null) { _logger.LogCritical("[ClassesOnMapStart] Human class from GameSettings is empty or null!"); } - if(DefaultZombie == null) + if (DefaultZombie == null) { _logger.LogCritical("[ClassesOnMapStart] Zombie class from GameSettings is empty or null!"); } - if(MotherZombie == null) + if (MotherZombie == null) { _logger.LogInformation("[ClassesOnMapStart] Mother Zombie class from GameSettings is empty or null, when player infect will use their selected zombie class instead."); } } - } + } public void ClassesOnClientPutInServer(CCSPlayerController client) { - if(DefaultHuman == null || DefaultZombie == null) + if (DefaultHuman == null || DefaultZombie == null) { _logger.LogError("[ClassesOnClientPutInServer] Default class is null!"); } - if(PlayerData.PlayerClassesData == null) + if (PlayerData.PlayerClassesData == null) { _logger.LogError("[ClassesOnClientPutInServer] PlayerClassesData is null!"); return; @@ -121,22 +121,22 @@ public void ClassesOnClientPutInServer(CCSPlayerController client) PlayerData.PlayerClassesData[client].HumanClass = DefaultHuman; PlayerData.PlayerClassesData[client].ZombieClass = DefaultZombie; - if(PlayerData.PlayerClassesData?[client].HumanClass == null) + if (PlayerData.PlayerClassesData?[client].HumanClass == null) _logger.LogInformation("[ClassesOnClientPutInServer] {0} is not null, but client got null anyway.", DefaultHuman?.Name); - if(PlayerData.PlayerClassesData?[client].ZombieClass == null) + if (PlayerData.PlayerClassesData?[client].ZombieClass == null) _logger.LogInformation("[ClassesOnClientPutInServer] {0} is not null, but client got null anyway.", DefaultZombie?.Name); } public void ClassesApplyToPlayer(CCSPlayerController client, ClassAttribute? data) { - if(client == null) + if (client == null) { _logger.LogError("[ClassesApplyToPlayer] Player is null!"); return; } - if(data == null) + if (data == null) { _logger.LogError("[ClassesApplyToPlayer] Class data is null!"); return; @@ -144,25 +144,25 @@ public void ClassesApplyToPlayer(CCSPlayerController client, ClassAttribute? dat var playerPawn = client.PlayerPawn.Value; - if(playerPawn == null) + if (playerPawn == null) { _logger.LogError("[ClassesApplyToPlayer] Player Pawn is null!"); return; } // if the model is not empty string and model path is not same as current model we have. - if(!string.IsNullOrEmpty(data.Model)) + if (!string.IsNullOrEmpty(data.Model)) { // set it. - if(data.Model != "default") + if (data.Model != "default") playerPawn.SetModel(data.Model); // change to cs2 default model blyat else { - if(data.Team == 0) + if (data.Team == 0) playerPawn.SetModel("characters/models/tm_phoenix/tm_phoenix.vmdl"); - + else playerPawn.SetModel("characters/models/ctm_sas/ctm_sas.vmdl"); } @@ -172,7 +172,7 @@ public void ClassesApplyToPlayer(CCSPlayerController client, ClassAttribute? dat playerPawn.Health = data.Health; // if zombie then remove their kevlar - if(data.Team == 0) + if (data.Team == 0) { playerPawn.ArmorValue = 0; client.PawnHasHelmet = false; @@ -187,23 +187,23 @@ public void ClassesApplyToPlayer(CCSPlayerController client, ClassAttribute? dat public void ClassesOnPlayerHurt(CCSPlayerController? client) { - _core?.AddTimer(0.3f, () => + _core?.AddTimer(0.3f, () => { // need to be alive - if(client == null || !client.PawnIsAlive) + if (client == null || !client.PawnIsAlive) return; // prevent error obviously. - if(!PlayerData.PlayerClassesData!.ContainsKey(client)) + if (!PlayerData.PlayerClassesData!.ContainsKey(client)) { return; } - if(PlayerData.PlayerClassesData?[client].ActiveClass == null) + if (PlayerData.PlayerClassesData?[client].ActiveClass == null) return; // if speed is equal to 250 then we don't need this. - if(PlayerData.PlayerClassesData?[client].ActiveClass?.Speed == 250f) + if (PlayerData.PlayerClassesData?[client].ActiveClass?.Speed == 250f) return; // set speed back to velomodify diff --git a/ZombieSharp/Plugin/Events.cs b/ZombieSharp/Plugin/Events.cs index b5ccf8b..22124cb 100644 --- a/ZombieSharp/Plugin/Events.cs +++ b/ZombieSharp/Plugin/Events.cs @@ -1,193 +1,187 @@ -using System.Xml.Schema; -using CounterStrikeSharp.API; -using CounterStrikeSharp.API.Core; -using CounterStrikeSharp.API.Modules.Utils; -using Microsoft.Extensions.Logging; -using ZombieSharp.Models; -using static CounterStrikeSharp.API.Core.Listeners; - -namespace ZombieSharp.Plugin; - -public class Events -{ - private readonly ZombieSharp _core; - private readonly Infect _infect; - private readonly Classes _classes; - private readonly GameSettings _settings; - private readonly Weapons _weapons; - private readonly ILogger _logger; - - public Events(ZombieSharp core, Infect infect, GameSettings settings, Classes classes, Weapons weapons, ILogger logger) - { - _core = core; - _infect = infect; - _settings = settings; - _classes = classes; - _weapons = weapons; - _logger = logger; - } - - public void EventOnLoad() - { - _core.RegisterListener(OnClientPutInServer); - _core.RegisterListener(OnClientDisconnect); - _core.RegisterListener(OnMapStart); - _core.RegisterListener(OnPrecahceResources); - - _core.RegisterEventHandler(OnPlayerHurt); - _core.RegisterEventHandler(OnPlayerSpawn); - _core.RegisterEventHandler(OnRoundFreezeEnd); - _core.RegisterEventHandler(OnRoundStart); - _core.RegisterEventHandler(OnPreRoundStart); - _core.RegisterEventHandler(OnPlayerDeath); - _core.RegisterEventHandler(OnWarmupEnd); - _core.RegisterEventHandler(OnRoundEnd); - } - - public void EventOnUnload() - { - _core.RemoveListener(OnClientPutInServer); - _core.RemoveListener(OnClientDisconnect); - _core.RemoveListener(OnMapStart); - _core.RemoveListener(OnPrecahceResources); - - _core.DeregisterEventHandler(OnPlayerHurt); - _core.DeregisterEventHandler(OnPlayerSpawn); - _core.DeregisterEventHandler(OnRoundFreezeEnd); - _core.DeregisterEventHandler(OnRoundStart); - _core.DeregisterEventHandler(OnPreRoundStart); - _core.DeregisterEventHandler(OnPlayerDeath); - _core.DeregisterEventHandler(OnWarmupEnd); - _core.DeregisterEventHandler(OnRoundEnd); - } - - public void OnClientPutInServer(int playerslot) - { - var client = Utilities.GetPlayerFromSlot(playerslot); - - if(client == null) - return; - - PlayerData.ZombiePlayerData?.Add(client, new()); - PlayerData.PlayerClassesData?.Add(client, new()); - - _classes?.ClassesOnClientPutInServer(client); - } - - public void OnClientDisconnect(int playerslot) - { - var client = Utilities.GetPlayerFromSlot(playerslot); - - if(client == null) - return; - - PlayerData.ZombiePlayerData?.Remove(client); - PlayerData.PlayerClassesData?.Remove(client); - } - - // if reload a plugin this part won't be executed until you change map; - public void OnMapStart(string mapname) - { - _settings.GameSettingsOnMapStart(); - _classes.ClassesOnMapStart(); - _weapons.WeaponsOnMapStart(); - Server.ExecuteCommand("sv_predictable_damage_tag_ticks 0"); - } - - public void OnPrecahceResources(ResourceManifest manifest) - { - if(Classes.ClassesConfig == null) - { - _logger.LogCritical("[OnPrecahceResources] The player classes config is null or not loaded yet!"); - return; - } - - foreach(var classes in Classes.ClassesConfig.Values) - { - if(!string.IsNullOrEmpty(classes.Model)) - { - if(classes.Model != "default") - manifest.AddResource(classes.Model!); - } - } - } - - public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info) - { - var client = @event.Userid; - var attacker = @event.Attacker; - var weapon = @event.Weapon; - var dmgHealth = @event.DmgHealth; - - _infect.InfectOnPlayerHurt(client, attacker); - Knockback.KnockbackClient(client, attacker, weapon, dmgHealth); - _classes.ClassesOnPlayerHurt(client); - - return HookResult.Continue; - } - - public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info) - { - // check the player count if there is any team that all dead. - Utils.CheckGameStatus(); - - // play sound for zombie when killed by human. - var client = @event.Userid; - var attacker = @event.Attacker; - - if(client == null || attacker == null) - return HookResult.Continue; - - if(Infect.IsClientHuman(attacker) && Infect.IsClientInfect(client)) - Utils.EmitSound(client, "zr.amb.zombie_die"); - - return HookResult.Continue; - } - - public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info) - { - var client = @event.Userid; - - if(client == null) - return HookResult.Continue; - - if(Infect.InfectHasStarted()) - _infect.InfectClient(client); - - else - _infect.HumanizeClient(client); - - return HookResult.Continue; - } - - public HookResult OnPreRoundStart(EventCsPreRestart @event, GameEventInfo info) - { - _infect.InfectOnPreRoundStart(); - return HookResult.Continue; - } - - public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info) - { - Server.PrintToChatAll($" {_core.Localizer["Prefix"]} {_core.Localizer["Infect.GameInfo"]}"); - return HookResult.Continue; - } - - public HookResult OnWarmupEnd(EventWarmupEnd @event, GameEventInfo info) - { - Infect.InfectStarted = false; - _infect.InfectKillInfectionTimer(); - return HookResult.Continue; - } - - public HookResult OnRoundFreezeEnd(EventRoundFreezeEnd @event, GameEventInfo info) - { - _infect.InfectOnRoundFreezeEnd(); - return HookResult.Continue; - } - - public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) - { - Infect.InfectStarted = false; - return HookResult.Continue; - } -} \ No newline at end of file +//using CounterStrikeSharp.API; +//using CounterStrikeSharp.API.Core; +//using CounterStrikeSharp.API.Modules.Utils; +//using Microsoft.Extensions.Logging; +//using ZombieSharp.Models; +//using ZombieSharp.Plugin.Listeners; + +//namespace ZombieSharp.Plugin; + +//public class Events123(ZombieSharp core, Infect infect, GameSettings settings, Classes classes, Weapons weapons, ILogger logger) +//{ +// private readonly ZombieSharp _core = core; +// private readonly Infect _infect = infect; +// private readonly Classes _classes = classes; +// private readonly GameSettings _settings = settings; +// private readonly Weapons _weapons = weapons; +// private readonly ILogger _logger = logger; + +// public void EventOnLoad() +// { +// _core.RegisterAllListeners(); +// //_core.RegisterAllEventHandlers(); + +// //_core.RegisterListener(OnClientPutInServer); +// //_core.RegisterListener(OnClientDisconnect); +// //_core.RegisterListener(OnMapStart); +// //_core.RegisterListener(OnPrecahceResources); + +// //_core.RegisterEventHandler(OnPlayerHurt); +// //_core.RegisterEventHandler(OnPlayerSpawn); +// //_core.RegisterEventHandler(OnRoundFreezeEnd); +// //_core.RegisterEventHandler(OnRoundStart); +// //_core.RegisterEventHandler(OnPreRoundStart); +// //_core.RegisterEventHandler(OnPlayerDeath); +// //_core.RegisterEventHandler(OnWarmupEnd); +// //_core.RegisterEventHandler(OnRoundEnd); +// } + +// public void EventOnUnload() +// { +// _core.RemoveAllListeners(); + +// //_core.RemoveListener(OnClientPutInServer); +// //_core.RemoveListener(OnClientDisconnect); +// //_core.RemoveListener(OnMapStart); +// //_core.RemoveListener(OnPrecahceResources); + +// //_core.DeregisterEventHandler(OnPlayerHurt); +// //_core.DeregisterEventHandler(OnPlayerSpawn); +// //_core.DeregisterEventHandler(OnRoundFreezeEnd); +// //_core.DeregisterEventHandler(OnRoundStart); +// //_core.DeregisterEventHandler(OnPreRoundStart); +// //_core.DeregisterEventHandler(OnPlayerDeath); +// //_core.DeregisterEventHandler(OnWarmupEnd); +// //_core.DeregisterEventHandler(OnRoundEnd); +// } + +// public void OnClientPutInServer(int playerslot) +// { +// var client = Utilities.GetPlayerFromSlot(playerslot); + +// if (client == null) +// return; + +// PlayerData.ZombiePlayerData?.Add(client, new()); +// PlayerData.PlayerClassesData?.Add(client, new()); + +// _classes?.ClassesOnClientPutInServer(client); +// } + +// public void OnClientDisconnect(int playerslot) +// { +// var client = Utilities.GetPlayerFromSlot(playerslot); + +// if (client == null) +// return; + +// PlayerData.ZombiePlayerData?.Remove(client); +// PlayerData.PlayerClassesData?.Remove(client); +// } + +// // if reload a plugin this part won't be executed until you change map; +// public void OnMapStart(string mapname) +// { +// _settings.GameSettingsOnMapStart(); +// _classes.ClassesOnMapStart(); +// _weapons.WeaponsOnMapStart(); +// Server.ExecuteCommand("sv_predictable_damage_tag_ticks 0"); +// } + +// public void OnPrecahceResources(ResourceManifest manifest) +// { +// if (Classes.ClassesConfig == null) +// { +// _logger.LogCritical("[OnPrecahceResources] The player classes config is null or not loaded yet!"); +// return; +// } + +// foreach (var classes in Classes.ClassesConfig.Values) +// { +// if (!string.IsNullOrEmpty(classes.Model)) +// { +// if (classes.Model != "default") +// manifest.AddResource(classes.Model!); +// } +// } +// } + +// public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info) +// { +// var client = @event.Userid; +// var attacker = @event.Attacker; +// var weapon = @event.Weapon; +// var dmgHealth = @event.DmgHealth; + +// _infect.InfectOnPlayerHurt(client, attacker); +// Knockback.KnockbackClient(client, attacker, weapon, dmgHealth); +// _classes.ClassesOnPlayerHurt(client); + +// return HookResult.Continue; +// } + +// public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info) +// { +// // check the player count if there is any team that all dead. +// Utils.CheckGameStatus(); + +// // play sound for zombie when killed by human. +// var client = @event.Userid; +// var attacker = @event.Attacker; + +// if (client == null || attacker == null) +// return HookResult.Continue; + +// if (Infect.IsClientHuman(attacker) && Infect.IsClientInfect(client)) +// Utils.EmitSound(client, "zr.amb.zombie_die"); + +// return HookResult.Continue; +// } + +// public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info) +// { +// var client = @event.Userid; + +// if (client == null) +// return HookResult.Continue; + +// if (Infect.InfectHasStarted()) +// _infect.InfectClient(client); + +// else +// _infect.HumanizeClient(client); + +// return HookResult.Continue; +// } + +// public HookResult OnPreRoundStart(EventCsPreRestart @event, GameEventInfo info) +// { +// _infect.InfectOnPreRoundStart(); +// return HookResult.Continue; +// } + +// public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info) +// { +// Server.PrintToChatAll($" {_core.Localizer["Prefix"]} {_core.Localizer["Infect.GameInfo"]}"); +// return HookResult.Continue; +// } + +// public HookResult OnWarmupEnd(EventWarmupEnd @event, GameEventInfo info) +// { +// Infect.InfectStarted = false; +// _infect.InfectKillInfectionTimer(); +// return HookResult.Continue; +// } + +// public HookResult OnRoundFreezeEnd(EventRoundFreezeEnd @event, GameEventInfo info) +// { +// _infect.InfectOnRoundFreezeEnd(); +// return HookResult.Continue; +// } + +// public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) +// { +// Infect.InfectStarted = false; +// return HookResult.Continue; +// } +//} \ No newline at end of file diff --git a/ZombieSharp/Plugin/Events/EventBase.cs b/ZombieSharp/Plugin/Events/EventBase.cs new file mode 100644 index 0000000..a2eb6ec --- /dev/null +++ b/ZombieSharp/Plugin/Events/EventBase.cs @@ -0,0 +1,26 @@ +namespace ZombieSharp.Plugin.Events; + +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Modules.Events; +using static CounterStrikeSharp.API.Core.BasePlugin; + +public abstract class EventBase : IEventBase where T : GameEvent +{ + public abstract GameEventHandler Handler { get; } + + public void Register(BasePlugin plugin) + { + plugin.RegisterEventHandler(Handler); + } + + public void Deregister(BasePlugin plugin) + { + plugin.DeregisterEventHandler(Handler); + } +} + +public interface IEventBase +{ + void Register(BasePlugin plugin); + void Deregister(BasePlugin plugin); +} \ No newline at end of file diff --git a/ZombieSharp/Plugin/Events/EventsExtensions.cs b/ZombieSharp/Plugin/Events/EventsExtensions.cs new file mode 100644 index 0000000..634f95c --- /dev/null +++ b/ZombieSharp/Plugin/Events/EventsExtensions.cs @@ -0,0 +1,26 @@ +namespace ZombieSharp.Plugin.Events; + +using CounterStrikeSharp.API.Core; + +public static class EventsExtensions +{ + private static readonly List _events = [ + new PlayerHurtEvent(), + ]; + + public static void RegisterAllEventHandlers(this BasePlugin plugin) + { + foreach (var @event in _events) + { + @event.Register(plugin); + } + } + + public static void RemoveAllEventHandlers(this BasePlugin plugin) + { + foreach (var eventInstance in _events) + { + eventInstance.Deregister(plugin); + } + } +} diff --git a/ZombieSharp/Plugin/Events/PlayerHurtEvent.cs b/ZombieSharp/Plugin/Events/PlayerHurtEvent.cs new file mode 100644 index 0000000..d80440b --- /dev/null +++ b/ZombieSharp/Plugin/Events/PlayerHurtEvent.cs @@ -0,0 +1,15 @@ +namespace ZombieSharp.Plugin.Events; + +using CounterStrikeSharp.API.Core; +using static CounterStrikeSharp.API.Core.BasePlugin; + +public class PlayerHurtEvent : EventBase +{ + public override GameEventHandler Handler => OnPlayerHurt; + + private HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info) + { + Console.WriteLine($"Player {@event.Userid} hurt with {@event.DmgHealth} damage."); + return HookResult.Continue; + } +} diff --git a/ZombieSharp/Plugin/GameSettings.cs b/ZombieSharp/Plugin/GameSettings.cs index c503f2e..5412e94 100644 --- a/ZombieSharp/Plugin/GameSettings.cs +++ b/ZombieSharp/Plugin/GameSettings.cs @@ -24,7 +24,7 @@ public void GameSettingsOnMapStart() var configPath = Path.Combine(ZombieSharp.ConfigPath, "gamesettings.jsonc"); - if(!File.Exists(configPath)) + if (!File.Exists(configPath)) { _logger.LogCritical("[GameSettingsOnMapStart] Couldn't find a gamesettings.jsonc file!"); return; @@ -33,4 +33,5 @@ public void GameSettingsOnMapStart() _logger.LogInformation("[GameSettingsOnMapStart] Load Game settings file."); Settings = JsonConvert.DeserializeObject(File.ReadAllText(configPath)); } +} } } \ No newline at end of file diff --git a/ZombieSharp/Plugin/Hook.cs b/ZombieSharp/Plugin/Hook.cs index cc0ec02..0237c9a 100644 --- a/ZombieSharp/Plugin/Hook.cs +++ b/ZombieSharp/Plugin/Hook.cs @@ -38,11 +38,11 @@ public HookResult OnCanAcquire(DynamicHook hook) var method = hook.GetParam(2); var client = itemService.Pawn.Value.Controller.Value?.As(); - if(client == null) + if (client == null) return HookResult.Continue; // if client is infect and weapon is not a knife. - if(Infect.IsClientInfect(client) && !weapon.Name.Contains("knife")) + if (Infect.IsClientInfect(client) && !weapon.Name.Contains("knife")) { hook.SetReturn(AcquireResult.NotAllowedByProhibition); return HookResult.Handled; @@ -52,14 +52,14 @@ public HookResult OnCanAcquire(DynamicHook hook) var purchaseEnable = GameSettings.Settings?.WeaponPurchaseEnable ?? false; // weapon restrict section. - if(restirctEnable) + if (restirctEnable) { // if player buy from menu tell them they can't. - if(Weapons.IsRestricted(weapon.Name)) + if (Weapons.IsRestricted(weapon.Name)) { var attribute = Weapons.GetWeaponAttributeByEntityName(weapon.Name); - if(method == AcquireMethod.Buy) + if (method == AcquireMethod.Buy) client.PrintToChat($" {_core.Localizer["Prefix"]} {_core.Localizer["Weapon.IsRestricted", attribute?.WeaponName!]}"); hook.SetReturn(AcquireResult.NotAllowedByProhibition); @@ -68,11 +68,11 @@ public HookResult OnCanAcquire(DynamicHook hook) else { - if(method == AcquireMethod.Buy && purchaseEnable) + if (method == AcquireMethod.Buy && purchaseEnable) { var attribute = Weapons.GetWeaponAttributeByEntityName(weapon.Name); - if(attribute != null) + if (attribute != null) { _weapons.PurchaseWeapon(client, attribute); hook.SetReturn(AcquireResult.NotAllowedByProhibition); @@ -84,11 +84,11 @@ public HookResult OnCanAcquire(DynamicHook hook) else { - if(method == AcquireMethod.Buy && purchaseEnable) + if (method == AcquireMethod.Buy && purchaseEnable) { var attribute = Weapons.GetWeaponAttributeByEntityName(weapon.Name); - if(attribute != null) + if (attribute != null) { _weapons.PurchaseWeapon(client, attribute); hook.SetReturn(AcquireResult.NotAllowedByProhibition); @@ -108,13 +108,14 @@ public HookResult OnTakeDamage(DynamicHook hook) var client = Utils.GetCCSPlayerController(victim); var attacker = Utils.GetCCSPlayerController(info.Attacker.Value); - if(client == null || attacker == null) + if (client == null || attacker == null) return HookResult.Continue; // prevent death from backstabing. - if(Infect.IsClientInfect(attacker) && Infect.IsClientHuman(client)) + if (Infect.IsClientInfect(attacker) && Infect.IsClientHuman(client)) info.Damage = 1; return HookResult.Continue; } +} } } \ No newline at end of file diff --git a/ZombieSharp/Plugin/Infect.cs b/ZombieSharp/Plugin/Infect.cs index 79d477e..29c9f4c 100644 --- a/ZombieSharp/Plugin/Infect.cs +++ b/ZombieSharp/Plugin/Infect.cs @@ -30,7 +30,7 @@ public void InfectOnRoundFreezeEnd() // kill timer just in case. InfectKillInfectionTimer(); - if(GameSettings.Settings == null) + if (GameSettings.Settings == null) { _logger.LogCritical("[InfectOnRoundFreezeEnd] Game Settings is null!"); _infectCountNumber = 15; @@ -41,9 +41,9 @@ public void InfectOnRoundFreezeEnd() _firstInfection = _core.AddTimer(_infectCountNumber + 1, InfectMotherZombie, TimerFlags.STOP_ON_MAPCHANGE); - _infectCountTimer = _core.AddTimer(1f, () => + _infectCountTimer = _core.AddTimer(1f, () => { - if(_infectCountNumber < 0) + if (_infectCountNumber < 0) { InfectKillInfectionTimer(); return; @@ -52,18 +52,18 @@ public void InfectOnRoundFreezeEnd() Utils.PrintToCenterAll($" {_core.Localizer["Infect.Countdown", _infectCountNumber]}"); _infectCountNumber--; - }, TimerFlags.REPEAT|TimerFlags.STOP_ON_MAPCHANGE); + }, TimerFlags.REPEAT | TimerFlags.STOP_ON_MAPCHANGE); } public void InfectKillInfectionTimer() { - if(_firstInfection != null) + if (_firstInfection != null) { _firstInfection.Kill(); _firstInfection = null; } - if(_infectCountTimer != null) + if (_infectCountTimer != null) { _infectCountTimer.Kill(); _infectCountTimer = null; @@ -73,14 +73,14 @@ public void InfectKillInfectionTimer() public void InfectMotherZombie() { // if infection already started then stop it. - if(InfectHasStarted()) + if (InfectHasStarted()) return; // we get how much zombie we need. var currentPlayer = Utilities.GetPlayers(); var ratio = 7f; - if(GameSettings.Settings == null) + if (GameSettings.Settings == null) { _logger.LogCritical("[InfectMotherZombie] Game Settings is null!"); ratio = 7f; @@ -94,37 +94,37 @@ public void InfectMotherZombie() // get the list of candidate List candidate = new(); - foreach(var player in currentPlayer) + foreach (var player in currentPlayer) { // null or not alive player. - if(player == null || !player.PawnIsAlive) + if (player == null || !player.PawnIsAlive) continue; // not in the data list. - if(!PlayerData.ZombiePlayerData!.ContainsKey(player)) + if (!PlayerData.ZombiePlayerData!.ContainsKey(player)) continue; - if(PlayerData.ZombiePlayerData[player].MotherZombie == ZombiePlayer.MotherZombieStatus.NONE) + if (PlayerData.ZombiePlayerData[player].MotherZombie == ZombiePlayer.MotherZombieStatus.NONE) candidate.Add(player); } // if candidate is not enough - if(candidate.Count < requireZombie) + if (candidate.Count < requireZombie) { // tell player that mother zombie cycle has been reset Server.PrintToChatAll($" {_core.Localizer["Prefix"]} {_core.Localizer["Infect.MotherZombieReset"]}"); - foreach(var player in currentPlayer) + foreach (var player in currentPlayer) { // null or not alive player. - if(player == null || !player.PawnIsAlive) + if (player == null || !player.PawnIsAlive) continue; // not in the data list. - if(!PlayerData.ZombiePlayerData!.ContainsKey(player)) + if (!PlayerData.ZombiePlayerData!.ContainsKey(player)) continue; - if(PlayerData.ZombiePlayerData[player].MotherZombie == ZombiePlayer.MotherZombieStatus.LAST) + if (PlayerData.ZombiePlayerData[player].MotherZombie == ZombiePlayer.MotherZombieStatus.LAST) { // add to candidate. candidate.Add(player); @@ -140,14 +140,14 @@ public void InfectMotherZombie() // variable check total zombie made. var infected = 0; - foreach(var player in candidate) + foreach (var player in candidate) { // we stop here if all zombie is made. - if(infected >= requireZombie) + if (infected >= requireZombie) return; // check every time. - if(player == null || !player.IsValid || !player.PawnIsAlive) + if (player == null || !player.IsValid || !player.PawnIsAlive) continue; // infect them with motherzombie true @@ -160,16 +160,16 @@ public void InfectMotherZombie() public void InfectOnPlayerHurt(CCSPlayerController? client, CCSPlayerController? attacker) { - if(client == null || attacker == null) + if (client == null || attacker == null) return; - if(IsClientHuman(client) && IsClientInfect(attacker)) + if (IsClientHuman(client) && IsClientInfect(attacker)) InfectClient(client, attacker); } public void InfectOnPreRoundStart() { - if(PlayerData.ZombiePlayerData == null) + if (PlayerData.ZombiePlayerData == null) { _logger.LogCritical("[InfectOnPreRoundStart] ZombiePlayers is invalid!"); return; @@ -177,7 +177,7 @@ public void InfectOnPreRoundStart() foreach (var player in Utilities.GetPlayers()) { - if(player == null || !player.IsValid) + if (player == null || !player.IsValid) { _logger.LogError("[InfectOnPreRoundStart] Player key is not invalid!"); continue; @@ -194,13 +194,13 @@ public void InfectOnPreRoundStart() public void InfectClient(CCSPlayerController client, CCSPlayerController? attacker = null, bool motherzombie = false, bool force = false) { // if infect is not started yet then tell them yeah. - if(!InfectHasStarted()) + if (!InfectHasStarted()) { InfectKillInfectionTimer(); InfectStarted = true; } - if(PlayerData.ZombiePlayerData == null) + if (PlayerData.ZombiePlayerData == null) { _logger.LogCritical("[InfectClient] ZombiePlayers data is null!"); return; @@ -210,7 +210,7 @@ public void InfectClient(CCSPlayerController client, CCSPlayerController? attack PlayerData.ZombiePlayerData[client].Zombie = true; // set motherzombie status to chosen - if(motherzombie) + if (motherzombie) PlayerData.ZombiePlayerData[client].MotherZombie = ZombiePlayer.MotherZombieStatus.CHOSEN; // switch team to terrorist @@ -226,7 +226,7 @@ public void InfectClient(CCSPlayerController client, CCSPlayerController? attack Server.NextFrame(() => _classes?.ClassesApplyToPlayer(client, PlayerData.PlayerClassesData?[client].ZombieClass!)); // create fake killfeed - if(attacker != null) + if (attacker != null) { // fire event. EventPlayerDeath @event = new EventPlayerDeath(false); @@ -247,12 +247,12 @@ public void InfectClient(CCSPlayerController client, CCSPlayerController? attack public void HumanizeClient(CCSPlayerController client, bool force = false) { - if(PlayerData.ZombiePlayerData == null) + if (PlayerData.ZombiePlayerData == null) { _logger.LogCritical("[InfectClient] ZombiePlayers data is null!"); return; } - + // set player zombie to false PlayerData.ZombiePlayerData[client].Zombie = false; @@ -260,7 +260,7 @@ public void HumanizeClient(CCSPlayerController client, bool force = false) client.SwitchTeam(CsTeam.CounterTerrorist); // if force then tell them that you have been force - if(force) + if (force) client.PrintToChat($" {_core.Localizer["Prefix"]} {_core.Localizer["Infect.BecomeHuman"]}"); // apply class attribute. @@ -274,7 +274,7 @@ public static bool InfectHasStarted() public static bool IsClientInfect(CCSPlayerController client) { - if(!PlayerData.ZombiePlayerData!.ContainsKey(client)) + if (!PlayerData.ZombiePlayerData!.ContainsKey(client)) return false; return PlayerData.ZombiePlayerData[client].Zombie; @@ -282,9 +282,11 @@ public static bool IsClientInfect(CCSPlayerController client) public static bool IsClientHuman(CCSPlayerController client) { - if(!PlayerData.ZombiePlayerData!.ContainsKey(client)) + if (!PlayerData.ZombiePlayerData!.ContainsKey(client)) return false; return !PlayerData.ZombiePlayerData[client].Zombie; } +} + } } diff --git a/ZombieSharp/Plugin/Knockback.cs b/ZombieSharp/Plugin/Knockback.cs index bfa91d0..edad83a 100644 --- a/ZombieSharp/Plugin/Knockback.cs +++ b/ZombieSharp/Plugin/Knockback.cs @@ -1,5 +1,4 @@ using CounterStrikeSharp.API.Core; -using CounterStrikeSharp.API.Modules.Entities; using CounterStrikeSharp.API.Modules.Utils; using Microsoft.Extensions.Logging; using ZombieSharp.Models; @@ -17,11 +16,11 @@ public Knockback(ILogger logger) public static void KnockbackClient(CCSPlayerController? client, CCSPlayerController? attacker, string weaponname, float dmgHealth) { - if(client == null || attacker == null) + if (client == null || attacker == null) return; // ent world also trigger hurt event - if(attacker.DesignerName != "cs_player_controller") + if (attacker.DesignerName != "cs_player_controller") return; // knockback is for zombie only. @@ -31,56 +30,56 @@ public static void KnockbackClient(CCSPlayerController? client, CCSPlayerControl var clientPos = client.PlayerPawn.Value?.AbsOrigin; var attackerPos = attacker.PlayerPawn.Value?.AbsOrigin; - if(clientPos == null || attackerPos == null) + if (clientPos == null || attackerPos == null) return; var direction = clientPos - attackerPos; var normalizedDir = NormalizeVector(direction); // Class Data section. - if(PlayerData.PlayerClassesData == null) + if (PlayerData.PlayerClassesData == null) { _logger?.LogError("[KnockbackClient] PlayerClassesData is null!"); return; } - if(!PlayerData.PlayerClassesData.ContainsKey(client)) + if (!PlayerData.PlayerClassesData.ContainsKey(client)) { _logger?.LogError("[KnockbackClient] client {0} is not in playerdata!", client.PlayerName); return; } - if(PlayerData.PlayerClassesData[client].ActiveClass == null) + if (PlayerData.PlayerClassesData[client].ActiveClass == null) { - _logger?.LogError("[KnockbackClient] client {0} active class is null!", client.PlayerName); + _logger?.LogError("[KnockbackClient] client {0} active class is null!", client.PlayerName); return; } float weaponknockback = 1.0f; // weapon knockback section - if(Weapons.WeaponsConfig == null) + if (Weapons.WeaponsConfig == null) _logger?.LogError("[KnockbackClient] Weapon Data is null!"); - if(weaponname.Contains("knife") || weaponname.Contains("hegrenade")) + if (weaponname.Contains("knife") || weaponname.Contains("hegrenade")) { var weapon = Weapons.GetWeaponAttributeByEntityName(weaponname); - if(weapon != null) + if (weapon != null) weaponknockback = weapon.Knockback; } - else + else { var activeWeapon = attacker.PlayerPawn.Value?.WeaponServices?.ActiveWeapon.Value; var weaponString = Utils.FindWeaponItemDefinition(activeWeapon); WeaponAttribute? weapon = null; - if(weaponString != null) + if (weaponString != null) weapon = Weapons.GetWeaponAttributeByEntityName(weaponString!); - if(weapon != null) + if (weapon != null) weaponknockback = weapon.Knockback; } @@ -105,4 +104,5 @@ private static Vector NormalizeVector(Vector vector) return new Vector(x, y, z); } +}} } \ No newline at end of file diff --git a/ZombieSharp/Plugin/Listeners/ListenerBase.cs b/ZombieSharp/Plugin/Listeners/ListenerBase.cs new file mode 100644 index 0000000..d55ca60 --- /dev/null +++ b/ZombieSharp/Plugin/Listeners/ListenerBase.cs @@ -0,0 +1,11 @@ +namespace ZombieSharp.Plugin.Listeners; + +public abstract class ListenerBase +{ + public abstract Delegate DelegateMethod { get; } +} + +public abstract class ListenerBase : ListenerBase where T : Delegate +{ + public abstract override T DelegateMethod { get; } +} \ No newline at end of file diff --git a/ZombieSharp/Plugin/Listeners/ListenersExtensions.cs b/ZombieSharp/Plugin/Listeners/ListenersExtensions.cs new file mode 100644 index 0000000..fe671e8 --- /dev/null +++ b/ZombieSharp/Plugin/Listeners/ListenersExtensions.cs @@ -0,0 +1,31 @@ +namespace ZombieSharp.Plugin.Listeners; + +using CounterStrikeSharp.API.Core; + +public static class ListenersExtensions +{ + // how can we get all instances of ListenerBase? + // reflection/source generator should be used + // TODO: implement reflection/source generator + private static readonly List _listeners = [ + new OnClientDisconnectListener(), + new OnClientPutInServerListener(), + new OnMapStartListener(), + ]; + + public static void RegisterAllListeners(this BasePlugin plugin) + { + foreach (var eventInstance in _listeners) + { + plugin.RegisterListener(eventInstance.DelegateMethod); + } + } + + public static void RemoveAllListeners(this BasePlugin plugin) + { + foreach (var eventInstance in _listeners) + { + plugin.RemoveListener(eventInstance.DelegateMethod); + } + } +} diff --git a/ZombieSharp/Plugin/Listeners/OnClientDisconnectListener.cs b/ZombieSharp/Plugin/Listeners/OnClientDisconnectListener.cs new file mode 100644 index 0000000..09f5860 --- /dev/null +++ b/ZombieSharp/Plugin/Listeners/OnClientDisconnectListener.cs @@ -0,0 +1,13 @@ +namespace ZombieSharp.Plugin.Listeners; + +using static CounterStrikeSharp.API.Core.Listeners; + +public class OnClientDisconnectListener : ListenerBase +{ + public override OnClientDisconnect DelegateMethod => OnClientDisconnectHandler; + + private void OnClientDisconnectHandler(int playerSlot) + { + Console.WriteLine($"Client disconnected: Player Slot {playerSlot}"); + } +} \ No newline at end of file diff --git a/ZombieSharp/Plugin/Listeners/OnClientPutInServerEvent .cs b/ZombieSharp/Plugin/Listeners/OnClientPutInServerEvent .cs new file mode 100644 index 0000000..a15d77b --- /dev/null +++ b/ZombieSharp/Plugin/Listeners/OnClientPutInServerEvent .cs @@ -0,0 +1,13 @@ +namespace ZombieSharp.Plugin.Listeners; + +using static CounterStrikeSharp.API.Core.Listeners; + +public class OnClientPutInServerListener : ListenerBase +{ + public override OnClientPutInServer DelegateMethod => OnClientPutInServerHandler; + + private void OnClientPutInServerHandler(int playerSlot) + { + Console.WriteLine($"Client put in server: Player Slot {playerSlot}"); + } +} \ No newline at end of file diff --git a/ZombieSharp/Plugin/Listeners/OnMapStartListener.cs b/ZombieSharp/Plugin/Listeners/OnMapStartListener.cs new file mode 100644 index 0000000..dc0f656 --- /dev/null +++ b/ZombieSharp/Plugin/Listeners/OnMapStartListener.cs @@ -0,0 +1,13 @@ +namespace ZombieSharp.Plugin.Listeners; + +using static CounterStrikeSharp.API.Core.Listeners; + +public class OnMapStartListener : ListenerBase +{ + public override OnMapStart DelegateMethod => OnMapStartHandler; + + private void OnMapStartHandler(string mapName) + { + Console.WriteLine($"Map started {mapName}"); + } +} diff --git a/ZombieSharp/Plugin/Utils.cs b/ZombieSharp/Plugin/Utils.cs index e68e3e4..d80a4b8 100644 --- a/ZombieSharp/Plugin/Utils.cs +++ b/ZombieSharp/Plugin/Utils.cs @@ -22,10 +22,10 @@ public Utils(ZombieSharp core, ILogger logger) public static void PrintToCenterAll(string message) { - if(string.IsNullOrEmpty(message)) + if (string.IsNullOrEmpty(message)) return; - foreach(var player in Utilities.GetPlayers()) + foreach (var player in Utilities.GetPlayers()) { player.PrintToCenter(message); } @@ -36,10 +36,10 @@ public static void CheckGameStatus() var ct = Utilities.GetPlayers().Where(player => player.TeamNum == 3 && player.PawnIsAlive).Count(); var t = Utilities.GetPlayers().Where(player => player.TeamNum == 2 && player.PawnIsAlive).Count(); - if(ct == 0 && t > 0) + if (ct == 0 && t > 0) TerminateRound(CsTeam.Terrorist); - else if(t == 0 && ct > 0) + else if (t == 0 && ct > 0) TerminateRound(CsTeam.CounterTerrorist); } @@ -47,7 +47,7 @@ public static void TerminateRound(CsTeam team) { var gameRules = Utilities.FindAllEntitiesByDesignerName("cs_gamerules").First().GameRules; - if(gameRules == null) + if (gameRules == null) { _logger?.LogError("[TerminateRound] Gamerules is invalid cannot terminated round!"); return; @@ -55,7 +55,7 @@ public static void TerminateRound(CsTeam team) RoundEndReason reason; - if(team == CsTeam.Terrorist) + if (team == CsTeam.Terrorist) reason = RoundEndReason.TerroristsWin; else @@ -66,7 +66,7 @@ public static void TerminateRound(CsTeam team) public static void EmitSound(CBaseEntity entity, string soundPath, int pitch = 100, float volume = 1.0f, float deley = 0.0f) { - if(entity == null || string.IsNullOrEmpty(soundPath)) + if (entity == null || string.IsNullOrEmpty(soundPath)) return; CBaseEntity_EmitSoundParamsFunc.Invoke(entity, soundPath, pitch, volume, deley); @@ -100,7 +100,7 @@ public static void EmitSound(CBaseEntity entity, string soundPath, int pitch = 1 public static string? FindWeaponItemDefinition(CBasePlayerWeapon? weapon) { - if(weapon == null) + if (weapon == null) return null; var item = (ItemDefinition)weapon.AttributeManager.Item.ItemDefinitionIndex; @@ -137,10 +137,10 @@ public static void EmitSound(CBaseEntity entity, string soundPath, int pitch = 1 public static void DropAllWeapon(CCSPlayerController client, bool remove = false) { - if(client == null) + if (client == null) return; - foreach(var weapon in WeaponList) + foreach (var weapon in WeaponList) { DropWeaponByDesignName(client, weapon, remove); } @@ -148,9 +148,9 @@ public static void DropAllWeapon(CCSPlayerController client, bool remove = false public static void DropWeaponByDesignName(CCSPlayerController client, string weaponName, bool remove = false) { - if(client == null) + if (client == null) return; - + var matchedWeapon = client.PlayerPawn.Value?.WeaponServices?.MyWeapons.Where(x => x.Value?.DesignerName == weaponName).FirstOrDefault(); if (matchedWeapon != null && matchedWeapon.IsValid) @@ -158,9 +158,10 @@ public static void DropWeaponByDesignName(CCSPlayerController client, string wea client.PlayerPawn.Value!.WeaponServices!.ActiveWeapon.Raw = matchedWeapon.Raw; // set timer to remove if remove is true. - if(remove) + if (remove) { - _core?.AddTimer(1f, () => { + _core?.AddTimer(1f, () => + { matchedWeapon.Value?.AddEntityIOEvent("Kill", matchedWeapon.Value, null, "", 0.1f); }); } @@ -171,53 +172,54 @@ public static void DropWeaponByDesignName(CCSPlayerController client, string wea public static bool IsClientInBuyZone(CCSPlayerController client) { - if(client == null) + if (client == null) return false; return client.PlayerPawn.Value?.InBuyZone ?? false; } - public static List WeaponList = new List - { - "weapon_deagle", - "weapon_elite", - "weapon_fiveseven", - "weapon_glock", - "weapon_ak47", - "weapon_aug", - "weapon_awp", - "weapon_famas", - "weapon_g3sg1", - "weapon_galilar", - "weapon_m249", - "weapon_m4a1", - "weapon_mac10", - "weapon_p90", - "weapon_mp5sd", - "weapon_ump45", - "weapon_xm1014", - "weapon_bizon", - "weapon_mag7", - "weapon_negev", - "weapon_sawedoff", - "weapon_tec9", - "weapon_hkp2000", - "weapon_mp7", - "weapon_mp9", - "weapon_nova", - "weapon_p250", - "weapon_scar20", - "weapon_sg556", - "weapon_ssg08", - "weapon_m4a1_silencer", - "weapon_usp_silencer", - "weapon_cz75a", - "weapon_revolver", - "weapon_hegrenade", - "weapon_incgrenade", - "weapon_decoy", - "weapon_molotov", - "weapon_flashbang", - "weapon_smokegrenade" + public static List WeaponList = new List + { + "weapon_deagle", + "weapon_elite", + "weapon_fiveseven", + "weapon_glock", + "weapon_ak47", + "weapon_aug", + "weapon_awp", + "weapon_famas", + "weapon_g3sg1", + "weapon_galilar", + "weapon_m249", + "weapon_m4a1", + "weapon_mac10", + "weapon_p90", + "weapon_mp5sd", + "weapon_ump45", + "weapon_xm1014", + "weapon_bizon", + "weapon_mag7", + "weapon_negev", + "weapon_sawedoff", + "weapon_tec9", + "weapon_hkp2000", + "weapon_mp7", + "weapon_mp9", + "weapon_nova", + "weapon_p250", + "weapon_scar20", + "weapon_sg556", + "weapon_ssg08", + "weapon_m4a1_silencer", + "weapon_usp_silencer", + "weapon_cz75a", + "weapon_revolver", + "weapon_hegrenade", + "weapon_incgrenade", + "weapon_decoy", + "weapon_molotov", + "weapon_flashbang", + "weapon_smokegrenade" }; +} }; } \ No newline at end of file diff --git a/ZombieSharp/Plugin/Weapons.cs b/ZombieSharp/Plugin/Weapons.cs index dacd781..70afff6 100644 --- a/ZombieSharp/Plugin/Weapons.cs +++ b/ZombieSharp/Plugin/Weapons.cs @@ -1,8 +1,6 @@ using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Commands; -using CounterStrikeSharp.API.Modules.Entities.Constants; -using CounterStrikeSharp.API.Modules.Utils; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using ZombieSharp.Models; @@ -33,7 +31,7 @@ public void WeaponsOnMapStart() var configPath = Path.Combine(ZombieSharp.ConfigPath, "weapons.jsonc"); - if(!File.Exists(configPath)) + if (!File.Exists(configPath)) { _logger.LogCritical("[WeaponsOnMapStart] Couldn't find a weapons.jsonc file!"); return; @@ -51,7 +49,7 @@ public void WeaponsOnMapStart() public void IntialWeaponPurchaseCommand() { // if it's not enabled or null then we don't have to. - if(!GameSettings.Settings?.WeaponPurchaseEnable ?? false) + if (!GameSettings.Settings?.WeaponPurchaseEnable ?? false) { // at least tell them that this is disable. _logger.LogInformation("[IntialWeaponPurchaseCommand] Purchasing is disabled"); @@ -59,24 +57,24 @@ public void IntialWeaponPurchaseCommand() } // if this part has been done before don't do it again. - if(weaponCommandInitialized) + if (weaponCommandInitialized) return; // safety first. - if(WeaponsConfig == null) + if (WeaponsConfig == null) { _logger.LogError("[IntialWeaponPurchaseCommand] Weapon Configs is null!"); return; } - foreach(var weapon in WeaponsConfig.Values) + foreach (var weapon in WeaponsConfig.Values) { - if(weapon.PurchaseCommand == null || weapon.PurchaseCommand.Count <= 0) + if (weapon.PurchaseCommand == null || weapon.PurchaseCommand.Count <= 0) continue; - foreach(var command in weapon.PurchaseCommand) + foreach (var command in weapon.PurchaseCommand) { - if(string.IsNullOrEmpty(command)) + if (string.IsNullOrEmpty(command)) continue; _core.AddCommand(command, $"Weapon {weapon.WeaponName} Purchase Command", WeaponPurchaseCommand); @@ -91,29 +89,29 @@ public void WeaponPurchaseCommand(CCSPlayerController? client, CommandInfo info) var command = info.GetArg(0); var weaponAttribute = WeaponsConfig?.Where(weapon => weapon.Value.PurchaseCommand!.Contains(command)).FirstOrDefault().Value; - if(weaponAttribute != null && client != null) + if (weaponAttribute != null && client != null) PurchaseWeapon(client, weaponAttribute); } public void PurchaseWeapon(CCSPlayerController client, WeaponAttribute attribute) { // if not enable then we don't have to proceed any further. - if(!GameSettings.Settings?.WeaponPurchaseEnable ?? false) + if (!GameSettings.Settings?.WeaponPurchaseEnable ?? false) return; // double check for possible - if(attribute == null || client == null) + if (attribute == null || client == null) return; // if not alive. - if(!client.PawnIsAlive) + if (!client.PawnIsAlive) { client.PrintToChat($" {_core.Localizer["Prefix"]} {_core.Localizer["Core.MustBeAlive"]}"); return; } // if is zombie - if(Infect.IsClientInfect(client)) + if (Infect.IsClientInfect(client)) { client.PrintToChat($" {_core.Localizer["Prefix"]} {_core.Localizer["Core.MustBeHuman"]}"); return; @@ -122,46 +120,46 @@ public void PurchaseWeapon(CCSPlayerController client, WeaponAttribute attribute // this check for buyzone if enable. var buyzone = GameSettings.Settings?.WeaponBuyZoneOnly ?? false; - if(buyzone && !Utils.IsClientInBuyZone(client)) + if (buyzone && !Utils.IsClientInBuyZone(client)) { client.PrintToChat($" {_core.Localizer["Prefix"]} {_core.Localizer["Weapon.BuyZoneOnly"]}"); return; } // if it's restricted - if(IsRestricted(attribute.WeaponEntity!)) + if (IsRestricted(attribute.WeaponEntity!)) { client.PrintToChat($" {_core.Localizer["Prefix"]} {_core.Localizer["Weapon.IsRestricted", attribute.WeaponName!]}"); - return; + return; } // check their cash in account - if(client.InGameMoneyServices?.Account < attribute.Price) + if (client.InGameMoneyServices?.Account < attribute.Price) { client.PrintToChat($" {_core.Localizer["Prefix"]} {_core.Localizer["Weapon.NotEnoughCash"]}"); return; } // Max Purchase section, will be added later. - + // we need to force drop weapon first before make an purchase var weapons = client.PlayerPawn.Value?.WeaponServices?.MyWeapons; - if(weapons == null) + if (weapons == null) { _logger.LogError("[PurchaseWeapon] {0} Weapon service is somehow null", client.PlayerName); return; } - foreach(var weapon in weapons) + foreach (var weapon in weapons) { var slot = (int)weapon.Value!.GetVData()!.GearSlot; // for primary and secondary only. - if(slot > 2) + if (slot > 2) continue; - if(slot == attribute.WeaponSlot) + if (slot == attribute.WeaponSlot) { // drop this weapon then break Utils.DropWeaponByDesignName(client, weapon.Value.DesignerName); @@ -170,7 +168,8 @@ public void PurchaseWeapon(CCSPlayerController client, WeaponAttribute attribute } // updated their cash. - Server.NextFrame(() => { + Server.NextFrame(() => + { // we give weapon to them this part can't be null unless server manager fucked it up. client.GiveNamedItem(attribute.WeaponEntity!); client.PrintToChat($" {_core.Localizer["Prefix"]} {_core.Localizer["Weapon.PurchaseSuccess", attribute.WeaponName!]}"); @@ -182,12 +181,12 @@ public void PurchaseWeapon(CCSPlayerController client, WeaponAttribute attribute public static bool IsRestricted(string weaponentity) { - if(WeaponsConfig == null) + if (WeaponsConfig == null) return false; var weapon = GetWeaponAttributeByEntityName(weaponentity); - if(weapon == null) + if (weapon == null) return false; return weapon.Restrict; @@ -195,9 +194,11 @@ public static bool IsRestricted(string weaponentity) public static WeaponAttribute? GetWeaponAttributeByEntityName(string? weaponentity) { - if(WeaponsConfig == null) + if (WeaponsConfig == null) return null; return WeaponsConfig.Where(data => data.Value.WeaponEntity == weaponentity).FirstOrDefault().Value; } +} + } } \ No newline at end of file diff --git a/ZombieSharp/ZombieSharp.cs b/ZombieSharp/ZombieSharp.cs index 9189292..8eab76a 100644 --- a/ZombieSharp/ZombieSharp.cs +++ b/ZombieSharp/ZombieSharp.cs @@ -3,6 +3,8 @@ using Microsoft.Extensions.Logging; using ZombieSharp.Models; using ZombieSharp.Plugin; +using ZombieSharp.Plugin.Events; +using ZombieSharp.Plugin.Listeners; namespace ZombieSharp; @@ -13,7 +15,6 @@ public class ZombieSharp : BasePlugin public override string ModuleAuthor => "Oylsister"; public override string ModuleDescription => "Infection/survival style gameplay for CS2 in C#"; - private Events? _event; private Infect? _infect; private Utils? _utils; private Hook? _hook; @@ -40,10 +41,13 @@ public override void Load(bool hotReload) _settings = new GameSettings(_logger); _weapons = new Weapons(this, _logger); _hook = new Hook(this, _weapons, _logger); - _event = new Events(this, _infect, _settings, _classes, _weapons, _logger); + + this.RegisterAllListeners(); + this.RegisterAllEventHandlers(); + _knockback = new Knockback(_logger); - if(hotReload) + if (hotReload) { _logger.LogWarning("[Load] The plugin is hotReloaded! This might cause instability to your server."); _settings?.GameSettingsOnMapStart(); @@ -54,7 +58,6 @@ public override void Load(bool hotReload) Server.ExecuteCommand("sv_predictable_damage_tag_ticks 0"); // initial - _event.EventOnLoad(); _hook.HookOnLoad(); } @@ -64,7 +67,8 @@ public override void Unload(bool hotReload) PlayerData.PlayerClassesData = null; PlayerData.PlayerPurchaseCount = null; - _event?.EventOnUnload(); + this.RemoveAllListeners(); + this.RegisterAllEventHandlers(); _hook?.HookOnUnload(); }