mirror of
https://github.com/Nereziel/cs2-WeaponPaints.git
synced 2026-02-17 18:39:07 +00:00
Merge branch 'Nereziel:main' into main
This commit is contained in:
175
WeaponPaints.cs
175
WeaponPaints.cs
@@ -91,7 +91,6 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
};
|
};
|
||||||
public override void Load(bool hotReload)
|
public override void Load(bool hotReload)
|
||||||
{
|
{
|
||||||
SetGlobalExceptionHandler();
|
|
||||||
if (!Config.GlobalShare)
|
if (!Config.GlobalShare)
|
||||||
{
|
{
|
||||||
BuildDatabaseConnectionString();
|
BuildDatabaseConnectionString();
|
||||||
@@ -113,10 +112,11 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
{
|
{
|
||||||
for (int i = 1; i <= Server.MaxPlayers; i++)
|
for (int i = 1; i <= Server.MaxPlayers; i++)
|
||||||
{
|
{
|
||||||
if (Config.Additional.KnifeEnabled)
|
|
||||||
await GetKnifeFromDatabase(i);
|
|
||||||
if (Config.Additional.SkinEnabled)
|
if (Config.Additional.SkinEnabled)
|
||||||
await GetWeaponPaintsFromDatabase(i);
|
await GetWeaponPaintsFromDatabase(i);
|
||||||
|
|
||||||
|
if (Config.Additional.KnifeEnabled)
|
||||||
|
await GetKnifeFromDatabase(i);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -218,21 +218,8 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
*/
|
*/
|
||||||
public override void Unload(bool hotReload)
|
public override void Unload(bool hotReload)
|
||||||
{
|
{
|
||||||
RemoveGlobalExceptionHandler();
|
|
||||||
base.Unload(hotReload);
|
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()
|
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) => { if (player == null) return; OnCommandWS(player, info); });
|
||||||
@@ -344,12 +331,13 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
GiveKnifeToPlayer(player);
|
GiveKnifeToPlayer(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return HookResult.Continue;
|
return HookResult.Continue;
|
||||||
}
|
}
|
||||||
private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
|
private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
if (!IsMatchZy) return HookResult.Continue;
|
if (!IsMatchZy) return HookResult.Continue;
|
||||||
|
*/
|
||||||
|
|
||||||
NativeAPI.IssueServerCommand("mp_t_default_melee \"\"");
|
NativeAPI.IssueServerCommand("mp_t_default_melee \"\"");
|
||||||
NativeAPI.IssueServerCommand("mp_ct_default_melee \"\"");
|
NativeAPI.IssueServerCommand("mp_ct_default_melee \"\"");
|
||||||
@@ -360,15 +348,24 @@ 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 (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)
|
RemoveKnifeFromPlayer(player);
|
||||||
&&
|
|
||||||
g_playersKnife[(int)player.EntityIndex!.Value.Value] != "weapon_knife")
|
AddTimer(0.1f, () =>
|
||||||
{
|
{
|
||||||
RefreshPlayerKnife(player, true);
|
if (!PlayerHasKnife(player))
|
||||||
|
GiveKnifeToPlayer(player);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (Config.Additional.SkinVisibilityFix)
|
||||||
|
{
|
||||||
|
AddTimer(0.25f, () => RefreshSkins(player));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -462,74 +459,70 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
}
|
}
|
||||||
private void GiveKnifeToPlayer(CCSPlayerController? player)
|
private void GiveKnifeToPlayer(CCSPlayerController? player)
|
||||||
{
|
{
|
||||||
if (!Config.Additional.KnifeEnabled) return;
|
if (!Config.Additional.KnifeEnabled || player == null || !player.IsValid) return;
|
||||||
if (player == null || !player.IsValid) return;
|
|
||||||
|
|
||||||
if (PlayerHasKnife(player)) return;
|
|
||||||
|
|
||||||
if (g_playersKnife.TryGetValue((int)player.EntityIndex!.Value.Value, out var knife))
|
if (g_playersKnife.TryGetValue((int)player.EntityIndex!.Value.Value, out var knife))
|
||||||
{
|
{
|
||||||
player.GiveNamedItem(knife);
|
player.GiveNamedItem(knife);
|
||||||
}
|
}
|
||||||
|
else if (Config.Additional.GiveRandomKnife)
|
||||||
|
{
|
||||||
|
Random random = new Random();
|
||||||
|
int index = random.Next(knifeTypes.Count);
|
||||||
|
var randomKnife = knifeTypes.Values.ElementAt(index);
|
||||||
|
player.GiveNamedItem(randomKnife);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (Config.Additional.GiveRandomKnife)
|
var defaultKnife = (CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife";
|
||||||
{
|
player.GiveNamedItem(defaultKnife);
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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;
|
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.IsValid && weapon.Value.IsValid)
|
||||||
if (weapon.Value.DesignerName.Contains("knife"))
|
|
||||||
{
|
{
|
||||||
weapon.Value.Remove();
|
//if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59)
|
||||||
return;
|
if (weapon.Value.DesignerName.Contains("knife"))
|
||||||
|
{
|
||||||
|
weapon.Value.Remove();
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* Causing crashes
|
||||||
private void RefreshPlayerKnife(CCSPlayerController? player, bool remove = false)
|
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))
|
if (PlayerHasKnife(player))
|
||||||
RemoveKnifeFromPlayer(player);
|
RemoveKnifeFromPlayer(player);
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
AddTimer(0.3f, () =>
|
GiveKnifeToPlayer(player);
|
||||||
{
|
|
||||||
if (!PlayerHasKnife(player))
|
|
||||||
GiveKnifeToPlayer(player);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Config.Additional.SkinVisibilityFix)
|
if (Config.Additional.SkinVisibilityFix)
|
||||||
{
|
{
|
||||||
AddTimer(0.3f, () => RefreshSkins(player));
|
AddTimer(0.25f, () => RefreshSkins(player));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
private void RefreshSkins(CCSPlayerController? player)
|
private void RefreshSkins(CCSPlayerController? player)
|
||||||
{
|
{
|
||||||
if (player == null || !player.IsValid || !player.PawnIsAlive) return;
|
if (player == null || !player.IsValid || player.IsBot || !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"));
|
||||||
@@ -545,7 +538,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
}
|
}
|
||||||
|
|
||||||
var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
|
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)
|
foreach (var weapon in weapons)
|
||||||
{
|
{
|
||||||
if (weapon.IsValid && weapon.Value.IsValid)
|
if (weapon.IsValid && weapon.Value.IsValid)
|
||||||
@@ -562,33 +555,39 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
{
|
{
|
||||||
if (!Config.Additional.KnifeEnabled) return;
|
if (!Config.Additional.KnifeEnabled) return;
|
||||||
var giveItemMenu = new ChatMenu(ReplaceTags(Config.Messages.KnifeMenuTitle));
|
var giveItemMenu = new ChatMenu(ReplaceTags(Config.Messages.KnifeMenuTitle));
|
||||||
var handleGive = (CCSPlayerController player, ChatMenuOption option) =>
|
var handleGive = (CCSPlayerController? player, ChatMenuOption option) =>
|
||||||
{
|
{
|
||||||
string temp = "";
|
if (player != null && player.IsValid)
|
||||||
if (knifeTypes.TryGetValue(option.Text, out var knife))
|
|
||||||
{
|
{
|
||||||
g_playersKnife[(int)player.EntityIndex!.Value.Value] = knifeTypes[option.Text];
|
string temp = "";
|
||||||
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenu))
|
if (knifeTypes.TryGetValue(option.Text, out var knife))
|
||||||
{
|
{
|
||||||
temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenu}".Replace("{KNIFE}", option.Text);
|
g_playersKnife[(int)player.EntityIndex!.Value.Value] = knifeTypes[option.Text];
|
||||||
player.PrintToChat(ReplaceTags(temp));
|
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenu))
|
||||||
}
|
{
|
||||||
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenuKill) && Config.Additional.CommandKillEnabled)
|
temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenu}".Replace("{KNIFE}", option.Text);
|
||||||
{
|
player.PrintToChat(ReplaceTags(temp));
|
||||||
temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenuKill}";
|
}
|
||||||
player.PrintToChat(ReplaceTags(temp));
|
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenuKill) && Config.Additional.CommandKillEnabled)
|
||||||
}
|
{
|
||||||
if (player.PawnIsAlive)
|
temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenuKill}";
|
||||||
{
|
player.PrintToChat(ReplaceTags(temp));
|
||||||
RemoveKnifeFromPlayer(player);
|
}
|
||||||
RefreshPlayerKnife(player);
|
if (player.PawnIsAlive)
|
||||||
}
|
{
|
||||||
Task.Run(() => SyncKnifeToDatabase((int)player.EntityIndex!.Value.Value, knife));
|
RemoveKnifeFromPlayer(player);
|
||||||
|
AddTimer(0.2f, () =>
|
||||||
|
{
|
||||||
|
GiveKnifeToPlayer(player);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Task.Run(() => SyncKnifeToDatabase((int)player.EntityIndex!.Value.Value, knife));
|
||||||
|
|
||||||
/* Old way
|
/* Old way
|
||||||
RemoveKnifeFromPlayer(player);
|
RemoveKnifeFromPlayer(player);
|
||||||
AddTimer(0.1f, () => GiveKnifeToPlayer(player));
|
AddTimer(0.1f, () => GiveKnifeToPlayer(player));
|
||||||
*/
|
*/
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
foreach (var knife in knifeTypes)
|
foreach (var knife in knifeTypes)
|
||||||
@@ -604,8 +603,10 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
var weaponSelectionMenu = new ChatMenu(ReplaceTags(Config.Messages.WeaponMenuTitle));
|
var weaponSelectionMenu = new ChatMenu(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;
|
||||||
|
|
||||||
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))
|
||||||
@@ -620,8 +621,10 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
var skinSubMenu = new ChatMenu(ReplaceTags(Config.Messages.SkinMenuTitle).Replace("{WEAPON}", selectedWeapon));
|
var skinSubMenu = new ChatMenu(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) =>
|
||||||
{
|
{
|
||||||
|
if (p == null || !p.IsValid) return;
|
||||||
|
|
||||||
var steamId = new SteamID(player.SteamID);
|
var steamId = new SteamID(player.SteamID);
|
||||||
var firstSkin = skinsList?.FirstOrDefault(skin =>
|
var firstSkin = skinsList?.FirstOrDefault(skin =>
|
||||||
{
|
{
|
||||||
@@ -713,7 +716,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
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) return;
|
if (player == null || !player.IsValid || player.IsBot) 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))
|
||||||
@@ -722,9 +725,17 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
|||||||
Task.Run(async () => await GetWeaponPaintsFromDatabase(playerIndex));
|
Task.Run(async () => await GetWeaponPaintsFromDatabase(playerIndex));
|
||||||
if (Config.Additional.KnifeEnabled)
|
if (Config.Additional.KnifeEnabled)
|
||||||
{
|
{
|
||||||
|
RemoveKnifeFromPlayer(player);
|
||||||
|
AddTimer(0.2f, () =>
|
||||||
|
{
|
||||||
|
GiveKnifeToPlayer(player);
|
||||||
|
});
|
||||||
|
|
||||||
Task.Run(async () => await GetKnifeFromDatabase(playerIndex));
|
Task.Run(async () => await GetKnifeFromDatabase(playerIndex));
|
||||||
|
/*
|
||||||
RemoveKnifeFromPlayer(player);
|
RemoveKnifeFromPlayer(player);
|
||||||
AddTimer(0.2f, () => GiveKnifeToPlayer(player));
|
AddTimer(0.2f, () => GiveKnifeToPlayer(player));
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
if (!string.IsNullOrEmpty(Config.Messages.SuccessRefreshCommand))
|
if (!string.IsNullOrEmpty(Config.Messages.SuccessRefreshCommand))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user