Assuming your game is CSGO this plugin should do the trick.
You need the csgoitems.inc to compile, although
CSGOItems itself is optional but definitely a personal recommendation as it allows the plugin to automatically determine the correctly ammo to be set dynamically per weapon
Grab CSGOItems
here (If you want)
There is currently a known issue which affects some people that have
CSGOItems installed, If you get script timeouts then open configs/core.cfg in the sourcemod directory and set "SlowScriptTimeout" to a higher value, something like 30.
PHP Code:
#include <sdktools>
#include <cstrike>
#include <csgoitems>
bool g_bCSGOItems = false;
public void OnPluginStart() {
g_bCSGOItems = LibraryExists("CSGO_Items");
}
public void OnLibraryAdded(const char[] szName) {
if (StrEqual(szName, "CSGO_Items")) {
g_bCSGOItems = true;
}
}
public void OnLibraryRemoved(const char[] szName) {
if (StrEqual(szName, "CSGO_Items")) {
g_bCSGOItems = false;
}
}
public void OnPlayerRunCmdPost(int iClient)
{
// Is he alive? If not we don't need to go further.
if (!IsPlayerAlive(iClient)) {
return;
}
// Here we will make sure we only do logic once per tick, just an optimization.
int iGameTick = GetGameTickCount();
static int iLastTick[MAXPLAYERS + 1];
if (iGameTick == iLastTick[iClient]) {
return;
}
iLastTick[iClient] = iGameTick;
// Lets grab his active weapon.
int iActiveWeapon = g_bCSGOItems ? CSGOItems_GetActiveWeapon(iClient) : GetEntPropEnt(iClient, Prop_Data, "m_hActiveWeapon");
// Is his weapon valid? If not then lets not do any further logic.
if (iActiveWeapon == -1) {
return;
}
// Now lets determine if the weapon is a primary or a secondary, otherwise we ain't interested in any further logic.
if (g_bCSGOItems) {
int iWeaponSlot = CSGOItems_GetWeaponSlotByWeapon(iActiveWeapon);
if (iWeaponSlot != CS_SLOT_SECONDARY && iWeaponSlot != CS_SLOT_PRIMARY) {
return;
}
} else if (iActiveWeapon != GetPlayerWeaponSlot(iClient, CS_SLOT_SECONDARY) && iActiveWeapon != GetPlayerWeaponSlot(iClient, CS_SLOT_PRIMARY)) {
return;
}
// Lets refill his gun.
RefillWeapon(iClient, iActiveWeapon);
}
void RefillWeapon(int iClient, int iWeapon)
{
/* // Check if he is a VIP first (Uncomment me.)
if(!VIP_IsClientFeatureUse(iClient, BULLETS)) {
/return;
}*/
if (!g_bCSGOItems) { // We don't have CSGOItems plugin installed.
// Determine what ammo should be set here, these are just examples and should be determined properly per weapon.
int iReserveAmmo = 32;
int iClipAmmo = 32;
SetWeaponAmmo(iWeapon, iReserveAmmo, iClipAmmo);
return;
}
// We do have CSGO Items, we can dynamically set the correct ammo values for this weapon!
CSGOItems_RefillClipAmmo(iWeapon);
CSGOItems_RefillReserveAmmo(iWeapon);
}
stock bool SetWeaponAmmo(int iWeapon, int iReserveAmmo, int iClipAmmo)
{
if (iReserveAmmo < 0 && iClipAmmo < 0) {
return false;
}
if (iReserveAmmo > -1) {
SetEntProp(iWeapon, Prop_Send, "m_iPrimaryReserveAmmoCount", iReserveAmmo);
}
if (iClipAmmo > -1) {
SetEntProp(iWeapon, Prop_Send, "m_iClip1", iClipAmmo);
}
return true;
}
Enjoy