using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Utils; using static src.jRandomSkills; using System.Collections.Concurrent; using src.utils; namespace src.player.skills { public class SecondLife : ISkill { private const Skills skillName = Skills.SecondLife; private static readonly ConcurrentDictionary secondLifePlayers = []; private static readonly object setLock = new(); public static void LoadSkill() { SkillUtils.RegisterSkill(skillName, SkillsInfo.GetValue(skillName, "color")); } public static void NewRound() { secondLifePlayers.Clear(); } public static void PlayerHurt(EventPlayerHurt @event) { var victim = PlayerManager.GetPlayerEvent(@event.Userid); if (!Instance.IsPlayerValid(victim)) return; var victimInfo = PlayerManager.GetPlayerByIndex(victim!.Index); if (victimInfo == null || victimInfo.Skill != skillName) return; var victimPawn = victim!.PlayerPawn.Value; if (victimPawn!.Health > 0 || (secondLifePlayers.TryGetValue(victim.Handle, out int tick) && tick + 4 < Server.TickCount)) return; lock (setLock) { SetHealth(victim, SkillsInfo.GetValue(skillName, "startHealth")); var spawnpoint = SkillUtils.GetSpawnPointVector(victim); if (spawnpoint == null) return; victimPawn.Teleport(spawnpoint, null, null); secondLifePlayers.TryAdd(victim.Handle, Server.TickCount); } } public static void EnableSkill(CCSPlayerController player) { SetHealth(player, SkillsInfo.GetValue(skillName, "startHealth")); } public static void DisableSkill(CCSPlayerController player) { secondLifePlayers.TryRemove(player.Handle, out _); if (player.PlayerPawn.Value == null) return; SetHealth(player, Math.Min(player.PlayerPawn.Value.Health + SkillsInfo.GetValue(skillName, "startHealth"), 100)); } private static void SetHealth(CCSPlayerController player, int health) { var pawn = player.PlayerPawn.Value; if (pawn == null || !pawn.IsValid) return; pawn.Health = health; Utilities.SetStateChanged(pawn, "CBaseEntity", "m_iHealth"); } public class SkillConfig(Skills skill = skillName, bool active = true, string color = "#d41c1c", CsTeam onlyTeam = CsTeam.None, bool disableOnFreezeTime = false, bool needsTeammates = false, string requiredPermission = "", int maxPerServer = -1, Rarity rarity = Rarity.Common, int startHealth = 50) : SkillsInfo.DefaultSkillInfo(skill, active, color, onlyTeam, disableOnFreezeTime, needsTeammates, requiredPermission, maxPerServer, rarity) { public int StartHealth { get; set; } = startHealth; } } }