Thanks so much!
I was also writing a SourceMod patch yesterday, which looks almost the same (but using the old syntax). Your solution hooking
item_pickup may be more reliable though, but you would want to use
EntIndexToEntRef() for the entity IDs I think just to be safe:
PHP Code:
public Action:CS_OnBuyCommand(clientID, const String:weapon[]) {
if (IsValidClient(clientID) && IsPlayerAlive(clientID) && !IsFakeClient(clientID)) {
if (StrEqual(weapon, "vest") || StrEqual(weapon, "vesthelm") || StrEqual(weapon, "nvgs")) {
/**
* Find out current ammo and clip size of player
*/
new PrimaryAmmo = -1;
new PrimaryClipSize = -1;
new SecondaryAmmo = -1;
new SecondaryClipSize = -1;
new PrimaryWeaponEntity = GetPlayerWeaponSlot(clientID, 0);
new PrimaryWeaponEntityRef = -1;
new SecondaryWeaponEntity = GetPlayerWeaponSlot(clientID, 1);
new SecondaryWeaponEntityRef = -1;
if (PrimaryWeaponEntity > -1) {
PrimaryWeaponEntityRef = EntIndexToEntRef(PrimaryWeaponEntity);
PrimaryAmmo = GetAmmo(clientID, 0);
PrimaryClipSize = GetClipSize(PrimaryWeaponEntity);
}
if (SecondaryWeaponEntity > -1) {
SecondaryWeaponEntityRef = EntIndexToEntRef(SecondaryWeaponEntity);
SecondaryAmmo = GetAmmo(clientID, 1);
SecondaryClipSize = GetClipSize(SecondaryWeaponEntity);
}
/**
* Cache information
*/
new Handle:handle = CreateDataPack();
WritePackCell(handle, GetClientUserId(clientID));
WritePackCell(handle, PrimaryWeaponEntityRef);
WritePackCell(handle, PrimaryAmmo);
WritePackCell(handle, PrimaryClipSize);
WritePackCell(handle, SecondaryWeaponEntityRef);
WritePackCell(handle, SecondaryAmmo);
WritePackCell(handle, SecondaryClipSize);
/**
* Restore in the next frame, otherwise this will have no effect
*/
RequestFrame(GameBugFix_RestoreClientAmmoAndClipSize, handle);
}
}
}
public GameBugFix_RestoreClientAmmoAndClipSize(Handle:handle) {
ResetPack(handle);
new clientID = GetClientOfUserId(ReadPackCell(handle));
new PrimaryWeaponEntity = ReadPackCell(handle);
new PrimaryAmmo = ReadPackCell(handle);
new PrimaryClipSize = ReadPackCell(handle);
new SecondaryWeaponEntity = ReadPackCell(handle);
new SecondaryAmmo = ReadPackCell(handle);
new SecondaryClipSize = ReadPackCell(handle);
if (IsValidClient(clientID) && IsPlayerAlive(clientID)) {
/**
* Primary
*/
if (PrimaryWeaponEntity != -1) {
PrimaryWeaponEntity = EntRefToEntIndex(PrimaryWeaponEntity);
if (IsValidEntity(PrimaryWeaponEntity)) {
SetPlayerAmmoForWeaponEntity(clientID, PrimaryWeaponEntity, PrimaryAmmo);
SetClipSize(PrimaryWeaponEntity, PrimaryClipSize);
}
}
/**
* Secondary
*/
if (SecondaryWeaponEntity != -1) {
SecondaryWeaponEntity = EntRefToEntIndex(SecondaryWeaponEntity);
if (IsValidEntity(SecondaryWeaponEntity)) {
SetPlayerAmmoForWeaponEntity(clientID, SecondaryWeaponEntity, SecondaryAmmo);
SetClipSize(SecondaryWeaponEntity, SecondaryClipSize);
}
}
}
CloseHandle(handle);
}
stock bool:IsValidClient(const clientID) {
if (!IsValidClientID(clientID)) {
return false;
}
if (!IsClientInGame(clientID)) {
return false;
}
return true;
}
stock GetAmmo(const clientID, const slot) {
new weapon = GetPlayerWeaponSlot(clientID, slot);
if (IsValidEntity(weapon)) {
new offset = GetEntProp(weapon, Prop_Send, "m_iPrimaryAmmoType", 1) * 4;
return GetEntData(clientID, Offset_m_iAmmo + offset);
}
return -1;
}
stock SetPlayerAmmoForWeaponEntity(const clientID, const entity, const amount) {
new offset = GetEntProp(entity, Prop_Send, "m_iPrimaryAmmoType", 1) * 4;
SetEntData(clientID, Offset_m_iAmmo + offset, amount, 4, true);
}
stock GetClipSize(const entity) {
return GetEntProp(entity, Prop_Send, "m_iClip1");
}
stock SetClipSize(const entity, const amount) {
SetEntProp(entity, Prop_Send, "m_iClip1", amount);
}