Raised This Month: $51 Target: $400
 12% 

OnWeaponUse Issue


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ShadowRanger
Junior Member
Join Date: Aug 2017
Old 10-29-2017 , 23:54   OnWeaponUse Issue
Reply With Quote #1

I'm having an issue with the OnWeaponUse event which I'm trying to use to prevent weapons from being picked up but also not removing the player's current weapon which happens if I don't add the GivePlayerItem at the end and also removing the weapon that gets dropped. The issue is that whenever I try and pick up a weapon, a few seconds after the client/server crashes. I'm not sure what I'm doing wrong.

Code:
public Action OnWeaponCanUse(int client, int weapon)
{
	// Exit if a fun round is not in progress
	if (!g_startedFunRound) return Plugin_Continue;

	// Run some other checks
	if (!IsClientInGame(client)) return Plugin_Continue;
	if (!IsPlayerAlive(client)) return Plugin_Stop;

	// Prevent the weapon from being picked up
	if (IsValidEntity(weapon))
	{
		AcceptEntityInput(weapon, "Kill");
	}

	if (!StrEqual(g_curRoundWeapon, "weapon_knife"))
	{
		GivePlayerItem(client, g_curRoundWeapon);
	}

	return Plugin_Handled;
}
ShadowRanger is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 10-30-2017 , 01:31   Re: OnWeaponUse Issue
Reply With Quote #2

