This commit is contained in:
Juzlus 2026-02-03 07:02:40 +01:00
parent ae13d6a15c
commit 53917a8b4b
62 changed files with 2643 additions and 935 deletions

View file

@ -1,19 +1,23 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using static src.jRandomSkills;
using System.Collections.Concurrent;
using src.utils;
using System.Collections.Concurrent;
using System.Drawing;
using static src.jRandomSkills;
namespace src.player.skills
{
public class SniperElite : ISkill
{
private const Skills skillName = Skills.SniperElite;
private static readonly ConcurrentDictionary<ulong, List<CEntityInstance>> playerAWPs = [];
private static readonly ConcurrentDictionary<ulong, string> sniperElites = [];
private static readonly ConcurrentDictionary<ulong, List<uint>> playerAWPIndexes = [];
private static readonly ConcurrentDictionary<ulong, string> savedWeapons = [];
private static readonly ConcurrentDictionary<ulong, byte> isProcessing = [];
private static readonly object setLock = new();
private const string weapon_awp = "weapon_awp";
private static readonly string[] rifles = [ "weapon_mp9", "weapon_mac10", "weapon_bizon", "weapon_mp7", "weapon_ump45", "weapon_p90",
"weapon_mp5sd", "weapon_famas", "weapon_galilar", "weapon_m4a1", "weapon_m4a1_silencer", "weapon_ak47",
"weapon_aug", "weapon_sg553", "weapon_ssg08", "weapon_awp", "weapon_scar20", "weapon_g3sg1",
@ -29,8 +33,9 @@ namespace src.player.skills
lock (setLock)
{
sniperElites.Clear();
playerAWPs.Clear();
savedWeapons.Clear();
playerAWPIndexes.Clear();
isProcessing.Clear();
}
}
@ -38,6 +43,7 @@ namespace src.player.skills
{
var player = @event.Userid;
if (player == null || !player.IsValid) return;
var playerInfo = Instance.SkillPlayer.FirstOrDefault(p => p.SteamID == player.SteamID);
if (playerInfo?.Skill == skillName)
DisableSkill(player);
@ -47,118 +53,169 @@ namespace src.player.skills
{
var player = @event.Userid;
if (player == null || !player.IsValid) return;
var playerInfo = Instance.SkillPlayer.FirstOrDefault(p => p.SteamID == player.SteamID);
if (playerInfo?.Skill == skillName)
{
if (rifles.Contains(@event.Item) && @event.Item != weapon_awp)
savedWeapons.AddOrUpdate(player.SteamID, string.Empty, (_, _) => string.Empty);
DeleteDroppedAWP(player);
}
}
public static void EnableSkill(CCSPlayerController player)
{
sniperElites.TryAdd(player.SteamID, "weapon_awp");
savedWeapons.TryAdd(player.SteamID, string.Empty);
}
public static void DisableSkill(CCSPlayerController player)
{
sniperElites.TryRemove(player.SteamID, out _);
playerAWPs.TryRemove(player.SteamID, out _);
player.ExecuteClientCommand("slot3");
if (playerAWPIndexes.TryGetValue(player.SteamID, out var AWPs))
foreach (var index in AWPs.ToList())
{
var ent = Utilities.GetEntityFromIndex<CBaseEntity>((int)index);
if (ent != null && ent.IsValid)
ent.AcceptInput("Kill");
}
if (savedWeapons.TryGetValue(player.SteamID, out string? savedWeapon) && !string.IsNullOrWhiteSpace(savedWeapon))
Server.NextFrame(() =>
player.PlayerPawn.Value?.ItemServices?.As<CCSPlayer_ItemServices>().GiveNamedItem<CEntityInstance>(savedWeapon));
Server.NextFrame(() => player.ExecuteClientCommand("lastinv"));
savedWeapons.TryRemove(player.SteamID, out _);
playerAWPIndexes.TryRemove(player.SteamID, out _);
isProcessing.TryRemove(player.SteamID, out _);
}
public static void UseSkill(CCSPlayerController player)
{
var playerPawn = player.PlayerPawn.Value;
if (playerPawn?.CBodyComponent == null) return;
if (sniperElites.ContainsKey(player.SteamID))
if (player.PlayerPawn.Value == null || !player.PlayerPawn.Value.IsValid || isProcessing.ContainsKey(player.SteamID)) return;
if (savedWeapons.ContainsKey(player.SteamID))
RemoveAndGiveWeapon(player);
}
private static void RemoveAndGiveWeapon(CCSPlayerController player)
{
string holdedWeapon = "weapon_awp";
if (sniperElites.TryGetValue(player.SteamID, out string? weapon))
if (!string.IsNullOrEmpty(weapon))
holdedWeapon = weapon;
ulong steamID = player.SteamID;
try
{
if (!isProcessing.TryAdd(steamID, 0)) return;
CBasePlayerWeapon? activeRifle = GetActiveRifle(player);
string weaponToGive = weapon_awp;
if (activeRifle != null && activeRifle.IsValid)
RemoveWeapon(player, activeRifle.DesignerName);
{
string currentWeaponName = SkillUtils.GetDesignerName(activeRifle);
bool isScriptAWP = false;
sniperElites.TryAdd(player.SteamID, (activeRifle != null && activeRifle.IsValid) ? SkillUtils.GetDesignerName(activeRifle) : "weapon_awp");
Server.NextFrame(() => {
string weapon = holdedWeapon ?? "weapon_awp";
if (player == null || !player.IsValid || player.PlayerPawn.Value == null || !player.PlayerPawn.Value.IsValid) return;
var createdWeapon = player.PlayerPawn.Value?.ItemServices?.As<CCSPlayer_ItemServices>().GiveNamedItem<CEntityInstance>(weapon);
lock (setLock)
isScriptAWP = playerAWPIndexes.TryGetValue(steamID, out var indexes) && indexes.Contains(activeRifle.Index);
if (createdWeapon != null && createdWeapon.IsValid && createdWeapon.DesignerName == "weapon_awp")
if (isScriptAWP)
{
lock (setLock)
if (savedWeapons.TryGetValue(steamID, out var savedWeapon) && !string.IsNullOrEmpty(savedWeapon))
{
if (playerAWPs.TryGetValue(player.SteamID, out var list))
list.Add(createdWeapon);
else
playerAWPs.TryAdd(player.SteamID, [createdWeapon]);
weaponToGive = savedWeapon;
savedWeapons.AddOrUpdate(steamID, string.Empty, (_, _) => string.Empty);
}
else
{
isProcessing.TryRemove(steamID, out _);
return;
}
}
else
{
savedWeapons.AddOrUpdate(steamID, currentWeaponName, (_, _) => currentWeaponName);
weaponToGive = weapon_awp + "_script";
}
activeRifle.AcceptInput("Kill");
}
else
{
if (savedWeapons.TryGetValue(steamID, out string? savedWeapon) && !string.IsNullOrEmpty(savedWeapon))
{
weaponToGive = savedWeapon;
savedWeapons.AddOrUpdate(steamID, string.Empty, (_, _) => string.Empty);
}
else
weaponToGive = weapon_awp + "_script";
}
Server.NextFrame(() =>
{
if (player != null && player.IsValid && player.PlayerPawn.Value != null && player.PlayerPawn.Value.IsValid)
{
var createdWeapon = player.PlayerPawn.Value?.ItemServices?.As<CCSPlayer_ItemServices>().GiveNamedItem<CEntityInstance>(weaponToGive.Replace("_script", ""));
if (createdWeapon != null && createdWeapon.IsValid && weaponToGive.EndsWith("_script"))
{
CBasePlayerWeapon weapon = createdWeapon.As<CBasePlayerWeapon>();
if (weapon?.AttributeManager.Item.NetworkedDynamicAttributes != null)
weapon.AttributeManager.Item.CustomName = player.GetTranslation("sniperelite_customname");
lock (setLock)
{
if (!playerAWPIndexes.ContainsKey(steamID))
playerAWPIndexes.TryAdd(steamID, []);
if (playerAWPIndexes.TryGetValue(steamID, out var list))
list.Add(createdWeapon.Index);
}
}
}
DeleteDroppedAWP(player);
isProcessing.TryRemove(steamID, out _);
});
}
catch { }
catch {
isProcessing.TryRemove(steamID, out _);
}
}
private static void DeleteDroppedAWP(CCSPlayerController player)
private static void DeleteDroppedAWP(CCSPlayerController? player)
{
var pawn = player.PlayerPawn.Value;
if (pawn == null || !pawn.IsValid) return;
var weaponServices = pawn.WeaponServices;
if (weaponServices == null) return;
if (player == null || !player.IsValid) return;
ConcurrentBag<uint> playerWeapons = [.. weaponServices.MyWeapons.Where(w => w != null && w.IsValid && w.Value != null && w.Value.IsValid)
.Select(w => w.Value!.Index)];
var pawn = player.PlayerPawn.Value;
if (pawn == null || !pawn.IsValid || pawn.WeaponServices == null) return;
var myWeapons = pawn.WeaponServices.MyWeapons;
ConcurrentBag<uint> myWeaponsIndexes = [.. myWeapons
.Where(w => w != null && w.IsValid && w.Value != null && w.Value.IsValid)
.Select(w => w.Value!.Index)];
lock (setLock)
{
if (playerAWPs.TryGetValue(player.SteamID, out var AWPs))
foreach (var awp in AWPs.ToList())
{
if (awp != null && awp.IsValid)
if (playerAWPIndexes.TryGetValue(player.SteamID, out var AWPs))
foreach (var index in AWPs.ToList())
if (!myWeaponsIndexes.Contains(index))
{
if (!playerWeapons.Contains(awp.Index))
{
awp.AcceptInput("Kill");
AWPs.Remove(awp);
}
var ent = Utilities.GetEntityFromIndex<CBaseEntity>((int)index);
if (ent != null && ent.IsValid)
ent.AcceptInput("Kill");
AWPs.Remove(index);
}
else
AWPs.Remove(awp!);
}
}
}
private static CBasePlayerWeapon? GetActiveRifle(CCSPlayerController player)
{
CBasePlayerWeapon? rifle = null;
var pawn = player.PlayerPawn.Value;
if (pawn == null || !pawn.IsValid) return rifle;
var weaponServices = pawn.WeaponServices;
if (weaponServices == null) return rifle;
foreach (var weapon in weaponServices.MyWeapons)
if (weapon != null && weapon.IsValid && weapon.Value != null && weapon.Value.IsValid)
if (rifles.Contains(weapon.Value.DesignerName))
rifle = weapon.Value;
return rifle;
}
private static void RemoveWeapon(CCSPlayerController player, string designerName)
{
var pawn = player.PlayerPawn.Value;
if (pawn == null || !pawn.IsValid || pawn.WeaponServices == null) return;
foreach (var item in pawn.WeaponServices.MyWeapons)
if (item != null && item.IsValid && item.Value != null && item.Value.IsValid && item.Value.DesignerName == designerName)
item.Value.AcceptInput("Kill");
if (pawn == null || !pawn.IsValid || pawn.WeaponServices == null) return null;
var activeWeapon = pawn.WeaponServices?.ActiveWeapon.Value;
if (activeWeapon != null && activeWeapon.IsValid && rifles.Contains(activeWeapon.DesignerName))
return activeWeapon;
return null;
}
public class SkillConfig(Skills skill = skillName, bool active = true, string color = "#e0873a", CsTeam onlyTeam = CsTeam.None, bool disableOnFreezeTime = false, bool needsTeammates = false, string requiredPermission = "") : SkillsInfo.DefaultSkillInfo(skill, active, color, onlyTeam, disableOnFreezeTime, needsTeammates, requiredPermission)