Compare commits

...

5 Commits

Author SHA1 Message Date
Nereziel
dcd68aed67 Merge pull request #68 from daffyyyy/fix-knife-3
IMPORTANT KNIVES FIX
2023-11-26 15:25:01 +01:00
daffyyyy
7bb97af619 IMPORTANT KNIVES FIX 2023-11-26 15:16:01 +01:00
Nereziel
cdc58df13c Merge pull request #67 from daffyyyy/fix-knife-3
Fixees
2023-11-25 23:30:31 +01:00
daffyyyy
fe5cc9a82a Update WeaponPaints.cs 2023-11-25 23:17:56 +01:00
daffyyyy
01542a7dc6 Fixees 2023-11-25 23:12:17 +01:00
2 changed files with 242 additions and 152 deletions

62
Utility.cs Normal file
View File

@@ -0,0 +1,62 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Utils;
using System.Reflection;
namespace WeaponPaints
{
public static class Utility
{
public static WeaponPaintsConfig? Config { get; set; }
public static bool IsPlayerValid(CCSPlayerController? player)
{
return (player != null && player.IsValid && !player.IsBot && !player.IsHLTV);
}
public static string ReplaceTags(string message)
{
if (message.Contains('{'))
{
string modifiedValue = message;
if (Config != null)
{
modifiedValue = modifiedValue.Replace("{WEBSITE}", Config.Website);
}
foreach (FieldInfo field in typeof(ChatColors).GetFields())
{
string pattern = $"{{{field.Name}}}";
if (message.Contains(pattern, StringComparison.OrdinalIgnoreCase))
{
modifiedValue = modifiedValue.Replace(pattern, field.GetValue(null)!.ToString(), StringComparison.OrdinalIgnoreCase);
}
}
return modifiedValue;
}
return message;
}
public static void Log(string message)
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("[WeaponPaints] " + message);
Console.ResetColor();
}
public static void ShowAd(string moduleVersion)
{
Console.WriteLine(" ");
Console.WriteLine(" _ _ _______ _______ _______ _______ __ _ _______ _______ ___ __ _ _______ _______ ");
Console.WriteLine("| | _ | || || _ || || || | | || || _ || | | | | || || |");
Console.WriteLine("| || || || ___|| |_| || _ || _ || |_| || _ || |_| || | | |_| ||_ _|| _____|");
Console.WriteLine("| || |___ | || |_| || | | || || |_| || || | | | | | | |_____ ");
Console.WriteLine("| || ___|| || ___|| |_| || _ || ___|| || | | _ | | | |_____ |");
Console.WriteLine("| _ || |___ | _ || | | || | | || | | _ || | | | | | | | _____| |");
Console.WriteLine("|__| |__||_______||__| |__||___| |_______||_| |__||___| |__| |__||___| |_| |__| |___| |_______|");
Console.WriteLine(" >> Version: " + moduleVersion);
Console.WriteLine(" >> GitHub: https://github.com/Nereziel/cs2-WeaponPaints");
Console.WriteLine(" ");
}
}
}

View File

