Raised This Month: $119 Target: $400
 29% 

Solved [CSGO] Fetch client's weapons, grenades, etc...


Post New Thread Reply   
 
Thread Tools Display Modes
JoaoRodrigoGamer
Junior Member
Join Date: Jul 2019
Location: Portugal
Old 08-22-2019 , 10:06   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #11

Quote:
Originally Posted by ShD3luxe View Post
For more tests try adding a cmd function !test to call the checking weapons function and see if then it detects if you have healthshot or teaser.
I updated the code of stock bool WeaponsClientHasWeapon(int client, const char weapon[32]) with that new debug version you posted and added the command sm_test:

PHP Code:
public Action test(int clientint args)
{
    
    if(
WeaponsClientHasWeapon(client,"weapon_taser"))
    {
        
PrintToChat(client"[sm_test] Client has a taser");
    }
    else
    {
        
PrintToChat(client"[sm_test] Client does not have a taser");
    }
    
    if(
WeaponsClientHasWeapon(client,"weapon_healthshot"))
    {
        
PrintToChat(client"[sm_test] Client has a health shot");
    }
    else
    {
        
PrintToChat(client"[sm_test] Client does not have a health shot");
    }

I ran the !test command with and without a taser/health shot in my inventory but the output was always negative...
JoaoRodrigoGamer is offline
ShD3luxe
Member
Join Date: Aug 2019
Location: Localhost
Old 08-22-2019 , 10:12   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #12

And what is the output for the print debug ? Is the teaser weapon or something with that name in the player's inventory ?
ShD3luxe is offline
JoaoRodrigoGamer
Junior Member
Join Date: Jul 2019
Location: Portugal
Old 08-22-2019 , 10:20   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #13

Sorry, I forgot to upload the chat log. Here it is:

EDIT: Now that I look to the debug again, it does not even go through the grenades on the PrintToChat() @ stock bool WeaponsClientHasWeapon(int client, const char weapon[32])
Attached Files
File Type: txt chatdebug.txt (1.2 KB, 6 views)

Last edited by JoaoRodrigoGamer; 08-22-2019 at 10:24. Reason: added edit
JoaoRodrigoGamer is offline
ShD3luxe
Member
Join Date: Aug 2019
Location: Localhost
Old 08-22-2019 , 10:54   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #14

I think that the problem is that you have the taser and the knife on the same slot and the GetPlayerWeaponSlot functions will only return the first weapon on that slot.That's why is only returning your first grenade only.
Try like this. This should store the first 3 weapons on that slot. Check to see what is the debug print output after you try this.
Code:
#include <sourcemod>
#include <sdktools>

#pragma newdecls required 
#define WEAPONS_MAX_LENGTH 32
#define WEAPONS_SLOTS_MAX 5
#define WEAPONS_ON_SLOTS_MAX 3

enum WeaponsSlot
{
    Slot_Invalid        = -1,   /** Invalid weapon (slot). */
    Slot_Primary        = 0,    /** Primary weapon slot. */
    Slot_Secondary      = 1,    /** Secondary weapon slot. */
    Slot_Melee          = 2,    /** Melee (knife) weapon slot. */
    Slot_Projectile     = 3,    /** Projectile (grenades, flashbangs, etc) weapon slot. */
    Slot_Explosive      = 4,    /** Explosive (c4) weapon slot. */
};
public void OnPluginStart()
{
	RegConsoleCmd("sm_test",TestCMD);
}
public Action TestCMD(int client,int args)
{
	bool hasWeapon = WeaponsClientHasWeapon(client,"weapon_healthshot");
	if(hasWeapon)
	{
		PrintToChatAll("Has healthshot");
	}
}
stock bool WeaponsClientHasWeapon(int client, const char weapon[32])
{
    // Get all of client's current weapons.
    int weapons[WeaponsSlot][WEAPONS_ON_SLOTS_MAX];
    WeaponsGetClientWeapons(client, weapons);  
    char classname[64];
    
    // x = slot index
    for (int x = 0; x < WEAPONS_SLOTS_MAX; x++)
    {
		for(int w = 0;w < WEAPONS_ON_SLOTS_MAX;w++) 
		{
			// If slot is empty, then stop.
			if (weapons[x][w] == -1) 
			{
				continue;
			}
			
			// If the weapon's classname matches, then return true.
			GetEdictClassname(weapons[x][w], classname, sizeof(classname));
			PrintToChat(client,"[DEBUG] User weapon %s , weapon to check for : %s",classname,weapon);
			
			if (StrEqual(weapon, classname, false))
			{
				return true;
			}
		}
    }   
    return false;
}
stock void WeaponsGetClientWeapons(int client, int weapons[WeaponsSlot][WEAPONS_ON_SLOTS_MAX])
{
    // x = Weapon slot.
	// Loop twice for the teaser and healthshot ( cuz are on the same slot as other weapons )
    for (int x = 0; x < WEAPONS_SLOTS_MAX; x++)
    {
		for(int w = 0;w < WEAPONS_ON_SLOTS_MAX;w++)
		{
		    // if there are more weapons this SHOULD save all of them from the slot if we loop more than once
			weapons[x][w] = GetPlayerWeaponSlot(client, x);
		}
    }
}

Last edited by ShD3luxe; 08-22-2019 at 10:56.
ShD3luxe is offline
JoaoRodrigoGamer
Junior Member
Join Date: Jul 2019
Location: Portugal
Old 08-22-2019 , 11:20   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #15

I copied the code you wrote to a new script, disabled the plugin in which I was working, and added the following code at the end of the file:
PHP Code:
public void OnPlayerSpawn(Handle event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    
GivePlayerItem(client"weapon_taser");
    
GivePlayerItem(client"weapon_tagrenade");
    
GivePlayerItem(client"weapon_hegrenade");
    
GivePlayerItem(client"weapon_flashbang");
    
GivePlayerItem(client"weapon_healthshot");

The chat output was the following:
Code:
[DEBUG] User weapon weapon_glock , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_glock , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_glock , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_knife , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_knife , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_knife , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_tagrenade , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_tagrenade , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_tagrenade , weapon to check for : weapon_healthshot
JoaoRodrigoGamer is offline
ShD3luxe
Member
Join Date: Aug 2019
Location: Localhost
Old 08-22-2019 , 19:00   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #16

AFAIK, is not getting the second weapon if there is already one in the slot.
A solution is to save it in the weapons array,delete that weapon ,check the slot again and after we are done with checking give back the items from the array . This may work , maybe someone has a better method.
PHP Code:
#include <sourcemod>
#include <sdktools>

#pragma newdecls required 
#define WEAPONS_MAX_LENGTH 32
#define WEAPONS_SLOTS_MAX 5
#define WEAPONS_ON_SLOTS_MAX 4

enum WeaponsSlot
{
    
Slot_Invalid        = -1,   /** Invalid weapon (slot). */
    
Slot_Primary        0,    /** Primary weapon slot. */
    
Slot_Secondary      1,    /** Secondary weapon slot. */
    
Slot_Melee          2,    /** Melee (knife) weapon slot. */
    
Slot_Projectile     3,    /** Projectile (grenades, flashbangs, etc) weapon slot. */
    
Slot_Explosive      4,    /** Explosive (c4) weapon slot. */
};
public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_test",TestCMD);
}
public 
Action TestCMD(int client,int args)
{
    
bool hasWeapon WeaponsClientHasWeapon(client,"weapon_healthshot");
    if(
hasWeapon)
    {
        
PrintToChatAll("Has healthshot");
    }
}
stock bool WeaponsClientHasWeapon(int client, const char weapon[32])
{
    
// Get all of client's current weapons.
    
int weapons[WeaponsSlot][WEAPONS_ON_SLOTS_MAX];
    
WeaponsGetClientWeapons(clientweapons);  
    
char classname[64];
    
    
// x = slot index
    
for (int x 0WEAPONS_SLOTS_MAXx++)
    {
        for(
int w 0;WEAPONS_ON_SLOTS_MAX;w++) 
        {
            
// If slot is empty, then stop.
            
if (weapons[x][w] == -1
            {
                continue;
            }
            
            
GetEdictClassname(weapons[x][w], classnamesizeof(classname));
            
// now give the user item back
            
int oldWeapon GivePlayerItem(clientclassname);
            
EquipPlayerWeapon(clientoldWeapon);
            
// Debug for test
            
PrintToChat(client,"[DEBUG] User weapon %s , weapon to check for : %s",classname,weapon);
            
// If the weapon's classname matches, then return true.
            
if (StrEqual(weaponclassnamefalse))
            {
                return 
true;
            }
        }
    }   
    return 
false;
}
stock void WeaponsGetClientWeapons(int clientint weapons[WeaponsSlot][WEAPONS_ON_SLOTS_MAX])
{
    
// x = Weapon slot.
    // Loop twice for the teaser and healthshot ( cuz are on the same slot as other weapons )
    
for (int x 0WEAPONS_SLOTS_MAXx++)
    {
        for(
int w 0;WEAPONS_ON_SLOTS_MAX;w++)
        {
            
// if there are more weapons this SHOULD save all of them from the slot if we loop more than once
            
int weaponIndex GetPlayerWeaponSlot(clientx);
            if(
weaponIndex != -1)
            {
                
weapons[x][w] = weaponIndex;
                
// remove it for the new item temporary and give it later back
                
RemovePlayerItem(clientweaponIndex);
                
AcceptEntityInput(weaponIndex"Kill");
            }
        }
    }

ShD3luxe is offline
JoaoRodrigoGamer
Junior Member
Join Date: Jul 2019
Location: Portugal
Old 08-23-2019 , 03:15   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #17

That code crashes the server...

Code:
Exception reported: World not allowed
L 08/23/2019 - 08:06:23: [SM] Blaming: inventoryCheck.smx
L 08/23/2019 - 08:06:23: [SM] Call stack trace:
L 08/23/2019 - 08:06:23: [SM]   [0] EquipPlayerWeapon
L 08/23/2019 - 08:06:23: [SM]   [1] Line 53, C:\Users\JoŅo Gonžalves\Desktop\test plugin\inventoryCheck.sp::WeaponsClientHasWeapon
L 08/23/2019 - 08:06:23: [SM]   [2] Line 25, C:\Users\JoŅo Gonžalves\Desktop\test plugin\inventoryCheck.sp::TestCMD
And when I sent the sm_test command, it started changing some cvars (probably the server trying to restart itself?)

I even tried to remove the EquipPlayerWeapon() line to see if it would change anything, but it instantly crashes the server, giving no time to check the console.
JoaoRodrigoGamer is offline
ShD3luxe
Member
Join Date: Aug 2019
Location: Localhost
Old 08-23-2019 , 03:54   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #18

Oh my bad it seems that the array is initialized with 0 and the 0 index is the world index ( and u can't create a weapon with that index ).
Here
PHP Code:
#include <sourcemod>
#include <sdktools>

#pragma newdecls required 
#define WEAPONS_MAX_LENGTH 32
#define WEAPONS_SLOTS_MAX 5
#define WEAPONS_ON_SLOTS_MAX 4

enum WeaponsSlot
{
    
Slot_Invalid        = -1,   /** Invalid weapon (slot). */
    
Slot_Primary        0,    /** Primary weapon slot. */
    
Slot_Secondary      1,    /** Secondary weapon slot. */
    
Slot_Melee          2,    /** Melee (knife) weapon slot. */
    
Slot_Projectile     3,    /** Projectile (grenades, flashbangs, etc) weapon slot. */
    
Slot_Explosive      4,    /** Explosive (c4) weapon slot. */
};
public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_test",TestCMD);
}
public 
Action TestCMD(int client,int args)
{
    
bool hasWeapon WeaponsClientHasWeapon(client,"weapon_healthshot");
    if(
hasWeapon)
    {
        
PrintToChatAll("Has healthshot");
    }
}
stock bool WeaponsClientHasWeapon(int client, const char weapon[32])
{
    
// Get all of client's current weapons.
    
int weapons[WeaponsSlot][WEAPONS_ON_SLOTS_MAX];
    
WeaponsGetClientWeapons(clientweapons);  
    
char classname[64];
    
    
// x = slot index
    
for (int x 0WEAPONS_SLOTS_MAXx++)
    {
        for(
int w 0;WEAPONS_ON_SLOTS_MAX;w++) 
        {
            
// If slot is empty or is 0(WORLD index) then stop.
            
if (weapons[x][w] <= 0
            {
                continue;
            }
                        
// Use the weapon index to get her name (weapon_...)
            
GetEdictClassname(weapons[x][w], classnamesizeof(classname));
            
// Now give the user item back 
            
int oldWeapon GivePlayerItem(clientclassname);
            
EquipPlayerWeapon(clientoldWeapon);
            
// Debug for test
            
PrintToChat(client,"[DEBUG] User weapon %s , weapon to check for : %s",classname,weapon);
            
// If the weapon's classname matches, then return true.
            
if (StrEqual(weaponclassnamefalse))
            {
                return 
true;
            }
        }
    }   
    return 
false;
}
stock void WeaponsGetClientWeapons(int clientint weapons[WeaponsSlot][WEAPONS_ON_SLOTS_MAX])
{
    
// x = Weapon slot.
    // Loop twice for the teaser and healthshot ( cuz are on the same slot as other weapons )
    
for (int x 0WEAPONS_SLOTS_MAXx++)
    {
        for(
int w 0;WEAPONS_ON_SLOTS_MAX;w++)
        {
                
// if there are more weapons this SHOULD save all of them from the slot if we loop more than once
            
int weaponIndex GetPlayerWeaponSlot(clientx);
            
// The 0 index is the world index and -1 is empty slot , check if the weapon index is greater than 0 to satisfy both cases 
            
if(weaponIndex 0)
            {
                                
// save the weapon index
                
weapons[x][w] = weaponIndex;
                
// remove it temporary (this will help us to detect the new item in the slot) and we will give it back later
                
RemovePlayerItem(clientweaponIndex);
                
AcceptEntityInput(weaponIndex"Kill");
            }
        }
    }


Last edited by ShD3luxe; 08-23-2019 at 03:59.
ShD3luxe is offline
JoaoRodrigoGamer
Junior Member
Join Date: Jul 2019
Location: Portugal
Old 08-23-2019 , 04:22   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #19

This time it is almost working. The server didn't crash and I got almost all items back after running the command.

chat debug:

Code:
僕の kill‎ : !test
[DEBUG] User weapon weapon_hkp2000 , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_knife , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_taser , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_tagrenade , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_hegrenade , weapon to check for : weapon_healthshot
[DEBUG] User weapon weapon_flashbang , weapon to check for : weapon_healthshot
The 2 pics attached, are the before and after I ran the command.
Attached Images
File Type: jpg before.jpg (41.5 KB, 2 views)
File Type: jpg after.jpg (41.0 KB, 1 views)
JoaoRodrigoGamer is offline
ShD3luxe
Member
Join Date: Aug 2019
Location: Localhost
Old 08-23-2019 , 04:42   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #20

It looks like now it's listing all the weapons except for the healthshot . In what slot do you have the healthshot ? It should be on the 5 (the c4 slot). Try checking if the debug command is showing weapon_c4 if you have it on you.

Check if there are any errors in console because I see that the grenades are not back to the player even if they are listed in the debug.
If you want to limit the user to just one healthshot(per round) use the cvar :


ammo_item_limit_healthshot 1

Also try just giving the item back to the player without equiping it:
PHP Code:
stock bool WeaponsClientHasWeapon(int client, const char weapon[32])
{
    
// Get all of client's current weapons.
    
int weapons[WeaponsSlot][WEAPONS_ON_SLOTS_MAX];
    
WeaponsGetClientWeapons(clientweapons);  
    
char classname[64];
    
    
// x = slot index
    
for (int x 0WEAPONS_SLOTS_MAXx++)
    {
        for(
int w 0;WEAPONS_ON_SLOTS_MAX;w++) 
        {
            
// If slot is empty or is 0(WORLD index) then stop.
            
if (weapons[x][w] <= 0
            {
                continue;
            }
                        
// Use the weapon index to get her name (weapon_...)
            
GetEdictClassname(weapons[x][w], classnamesizeof(classname));
            
// Now give the user item back 
            
GivePlayerItem(clientclassname);
            
// Debug for test
            
PrintToChat(client,"[DEBUG] User weapon %s , weapon to check for : %s",classname,weapon);
            
// If the weapon's classname matches, then return true.
            
if (StrEqual(weaponclassnamefalse))
            {
                return 
true;
            }
        }
    }   
    return 
false;


Last edited by ShD3luxe; 08-23-2019 at 04:55.
ShD3luxe is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:48.


Powered by vBulletin®
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Theme made by Freecode