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

Cancelling a player hurt event


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 06-24-2017 , 13:46   Cancelling a player hurt event
Reply With Quote #1

Hello,
How can I cancel a player hurt event?
What I'm talking about is that I want to make it so a Terrorist can't hurt a Counter-Terrorist during a specific time.

This is the code I've got so far:
PHP Code:
public void OnPlayerHurt(Event event, const char[] namebool dontBroadcast) {
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    
int attacker GetClientOfUserId(GetEventInt(event"attacker"));
    
    if(
hnsActive == && GetClientTeam(client) == CS_TEAM_CT && cvHnSGod.IntValue == 1) {
        if(!
IsFakeClient(attacker) && GetClientTeam(attacker) == CS_TEAM_T && GetClientTeam(client) == CS_TEAM_CT) {
            
event.Cancel(); // This was supposed to cancel the actual withdrawal of HP from the victim
            
CPrintToChat(attacker"%s %t"cmenuPrefix"No Rebel HnS");
        }
    }

So event.Cancel() doesn't seem to work. How can I do this in another way?
__________________
condolent is offline
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 06-24-2017 , 14:22   Re: Cancelling a player hurt event
Reply With Quote #2

Use SDKHook TakeDamage event.
Kailo is offline
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 06-24-2017 , 14:32   Re: Cancelling a player hurt event
Reply With Quote #3

Quote:
Originally Posted by Kailo View Post
Use SDKHook TakeDamage event.
So after the information I found; this should do the trick, correct?

PHP Code:
public Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetypeint &weaponfloat damageForce[3], float damagePosition[3], int damagecustom) {
    if(
hnsActive == && GetClientTeam(victim) == CS_TEAM_CT && cvHnSGod.IntValue == 1) {
        if(!
IsFakeClient(attacker) && GetClientTeam(attacker) == CS_TEAM_T) {
            
CPrintToChat(attacker"%s %t"cmenuPrefix"No Rebel HnS");
            return 
Plugin_Handled// Stops the damage from being done
        
}
    }

__________________
condolent is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 06-24-2017 , 15:47   Re: Cancelling a player hurt event
Reply With Quote #4

try it
8guawong is offline
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 06-24-2017 , 15:49   Re: Cancelling a player hurt event
Reply With Quote #5

Quote:
Originally Posted by 8guawong View Post
try it
I did and it didn't work.
It didn't exec at all, neither did it block the damage nor did it print a message to the attacker
__________________

Last edited by condolent; 06-24-2017 at 15:50.
condolent is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 06-24-2017 , 15:53   Re: Cancelling a player hurt event
Reply With Quote #6

Did you hook all clients?
__________________
WildCard65 is offline
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 06-24-2017 , 16:59   Re: Cancelling a player hurt event
Reply With Quote #7

Quote:
Originally Posted by WildCard65 View Post
Did you hook all clients?
Never really used SDKHooks, so no I guess
__________________
condolent is offline
Chaosxk
Veteran Member
Join Date: Aug 2010
Location: Westeros
Old 06-24-2017 , 22:07   Re: Cancelling a player hurt event
Reply With Quote #8

Hook like so:
PHP Code:
public void OnPluginStart()
{
    
//Hook all clients in server with SDKHooks
    
for (int i 1<= MaxClientsi++)
    {
        if (!
IsClientInGame(i))
            continue;
        
SDKHook(iSDKHook_OnTakeDamageOnTakeDamage);
    }
}

//Hook connecting clients with SDKHooks
public void OnClientPutInServer(int client)
{
    
SDKHook(clientSDKHook_OnTakeDamageOnTakeDamage);

Also your OnTakeDamage callback, check if victim and attacker are between 1 <= MaxClients before GetClientTeam, otherwise your going to get some errors in your logs.
__________________

Last edited by Chaosxk; 06-24-2017 at 22:07.
Chaosxk is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 06-24-2017 , 23:06   Re: Cancelling a player hurt event
Reply With Quote #9

You should hook SDKHook_OnTakeDamageAlive to properly cancel damage.
__________________
Neuro Toxin is offline
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 06-25-2017 , 04:31   Re: Cancelling a player hurt event
Reply With Quote #10

Quote:
Originally Posted by Chaosxk View Post
Hook like so:
PHP Code:
public void OnPluginStart()
{
    
//Hook all clients in server with SDKHooks
    
for (int i 1<= MaxClientsi++)
    {
        if (!
IsClientInGame(i))
            continue;
        
SDKHook(iSDKHook_OnTakeDamageOnTakeDamage);
    }
}

//Hook connecting clients with SDKHooks
public void OnClientPutInServer(int client)
{
    
SDKHook(clientSDKHook_OnTakeDamageOnTakeDamage);

Also your OnTakeDamage callback, check if victim and attacker are between 1 <= MaxClients before GetClientTeam, otherwise your going to get some errors in your logs.
Thanks!
I made sort of the same code after my post yesterday but it was in OnMapStart, didn't realize that wouldn't work too well with newly connected players ;)

Quote:
Originally Posted by Neuro Toxin View Post
You should hook SDKHook_OnTakeDamageAlive to properly cancel damage.
Okay, but how would I cancel it? Is it via a return Plugin_Stop or something like that?
__________________
condolent 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 05:07.


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