Raised This Month: $ Target: $400
 0% 

Detecting specific chat message


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Alurict
Junior Member
Join Date: Feb 2013
Old 11-26-2013 , 18:42   Detecting specific chat message
Reply With Quote #1

Hey guys,

I'm trying to build a plugin that will let people type !jumper and it'll give them a sticky jumper, regardless of what class they are.

I've set up the code so that it automatically gives people the sticky jumper when they spawn, but I have no idea how to go about detecting when they type !jumper and doing it.

Here's the code as it stands:
Code:
#include <sourcemod>
#include <tf2items>
#include <tf2_stocks>
#include <tf2items_giveweapon>

new Handle:sm_callforsticky = INVALID_HANDLE;
 
public Plugin:myinfo =
{
    name = "[TF2] Callforsticky",
    author = "Alurict",
    description = "Type !jumper to get a sticky jumper",
    version = "1",
    url = ""
};
 
public OnPluginStart()
{
    RegAdminCmd("sm_togglesticky", Command_ToggleStickyMode, ADMFLAG_SLAY);
    sm_callforsticky = CreateConVar("sm_callforsticky", "1", "1 allows people to call for a sticky jumper, 0 means they cannot");
}

public Action:Command_ToggleStickyMode(client, args)
{
    new currentstate = GetConVarInt(sm_callforsticky);
    if (currentstate == 1)
    {
        SetConVarInt(sm_callforsticky, 0);
        PrintToChatAll("[SM] '!jumper' calls disabled... :[");
        TF2_RespawnPlayer(client);
        return Plugin_Handled;
    }
    else if (currentstate == 0)
    {
        SetConVarInt(sm_callforsticky, 1);
        PrintToChatAll("[SM] Type '!jumper' to get a sticky jumper!");
        return Plugin_Handled;
    }
    else
    {
        ReplyToCommand(client, "Invalid Argument");
        return Plugin_Handled;
    }
}

public Action:Listener_Say(client, const String:command[], argc)
{
    if (!client || client > MaxClients || !IsClientInGame(client)) {
        decl String:strChat[100];
        GetCmdArgString(strChat, sizeof(strChat));
        
        if (StrContains(strChat, "!jumper")) {
            TF2_RemoveWeaponSlot( client, 1 );
            TF2Items_GiveWeapon( client, 9979);
        }
    }
    
    return Plugin_Handled;
}
I also have issues where the weapon models don't work correctly on the Heavy, Pyro, and Spy. Not sure if these are fixable, does anyone know?

Thanks!

Last edited by Alurict; 11-26-2013 at 19:50. Reason: Made code example a lot more relevant to objective
Alurict is offline
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 11-26-2013 , 19:09   Re: Detecting specific chat message
Reply With Quote #2

Code:
#include <sourcemod>
#include <tf2items>
#include <tf2_stocks>
#include <tf2items_giveweapon>

new Handle:sm_errybodysticky = INVALID_HANDLE;

public Plugin:myinfo =
{
	name = "[TF2] ErrybodySticky",
	author = "Alurict",
	description = "Gives the Sticky Jumper to all classes",
	version = "1",
	url = ""
};

public OnPluginStart()
{
	RegAdminCmd("sm_togglesticky", Command_ToggleStickyMode, ADMFLAG_SLAY);
	RegConsoleCmd("sm_jumper", GiveJumper);
	sm_errybodysticky = CreateConVar("sm_errybodysticky", "1", "1 means everybody gets a sticky jumper, 0 means they get their default weapons");

	HookEvent( "post_inventory_application", InitStickyMode );
	HookEvent( "player_spawn", InitStickyMode );
}

public Action:GiveJumper(client, args)
{
	TF2Items_GiveWeapon(client, 9979);
	return Plugin_Handled;
}

public Action:Command_ToggleStickyMode(client, args)
{
	new currentstate = GetConVarInt(sm_errybodysticky);
	switch (currentstate)
	{
		case 0:
			{
				SetConVarInt(sm_errybodysticky, 1);
				PrintToChatAll("[SM] Errybody Sticky Jump!");
				return Plugin_Handled;
			}
		case 1:
			{
				SetConVarInt(sm_errybodysticky, 0);
				PrintToChatAll("[SM] No more sticky jumping... :[");
				TF2_RespawnPlayer(client);
				return Plugin_Handled;
			}
	}
}

