Skip to content

Commit

Permalink
3.1c
Browse files Browse the repository at this point in the history
- Fix for custom weapons
  • Loading branch information
daffyyyy committed Oct 28, 2024
1 parent 475e91e commit ad6c7df
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 73 deletions.
13 changes: 5 additions & 8 deletions Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,15 @@ private void OnCommandStattrak(CCSPlayerController? player, CommandInfo commandI
{
if (player == null || !player.IsValid) return;

if (!GPlayerWeaponsInfo.TryGetValue(player.Slot, out var teamInfo) ||
!teamInfo.TryGetValue(player.Team, out var teamWeapons) )
return;

var weapon = player.PlayerPawn.Value?.WeaponServices?.ActiveWeapon.Value;

if (weapon == null || !weapon.IsValid)
return;
if (!teamWeapons.TryGetValue(weapon.AttributeManager.Item.ItemDefinitionIndex, out var teamWeapon))
return;

teamWeapon.StatTrak = !teamWeapon.StatTrak;
if (!HasChangedPaint(player, weapon.AttributeManager.Item.ItemDefinitionIndex, out var weaponInfo) || weaponInfo == null)
return;

weaponInfo.StatTrak = !weaponInfo.StatTrak;
RefreshWeapons(player);

if (!string.IsNullOrEmpty(Localizer["wp_stattrak_action"]))
Expand Down Expand Up @@ -337,7 +334,7 @@ private void SetupSkinsMenu()
value.Seed = 0;
}
PlayerInfo playerInfo = new PlayerInfo
var playerInfo = new PlayerInfo
{
UserId = p.UserId,
Slot = p.Slot,
Expand Down
31 changes: 12 additions & 19 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Entities.Constants;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;

Expand Down Expand Up @@ -194,7 +193,7 @@ private HookResult OnRoundMvp(EventRoundMvp @event, GameEventInfo info)
return HookResult.Continue;
}

public HookResult OnGiveNamedItemPost(DynamicHook hook)
private HookResult OnGiveNamedItemPost(DynamicHook hook)
{
try
{
Expand All @@ -206,23 +205,15 @@ public HookResult OnGiveNamedItemPost(DynamicHook hook)
var player = GetPlayerFromItemServices(itemServices);
if (player != null)
{
var weaponServices = player.PlayerPawn.Value?.WeaponServices;

GivePlayerWeaponSkin(player, weapon);

if (weaponServices is { MyWeapons.Count: 1 })
{
// player.GiveNamedItem(CsItem.Healthshot);
// newWeapon.AddEntityIOEvent("Kill", newWeapon, null, "", 0.1f);
}
}
}
catch { }

return HookResult.Continue;
}

public void OnEntityCreated(CEntityInstance entity)
private void OnEntityCreated(CEntityInstance entity)
{
var designerName = entity.DesignerName;

Expand Down Expand Up @@ -289,9 +280,15 @@ public HookResult OnItemPickup(EventItemPickup @event, GameEventInfo _)
{
// if (!IsWindows) return HookResult.Continue;
var player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot) return HookResult.Continue;
if (!@event.Item.Contains("knife")) return HookResult.Continue;

var weaponDefIndex = (int)@event.Defindex;

if (!HasChangedKnife(player, out var _) || !HasChangedPaint(player, weaponDefIndex, out var _))
return HookResult.Continue;

if (player != null && player is { IsValid: true, Connected: PlayerConnectedState.PlayerConnected, PawnIsAlive: true, PlayerPawn.IsValid: true })
if (player is { Connected: PlayerConnectedState.PlayerConnected, PawnIsAlive: true, PlayerPawn.IsValid: true })
{
GiveOnItemPickup(player);
}
Expand All @@ -309,20 +306,16 @@ private HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)

if (victim == null || !victim.IsValid || victim == player)
return HookResult.Continue;

if (!GPlayerWeaponsInfo.TryGetValue(player.Slot, out var teamInfo) ||
!teamInfo.TryGetValue(player.Team, out var teamWeapons) )
return HookResult.Continue;


CBasePlayerWeapon? weapon = player.PlayerPawn.Value?.WeaponServices?.ActiveWeapon.Value;

if (weapon == null) return HookResult.Continue;

int weaponDefIndex = weapon.AttributeManager.Item.ItemDefinitionIndex;

if (!teamWeapons.TryGetValue(weaponDefIndex, out var weaponInfo) || weaponInfo.Paint == 0)
if (!HasChangedPaint(player, weaponDefIndex, out var weaponInfo) || weaponInfo == null)
return HookResult.Continue;

if (!weaponInfo.StatTrak) return HookResult.Continue;

weaponInfo.StatTrakCount += 1;
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1b
3.1c
1 change: 0 additions & 1 deletion Variables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public partial class WeaponPaints
internal static readonly ConcurrentDictionary<int, ConcurrentDictionary<CsTeam, ushort>> GPlayersMusic = new();
internal static readonly ConcurrentDictionary<int, ConcurrentDictionary<CsTeam, ushort>> GPlayersPin = new();
internal static readonly ConcurrentDictionary<int, (string? CT, string? T)> GPlayersAgent = new();
internal static readonly Dictionary<int, int> GPlayersKnivesPickup = [];
internal static readonly ConcurrentDictionary<int, ConcurrentDictionary<CsTeam, ConcurrentDictionary<int, WeaponInfo>>> GPlayerWeaponsInfo = new();
internal static List<JObject> SkinsList = [];
internal static List<JObject> PinsList = [];
Expand Down
97 changes: 56 additions & 41 deletions WeaponAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,30 @@ private void GivePlayerWeaponSkin(CCSPlayerController player, CBasePlayerWeapon

bool isKnife = weapon.DesignerName.Contains("knife") || weapon.DesignerName.Contains("bayonet");

if (isKnife && (!GPlayersKnife.ContainsKey(player.Slot) ||
!GPlayersKnife[player.Slot].ContainsKey(player.Team) ||
GPlayersKnife[player.Slot][player.Team] == "weapon_knife"))
return;

if (isKnife)
switch (isKnife)
{
var newDefIndex = WeaponDefindex.FirstOrDefault(x => x.Value == GPlayersKnife[player.Slot][player.Team]);
if (newDefIndex.Key == 0) return;

if (weapon.AttributeManager.Item.ItemDefinitionIndex != newDefIndex.Key)
case true when !HasChangedKnife(player, out var _):
return;
case true:
{
SubclassChange(weapon, (ushort)newDefIndex.Key);
}
var newDefIndex = WeaponDefindex.FirstOrDefault(x => x.Value == GPlayersKnife[player.Slot][player.Team]);
if (newDefIndex.Key == 0) return;

weapon.AttributeManager.Item.ItemDefinitionIndex = (ushort)newDefIndex.Key;
weapon.AttributeManager.Item.EntityQuality = 3;
}
else
{
weapon.AttributeManager.Item.EntityQuality = 0;
if (weapon.AttributeManager.Item.ItemDefinitionIndex != newDefIndex.Key)
{
SubclassChange(weapon, (ushort)newDefIndex.Key);
}

weapon.AttributeManager.Item.ItemDefinitionIndex = (ushort)newDefIndex.Key;
weapon.AttributeManager.Item.EntityQuality = 3;
break;
}
default:
weapon.AttributeManager.Item.EntityQuality = 0;
break;
}

UpdatePlayerEconItemId(weapon.AttributeManager.Item);

int weaponDefIndex = weapon.AttributeManager.Item.ItemDefinitionIndex;
Expand All @@ -54,9 +55,7 @@ private void GivePlayerWeaponSkin(CCSPlayerController player, CBasePlayerWeapon
bool isLegacyModel;

if (_config.Additional.GiveRandomSkin &&
GPlayerWeaponsInfo.ContainsKey(player.Slot) &&
(!GPlayerWeaponsInfo[player.Slot].ContainsKey(player.Team) ||
!GPlayerWeaponsInfo[player.Slot][player.Team].ContainsKey(weaponDefIndex)))
!HasChangedPaint(player, weaponDefIndex, out _))
{
// Random skins
weapon.FallbackPaintKit = GetRandomPaint(weaponDefIndex);
Expand Down Expand Up @@ -89,13 +88,9 @@ private void GivePlayerWeaponSkin(CCSPlayerController player, CBasePlayerWeapon
return;
}

if (!GPlayerWeaponsInfo.TryGetValue(player.Slot, out var teamInfo) ||
!teamInfo.TryGetValue(player.Team, out var teamWeapons) )
return;
if (!teamWeapons.TryGetValue(weaponDefIndex, out var value))
if (!HasChangedPaint(player, weaponDefIndex, out var weaponInfo) || weaponInfo == null)
return;

var weaponInfo = value;
//Log($"Apply on {weapon.DesignerName}({weapon.AttributeManager.Item.ItemDefinitionIndex}) paint {gPlayerWeaponPaints[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} seed {gPlayerWeaponSeed[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} wear {gPlayerWeaponWear[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]}");

weapon.AttributeManager.Item.AttributeList.Attributes.RemoveAll();
Expand Down Expand Up @@ -146,9 +141,7 @@ private void GivePlayerWeaponSkin(CCSPlayerController player, CBasePlayerWeapon
private void IncrementWearForWeaponWithStickers(CCSPlayerController player, CBasePlayerWeapon weapon)
{
int weaponDefIndex = weapon.AttributeManager.Item.ItemDefinitionIndex;
if (!GPlayerWeaponsInfo.TryGetValue(player.Slot, out var playerWeapons) ||
!playerWeapons.TryGetValue(player.Team, out var weaponInfoDict) ||
!weaponInfoDict.TryGetValue(weaponDefIndex, out var weaponInfo) ||
if (!HasChangedPaint(player, weaponDefIndex, out var weaponInfo) || weaponInfo == null ||
weaponInfo.Stickers.Count <= 0) return;

float wearIncrement = 0.001f;
Expand All @@ -171,11 +164,8 @@ private void SetStickers(CCSPlayerController? player, CBasePlayerWeapon weapon)

int weaponDefIndex = weapon.AttributeManager.Item.ItemDefinitionIndex;

if (!GPlayerWeaponsInfo.TryGetValue(player.Slot, out var playerWeapons) ||
!playerWeapons[player.Team].TryGetValue(weaponDefIndex, out var weaponInfo))
{
if (!HasChangedPaint(player ,weaponDefIndex, out var weaponInfo) || weaponInfo == null)
return;
}

foreach (var sticker in weaponInfo.Stickers)
{
Expand Down Expand Up @@ -211,10 +201,7 @@ private void SetKeychain(CCSPlayerController? player, CBasePlayerWeapon weapon)

int weaponDefIndex = weapon.AttributeManager.Item.ItemDefinitionIndex;

if (!GPlayerWeaponsInfo.TryGetValue(player.Slot, out var playerWeaponsInfo) ||
!playerWeaponsInfo.TryGetValue(player.Team, out var teamWeaponsInfo) ||
!teamWeaponsInfo.TryGetValue(weaponDefIndex, out var value) ||
value.KeyChain == null)
if (!HasChangedPaint(player, weaponDefIndex, out var value) || value?.KeyChain == null)
return;

var keyChain = value.KeyChain;
Expand Down Expand Up @@ -406,9 +393,7 @@ private void GivePlayerGloves(CCSPlayerController player)
if (!GPlayersGlove.TryGetValue(player.Slot, out var gloveInfo) ||
!gloveInfo.TryGetValue(player.Team, out var gloveId) ||
gloveId == 0 ||
!GPlayerWeaponsInfo.TryGetValue(player.Slot, out var playerWeaponsInfo) ||
!playerWeaponsInfo.TryGetValue(player.Team, out var teamWeaponsInfo) ||
!teamWeaponsInfo.TryGetValue(gloveId, out var weaponInfo))
!HasChangedPaint(player, gloveId, out var weaponInfo) || weaponInfo == null)
return;
item.ItemDefinitionIndex = gloveId;
Expand Down Expand Up @@ -583,6 +568,36 @@ private void UpdatePlayerEconItemId(CEconItemView econItemView)
return viewModel.Value == null ? null : viewModel.Value;
}

private static bool HasChangedKnife(CCSPlayerController player, out string? knifeValue)
{
knifeValue = null;

// Check if player has knife info for their slot and team
if (!GPlayersKnife.TryGetValue(player.Slot, out var knife) ||
!knife.TryGetValue(player.Team, out var value) ||
value == "weapon_knife") return false;
knifeValue = value; // Assign the knife value to the out parameter
return true;
}

private static bool HasChangedPaint(CCSPlayerController player, int weaponDefIndex, out WeaponInfo? weaponInfo)
{
weaponInfo = null;

// Check if player has weapons info for their slot and team
if (!GPlayerWeaponsInfo.TryGetValue(player.Slot, out var teamInfo) ||
!teamInfo.TryGetValue(player.Team, out var teamWeapons))
{
return false;
}

// Check if the specified weapon has a paint/skin change
if (!teamWeapons.TryGetValue(weaponDefIndex, out var value) || value.Paint <= 0) return false;

weaponInfo = value; // Assign the out variable when it exists
return true;
}

private static float ViewAsFloat(uint value)
{
return BitConverter.Int32BitsToSingle((int)value);
Expand Down
4 changes: 1 addition & 3 deletions WeaponPaints.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using Microsoft.Extensions.Logging;
using MySqlConnector;

Expand All @@ -17,7 +16,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
public override string ModuleAuthor => "Nereziel & daffyy";
public override string ModuleDescription => "Skin, gloves, agents and knife selector, standalone and web-based";
public override string ModuleName => "WeaponPaints";
public override string ModuleVersion => "3.1b";
public override string ModuleVersion => "3.1c";

public override void Load(bool hotReload)
{
Expand All @@ -40,7 +39,6 @@ public override void Load(bool hotReload)
!string.IsNullOrEmpty(player.IpAddress) && player is
{ IsBot: false, Connected: PlayerConnectedState.PlayerConnected }))
{
GPlayersKnivesPickup[player.Slot] = 0;
var playerInfo = new PlayerInfo
{
UserId = player.UserId,
Expand Down

0 comments on commit ad6c7df

Please sign in to comment.