@@ -21,7 +21,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
public override string ModuleName => "WeaponPaints"; public override string ModuleName => "WeaponPaints";
public override string ModuleDescription => "Skin and knife selector, standalone and web-based"; public override string ModuleDescription => "Skin and knife selector, standalone and web-based";
public override string ModuleAuthor => "Nereziel & daffyy"; public override string ModuleAuthor => "Nereziel & daffyy";
public override string ModuleVersion => "1.2"; public override string ModuleVersion => "1.2a";
public WeaponPaintsConfig Config { get; set; } = new(); public WeaponPaintsConfig Config { get; set; } = new();
private string DatabaseConnectionString = string.Empty; private string DatabaseConnectionString = string.Empty;
@@ -35,21 +35,9 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
private Dictionary<int, Dictionary<int, int>> gPlayerWeaponSeed = new(); private Dictionary<int, Dictionary<int, int>> gPlayerWeaponSeed = new();
private Dictionary<int, Dictionary<int, float>> gPlayerWeaponWear = new(); private Dictionary<int, Dictionary<int, float>> gPlayerWeaponWear = new();
private Dictionary<int, string> g_playersKnife = new(); private Dictionary<int, string> g_playersKnife = new();
private List<int> g_changedKnife = new();
private static List<JObject> skinsList = new List<JObject>(); private static List<JObject> skinsList = new List<JObject>();
private static readonly Dictionary<string, string> knifeTypes = new()
{
{ "m9", "weapon_knife_m9_bayonet" }, { "karambit", "weapon_knife_karambit" },
{ "bayonet", "weapon_bayonet" }, { "bowie", "weapon_knife_survival_bowie" },
{ "butterfly", "weapon_knife_butterfly" }, { "falchion", "weapon_knife_falchion" },
{ "flip", "weapon_knife_flip" }, { "gut", "weapon_knife_gut" },
{ "tactical", "weapon_knife_tactical" }, { "shadow", "weapon_knife_push" },
{ "navaja", "weapon_knife_gypsy_jackknife" }, { "stiletto", "weapon_knife_stiletto" },
{ "talon", "weapon_knife_widowmaker" }, { "ursus", "weapon_knife_ursus" },
{ "css", "weapon_knife_css" }, { "paracord", "weapon_knife_cord" },
{ "survival", "weapon_knife_canis" }, { "nomad", "weapon_knife_outdoor" },
{ "skeleton", "weapon_knife_skeleton" }, { "default", "weapon_knife" }
};
private static readonly Dictionary<string, string> weaponList = new() private static readonly Dictionary<string, string> weaponList = new()
{ {
{"weapon_deagle", "Desert Eagle"}, {"weapon_deagle", "Desert Eagle"},
@@ -86,9 +74,28 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{"weapon_usp_silencer", "USP-S"}, {"weapon_usp_silencer", "USP-S"},
{"weapon_cz75a", "CZ75-Auto"}, {"weapon_cz75a", "CZ75-Auto"},
{"weapon_revolver", "R8 Revolver"}, {"weapon_revolver", "R8 Revolver"},
{"weapon_bayonet", "Bayonet Knife"}, { "weapon_knife", "Default Knife" },
{"weapon_knife", "Default Knife"} { "weapon_knife_m9_bayonet", "M9 Bayonet" },
{ "weapon_knife_karambit", "Karambit" },
{ "weapon_bayonet", "Bayonet" },
{ "weapon_knife_survival_bowie", "Bowie Knife" },
{ "weapon_knife_butterfly", "Butterfly Knife" },
{ "weapon_knife_falchion", "Falchion Knife" },
{ "weapon_knife_flip", "Flip Knife" },
{ "weapon_knife_gut", "Gut Knife" },
{ "weapon_knife_tactical", "Huntsman Knife" },
{ "weapon_knife_push", "Shadow Daggers" },
{ "weapon_knife_gypsy_jackknife", "Navaja Knife" },
{ "weapon_knife_stiletto", "Stiletto Knife" },
{ "weapon_knife_widowmaker", "Talon Knife" },
{ "weapon_knife_ursus", "Ursus Knife" },
{ "weapon_knife_css", "Classic Knife" },
{ "weapon_knife_cord", "Paracord Knife" },
{ "weapon_knife_canis", "Survival Knife" },
{ "weapon_knife_outdoor", "Nomad Knife" },
{ "weapon_knife_skeleton", "Skeleton Knife" }
}; };
public override void Load(bool hotReload) public override void Load(bool hotReload)
{ {
if (!Config.GlobalShare) if (!Config.GlobalShare)
@@ -141,10 +148,9 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
Config = config; Config = config;
Utility.Config = config;
ShowAd(); Utility.ShowAd(ModuleVersion);
} }
private void BuildDatabaseConnectionString() private void BuildDatabaseConnectionString()
{ {
var builder = new MySqlConnectionStringBuilder var builder = new MySqlConnectionStringBuilder
@@ -222,14 +228,21 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
private void RegisterCommands() private void RegisterCommands()
{ {
AddCommand($"css_{Config.Additional.CommandSkin}", "Skins info", (player, info) => { if (player == null) return; OnCommandWS(player, info); }); AddCommand($"css_{Config.Additional.CommandSkin}", "Skins info", (player, info) =>
AddCommand($"css_{Config.Additional.CommandRefresh}", "Skins refresh", (player, info) => { if (player == null) return; OnCommandRefresh(player, info); }); {
if (!Utility.IsPlayerValid(player)) return;
OnCommandWS(player, info);
});
AddCommand($"css_{Config.Additional.CommandRefresh}", "Skins refresh", (player, info) =>
{
if (!Utility.IsPlayerValid(player)) return;
OnCommandRefresh(player, info);
});
if (Config.Additional.CommandKillEnabled) if (Config.Additional.CommandKillEnabled)
{ {
AddCommand($"css_{Config.Additional.CommandKill}", "kill yourself", (player, info) => AddCommand($"css_{Config.Additional.CommandKill}", "kill yourself", (player, info) =>
{ {
if (player == null || !player.IsValid || !player.PlayerPawn.IsValid) if (!Utility.IsPlayerValid(player) || !player!.PlayerPawn.IsValid) return;
return;
player.PlayerPawn.Value.CommitSuicide(true, false); player.PlayerPawn.Value.CommitSuicide(true, false);
}); });
@@ -307,10 +320,8 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
private void OnClientDisconnect(int playerSlot) private void OnClientDisconnect(int playerSlot)
{ {
int playerIndex = playerSlot + 1;
CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot); CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot);
if (player == null || !player.IsValid || player.IsBot) return; if (!Utility.IsPlayerValid(player)) return;
// TODO: Clean up after player
if (Config.Additional.KnifeEnabled) if (Config.Additional.KnifeEnabled)
g_playersKnife.Remove((int)player.EntityIndex!.Value.Value); g_playersKnife.Remove((int)player.EntityIndex!.Value.Value);
if (Config.Additional.SkinEnabled) if (Config.Additional.SkinEnabled)
@@ -331,6 +342,11 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
GiveKnifeToPlayer(player); GiveKnifeToPlayer(player);
} }
if (Config.Additional.SkinVisibilityFix)
{
AddTimer(0.3f, () => RefreshSkins(player));
}
return HookResult.Continue; return HookResult.Continue;
} }
private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info) private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
@@ -349,15 +365,16 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
if (@event.Defindex == 42 || @event.Defindex == 59) if (@event.Defindex == 42 || @event.Defindex == 59)
{ {
CCSPlayerController? player = @event.Userid; CCSPlayerController? player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot || !player.PawnIsAlive) return HookResult.Continue; if (!Utility.IsPlayerValid(player) || !player.PawnIsAlive) return HookResult.Continue;
if (g_playersKnife.ContainsKey((int)player.EntityIndex!.Value.Value) if (g_playersKnife.ContainsKey((int)player.EntityIndex!.Value.Value)
&& &&
g_playersKnife[(int)player.EntityIndex!.Value.Value] != "weapon_knife") g_playersKnife[(int)player.EntityIndex!.Value.Value] != "weapon_knife")
{ {
RemoveKnifeFromPlayer(player); if (PlayerHasKnife(player))
RemoveKnifeFromPlayer(player);
AddTimer(0.1f, () => AddTimer(0.3f, () =>
{ {
if (!PlayerHasKnife(player)) if (!PlayerHasKnife(player))
GiveKnifeToPlayer(player); GiveKnifeToPlayer(player);
@@ -390,13 +407,30 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{ {
if (!weapon.IsValid) return; if (!weapon.IsValid) return;
if (weapon.OwnerEntity.Value == null) return; if (weapon.OwnerEntity.Value == null) return;
if (!weapon.OwnerEntity.Value.EntityIndex.HasValue)
{
for (int i = 1; i <= Server.MaxPlayers; i++)
{
CCSPlayerController? ghostPlayer = Utilities.GetPlayerFromIndex(i);
if (!Utility.IsPlayerValid(ghostPlayer)) continue;
if (g_changedKnife.Contains((int)ghostPlayer.EntityIndex!.Value.Value))
{
ChangeWeaponAttributes(weapon, ghostPlayer, isKnife);
g_changedKnife.Remove((int)ghostPlayer.EntityIndex!.Value.Value);
break;
}
}
return;
}
if (!weapon.OwnerEntity.Value.EntityIndex.HasValue) return; if (!weapon.OwnerEntity.Value.EntityIndex.HasValue) return;
int weaponOwner = (int)weapon.OwnerEntity.Value.EntityIndex.Value.Value; int weaponOwner = (int)weapon.OwnerEntity.Value.EntityIndex.Value.Value;
var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex(weaponOwner)); var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex(weaponOwner));
if (!pawn.IsValid) return; if (!pawn.IsValid) return;
var playerIndex = (int)pawn.Controller.Value.EntityIndex!.Value.Value; var playerIndex = (int)pawn.Controller.Value.EntityIndex!.Value.Value;
var player = Utilities.GetPlayerFromIndex(playerIndex); var player = Utilities.GetPlayerFromIndex(playerIndex);
if (player == null || !player.IsValid || player.IsBot) return; if (!Utility.IsPlayerValid(player)) return;
// TODO: Remove knife crashes here, needs another solution // TODO: Remove knife crashes here, needs another solution
/*if (isKnife && g_playersKnife[(int)player.EntityIndex!.Value.Value] != "weapon_knife" && (weapon.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.AttributeManager.Item.ItemDefinitionIndex == 59)) /*if (isKnife && g_playersKnife[(int)player.EntityIndex!.Value.Value] != "weapon_knife" && (weapon.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.AttributeManager.Item.ItemDefinitionIndex == 59))
{ {
@@ -410,11 +444,15 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
private void ChangeWeaponAttributes(CBasePlayerWeapon? weapon, CCSPlayerController? player, bool isKnife = false) private void ChangeWeaponAttributes(CBasePlayerWeapon? weapon, CCSPlayerController? player, bool isKnife = false)
{ {
if (weapon == null || !weapon.IsValid || player == null || player.IsBot) return; if (weapon == null || !weapon.IsValid || !Utility.IsPlayerValid(player)) return;
int playerIndex = (int)player.EntityIndex!.Value.Value; int playerIndex = (int)player!.EntityIndex!.Value.Value;
if (!gPlayerWeaponPaints.ContainsKey(playerIndex)) return;
if (Config.Additional.GiveRandomSkin && !gPlayerWeaponPaints[playerIndex].ContainsKey(weapon.AttributeManager.Item.ItemDefinitionIndex)) if (isKnife && !g_playersKnife.ContainsKey(playerIndex) || isKnife && g_playersKnife[playerIndex] == "weapon_knife") return;
if (Config.Additional.GiveRandomSkin &&
!gPlayerWeaponPaints[playerIndex].ContainsKey(weapon.AttributeManager.Item.ItemDefinitionIndex))
{ {
// Random skins // Random skins
weapon.AttributeManager.Item.ItemID = 16384; weapon.AttributeManager.Item.ItemID = 16384;
@@ -467,10 +505,13 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
else if (Config.Additional.GiveRandomKnife) else if (Config.Additional.GiveRandomKnife)
{ {
Random random = new Random(); var knifeTypes = weaponList.Where(pair => pair.Key.StartsWith("weapon_knife") || pair.Key.StartsWith("weapon_bayonet")).ToDictionary(pair => pair.Key, pair => pair.Value);
Random random = new();
int index = random.Next(knifeTypes.Count); int index = random.Next(knifeTypes.Count);
var randomKnife = knifeTypes.Values.ElementAt(index); var randomKnifeClass = knifeTypes.Keys.ElementAt(index);
player.GiveNamedItem(randomKnife);
player.GiveNamedItem(randomKnifeClass);
} }
else else
{ {
@@ -481,18 +522,29 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
private void RemoveKnifeFromPlayer(CCSPlayerController? player) private void RemoveKnifeFromPlayer(CCSPlayerController? player)
{ {
if (player == null || !player.IsValid || !player.PawnIsAlive) return; if (player == null || !player.IsValid || !player.PawnIsAlive) return;
var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons; if (player.PlayerPawn.Value.WeaponServices == null || player.PlayerPawn.Value.ItemServices == null) return;
var weapons = player.PlayerPawn.Value.WeaponServices.MyWeapons;
if (weapons != null && weapons.Count > 0) if (weapons != null && weapons.Count > 0)
{ {
CCSPlayer_ItemServices service = new CCSPlayer_ItemServices(player.PlayerPawn.Value.ItemServices.Handle);
var dropWeapon = VirtualFunction.CreateVoid<nint, nint>(service.Handle, GameData.GetOffset("CCSPlayer_ItemServices_DropActivePlayerWeapon"));
foreach (var weapon in weapons) foreach (var weapon in weapons)
{ {
if (weapon.IsValid && weapon.Value.IsValid) if (weapon != null && weapon.IsValid && weapon.Value.IsValid)
{ {
//if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59) //if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59)
if (weapon.Value.DesignerName.Contains("knife")) if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet"))
{ {
weapon.Value.Remove(); NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3");
return; AddTimer(0.2f, () =>
{
CEntityInstance knife = new(weapon.Value.Handle);
dropWeapon(service.Handle, weapon.Handle);
AddTimer(0.3f, () => knife.Remove());
});
break;
} }
} }
} }
@@ -522,7 +574,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
*/ */
private void RefreshSkins(CCSPlayerController? player) private void RefreshSkins(CCSPlayerController? player)
{ {
if (player == null || !player.IsValid || player.IsBot || !player.PawnIsAlive) return; if (!Utility.IsPlayerValid(player) || !player!.PawnIsAlive) return;
AddTimer(0.18f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3")); AddTimer(0.18f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3"));
AddTimer(0.25f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot2")); AddTimer(0.25f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot2"));
@@ -541,9 +593,9 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
if (weapons == null || weapons.Count <= 0) return false; if (weapons == null || weapons.Count <= 0) return false;
foreach (var weapon in weapons) foreach (var weapon in weapons)
{ {
if (weapon.IsValid && weapon.Value.IsValid) if (weapon != null && weapon.IsValid && weapon.Value.IsValid)
{ {
if (weapon.Value.DesignerName.Contains("knife")) if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet"))
{ {
return true; return true;
} }
@@ -554,60 +606,84 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
private void SetupKnifeMenu() private void SetupKnifeMenu()
{ {
if (!Config.Additional.KnifeEnabled) return; if (!Config.Additional.KnifeEnabled) return;
var giveItemMenu = new ChatMenu(ReplaceTags(Config.Messages.KnifeMenuTitle));
var knivesOnly = weaponList
.Where(pair => pair.Key.StartsWith("weapon_knife") || pair.Key.StartsWith("weapon_bayonet"))
.ToDictionary(pair => pair.Key, pair => pair.Value);
var giveItemMenu = new ChatMenu(Utility.ReplaceTags($" {Config.Messages.KnifeMenuTitle}"));
var handleGive = (CCSPlayerController? player, ChatMenuOption option) => var handleGive = (CCSPlayerController? player, ChatMenuOption option) =>
{ {
if (player != null && player.IsValid) if (Utility.IsPlayerValid(player))
{ {
string temp = ""; var knifeName = option.Text;
if (knifeTypes.TryGetValue(option.Text, out var knife)) var knifeKey = knivesOnly.FirstOrDefault(x => x.Value == knifeName).Key;
if (!string.IsNullOrEmpty(knifeKey))
{ {
g_playersKnife[(int)player.EntityIndex!.Value.Value] = knifeTypes[option.Text]; string temp = "";
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenu)) if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenu))
{ {
temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenu}".Replace("{KNIFE}", option.Text); temp = $" {Config.Prefix} {Config.Messages.ChosenKnifeMenu}".Replace("{KNIFE}", knifeName);
player.PrintToChat(ReplaceTags(temp)); player!.PrintToChat(Utility.ReplaceTags(temp));
} }
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenuKill) && Config.Additional.CommandKillEnabled) if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenuKill) && Config.Additional.CommandKillEnabled)
{ {
temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenuKill}"; temp = $" {Config.Prefix} {Config.Messages.ChosenKnifeMenuKill}";
player.PrintToChat(ReplaceTags(temp)); player!.PrintToChat(Utility.ReplaceTags(temp));
} }
if (player.PawnIsAlive)
{
RemoveKnifeFromPlayer(player);
AddTimer(0.2f, () =>
{
GiveKnifeToPlayer(player);
});
}
Task.Run(() => SyncKnifeToDatabase((int)player.EntityIndex!.Value.Value, knife));
/* Old way g_playersKnife[(int)player!.EntityIndex!.Value.Value] = knifeKey;
RemoveKnifeFromPlayer(player);
AddTimer(0.1f, () => GiveKnifeToPlayer(player)); if (player!.PawnIsAlive)
*/ {
if (PlayerHasKnife(player))
RemoveKnifeFromPlayer(player);
g_changedKnife.Add((int)player.EntityIndex!.Value.Value);
GiveKnifeToPlayer(player);
}
Task.Run(() => SyncKnifeToDatabase((int)player.EntityIndex!.Value.Value, knifeKey));
} }
} }
}; };
foreach (var knife in knifeTypes) foreach (var knifePair in knivesOnly)
{ {
giveItemMenu.AddMenuOption(knife.Key, handleGive); giveItemMenu.AddMenuOption(knifePair.Value, handleGive);
} }
AddCommand($"css_{Config.Additional.CommandKnife}", "Knife Menu", (player, info) => { if (player == null) return; ChatMenus.OpenMenu(player, giveItemMenu); }); AddCommand($"css_{Config.Additional.CommandKnife}", "Knife Menu", (player, info) =>
{
if (!Utility.IsPlayerValid(player)) return;
int playerIndex = (int)player!.EntityIndex!.Value.Value;
if (commandCooldown != null && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds) && playerIndex > 0 && playerIndex < commandCooldown.Length)
{
commandCooldown[playerIndex] = DateTime.UtcNow;
ChatMenus.OpenMenu(player, giveItemMenu);
return;
}
if (!string.IsNullOrEmpty(Config.Messages.CooldownRefreshCommand))
{
string temp = $" {Config.Prefix} {Config.Messages.CooldownRefreshCommand}";
player.PrintToChat(Utility.ReplaceTags(temp));
}
});
} }
private void SetupSkinsMenu() private void SetupSkinsMenu()
{ {
var classNamesByWeapon = weaponList.ToDictionary(kvp => kvp.Value, kvp => kvp.Key); var classNamesByWeapon = weaponList.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
var weaponSelectionMenu = new ChatMenu(ReplaceTags(Config.Messages.WeaponMenuTitle)); var weaponSelectionMenu = new ChatMenu(Utility.ReplaceTags($" {Config.Messages.WeaponMenuTitle}"));
// Function to handle skin selection for a specific weapon // Function to handle skin selection for a specific weapon
var handleWeaponSelection = (CCSPlayerController? player, ChatMenuOption option) => var handleWeaponSelection = (CCSPlayerController? player, ChatMenuOption option) =>
{ {
if (player == null || !player.IsValid) return; if (!Utility.IsPlayerValid(player)) return;
int playerIndex = (int)player.EntityIndex!.Value.Value; int playerIndex = (int)player!.EntityIndex!.Value.Value;
string selectedWeapon = option.Text; string selectedWeapon = option.Text;
if (classNamesByWeapon.TryGetValue(selectedWeapon, out string? selectedWeaponClassname)) if (classNamesByWeapon.TryGetValue(selectedWeapon, out string? selectedWeaponClassname))
{ {
@@ -618,7 +694,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
weaponName?.ToString() == selectedWeaponClassname weaponName?.ToString() == selectedWeaponClassname
)?.ToList(); )?.ToList();
var skinSubMenu = new ChatMenu(ReplaceTags(Config.Messages.SkinMenuTitle).Replace("{WEAPON}", selectedWeapon)); var skinSubMenu = new ChatMenu(Utility.ReplaceTags($" {Config.Messages.SkinMenuTitle}").Replace("{WEAPON}", selectedWeapon));
// Function to handle skin selection for the chosen weapon // Function to handle skin selection for the chosen weapon
var handleSkinSelection = (CCSPlayerController? p, ChatMenuOption opt) => var handleSkinSelection = (CCSPlayerController? p, ChatMenuOption opt) =>
@@ -643,8 +719,8 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
int.TryParse(weaponDefIndexObj.ToString(), out var weaponDefIndex) && int.TryParse(weaponDefIndexObj.ToString(), out var weaponDefIndex) &&
int.TryParse(selectedPaintID, out var paintID)) int.TryParse(selectedPaintID, out var paintID))
{ {
string temp = $"{Config.Prefix} {Config.Messages.ChosenSkinMenu}".Replace("{SKIN}", selectedSkin); string temp = $" {Config.Prefix} {Config.Messages.ChosenSkinMenu}".Replace("{SKIN}", selectedSkin);
p.PrintToChat(ReplaceTags(temp)); p.PrintToChat(Utility.ReplaceTags(temp));
gPlayerWeaponPaints[playerIndex][weaponDefIndex] = paintID; gPlayerWeaponPaints[playerIndex][weaponDefIndex] = paintID;
gPlayerWeaponWear[playerIndex][weaponDefIndex] = 0.0f; gPlayerWeaponWear[playerIndex][weaponDefIndex] = 0.0f;
gPlayerWeaponSeed[playerIndex][weaponDefIndex] = 0; gPlayerWeaponSeed[playerIndex][weaponDefIndex] = 0;
@@ -685,20 +761,13 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
string weaponName = weaponList[weaponClass]; string weaponName = weaponList[weaponClass];
weaponSelectionMenu.AddMenuOption(weaponName, handleWeaponSelection); weaponSelectionMenu.AddMenuOption(weaponName, handleWeaponSelection);
} }
foreach (var knifeClass in knifeTypes.Keys)
{
string knifeName = knifeTypes[knifeClass];
weaponSelectionMenu.AddMenuOption(knifeName, handleWeaponSelection);
}
// Command to open the weapon selection menu for players // Command to open the weapon selection menu for players
AddCommand($"css_{Config.Additional.CommandSkinSelection}", "Skins selection menu", (player, info) => AddCommand($"css_{Config.Additional.CommandSkinSelection}", "Skins selection menu", (player, info) =>
{ {
if (player == null || !player.IsValid) return; if (!Utility.IsPlayerValid(player)) return;
int playerIndex = (int)player.EntityIndex!.Value.Value; int playerIndex = (int)player!.EntityIndex!.Value.Value;
if (commandCooldown != null && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds)) if (commandCooldown != null && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds) && playerIndex > 0 && playerIndex < commandCooldown.Length)
{ {
commandCooldown[playerIndex] = DateTime.UtcNow; commandCooldown[playerIndex] = DateTime.UtcNow;
ChatMenus.OpenMenu(player, weaponSelectionMenu); ChatMenus.OpenMenu(player, weaponSelectionMenu);
@@ -707,26 +776,29 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
if (!string.IsNullOrEmpty(Config.Messages.CooldownRefreshCommand)) if (!string.IsNullOrEmpty(Config.Messages.CooldownRefreshCommand))
{ {
string temp = $"{Config.Prefix} {Config.Messages.CooldownRefreshCommand}"; string temp = $"{Config.Prefix} {Config.Messages.CooldownRefreshCommand}";
player.PrintToChat(ReplaceTags(temp)); player.PrintToChat(Utility.ReplaceTags(temp));
} }
}); });
} }
// [ConsoleCommand($"css_{Config.Additional.CommandRefresh}", "refreshskins")] // [ConsoleCommand($"css_{Config.Additional.CommandRefresh}", "refreshskins")]
private void OnCommandRefresh(CCSPlayerController? player, CommandInfo command) private void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
{ {
if (!Config.Additional.CommandWpEnabled || !Config.Additional.SkinEnabled) return; if (!Config.Additional.CommandWpEnabled || !Config.Additional.SkinEnabled) return;
if (player == null || !player.IsValid || player.IsBot) return; if (!Utility.IsPlayerValid(player)) return;
string temp = ""; string temp = "";
int playerIndex = (int)player.EntityIndex!.Value.Value; int playerIndex = (int)player!.EntityIndex!.Value.Value;
if (commandCooldown != null && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds)) if (commandCooldown != null && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds))
{ {
commandCooldown[playerIndex] = DateTime.UtcNow; commandCooldown[playerIndex] = DateTime.UtcNow;
Task.Run(async () => await GetWeaponPaintsFromDatabase(playerIndex)); Task.Run(async () => await GetWeaponPaintsFromDatabase(playerIndex));
if (Config.Additional.KnifeEnabled) if (Config.Additional.KnifeEnabled)
{ {
RemoveKnifeFromPlayer(player); if (PlayerHasKnife(player))
AddTimer(0.2f, () => RemoveKnifeFromPlayer(player);
AddTimer(0.3f, () =>
{ {
GiveKnifeToPlayer(player); GiveKnifeToPlayer(player);
}); });
@@ -739,40 +811,40 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
if (!string.IsNullOrEmpty(Config.Messages.SuccessRefreshCommand)) if (!string.IsNullOrEmpty(Config.Messages.SuccessRefreshCommand))
{ {
temp = $"{Config.Prefix} {Config.Messages.SuccessRefreshCommand}"; temp = $" {Config.Prefix} {Config.Messages.SuccessRefreshCommand}";
player.PrintToChat(ReplaceTags(temp)); player.PrintToChat(Utility.ReplaceTags(temp));
} }
return; return;
} }
if (!string.IsNullOrEmpty(Config.Messages.CooldownRefreshCommand)) if (!string.IsNullOrEmpty(Config.Messages.CooldownRefreshCommand))
{ {
temp = $"{Config.Prefix} {Config.Messages.CooldownRefreshCommand}"; temp = $" {Config.Prefix} {Config.Messages.CooldownRefreshCommand}";
player.PrintToChat(ReplaceTags(temp)); player.PrintToChat(Utility.ReplaceTags(temp));
} }
} }
// [ConsoleCommand($"css_{Config.Additional.CommandSkin}", "weaponskins")] // [ConsoleCommand($"css_{Config.Additional.CommandSkin}", "weaponskins")]
private void OnCommandWS(CCSPlayerController? player, CommandInfo command) private void OnCommandWS(CCSPlayerController? player, CommandInfo command)
{ {
if (!Config.Additional.SkinEnabled) return; if (!Config.Additional.SkinEnabled) return;
if (player == null) return; if (!Utility.IsPlayerValid(player)) return;
string temp = ""; string temp = "";
if (!string.IsNullOrEmpty(Config.Messages.WebsiteMessageCommand)) if (!string.IsNullOrEmpty(Config.Messages.WebsiteMessageCommand))
{ {
temp = $"{Config.Prefix} {Config.Messages.WebsiteMessageCommand}"; temp = $" {Config.Prefix} {Config.Messages.WebsiteMessageCommand}";
player.PrintToChat(ReplaceTags(temp)); player!.PrintToChat(Utility.ReplaceTags(temp));
} }
if (!string.IsNullOrEmpty(Config.Messages.SynchronizeMessageCommand)) if (!string.IsNullOrEmpty(Config.Messages.SynchronizeMessageCommand))
{ {
temp = $"{Config.Prefix} {Config.Messages.SynchronizeMessageCommand}"; temp = $" {Config.Prefix} {Config.Messages.SynchronizeMessageCommand}";
player.PrintToChat(ReplaceTags(temp)); player!.PrintToChat(Utility.ReplaceTags(temp));
} }
if (!Config.Additional.KnifeEnabled) return; if (!Config.Additional.KnifeEnabled) return;
if (!string.IsNullOrEmpty(Config.Messages.KnifeMessageCommand)) if (!string.IsNullOrEmpty(Config.Messages.KnifeMessageCommand))
{ {
temp = $"{Config.Prefix} {Config.Messages.KnifeMessageCommand}"; temp = $" {Config.Prefix} {Config.Messages.KnifeMessageCommand}";
player.PrintToChat(ReplaceTags(temp)); player!.PrintToChat(Utility.ReplaceTags(temp));
} }
} }
private static CSkeletonInstance GetSkeletonInstance(CGameSceneNode node) private static CSkeletonInstance GetSkeletonInstance(CGameSceneNode node)
@@ -785,7 +857,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
if (!Config.Additional.SkinEnabled) return; if (!Config.Additional.SkinEnabled) return;
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
if (player == null || !player.IsValid || player.IsBot) return; if (!Utility.IsPlayerValid(player)) return;
var steamId = new SteamID(player.SteamID); var steamId = new SteamID(player.SteamID);
@@ -873,7 +945,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
catch (Exception e) catch (Exception e)
{ {
Log(e.Message); Utility.Log(e.Message);
return; return;
} }
} }
@@ -883,7 +955,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
try try
{ {
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
if (player == null || !player.IsValid || player.IsBot) return; if (!Utility.IsPlayerValid(player)) return;
var steamId = new SteamID(player.SteamID); var steamId = new SteamID(player.SteamID);
if (Config.GlobalShare) if (Config.GlobalShare)
@@ -944,7 +1016,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
catch (Exception e) catch (Exception e)
{ {
Log(e.Message); Utility.Log(e.Message);
return; return;
} }
} }
@@ -965,16 +1037,16 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
catch (Exception e) catch (Exception e)
{ {
Log(e.Message); Utility.Log(e.Message);
return; return;
} }
} }
private async Task SyncWeaponPaintsToDatabase(CCSPlayerController? player) private async Task SyncWeaponPaintsToDatabase(CCSPlayerController? player)
{ {
if (player == null || !player.IsValid || player.IsBot) return; if (!Utility.IsPlayerValid(player)) return;
int playerIndex = (int)player.EntityIndex!.Value.Value; int playerIndex = (int)player!.EntityIndex!.Value.Value;
string steamId = new SteamID(player.SteamID).SteamId64.ToString(); string steamId = new SteamID(player.SteamID).SteamId64.ToString();
@@ -986,7 +1058,6 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
foreach (var weaponDefIndex in gPlayerWeaponPaints[playerIndex].Keys) foreach (var weaponDefIndex in gPlayerWeaponPaints[playerIndex].Keys)
{ {
Console.WriteLine("WeaponDEFINDEX : " + weaponDefIndex);
int paintId = gPlayerWeaponPaints[playerIndex][weaponDefIndex]; int paintId = gPlayerWeaponPaints[playerIndex][weaponDefIndex];
float wear = gPlayerWeaponWear.TryGetValue(playerIndex, out var wearDictionary) float wear = gPlayerWeaponWear.TryGetValue(playerIndex, out var wearDictionary)
&& wearDictionary.TryGetValue(weaponDefIndex, out var retrievedWear) && wearDictionary.TryGetValue(weaponDefIndex, out var retrievedWear)
@@ -1018,26 +1089,6 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
await connection.CloseAsync(); await connection.CloseAsync();
} }
private string ReplaceTags(string message)
{
if (message.Contains('{'))
{
string modifiedValue = message;
modifiedValue = modifiedValue.Replace("{WEBSITE}", Config.Website);
foreach (FieldInfo field in typeof(ChatColors).GetFields())
{
string pattern = $"{{{field.Name}}}";
if (message.Contains(pattern, StringComparison.OrdinalIgnoreCase))
{
modifiedValue = modifiedValue.Replace(pattern, field.GetValue(null)!.ToString(), StringComparison.OrdinalIgnoreCase);
}
}
return modifiedValue;
}
return message;
}
private static int GetRandomPaint(int defindex) private static int GetRandomPaint(int defindex)
{ {
Random rnd = new Random(); Random rnd = new Random();
@@ -1077,27 +1128,4 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
throw new FileNotFoundException("File not found.", filePath); throw new FileNotFoundException("File not found.", filePath);
} }
} }
private static void Log(string message)
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("[WeaponPaints] " + message);
Console.ResetColor();
}
private void ShowAd()
{
Console.WriteLine(" ");
Console.WriteLine(" _ _ _______ _______ _______ _______ __ _ _______ _______ ___ __ _ _______ _______ ");
Console.WriteLine("| | _ | || || _ || || || | | || || _ || | | | | || || |");
Console.WriteLine("| || || || ___|| |_| || _ || _ || |_| || _ || |_| || | | |_| ||_ _|| _____|");
Console.WriteLine("| || |___ | || |_| || | | || || |_| || || | | | | | | |_____ ");
Console.WriteLine("| || ___|| || ___|| |_| || _ || ___|| || | | _ | | | |_____ |");
Console.WriteLine("| _ || |___ | _ || | | || | | || | | _ || | | | | | | | _____| |");
Console.WriteLine("|__| |__||_______||__| |__||___| |_______||_| |__||___| |__| |__||___| |_| |__| |___| |_______|");
Console.WriteLine(" >> Version: " + ModuleVersion);
Console.WriteLine(" >> GitHub: https://github.com/Nereziel/cs2-WeaponPaints");
Console.WriteLine(" ");
}
} }