public InitStickyMode( Handle:hEvent, const String:strEventName[], bool:bDontBroadcast )
{    
	new iClient = GetClientOfUserId( GetEventInt( hEvent, "userid" ) );
	new cvar_errybodysticky = GetConVarInt(sm_errybodysticky);
	new class = TF2_GetPlayerClass(iClient);
	if (cvar_errybodysticky == 1)
	{
		switch (class)
		{
		case TFClass_Scout, TFClass_Pyro, TFClass_DemoMan, TFClass_Heavy, TFClass_Engineer, TFClass_Medic, TFClass_Sniper:
			{
				TF2_RemoveWeaponSlot( iClient, 1 );
				TF2Items_GiveWeapon(iClient, 9979);
			}
		case TFClass_Soldier:
			{
				TF2_RemoveWeaponSlot( iClient, 2 );
				TF2Items_GiveWeapon(iClient, 237);
			}
		}
	}
}
Didn't compile it or test it.

Last edited by Drixevel; 11-26-2013 at 19:10.
Drixevel is offline
Alurict
Junior Member
Join Date: Feb 2013
Old 11-26-2013 , 19:28   Re: Detecting specific chat message
Reply With Quote #3

That does seem to be a lot simpler way to do the item giving, but I intend to make some adjustments to the item given to each class, that's why I set it up the way I did.

Do you have any idea how to detect when a person types "!jumper" and take actions based on that?

Thanks!
Alurict is offline
Alurict
Junior Member
Join Date: Feb 2013
Old 11-26-2013 , 19:50   Re: Detecting specific chat message
Reply With Quote #4

I updated the code above to more clearly show what I'm trying to do. Sorry about the confusion.
Alurict is offline
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 11-26-2013 , 19:51   Re: Detecting specific chat message
Reply With Quote #5

Quote:
Originally Posted by Alurict View Post
I updated the code above to more clearly show what I'm trying to do. Sorry about the confusion.
With the code I posted, they can just type in !jumper or /jumper in chat and it will give them the weapon. You don't have to do any sort of say listening for that.
Drixevel is offline
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 11-26-2013 , 19:57   Re: Detecting specific chat message
Reply With Quote #6

Code:
#include <sourcemod>
#include <tf2items>
#include <tf2_stocks>
#include <tf2items_giveweapon>

public Plugin:myinfo =
{
	name = "[TF2] ErrybodySticky",
	author = "Alurict",
	description = "Gives the Sticky Jumper to all classes",
	version = "1",
	url = ""
};

public OnPluginStart()
{
	RegConsoleCmd("sm_jumper", GiveJumper);
}

public Action:GiveJumper(client, args)
{
	new class = TF2_GetPlayerClass(client);
	switch (class)
	{
		case TFClass_Scout:
			{
				TF2_RemoveWeaponSlot( iClient, 1 );
				TF2Items_GiveWeapon(iClient, 9979);
				//PrintToChat(client, "You have been given a Sticky Jumper.");
			}
		case TFClass_Soldier:
			{
				TF2_RemoveWeaponSlot( iClient, 1 );
				TF2Items_GiveWeapon(iClient, 9979);
			}
		case TFClass_Pyro:
			{
				TF2_RemoveWeaponSlot( iClient, 1 );
				TF2Items_GiveWeapon(iClient, 9979);
			}
		case TFClass_DemoMan:
			{
				TF2_RemoveWeaponSlot( iClient, 1 );
				TF2Items_GiveWeapon(iClient, 9979);
			}
		case TFClass_Heavy:
			{
				TF2_RemoveWeaponSlot( iClient, 1 );
				TF2Items_GiveWeapon(iClient, 9979);
			}
		case TFClass_Engineer:
			{
				TF2_RemoveWeaponSlot( iClient, 1 );
				TF2Items_GiveWeapon(iClient, 9979);
			}
		case TFClass_Medic:
			{
				TF2_RemoveWeaponSlot( iClient, 1 );
				TF2Items_GiveWeapon(iClient, 9979);
			}
		case TFClass_Sniper:
			{
				TF2_RemoveWeaponSlot( iClient, 1 );
				TF2Items_GiveWeapon(iClient, 9979);
			}
		case TFClass_Spy:
			{
				TF2_RemoveWeaponSlot( iClient, 1 );
				TF2Items_GiveWeapon(iClient, 9979);
			}
	}
	//PrintToChat(client, "You have been given a weapon.");
	return Plugin_Handled;
}
There's an example of a command that the clients can type on the server (IE !jumper in chat like usual) and it will give them the weapon and whatever else you put into the cases themselves for each class. I added some examples & ideas such as the PrintToChats, etc.
Drixevel is offline
NIGathan
Senior Member
Join Date: Aug 2011
Location: /dev/null
Old 11-26-2013 , 21:22   Re: Detecting specific chat message
Reply With Quote #7

