Compare commits

...

8 Commits

Author SHA1 Message Date
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
Nereziel
e1f24abd5d removed exception 2023-11-25 13:10:52 +01:00
Nereziel
646050fb72 Merge pull request #64 from daffyyyy/fix-knife-2
Still knife fix :D (Previous sometimes give random knife to player)
2023-11-24 08:11:37 +01:00
daffyyyy
87dadb9c62 Update WeaponPaints.cs 2023-11-23 01:32:07 +01:00
Nereziel
cf421c5614 Merge pull request #63 from daffyyyy/fix-knife-2
Attempting to repair knives when a map or other plugin was giving them
2023-11-23 00:33:16 +01:00
daffyyyy
fc64e1d261 Additional checks 2023-11-23 00:21:04 +01:00
2 changed files with 259 additions and 192 deletions

55
Utility.cs Normal file
View File

@@ -0,0 +1,55 @@
using CounterStrikeSharp.API.Modules.Utils;
using System.Reflection;
namespace WeaponPaints
{
public static class Utility
{
public static WeaponPaintsConfig? Config { get; set; }
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 ModuleDescription => "Skin and knife selector, standalone and web-based";
public override string ModuleAuthor => "Nereziel & daffyy";
public override string ModuleVersion => "1.2";
public override string ModuleVersion => "1.2a";
public WeaponPaintsConfig Config { get; set; } = new();
private string DatabaseConnectionString = string.Empty;
@@ -37,19 +37,6 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
private Dictionary<int, string> g_playersKnife = new();
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()
{
{"weapon_deagle", "Desert Eagle"},
@@ -86,12 +73,30 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{"weapon_usp_silencer", "USP-S"},
{"weapon_cz75a", "CZ75-Auto"},
{"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)
{
SetGlobalExceptionHandler();
if (!Config.GlobalShare)
{
BuildDatabaseConnectionString();
@@ -106,6 +111,8 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
RegisterEventHandler<EventRoundStart>(OnRoundStart, HookMode.Pre);
RegisterEventHandler<EventItemPickup>(OnItemPickup, HookMode.Pre);
OnMapStart(string.Empty);
if (hotReload)
{
OnMapStart(string.Empty);
@@ -113,10 +120,11 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{
for (int i = 1; i <= Server.MaxPlayers; i++)
{
if (Config.Additional.KnifeEnabled)
await GetKnifeFromDatabase(i);
if (Config.Additional.SkinEnabled)
await GetWeaponPaintsFromDatabase(i);
if (Config.Additional.KnifeEnabled)
await GetKnifeFromDatabase(i);
}
});
}
@@ -141,8 +149,8 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
}
Config = config;
ShowAd();
Utility.Config = config;
Utility.ShowAd(ModuleVersion);
}
private void BuildDatabaseConnectionString()
@@ -218,36 +226,31 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
*/
public override void Unload(bool hotReload)
{
RemoveGlobalExceptionHandler();
base.Unload(hotReload);
}
private void GlobalExceptionHandler(object? sender, FirstChanceExceptionEventArgs @event)
{
Log(@event.Exception.ToString());
}
private void SetGlobalExceptionHandler()
{
AppDomain.CurrentDomain.FirstChanceException += this.GlobalExceptionHandler;
}
private void RemoveGlobalExceptionHandler()
{
AppDomain.CurrentDomain.FirstChanceException -= this.GlobalExceptionHandler;
}
private void RegisterCommands()
{
AddCommand($"css_{Config.Additional.CommandSkin}", "Skins info", (player, info) => { if (player == null) return; OnCommandWS(player, info); });
AddCommand($"css_{Config.Additional.CommandRefresh}", "Skins refresh", (player, info) => { if (player == null) return; OnCommandRefresh(player, info); });
AddCommand($"css_{Config.Additional.CommandSkin}", "Skins info", (player, info) =>
{
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
OnCommandWS(player, info);
});
AddCommand($"css_{Config.Additional.CommandRefresh}", "Skins refresh", (player, info) =>
{
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
OnCommandRefresh(player, info);
});
if (Config.Additional.CommandKillEnabled)
{
AddCommand($"css_{Config.Additional.CommandKill}", "kill yourself", (player, info) =>
{
if (player == null || !player.IsValid || !player.PlayerPawn.IsValid)
return;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || !player.PlayerPawn.IsValid) return;
player.PlayerPawn.Value.CommitSuicide(true, false);
});
}
}
private void IncompatibilityCheck()
{
// MatchZy
@@ -320,10 +323,8 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
}
private void OnClientDisconnect(int playerSlot)
{
int playerIndex = playerSlot + 1;
CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot);
if (player == null || !player.IsValid || player.IsBot) return;
// TODO: Clean up after player
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
if (Config.Additional.KnifeEnabled)
g_playersKnife.Remove((int)player.EntityIndex!.Value.Value);
if (Config.Additional.SkinEnabled)
@@ -344,12 +345,18 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
GiveKnifeToPlayer(player);
}
if (Config.Additional.SkinVisibilityFix)
{
AddTimer(0.3f, () => RefreshSkins(player));
}
return HookResult.Continue;
}
private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
/*
if (!IsMatchZy) return HookResult.Continue;
*/
NativeAPI.IssueServerCommand("mp_t_default_melee \"\"");
NativeAPI.IssueServerCommand("mp_ct_default_melee \"\"");
@@ -360,15 +367,24 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{
if (@event.Defindex == 42 || @event.Defindex == 59)
{
CCSPlayerController player = @event.Userid;
CCSPlayerController? player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || !player.PawnIsAlive) return HookResult.Continue;
if (player.IsValid && !player.IsBot && @event.Item == "knife")
if (g_playersKnife.ContainsKey((int)player.EntityIndex!.Value.Value)
&&
g_playersKnife[(int)player.EntityIndex!.Value.Value] != "weapon_knife")
{
if (g_playersKnife.ContainsKey((int)player.EntityIndex!.Value.Value)
&&
g_playersKnife[(int)player.EntityIndex!.Value.Value] != "weapon_knife")
RemoveKnifeFromPlayer(player);
AddTimer(0.3f, () =>
{
RefreshPlayerKnife(player, true);
if (!PlayerHasKnife(player))
GiveKnifeToPlayer(player);
});
if (Config.Additional.SkinVisibilityFix)
{
AddTimer(0.25f, () => RefreshSkins(player));
}
}
}
@@ -399,7 +415,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
if (!pawn.IsValid) return;
var playerIndex = (int)pawn.Controller.Value.EntityIndex!.Value.Value;
var player = Utilities.GetPlayerFromIndex(playerIndex);
if (player == null || !player.IsValid || player.IsBot) return;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
// 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))
{
@@ -413,11 +429,15 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
}
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 || player == null || player.IsBot || player.IsHLTV) return;
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
weapon.AttributeManager.Item.ItemID = 16384;
@@ -462,74 +482,73 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
}
private void GiveKnifeToPlayer(CCSPlayerController? player)
{
if (!Config.Additional.KnifeEnabled) return;
if (player == null || !player.IsValid) return;
if (PlayerHasKnife(player)) return;
if (!Config.Additional.KnifeEnabled || player == null || !player.IsValid) return;
if (g_playersKnife.TryGetValue((int)player.EntityIndex!.Value.Value, out var knife))
{
player.GiveNamedItem(knife);
}
else if (Config.Additional.GiveRandomKnife)
{
var knifeTypes = weaponList.Where(pair => pair.Key.StartsWith("weapon_knife")).ToDictionary(pair => pair.Key, pair => pair.Value);
Random random = new();
int index = random.Next(knifeTypes.Count);
var randomKnifeClass = knifeTypes.Keys.ElementAt(index);
player.GiveNamedItem(randomKnifeClass);
}
else
{
if (Config.Additional.GiveRandomKnife)
{
Random random = new Random();
int index = random.Next(knifeTypes.Count);
player.GiveNamedItem(knifeTypes.Values.ElementAt(index));
}
else
{
player.GiveNamedItem((CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife");
}
var defaultKnife = (CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife";
player.GiveNamedItem(defaultKnife);
}
}
private void RemoveKnifeFromPlayer(CCSPlayerController? player)
{
if (player == null || !player.IsValid || !player.PawnIsAlive) return;
var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
foreach (var weapon in weapons)
if (weapons != null && weapons.Count > 0)
{
if (weapon.IsValid && weapon.Value.IsValid)
foreach (var weapon in weapons)
{
//if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59)
if (weapon.Value.DesignerName.Contains("knife"))
if (weapon.IsValid && weapon.Value.IsValid)
{
weapon.Value.Remove();
return;
//if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59)
if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet"))
{
weapon.Value.Remove();
return;
}
}
}
}
}
/* Causing crashes
private void RefreshPlayerKnife(CCSPlayerController? player, bool remove = false)
{
if (player == null || !player.IsValid || !player.PawnIsAlive) return;
if (player == null || !player.IsValid || player.IsBot || !player.PawnIsAlive) return;
if (remove == true)
AddTimer(0.1f, () =>
{
AddTimer(0.1f, () =>
if (remove == true)
{
if (PlayerHasKnife(player))
RemoveKnifeFromPlayer(player);
});
}
}
AddTimer(0.3f, () =>
{
if (!PlayerHasKnife(player))
GiveKnifeToPlayer(player);
GiveKnifeToPlayer(player);
});
if (Config.Additional.SkinVisibilityFix)
{
AddTimer(0.3f, () => RefreshSkins(player));
AddTimer(0.25f, () => RefreshSkins(player));
}
}
*/
private void RefreshSkins(CCSPlayerController? player)
{
if (player == null || !player.IsValid || !player.PawnIsAlive) return;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || !player.PawnIsAlive) return;
AddTimer(0.18f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3"));
AddTimer(0.25f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot2"));
@@ -545,12 +564,12 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
}
var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
if (weapons == null) return false;
if (weapons == null || weapons.Count <= 0) return false;
foreach (var weapon in weapons)
{
if (weapon.IsValid && weapon.Value.IsValid)
{
if (weapon.Value.DesignerName.Contains("knife"))
if (weapon.Value.DesignerName.Contains("knife") || weapon.Value.DesignerName.Contains("bayonet"))
{
return true;
}
@@ -561,51 +580,83 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
private void SetupKnifeMenu()
{
if (!Config.Additional.KnifeEnabled) return;
var giveItemMenu = new ChatMenu(ReplaceTags(Config.Messages.KnifeMenuTitle));
var handleGive = (CCSPlayerController player, ChatMenuOption option) =>
{
string temp = "";
if (knifeTypes.TryGetValue(option.Text, out var knife))
{
g_playersKnife[(int)player.EntityIndex!.Value.Value] = knifeTypes[option.Text];
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenu))
{
temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenu}".Replace("{KNIFE}", option.Text);
player.PrintToChat(ReplaceTags(temp));
}
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenuKill) && Config.Additional.CommandKillEnabled)
{
temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenuKill}";
player.PrintToChat(ReplaceTags(temp));
}
if (player.PawnIsAlive)
{
RemoveKnifeFromPlayer(player);
RefreshPlayerKnife(player);
}
Task.Run(() => SyncKnifeToDatabase((int)player.EntityIndex!.Value.Value, knife));
/* Old way
RemoveKnifeFromPlayer(player);
AddTimer(0.1f, () => GiveKnifeToPlayer(player));
*/
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) =>
{
if (player != null && player.IsValid && !player.IsBot && !player.IsHLTV)
{
var knifeName = option.Text;
var knifeKey = knivesOnly.FirstOrDefault(x => x.Value == knifeName).Key;
if (!string.IsNullOrEmpty(knifeKey))
{
string temp = "";
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenu))
{
temp = $" {Config.Prefix} {Config.Messages.ChosenKnifeMenu}".Replace("{KNIFE}", knifeName);
player.PrintToChat(Utility.ReplaceTags(temp));
}
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenuKill) && Config.Additional.CommandKillEnabled)
{
temp = $" {Config.Prefix} {Config.Messages.ChosenKnifeMenuKill}";
player.PrintToChat(Utility.ReplaceTags(temp));
}
g_playersKnife[(int)player.EntityIndex!.Value.Value] = knifeKey;
if (player.PawnIsAlive)
{
RemoveKnifeFromPlayer(player);
AddTimer(0.5f, () =>
{
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 (player == null || !player.IsValid || player.IsBot || player.IsHLTV) 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()
{
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
var handleWeaponSelection = (CCSPlayerController player, ChatMenuOption option) =>
var handleWeaponSelection = (CCSPlayerController? player, ChatMenuOption option) =>
{
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
int playerIndex = (int)player.EntityIndex!.Value.Value;
string selectedWeapon = option.Text;
if (classNamesByWeapon.TryGetValue(selectedWeapon, out string? selectedWeaponClassname))
@@ -617,11 +668,13 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
weaponName?.ToString() == selectedWeaponClassname
)?.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
var handleSkinSelection = (CCSPlayerController p, ChatMenuOption opt) =>
var handleSkinSelection = (CCSPlayerController? p, ChatMenuOption opt) =>
{
if (p == null || !p.IsValid) return;
var steamId = new SteamID(player.SteamID);
var firstSkin = skinsList?.FirstOrDefault(skin =>
{
@@ -640,8 +693,8 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
int.TryParse(weaponDefIndexObj.ToString(), out var weaponDefIndex) &&
int.TryParse(selectedPaintID, out var paintID))
{
string temp = $"{Config.Prefix} {Config.Messages.ChosenSkinMenu}".Replace("{SKIN}", selectedSkin);
p.PrintToChat(ReplaceTags(temp));
string temp = $" {Config.Prefix} {Config.Messages.ChosenSkinMenu}".Replace("{SKIN}", selectedSkin);
p.PrintToChat(Utility.ReplaceTags(temp));
gPlayerWeaponPaints[playerIndex][weaponDefIndex] = paintID;
gPlayerWeaponWear[playerIndex][weaponDefIndex] = 0.0f;
gPlayerWeaponSeed[playerIndex][weaponDefIndex] = 0;
@@ -682,20 +735,13 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
string weaponName = weaponList[weaponClass];
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
AddCommand($"css_{Config.Additional.CommandSkinSelection}", "Skins selection menu", (player, info) =>
{
if (player == null || !player.IsValid) return;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
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;
ChatMenus.OpenMenu(player, weaponSelectionMenu);
@@ -704,16 +750,18 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
if (!string.IsNullOrEmpty(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")]
private void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
{
if (!Config.Additional.CommandWpEnabled || !Config.Additional.SkinEnabled) return;
if (player == null) return;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
string temp = "";
int playerIndex = (int)player.EntityIndex!.Value.Value;
if (commandCooldown != null && DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds))
@@ -722,46 +770,54 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
Task.Run(async () => await GetWeaponPaintsFromDatabase(playerIndex));
if (Config.Additional.KnifeEnabled)
{
RemoveKnifeFromPlayer(player);
AddTimer(0.3f, () =>
{
GiveKnifeToPlayer(player);
});
Task.Run(async () => await GetKnifeFromDatabase(playerIndex));
/*
RemoveKnifeFromPlayer(player);
AddTimer(0.2f, () => GiveKnifeToPlayer(player));
*/
}
if (!string.IsNullOrEmpty(Config.Messages.SuccessRefreshCommand))
{
temp = $"{Config.Prefix} {Config.Messages.SuccessRefreshCommand}";
player.PrintToChat(ReplaceTags(temp));
temp = $" {Config.Prefix} {Config.Messages.SuccessRefreshCommand}";
player.PrintToChat(Utility.ReplaceTags(temp));
}
return;
}
if (!string.IsNullOrEmpty(Config.Messages.CooldownRefreshCommand))
{
temp = $"{Config.Prefix} {Config.Messages.CooldownRefreshCommand}";
player.PrintToChat(ReplaceTags(temp));
temp = $" {Config.Prefix} {Config.Messages.CooldownRefreshCommand}";
player.PrintToChat(Utility.ReplaceTags(temp));
}
}
// [ConsoleCommand($"css_{Config.Additional.CommandSkin}", "weaponskins")]
private void OnCommandWS(CCSPlayerController? player, CommandInfo command)
{
if (!Config.Additional.SkinEnabled) return;
if (player == null) return;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
string temp = "";
if (!string.IsNullOrEmpty(Config.Messages.WebsiteMessageCommand))
{
temp = $"{Config.Prefix} {Config.Messages.WebsiteMessageCommand}";
player.PrintToChat(ReplaceTags(temp));
temp = $" {Config.Prefix} {Config.Messages.WebsiteMessageCommand}";
player.PrintToChat(Utility.ReplaceTags(temp));
}
if (!string.IsNullOrEmpty(Config.Messages.SynchronizeMessageCommand))
{
temp = $"{Config.Prefix} {Config.Messages.SynchronizeMessageCommand}";
player.PrintToChat(ReplaceTags(temp));
temp = $" {Config.Prefix} {Config.Messages.SynchronizeMessageCommand}";
player.PrintToChat(Utility.ReplaceTags(temp));
}
if (!Config.Additional.KnifeEnabled) return;
if (!string.IsNullOrEmpty(Config.Messages.KnifeMessageCommand))
{
temp = $"{Config.Prefix} {Config.Messages.KnifeMessageCommand}";
player.PrintToChat(ReplaceTags(temp));
temp = $" {Config.Prefix} {Config.Messages.KnifeMessageCommand}";
player.PrintToChat(Utility.ReplaceTags(temp));
}
}
private static CSkeletonInstance GetSkeletonInstance(CGameSceneNode node)
@@ -774,7 +830,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
if (!Config.Additional.SkinEnabled) return;
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
if (player == null || !player.IsValid || player.IsBot) return;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
var steamId = new SteamID(player.SteamID);
@@ -862,7 +918,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
}
catch (Exception e)
{
Log(e.Message);
Utility.Log(e.Message);
return;
}
}
@@ -872,7 +928,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
try
{
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
if (player == null || !player.IsValid || player.IsBot) return;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
var steamId = new SteamID(player.SteamID);
if (Config.GlobalShare)
@@ -933,7 +989,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
}
catch (Exception e)
{
Log(e.Message);
Utility.Log(e.Message);
return;
}
}
@@ -954,14 +1010,14 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
}
catch (Exception e)
{
Log(e.Message);
Utility.Log(e.Message);
return;
}
}
private async Task SyncWeaponPaintsToDatabase(CCSPlayerController? player)
{
if (player == null || !player.IsValid || player.IsBot) return;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
int playerIndex = (int)player.EntityIndex!.Value.Value;
@@ -975,7 +1031,6 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
foreach (var weaponDefIndex in gPlayerWeaponPaints[playerIndex].Keys)
{
Console.WriteLine("WeaponDEFINDEX : " + weaponDefIndex);
int paintId = gPlayerWeaponPaints[playerIndex][weaponDefIndex];
float wear = gPlayerWeaponWear.TryGetValue(playerIndex, out var wearDictionary)
&& wearDictionary.TryGetValue(weaponDefIndex, out var retrievedWear)
@@ -1007,26 +1062,6 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
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)
{
Random rnd = new Random();
@@ -1066,27 +1101,4 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
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(" ");
}
}