PHP Code:
public Action OnWeaponCanUse(int clientint weapon)
{
    if (!
g_startedFunRound) return Plugin_Continue;

    if (!
IsClientInGame(client)) return Plugin_Continue;
    if (!
IsPlayerAlive(client)) return Plugin_Stop// this should return Plugin_Continue

    
if (IsValidEntity(weapon)) // This will happen any time the player's weapon is a valid weapon, is this intended behavior???
    
{
        
AcceptEntityInput(weapon"Kill"); // If you don't want them to pick the item up, return Plugin_Handled
    
}


    if (!
StrEqual(g_curRoundWeapon"weapon_knife")) // Any time this OnCanUse is called you want to give them a weapon? Why here
    
{
        
GivePlayerItem(clientg_curRoundWeapon);
    }

    return 
Plugin_Handled// Why Plugin_Handled here??

I'm not quite sure you understand the differences between Plugin_Handled, Plugin_Continue, and Plugin_Stop. Here you go.

By the way, Plugin_Stop is only used in timer callbacks

Last edited by headline; 10-30-2017 at 01:33.
headline is offline
ShadowRanger
Junior Member
Join Date: Aug 2017
Old 10-30-2017 , 05:34   Re: OnWeaponUse Issue
Reply With Quote #3

Quote:
Originally Posted by Headline View Post
PHP Code:
public Action OnWeaponCanUse(int clientint weapon)
{
    if (!
g_startedFunRound) return Plugin_Continue;

    if (!
IsClientInGame(client)) return Plugin_Continue;
    if (!
IsPlayerAlive(client)) return Plugin_Stop// this should return Plugin_Continue

    
if (IsValidEntity(weapon)) // This will happen any time the player's weapon is a valid weapon, is this intended behavior???
    
{
        
AcceptEntityInput(weapon"Kill"); // If you don't want them to pick the item up, return Plugin_Handled
    
}


    if (!
StrEqual(g_curRoundWeapon"weapon_knife")) // Any time this OnCanUse is called you want to give them a weapon? Why here
    
{
        
GivePlayerItem(clientg_curRoundWeapon);
    }

    return 
Plugin_Handled// Why Plugin_Handled here??

I'm not quite sure you understand the differences between Plugin_Handled, Plugin_Continue, and Plugin_Stop. Here you go.

By the way, Plugin_Stop is only used in timer callbacks
If I just return Plugin_Handled, and don't do any of the other things such as removing the dropped weapon and giving them a new weapon, whenever they walk over a weapon it creates one over it and drops it and then removes the current weapon from the player. I've got no idea why. I just want it to prevent them from picking it up and leaving them with their current weapon as well as removing the one that's created when they can't pick it up.
ShadowRanger is offline
ShadowRanger
Junior Member
Join Date: Aug 2017
Old 11-01-2017 , 22:38   Re: OnWeaponUse Issue
Reply With Quote #4

Bump
ShadowRanger is offline
MasterMind420
BANNED
Join Date: Nov 2010
Old 11-02-2017 , 02:23   Re: OnWeaponUse Issue
Reply With Quote #5

best way i know how is to use onentitycreated and hook all the entities you'd like to prevent picking up with sdkhook onplayeruse, return plugin_handled in there. if u cant figure it out, let me know and i'll give u an example on how to do it.

Last edited by MasterMind420; 11-02-2017 at 02:27.
MasterMind420 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 11-02-2017 , 03:14   Re: OnWeaponUse Issue
Reply With Quote #6

logic logic
PHP Code:
public Action OnWeaponCanUse(int clientint weapon)
{
    
// Exit if a fun round is not in progress
    
if (!g_startedFunRound) return Plugin_Continue;

    
// Run some other checks
    
if (!IsClientInGame(client)) return Plugin_Continue;
    if (!
IsPlayerAlive(client)) return Plugin_Continue;



    
char Classname[30];
    
GetEntityClassname(weaponClassnamesizeof(Classname));

    if(
StrContains(Classname"knife"false) != -1) return Plugin_Continue// allow knifes always

    
if (StrEqual(g_curRoundWeaponClassnamefalse))  return Plugin_Continue// allow roundweapon


    // Prevent the weapon from being picked up
    
if (IsValidEntity(weapon))
    {
        
AcceptEntityInput(weapon"Kill");
    }

    return 
Plugin_Handled;

__________________
Do not Private Message @me

Last edited by Bacardi; 11-02-2017 at 03:15.
Bacardi is offline
MasterMind420
BANNED
Join Date: Nov 2010
Old 11-02-2017 , 08:58   Re: OnWeaponUse Issue
Reply With Quote #7

Why remove the weapon completely?
Here's the proper way to do it...

Code:
char gClassname[64];

public void OnPluginStart()
{
	//This allows you to rehook all entities midgame, if u reload the plugin...
	for (int i = MAXPLAYERS; i <= 2048; i++)
	{
		if (IsValidEntity(i))
			OnEntityCreated(i, gClassname);
	}
}

public void OnEntityCreated(int entity, const char[] classname)
{
	if(!IsValidEntity(entity))
		return;

	char Classname[32];
	GetEntityClassname(entity, Classname, sizeof(Classname));

        if (!g_startedFunRound || StrEqual(Classname, g_curRoundWeapon))
                //false isn't necessary in the StrEqual because it defaults to false
                return;

	if (StrContains(Classname, "knife") > -1)
                return;

        //this will hook every item on the server that's created, except the knife & roundweapon
        //Anything u want to pickup will need to be return'd above
	SDKHook(entity, SDKHook_Use, OnPlayerUse);
}

public Action OnPlayerUse(int entity, int activator, int caller, UseType type, float value)
{
	return Plugin_Handled; //This prevents you from picking up the hooked entity
}
Doing it this way you should have no issues, good luck...

Last edited by MasterMind420; 11-02-2017 at 09:06.
MasterMind420 is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 11-02-2017 , 10:19   Re: OnWeaponUse Issue
Reply With Quote #8

Quote:
//this will hook every item on the server that's created, except the knife & roundweapon
//Anything u want to pickup will need to be return'd above
I see you like to live dangerously.. Even if you checked to see if the entity had "weapon_" at the beginning I would advise against hooking ever weapon entity like this. Do what Barcadi said, just hook the player's OnWeaponCanUse and just ignore the pickup. Obviously removing the weapon can be stripped out of the code.

Last edited by Mitchell; 11-02-2017 at 10:20.
Mitchell is offline
MasterMind420
BANNED
Join Date: Nov 2010
Old 11-02-2017 , 10:45   Re: OnWeaponUse Issue
Reply With Quote #9

Quote:
Originally Posted by Mitchell View Post
I see you like to live dangerously.. Even if you checked to see if the entity had "weapon_" at the beginning I would advise against hooking ever weapon entity like this. Do what Barcadi said, just hook the player's OnWeaponCanUse and just ignore the pickup. Obviously removing the weapon can be stripped out of the code.
I love living dangerously lol...but besides that i still fail to see how hooking every entity would be an issue if he properly implements blocking items/weapons he wants to pickup from being hooked. I've experimented with every method of blocking weapon pickup in L4D2...This method works flawlessly if implemented correctly...However, depending on what game he's doing this for there may be unforeseen issues that i'm unaware of but in L4D2 it works better than using any of the OnWeaponCanUse hooks trying to block weapon pickup...i've experienced issues with trying to block weapon pickup that way. OnWeaponCanUse also effects the weapons u are already carrying, preventing u from using them if you have blocked them this way. Which for him would be a none issue. He could also use OnPlayerRunCmd to block weapon pickup blocking IN_USE for specific weapons.
MasterMind420 is offline
ShadowRanger
Junior Member
Join Date: Aug 2017
Old 11-02-2017 , 10:47   Re: OnWeaponUse Issue
Reply With Quote #10

I appreciate all of your help. I tried Bacardi's method and it does work for the most part but for some reason whenever a player walks over the weapon, the gun they have equipped at the time bugs out and the hands disappear from first person and after you switch to the knife it completely removes the gun. This is the reason why I originally had the GivePlayerItem in, as a workaround for this issue. But surely I don't have to do this.

Also, how come you suggested removing the part that removes the weapon Mitchell? The only reason that's in is because I don't want a weapon to be dropped every time a player walks over one which happens without it.

I'll give MasterMind420's method a go today if I can't get the original way of doing it working. Not sure why it's proving so difficult to achieve for me.

Last edited by ShadowRanger; 11-02-2017 at 10:48.
ShadowRanger is offline
Reply



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 02:02.


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