When you create a command prefixed with "sm_" then sourcemod will automatically add chat commands for it. The "sm_" is dropped and replaced with either ! or /. So the RegConsoleCmd("sm_jumper", GiveJumper); line sets this up so that when a client types !jumper the function GiveJumper() will be run with the client's id as the client var, and anything they type after the command is set as a string in args.
NIGathan is offline
Alurict
Junior Member
Join Date: Feb 2013
Old 11-26-2013 , 22:10   Re: Detecting specific chat message
Reply With Quote #8

I see! I didn't know RegConsoleCmd would take care of that already. Thanks!

For anyone who is interested, here is some the (nearly) finished product:

Code:
#include <sourcemod>
#include <tf2items>
#include <tf2_stocks>
#include <tf2items_giveweapon>

new Handle:sm_jumperstate;

public Plugin:myinfo =
{
    name = "[TF2] Jumper Mode",
    author = "Alurict",
    description = "Type !jumper to get a sticky jumper",
    version = "1",
    url = ""
};

public OnPluginStart()
{
    RegAdminCmd("sm_jumpermode", Command_JumperMode, ADMFLAG_SLAY);
    sm_jumperstate = CreateConVar("sm_jumperstate", "0", "1 means calling for sticky is allowed, 0 means disabled");
    RegConsoleCmd("sm_jumper", GiveJumper);
}

public Action:Command_JumperMode(client, args)
{
    new String:arg1[2];
    new currentstate = GetConVarInt(sm_jumperstate);
    GetCmdArg(1, arg1, sizeof(arg1));
    new passmode = StringToInt(arg1);
    if (passmode == 1)
    {
        if (currentstate == 1)
        {
            PrintToAdmins("\x03[JumperMode]\x01 Jumper Already Enabled", "b");
            return;
        } else if (currentstate == 0)
        {
            SetConVarInt(sm_jumperstate, 1);
            PrintToChatAll("\x03[JumperMode]\x01 Calling for stickies enabled!");
            PrintToChatAll("\x03[JumperMode]\x01 Type '!jumper' to get a sticky or rocket jumper!");
            return;
        } else
        {
            PrintToAdmins("[Error]: Jumper Mode Plugin: ConVar Value Invalid.", "b");
            return;
        }
    }
    else if (passmode == 0)
    {
        if (currentstate == 1)
        {
            SetConVarInt(sm_jumperstate, 0);
            for(new i = 1; i <= MaxClients; i++)
            {
                if(IsClientInGame(i) && IsPlayerAlive(i))
                {
                    TF2_RespawnPlayer(i);
                }
            }
            PrintToChatAll("\x03[JumperMode]\x01 Calling for stickies disabled... :[");
            return;
        } else if (currentstate == 0)
        {
            PrintToAdmins("\x03[SM]\x01 Jumper Mode Already Disabled", "b");
            return;
        } else
        {
            PrintToAdmins("[Error]: Jumper Mode Plugin: ConVar Value Invalid.", "b");
            return;
        }
    }
    else
    {
        PrintToChatAll("[Error]: Jumper Mode Plugin: Sent Invalid Argument: %s", passmode);
        return;
    }
}

