From d3ff352a2b9ae7d0c0c5eeef7f421141da55829d Mon Sep 17 00:00:00 2001 From: NockyCZ <63038995+NockyCZ@users.noreply.github.com> Date: Fri, 7 Jun 2024 21:43:18 +0200 Subject: [PATCH] Fix crashes --- source/Deathmatch/API.cs | 7 +- source/Deathmatch/Deathmatch.cs | 10 ++- source/Deathmatch/Deathmatch.csproj | 2 +- source/Deathmatch/Events.cs | 55 +++++++++---- source/Deathmatch/Functions/Commands.cs | 4 +- source/Deathmatch/Functions/Players.cs | 58 ++++++------- source/Deathmatch/Functions/Spawns.cs | 38 +++++---- source/Deathmatch/Functions/Weapons.cs | 82 ++----------------- .../DeathmatchAPI.cs | 7 +- .../DeathmatchAPI.csproj | 4 + .../{DeathmachAPI => DeathmatchAPI}/Events.cs | 0 .../Helpers.cs | 0 12 files changed, 120 insertions(+), 147 deletions(-) rename source/{DeathmachAPI => DeathmatchAPI}/DeathmatchAPI.cs (77%) rename source/{DeathmachAPI => DeathmatchAPI}/DeathmatchAPI.csproj (66%) rename source/{DeathmachAPI => DeathmatchAPI}/Events.cs (100%) rename source/{DeathmachAPI => DeathmatchAPI}/Helpers.cs (100%) diff --git a/source/Deathmatch/API.cs b/source/Deathmatch/API.cs index 4a96319..418406a 100644 --- a/source/Deathmatch/API.cs +++ b/source/Deathmatch/API.cs @@ -1,3 +1,4 @@ +using CounterStrikeSharp.API.Modules.Utils; using DeathmatchAPI; using DeathmatchAPI.Events; using DeathmatchAPI.Helpers; @@ -41,14 +42,14 @@ public void ChangeCheckDistance(int distance) CheckedEnemiesDistance = distance; } - public void SetupCustomSpawns(string team, Dictionary spawns) + public void SetupCustomSpawns(string team, Dictionary spawns) { if (team.Equals("ct")) { spawnPositionsCT.Clear(); foreach (var spawn in spawns) { - spawnPositionsCT.Add(ParseVector(spawn.Key), ParseQAngle(spawn.Value)); + spawnPositionsCT.Add(spawn.Key, spawn.Value); } } else if (team.Equals("t")) @@ -56,7 +57,7 @@ public void SetupCustomSpawns(string team, Dictionary spawns) spawnPositionsT.Clear(); foreach (var spawn in spawns) { - spawnPositionsT.Add(ParseVector(spawn.Key), ParseQAngle(spawn.Value)); + spawnPositionsT.Add(spawn.Key, spawn.Value); } } else diff --git a/source/Deathmatch/Deathmatch.cs b/source/Deathmatch/Deathmatch.cs index 434baed..57765d3 100644 --- a/source/Deathmatch/Deathmatch.cs +++ b/source/Deathmatch/Deathmatch.cs @@ -20,7 +20,7 @@ public partial class Deathmatch : BasePlugin, IPluginConfig { public override string ModuleName => "Deathmatch Core"; public override string ModuleAuthor => "Nocky"; - public override string ModuleVersion => "1.1.4"; + public override string ModuleVersion => "1.1.5"; public void OnConfigParsed(DeathmatchConfig config) { @@ -31,16 +31,16 @@ public override void Load(bool hotReload) { var API = new Deathmatch(); Capabilities.RegisterPluginCapability(DeathmatchAPI, () => API); - IsLinuxServer = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + if (IsLinuxServer) { GetCSWeaponDataFromKeyFunc = new(GameData.GetSignature("GetCSWeaponDataFromKey")); CCSPlayer_CanAcquireFunc = new(GameData.GetSignature("CCSPlayer_CanAcquire")); CCSPlayer_CanAcquireFunc.Hook(OnWeaponCanAcquire, HookMode.Pre); + VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnTakeDamage, HookMode.Pre); } - VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnTakeDamage, HookMode.Pre); LoadCustomModes(); LoadWeaponsRestrict(); @@ -171,10 +171,12 @@ public override void Load(bool hotReload) public override void Unload(bool hotReload) { - VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(OnTakeDamage, HookMode.Pre); if (IsLinuxServer) + { CCSPlayer_CanAcquireFunc?.Unhook(OnWeaponCanAcquire, HookMode.Pre); + VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(OnTakeDamage, HookMode.Pre); + } //else // VirtualFunctions.CCSPlayer_WeaponServices_CanUseFunc.Unhook(OnWeaponCanUse, HookMode.Pre); } diff --git a/source/Deathmatch/Deathmatch.csproj b/source/Deathmatch/Deathmatch.csproj index 933cc59..a660fdd 100644 --- a/source/Deathmatch/Deathmatch.csproj +++ b/source/Deathmatch/Deathmatch.csproj @@ -7,7 +7,7 @@ - + diff --git a/source/Deathmatch/Events.cs b/source/Deathmatch/Events.cs index c375759..bb8cd2b 100644 --- a/source/Deathmatch/Events.cs +++ b/source/Deathmatch/Events.cs @@ -6,16 +6,17 @@ using CounterStrikeSharp.API.Modules.Commands; using CounterStrikeSharp.API.Modules.Admin; using System.Runtime.InteropServices; +using CounterStrikeSharp.API.Modules.Utils; namespace Deathmatch { public partial class Deathmatch { - [GameEventHandler] + [GameEventHandler(HookMode.Post)] public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info) { var player = @event.Userid; - if (IsPlayerValid(player!) && !playerData.ContainsPlayer(player)) + if (player != null && player.IsValid && !player.IsBot && !player.IsHLTV && player.SteamID.ToString().Length == 17 && !playerData.ContainsPlayer(player)) { bool IsVIP = AdminManager.PlayerHasPermissions(player, Config.PlayersSettings.VIPFlag); DeathmatchPlayerData setupPlayerData = new DeathmatchPlayerData @@ -31,9 +32,9 @@ public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventIn HudMessages = Config.PlayersPreferences.HudMessages.Enabled ? ((Config.PlayersPreferences.HudMessages.OnlyVIP && IsVIP ? Config.PlayersPreferences.HudMessages.DefaultValue : false) || (!Config.PlayersPreferences.HudMessages.OnlyVIP ? Config.PlayersPreferences.HudMessages.DefaultValue : false)) : false, SpawnProtection = false, OpenedMenu = 0, - LastSpawn = null! + LastSpawn = new Vector() }; - playerData[player!] = setupPlayerData; + playerData[player] = setupPlayerData; } return HookResult.Continue; } @@ -41,7 +42,7 @@ public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventIn public HookResult OnPlayerConnectDisconnect(EventPlayerDisconnect @event, GameEventInfo info) { var player = @event.Userid; - if (playerData.ContainsPlayer(player)) + if (player != null && player.IsValid && playerData.ContainsPlayer(player)) playerData.RemovePlayer(player); return HookResult.Continue; @@ -59,16 +60,17 @@ public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info) [GameEventHandler(HookMode.Pre)] public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info) { - if (@event.Userid == null || !@event.Userid.IsValid) - return HookResult.Continue; - var attacker = @event.Attacker; var player = @event.Userid; + + if (player == null || !player.IsValid) + return HookResult.Continue; + if (ActiveMode != null && attacker != null && playerData.ContainsPlayer(attacker)) { if (ActiveMode.OnlyHS) { - if (@event.Hitgroup == 1 && (!@event.Weapon.Contains("knife") || !@event.Weapon.Contains("bayonet")) && playerData[attacker].HitSound) + if (@event.Hitgroup == 1 && playerData[attacker].HitSound && (!@event.Weapon.Contains("knife") || !@event.Weapon.Contains("bayonet"))) attacker.ExecuteClientCommand("play " + Config.PlayersPreferences.HitSound.Path); } else @@ -84,9 +86,26 @@ public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info) attacker.ExecuteClientCommand("play " + Config.PlayersPreferences.HitSound.Path); } } + + if (!IsLinuxServer) + { + if (playerData.ContainsPlayer(player) && playerData[player].SpawnProtection) + { + player!.PlayerPawn.Value!.Health = player.PlayerPawn.Value.Health >= 100 ? 100 : player.PlayerPawn.Value.Health + @event.DmgHealth; + player.PlayerPawn.Value.ArmorValue = player.PlayerPawn.Value.ArmorValue >= 100 ? 100 : player.PlayerPawn.Value.ArmorValue + @event.DmgArmor; + return HookResult.Continue; + } + if (!ActiveMode.KnifeDamage && (@event.Weapon.Contains("knife") || @event.Weapon.Contains("bayonet"))) + { + attacker.PrintToCenter(Localizer["Hud.KnifeDamageIsDisabled"]); + player!.PlayerPawn.Value!.Health = player.PlayerPawn.Value.Health >= 100 ? 100 : player.PlayerPawn.Value.Health + @event.DmgHealth; + player.PlayerPawn.Value.ArmorValue = player.PlayerPawn.Value.ArmorValue >= 100 ? 100 : player.PlayerPawn.Value.ArmorValue + @event.DmgArmor; + } + } } return HookResult.Continue; } + [GameEventHandler(HookMode.Pre)] public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info) { @@ -221,13 +240,14 @@ public HookResult OnItemPurcharsed(EventItemPurchase @event, GameEventInfo info) player.PrintToChat($"{Localizer["Chat.Prefix"]} {Localizer["Chat.WeaponIsDisabled", replacedweaponName]}"); } player.RemoveWeapons(); - GivePlayerWeapons(player, false, false); + GivePlayerWeapons(player, false, false, true); return HookResult.Continue; } string localizerWeaponName = Localizer[weaponName]; bool IsVIP = AdminManager.PlayerHasPermissions(player, Config.PlayersSettings.VIPFlag); - if (CheckIsWeaponRestricted(weaponName, IsVIP, player.Team)) + bool IsPrimary = AllowedPrimaryWeaponsList.Contains(weaponName); + if (CheckIsWeaponRestricted(weaponName, IsVIP, player.Team, IsPrimary)) { if (!string.IsNullOrEmpty(Config.SoundSettings.CantEquipSound)) player.ExecuteClientCommand("play " + Config.SoundSettings.CantEquipSound); @@ -238,7 +258,6 @@ public HookResult OnItemPurcharsed(EventItemPurchase @event, GameEventInfo info) return HookResult.Continue; } - bool IsPrimary = AllowedPrimaryWeaponsList.Contains(weaponName); if (IsPrimary) { if (weaponName == playerData[player].PrimaryWeapon) @@ -249,7 +268,7 @@ public HookResult OnItemPurcharsed(EventItemPurchase @event, GameEventInfo info) if (!Config.Gameplay.SwitchWeapons) { player.RemoveWeapons(); - GivePlayerWeapons(player, false, false); + GivePlayerWeapons(player, false, false, true); return HookResult.Continue; } playerData[player].PrimaryWeapon = weaponName; @@ -265,7 +284,7 @@ public HookResult OnItemPurcharsed(EventItemPurchase @event, GameEventInfo info) if (!Config.Gameplay.SwitchWeapons) { player.RemoveWeapons(); - GivePlayerWeapons(player, false, false); + GivePlayerWeapons(player, false, false, true); return HookResult.Continue; } playerData[player].SecondaryWeapon = weaponName; @@ -290,6 +309,9 @@ private HookResult OnRandomWeapons(CCSPlayerController? player, CommandInfo info private HookResult OnTakeDamage(DynamicHook hook) { + //if (!IsLinuxServer) + // return HookResult.Continue; + var damageInfo = hook.GetParam(1); var playerPawn = hook.GetParam(0); @@ -380,7 +402,9 @@ private HookResult OnWeaponCanAcquire(DynamicHook hook) { string localizerWeaponName = Localizer[vdata.Name]; bool IsVIP = AdminManager.PlayerHasPermissions(player, Config.PlayersSettings.VIPFlag); - if (CheckIsWeaponRestricted(vdata.Name, IsVIP, player.Team)) + + bool IsPrimary = AllowedPrimaryWeaponsList.Contains(vdata.Name); + if (CheckIsWeaponRestricted(vdata.Name, IsVIP, player.Team, IsPrimary)) { if (!string.IsNullOrEmpty(Config.SoundSettings.CantEquipSound)) player.ExecuteClientCommand("play " + Config.SoundSettings.CantEquipSound); @@ -391,7 +415,6 @@ private HookResult OnWeaponCanAcquire(DynamicHook hook) return HookResult.Stop; } - bool IsPrimary = AllowedPrimaryWeaponsList.Contains(vdata.Name); if (IsPrimary) { if (vdata.Name == playerData[player].PrimaryWeapon) diff --git a/source/Deathmatch/Functions/Commands.cs b/source/Deathmatch/Functions/Commands.cs index 6ac9b17..39b3fef 100644 --- a/source/Deathmatch/Functions/Commands.cs +++ b/source/Deathmatch/Functions/Commands.cs @@ -182,7 +182,7 @@ public void OnAddSpawnCT_CMD(CCSPlayerController player, CommandInfo info) } var position = player.PlayerPawn.Value!.AbsOrigin; var angle = player.PlayerPawn.Value.AbsRotation; - AddNewSpawnPoint(ModuleDirectory + $"/spawns/{Server.MapName}.json", $"{position}", $"{angle}", "ct"); + AddNewSpawnPoint(ModuleDirectory + $"/spawns/{Server.MapName}.json", position!, angle!, "ct"); info.ReplyToCommand($"{Localizer["Chat.Prefix"]} Spawn for the CT team has been added. (Total: {ChatColors.Green}{spawnPositionsCT.Count}{ChatColors.Default})"); } @@ -203,7 +203,7 @@ public void OnAddSpawnT_CMD(CCSPlayerController player, CommandInfo info) } var position = player.PlayerPawn.Value!.AbsOrigin; var angle = player.PlayerPawn.Value.AbsRotation; - AddNewSpawnPoint(ModuleDirectory + $"/spawns/{Server.MapName}.json", $"{position}", $"{angle}", "t"); + AddNewSpawnPoint(ModuleDirectory + $"/spawns/{Server.MapName}.json", position!, angle!, "t"); info.ReplyToCommand($"{Localizer["Chat.Prefix"]} Spawn for the T team has been added. (Total: {ChatColors.Green}{spawnPositionsT.Count}{ChatColors.Default})"); } diff --git a/source/Deathmatch/Functions/Players.cs b/source/Deathmatch/Functions/Players.cs index 66a775b..7721032 100644 --- a/source/Deathmatch/Functions/Players.cs +++ b/source/Deathmatch/Functions/Players.cs @@ -102,7 +102,7 @@ public void SetupPlayerWeapons(CCSPlayerController player, string weaponName, Co info.ReplyToCommand($"{Localizer["Chat.Prefix"]} {Localizer["Chat.WeaponsIsAlreadySet", localizerWeaponName]}"); return; } - if (CheckIsWeaponRestricted(weaponName, IsVIP, player.Team)) + if (CheckIsWeaponRestricted(weaponName, IsVIP, player.Team, true)) { if (!string.IsNullOrEmpty(Config.SoundSettings.CantEquipSound)) player.ExecuteClientCommand("play " + Config.SoundSettings.CantEquipSound); @@ -140,7 +140,7 @@ public void SetupPlayerWeapons(CCSPlayerController player, string weaponName, Co info.ReplyToCommand($"{Localizer["Chat.Prefix"]} {Localizer["Chat.WeaponsIsAlreadySet", localizerWeaponName]}"); return; } - if (CheckIsWeaponRestricted(weaponName, IsVIP, player.Team)) + if (CheckIsWeaponRestricted(weaponName, IsVIP, player.Team, false)) { if (!string.IsNullOrEmpty(Config.SoundSettings.CantEquipSound)) player.ExecuteClientCommand("play " + Config.SoundSettings.CantEquipSound); @@ -177,13 +177,15 @@ public void SetupPlayerWeapons(CCSPlayerController player, string weaponName, Co return; } } - public void GivePlayerWeapons(CCSPlayerController player, bool bNewMode, bool giveUtilities = true) + public void GivePlayerWeapons(CCSPlayerController player, bool bNewMode, bool giveUtilities = true, bool giveKnife = false) { string PrimaryWeapon = ""; string SecondaryWeapon = ""; if (playerData.ContainsPlayer(player) && player.PlayerPawn.Value != null) { - player.InGameMoneyServices!.Account = Config.Gameplay.AllowBuyMenu ? 16000 : 0; + if (player.InGameMoneyServices != null) + player.InGameMoneyServices.Account = Config.Gameplay.AllowBuyMenu ? 16000 : 0; + if (!bNewMode) { var timer = AdminManager.PlayerHasPermissions(player, Config.PlayersSettings.VIPFlag) ? Config.PlayersSettings.ProtectionTimeVIP : Config.PlayersSettings.ProtectionTime; @@ -213,7 +215,6 @@ public void GivePlayerWeapons(CCSPlayerController player, bool bNewMode, bool gi AddTimer(0.25f, () => blockRandomWeaponsIntegeration.Remove(player), TimerFlags.STOP_ON_MAPCHANGE); } } - if (ActiveMode == null) return; @@ -224,7 +225,7 @@ public void GivePlayerWeapons(CCSPlayerController player, bool bNewMode, bool gi PrimaryWeapon = playerData[player].PrimaryWeapon; if (ActiveMode.RandomWeapons) { - PrimaryWeapon = GetRandomWeaponFromList(AllowedPrimaryWeaponsList, IsVIP, player.Team); + PrimaryWeapon = GetRandomWeaponFromList(AllowedPrimaryWeaponsList, IsVIP, player.Team, true); } else if (string.IsNullOrEmpty(PrimaryWeapon) || !AllowedPrimaryWeaponsList.Contains(PrimaryWeapon)) { @@ -233,7 +234,7 @@ public void GivePlayerWeapons(CCSPlayerController player, bool bNewMode, bool gi 1 => AllowedPrimaryWeaponsList[0], _ => Config.Gameplay.DefaultModeWeapons switch { - 2 => GetRandomWeaponFromList(AllowedPrimaryWeaponsList, IsVIP, player.Team), + 2 => GetRandomWeaponFromList(AllowedPrimaryWeaponsList, IsVIP, player.Team, true), 1 => AllowedPrimaryWeaponsList[0], _ => "" } @@ -246,7 +247,7 @@ public void GivePlayerWeapons(CCSPlayerController player, bool bNewMode, bool gi SecondaryWeapon = playerData[player].SecondaryWeapon; if (ActiveMode.RandomWeapons) { - SecondaryWeapon = GetRandomWeaponFromList(AllowedSecondaryWeaponsList, IsVIP, player.Team); + SecondaryWeapon = GetRandomWeaponFromList(AllowedSecondaryWeaponsList, IsVIP, player.Team, false); } else if (string.IsNullOrEmpty(SecondaryWeapon) || !AllowedSecondaryWeaponsList.Contains(SecondaryWeapon)) { @@ -255,7 +256,7 @@ public void GivePlayerWeapons(CCSPlayerController player, bool bNewMode, bool gi 1 => AllowedSecondaryWeaponsList[0], _ => Config.Gameplay.DefaultModeWeapons switch { - 2 => GetRandomWeaponFromList(AllowedSecondaryWeaponsList, IsVIP, player.Team), + 2 => GetRandomWeaponFromList(AllowedSecondaryWeaponsList, IsVIP, player.Team, false), 1 => AllowedSecondaryWeaponsList[0], _ => "" } @@ -263,21 +264,26 @@ public void GivePlayerWeapons(CCSPlayerController player, bool bNewMode, bool gi } } - Server.NextFrame(() => + if (!string.IsNullOrEmpty(PrimaryWeapon)) + player.GiveNamedItem(PrimaryWeapon); + + if (!string.IsNullOrEmpty(SecondaryWeapon)) + player.GiveNamedItem(SecondaryWeapon); + + if (giveKnife) + player.GiveNamedItem("weapon_knife"); + + if (giveUtilities && ActiveMode.Utilities != null && ActiveMode.Utilities.Count() > 0) { - GiveOrReplaceWeapons(player, PrimaryWeapon, SecondaryWeapon); - if (giveUtilities && ActiveMode.Utilities != null && ActiveMode.Utilities.Count() > 0) - { - foreach (var item in ActiveMode.Utilities) - player.GiveNamedItem(item); - } - }); + foreach (var item in ActiveMode.Utilities) + player.GiveNamedItem(item); + } return; } AddTimer(0.2f, () => { - if (player == null || !player.IsValid) + if (player == null || !player.IsValid || GameRules().WarmupPeriod) return; if (player.InGameMoneyServices != null) @@ -285,22 +291,18 @@ public void GivePlayerWeapons(CCSPlayerController player, bool bNewMode, bool gi if (AllowedPrimaryWeaponsList.Count != 0) { - PrimaryWeapon = GetRandomWeaponFromList(AllowedPrimaryWeaponsList, false, player.Team); + PrimaryWeapon = GetRandomWeaponFromList(AllowedPrimaryWeaponsList, false, player.Team, true); + if (!string.IsNullOrEmpty(PrimaryWeapon)) + player.GiveNamedItem(PrimaryWeapon); } if (AllowedSecondaryWeaponsList.Count != 0) { - SecondaryWeapon = GetRandomWeaponFromList(AllowedSecondaryWeaponsList, false, player.Team); + SecondaryWeapon = GetRandomWeaponFromList(AllowedSecondaryWeaponsList, false, player.Team, false); + if (!string.IsNullOrEmpty(SecondaryWeapon)) + player.GiveNamedItem(SecondaryWeapon); } - - GiveOrReplaceWeapons(player, PrimaryWeapon, SecondaryWeapon); }, TimerFlags.STOP_ON_MAPCHANGE); } - public static bool IsPlayerValid(CCSPlayerController player) - { - if (player is null || !player.IsValid || !player.PlayerPawn.IsValid || player.IsBot || player.IsHLTV || player.SteamID.ToString().Length != 17) - return false; - return true; - } private static bool GetPrefsValue(CCSPlayerController player, int preference) { diff --git a/source/Deathmatch/Functions/Spawns.cs b/source/Deathmatch/Functions/Spawns.cs index 670fc43..8769b18 100644 --- a/source/Deathmatch/Functions/Spawns.cs +++ b/source/Deathmatch/Functions/Spawns.cs @@ -39,7 +39,7 @@ public void PerformRespawn(CCSPlayerController player, CsTeam team, bool IsBot) player.PlayerPawn.Value.Teleport(randomSpawn.Key, randomSpawn.Value, new Vector(0, 0, 0)); spawnsDictionary.Remove(randomSpawn.Key); - AddTimer(5.0f, () => + AddTimer(10.0f, () => { if (!spawnsDictionary.ContainsKey(randomSpawn.Key)) spawnsDictionary.Add(randomSpawn.Key, randomSpawn.Value); @@ -76,15 +76,15 @@ private KeyValuePair GetAvailableSpawn(CCSPlayerController playe double closestDistance = 4000; foreach (var playerPos in playerPositions) { - if (playerPos != null) + if (playerPos == null) + continue; + + double distance = GetDistance(playerPos, spawn.Key); + //Console.WriteLine($"Distance {distance} | {closestDistance}"); + if (distance < closestDistance) { - double distance = GetDistance(playerPos, spawn.Key); - //Console.WriteLine($"Distance {distance} | {closestDistance}"); - if (distance < closestDistance) - { - //Console.WriteLine($"ClosestDistance Distance {distance}"); - closestDistance = distance; - } + //Console.WriteLine($"ClosestDistance Distance {distance}"); + closestDistance = distance; } } if (closestDistance > CheckedEnemiesDistance) @@ -105,15 +105,25 @@ private KeyValuePair GetAvailableSpawn(CCSPlayerController playe return randomSpawn; } - public void AddNewSpawnPoint(string filepath, string posValue, string angleValue, string team) + public void AddNewSpawnPoint(string filepath, Vector posValue, QAngle angleValue, string team) { + string FormatValue(float value) + { + return value.ToString("N2", CultureInfo.InvariantCulture); + } + + string formattedPosValue = $"{FormatValue(posValue.X)} {FormatValue(posValue.Y)} {FormatValue(posValue.Z)}"; + string formattedAngleValue = $"{FormatValue(angleValue.X)} {FormatValue(angleValue.Y)} {FormatValue(angleValue.Z)}"; + + //Server.PrintToChatAll($"Edited: {formattedPosValue} | {formattedAngleValue}"); + //Server.PrintToChatAll($"Default: {posValue} | {angleValue}"); if (!File.Exists(filepath)) { JObject newRow = new JObject { { "team", team }, - { "pos", posValue }, - { "angle", angleValue } + { "pos", formattedPosValue }, + { "angle", formattedAngleValue } }; JObject jsonData = new JObject @@ -130,8 +140,8 @@ public void AddNewSpawnPoint(string filepath, string posValue, string angleValue JObject newRow = new JObject { { "team", team }, - { "pos", posValue }, - { "angle", angleValue } + { "pos", formattedPosValue }, + { "angle", formattedAngleValue } }; JArray spawnpointsArray = (JArray)jsonData["spawnpoints"]!; diff --git a/source/Deathmatch/Functions/Weapons.cs b/source/Deathmatch/Functions/Weapons.cs index eaeafeb..2d1810d 100644 --- a/source/Deathmatch/Functions/Weapons.cs +++ b/source/Deathmatch/Functions/Weapons.cs @@ -6,9 +6,8 @@ namespace Deathmatch { public partial class Deathmatch { - public bool CheckIsWeaponRestricted(string weaponName, bool isVIP, CsTeam team) + public bool CheckIsWeaponRestricted(string weaponName, bool isVIP, CsTeam team, bool bPrimary) { - bool bPrimary = PrimaryWeaponsList.Contains(weaponName); if (bPrimary) { if (AllowedPrimaryWeaponsList.Count == 1) @@ -19,6 +18,7 @@ public bool CheckIsWeaponRestricted(string weaponName, bool isVIP, CsTeam team) if (AllowedSecondaryWeaponsList.Count == 1) return false; } + if (RestrictedWeapons.ContainsKey(weaponName)) { int restrictValue = GetWeaponRestrict(weaponName, isVIP, team); @@ -85,88 +85,18 @@ public bool CheckIsWeaponRestricted(string weaponName, bool isVIP, CsTeam team) .FirstOrDefault(); } - public void GiveOrReplaceWeapons(CCSPlayerController player, string PrimaryWeapon, string SecondaryWeapon) - { - if (player == null || !player.IsValid || player.PlayerPawn == null || player.PlayerPawn.Value == null || !player.PawnIsAlive) - return; - - var weaponServices = player.PlayerPawn.Value.WeaponServices; - if (weaponServices == null) - return; - var weaponsList = weaponServices.MyWeapons.Select(weapon => weapon.Value?.As()).ToList(); - if (weaponsList == null) - return; - - var weapon = weaponsList - .Where(weaponBase => weaponBase != null && weaponBase.VData != null && weaponBase.VData.GearSlot == gear_slot_t.GEAR_SLOT_RIFLE) - .FirstOrDefault(); - - if (weapon == null) - { - if (string.IsNullOrEmpty(PrimaryWeapon)) - player.PrintToChat($"{Localizer["Chat.Prefix"]} {Localizer["Chat.SetupWeaponsCommand"]}"); - else - player.GiveNamedItem(PrimaryWeapon); - } - else - { - if (string.IsNullOrEmpty(PrimaryWeapon)) - player.PrintToChat($"{Localizer["Chat.Prefix"]} {Localizer["Chat.SetupWeaponsCommand"]}"); - else - { - player.RemoveItemByDesignerName(weapon.DesignerName, false); - player.GiveNamedItem(PrimaryWeapon); - } - } - - weapon = weaponsList - .Where(weaponBase => weaponBase != null && weaponBase.VData != null && weaponBase.VData.GearSlot == gear_slot_t.GEAR_SLOT_PISTOL) - .FirstOrDefault(); - - if (weapon == null) - { - if (string.IsNullOrEmpty(SecondaryWeapon)) - player.PrintToChat($"{Localizer["Chat.Prefix"]} {Localizer["Chat.SetupWeaponsCommand"]}"); - else - player.GiveNamedItem(SecondaryWeapon); - } - else - { - if (string.IsNullOrEmpty(SecondaryWeapon)) - player.PrintToChat($"{Localizer["Chat.Prefix"]} {Localizer["Chat.SetupWeaponsCommand"]}"); - else - { - player.RemoveItemByDesignerName(weapon.DesignerName, false); - player.GiveNamedItem(SecondaryWeapon); - } - } - - weapon = weaponsList - .Where(weaponBase => weaponBase != null && weaponBase.VData != null && weaponBase.VData.GearSlot == gear_slot_t.GEAR_SLOT_KNIFE) - .FirstOrDefault(); - if (weapon == null) - player.GiveNamedItem("weapon_knife"); - } - - private string GetRandomWeaponFromList(List weaponsList, bool isVIP, CsTeam team) + private string GetRandomWeaponFromList(List weaponsList, bool isVIP, CsTeam team, bool bPrimary) { if (Config.Gameplay.RemoveRestrictedWeapons) - { - foreach (var weapon in weaponsList) - { - if (CheckIsWeaponRestricted(weapon, isVIP, team)) - weaponsList.Remove(weapon); - } - } + weaponsList.RemoveAll(weapon => CheckIsWeaponRestricted(weapon, isVIP, team, bPrimary)); + int index = Random.Next(weaponsList.Count); return weaponsList[index]; } public int GetWeaponRestrict(string weaponName, bool isVIP, CsTeam team) { - if (!RestrictedWeapons.ContainsKey(weaponName)) - return 0; - if (!RestrictedWeapons[weaponName].ContainsKey(ActiveCustomMode.ToString())) + if (!RestrictedWeapons.ContainsKey(weaponName) || !RestrictedWeapons[weaponName].ContainsKey(ActiveCustomMode.ToString())) return 0; var restrictInfo = RestrictedWeapons[weaponName][ActiveCustomMode.ToString()][isVIP ? RestrictType.VIP : RestrictType.NonVIP]; diff --git a/source/DeathmachAPI/DeathmatchAPI.cs b/source/DeathmatchAPI/DeathmatchAPI.cs similarity index 77% rename from source/DeathmachAPI/DeathmatchAPI.cs rename to source/DeathmatchAPI/DeathmatchAPI.cs index ce0d334..9ec7ef2 100644 --- a/source/DeathmachAPI/DeathmatchAPI.cs +++ b/source/DeathmatchAPI/DeathmatchAPI.cs @@ -1,4 +1,5 @@ -using DeathmatchAPI.Events; +using CounterStrikeSharp.API.Modules.Utils; +using DeathmatchAPI.Events; using DeathmatchAPI.Helpers; namespace DeathmatchAPI; @@ -12,9 +13,9 @@ public interface IDeathmatchAPI /* Team String - Available values: ct | t - Spawns Dictionary - Vector & QAngle to string + Spawns Dictionary - Vector & QAngle */ - public void SetupCustomSpawns(string team, Dictionary spawns); + public void SetupCustomSpawns(string team, Dictionary spawns); public void SwapHudMessageVisibility(bool visible); public int GetActiveModeId(); public int GetActiveModeRemainingTime(); diff --git a/source/DeathmachAPI/DeathmatchAPI.csproj b/source/DeathmatchAPI/DeathmatchAPI.csproj similarity index 66% rename from source/DeathmachAPI/DeathmatchAPI.csproj rename to source/DeathmatchAPI/DeathmatchAPI.csproj index fa71b7a..75502cc 100644 --- a/source/DeathmachAPI/DeathmatchAPI.csproj +++ b/source/DeathmatchAPI/DeathmatchAPI.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/source/DeathmachAPI/Events.cs b/source/DeathmatchAPI/Events.cs similarity index 100% rename from source/DeathmachAPI/Events.cs rename to source/DeathmatchAPI/Events.cs diff --git a/source/DeathmachAPI/Helpers.cs b/source/DeathmatchAPI/Helpers.cs similarity index 100% rename from source/DeathmachAPI/Helpers.cs rename to source/DeathmatchAPI/Helpers.cs