public Action:GiveJumper(client, args)
{
    if (GetConVarInt(sm_jumperstate) == 1) {
        new pClass = TF2_GetPlayerClass(client);
        switch (pClass)
        {
            case TFClass_Scout, TFClass_DemoMan, TFClass_Engineer, TFClass_Medic, TFClass_Sniper:
                {
                    TF2_RemoveWeaponSlot( client, 1 );
                    TF2Items_GiveWeapon(client, 9979);
                    PrintToChat(client, "\x03[JumperMode]\x01 You have been given a Sticky Jumper.");
                }
            case TFClass_Soldier, TFClass_Pyro, TFClass_Heavy, TFClass_Spy:
                {
                    TF2_RemoveWeaponSlot( client, 1 );
                    TF2Items_GiveWeapon(client, 237);
                    PrintToChat(client, "\x03[JumperMode]\x01 You have been given a Rocket Jumper.");
                }
        }
    } else {
        PrintToChat(client, "\x03[JumperMode]\x01 Calling for stickies currently disabled.");
    }
    return Plugin_Handled;
}

// Stocks for PrintToAdmins
stock PrintToAdmins(const String:message[], const String:flags[]) 
{ 
    for (new x = 1; x <= MaxClients; x++) 
    { 
        if (IsValidClient(x) && IsValidAdmin(x, flags)) 
        { 
            PrintToChat(x, message); 
        } 
    } 
} 

stock bool:IsValidClient(client, bool:nobots = true) 
{  
    if (client <= 0 || client > MaxClients || !IsClientConnected(client) || (nobots && IsFakeClient(client))) 
    {  
        return false;  
    }  
    return IsClientInGame(client);  
} 

stock bool:IsValidAdmin(client, const String:flags[]) 
{ 
    new ibFlags = ReadFlagString(flags); 
    if ((GetUserFlagBits(client) & ibFlags) == ibFlags) 
    { 
        return true; 
    } 
    if (GetUserFlagBits(client) & ADMFLAG_ROOT) 
    { 
        return true; 
    } 
    return false; 
}
Then I used a Custom Admin Menu with the following settings:

Code:
        "Set Jumper Mode"
        {
            "cmd"    "sm_jumpermode @1"
            "admin"    "sm_slay"
            "execute"    "server"
            "1"
            {
                "type"        "list"
                "title"        "Set Jumper"
                "1"        "1"
                "1."        "On"
                "2"        "0"
                "2."        "Off"
            }
        }
And TF2_giveweapons plugin with some custom weapons set up.

There are issues, the biggest one being that the rocket jumper model isn't visible on the heavy or spy, and that it is a little awkward looking on the pyro. I set them to use the rocket jumper instead of the sticky launcher because the sticky doesn't work at all on them.

I think I am going to set it up with a custom weapon and maybe try putting it in different weapon slots. Another thing that might be helpful is if we can somehow make the plugin store/remember the items that it replaces with the sticky jumper, and put them back in cases where Jumper Mode is disabled or when the weapon runs out of ammo.

But for now it works well enough. Thanks again r3dw3r3w0lf!

Last edited by Alurict; 11-26-2013 at 22:15.
Alurict is offline
NIGathan
Senior Member
Join Date: Aug 2011
Location: /dev/null
Old 11-26-2013 , 22:17   Re: Detecting specific chat message
Reply With Quote #9

Not only RegConsoleCmd(), but all of the functions that create a command.

That means, this command falls into this category too:
Code:
RegAdminCmd("sm_jumpermode", Command_JumperMode, ADMFLAG_SLAY);
Try typing !jumpermode into your chat

Last edited by NIGathan; 11-26-2013 at 22:17.
NIGathan is offline
Alurict
Junior Member
Join Date: Feb 2013
Old 11-26-2013 , 22:23   Re: Detecting specific chat message
Reply With Quote #10

Thanks for explaining, NIGathan. I'm not a programmer and pretty unfamiliar with sourcemod as well. I've gone through a bunch of other plugins and read them and found the bits of code that looked like what I needed, then refitted them to my own needs. The closest coding I've ever done to this is JavaScript, so I'm kind of limping through this process based on that atm, haha.

I'm sure I'm doing a bunch of stuff in the above code wrong/the hard way, lol. It works though, so there's that. =p
Alurict 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 19:28.


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