Raised This Month: $114 Target: $400
 28% 

Execute command from a menu


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Exanero
Junior Member
Join Date: Dec 2018
Old 12-28-2018 , 15:03   Execute command from a menu
Reply With Quote #1

Hello! So i'm currently in the proccess of writing my very first SourceMod plugin. A VIP plguin (Menu & Commands) for my community. Though i am having some problems when trying to execute commands.

I have gotten so far so if the player writes the commands (!vipheal) or uses the menu and selects that option it prints in the chat "Fullt HP" (Swedish for full hp) BUT it does not give them full HP. I have searched all through the web and tried many different things but i can't seem to get it to work. Any help would be appreciated!

if they use the menu.
NOTE: It does print "Fullt HP" in the chat for that player. SO that part if working.
Code:
case 2:
			{
				PrintToChat(param1, "Fullt HP");
				PrintToServer("sm_hp %d 100", param1);
			}
If they write !vipheal or /vipheal
Code:
public Action:Cmd_vipHeal(int client, int args) 
{
	PrintToChat(client, "Fullt HP");

	ServerCommand("sm_hp %s 100", client);
		
	return Plugin_Handled;
}

Last edited by Exanero; 12-28-2018 at 15:45.
Exanero is offline
sdz
Senior Member
Join Date: Feb 2012
Location: United States
Old 12-28-2018 , 15:08   Re: Execute command from a menu
Reply With Quote #2

first things first i can't recommend mixing the two syntaxes, it looks bad and inconsistent as well as people on this forum will cry for weeks on end
continuing on - the blow example is wrong, followed by the correct method: %s is used for strings and/or char arrays, you'll want %i or %d for integers, or alternatively %N for the client's name (only if passing a client index, this is sourcemod specific), however none of these are recommended at all

PHP Code:
//bad 
public Action Cmd_vipHeal(int clientint args
{
    
PrintToChat(client"Fullt HP");

    
ServerCommand("sm_hp %s 100"client);
        
    return 
Plugin_Handled;
}

//"good"
public Action Cmd_vipHeal(int clientint args
{
    
PrintToChat(client"Fullt HP");
    
/* If we really wanted to do it this way, this is how: 
    ServerCommand("sm_hp %i 100", GetClientUserId(client));
    Better than calling and passing a client index through a servercommand, https://sm.alliedmods.net/new-api/entity_prop_stocks/SetEntityHealth
    */
    
SetEntityHealth(client100);
    return 
Plugin_Handled;

and I think you'll just need to use SetEntityHealth in your menu handler
just to clarify, clients are edicts, which are entities
__________________
lol

Last edited by sdz; 12-28-2018 at 15:12.
sdz is offline
Exanero
Junior Member
Join Date: Dec 2018
Old 12-28-2018 , 15:12   Re: Execute command from a menu
Reply With Quote #3

Thanks so much for the fast response!

First of all, about the using both ServerCommand() and PrintToServer() That is me just testing if anything works, ofcourse i will NOT use both when it gets to it, but thanks for pointing it out!

Secondly, is the error that i'm using "%s"? When i use a command in-game i get an error in the console:
"Tried to look up command say as if it were a variable."

if that's related (ofcourse it is somehow but i don't know if it's the problem)

Secondly.
The set_entity_health. Yeah that might work, thought i also have an option to respawn and set gravity where i also want to use the sm_gravity / sm_respawn commands. How would i go about it there? Is it still the "%s" usage that is the problem and what i should use then?

Thanks again for the answer

Last edited by Exanero; 12-28-2018 at 15:17.
Exanero is offline
Exanero
Junior Member
Join Date: Dec 2018
Old 12-28-2018 , 15:48   Re: Execute command from a menu
Reply With Quote #4

I tried the
SetEntityHealth(client, 100);
And that worked fine. So thanks!

I still have my problem for the Revive and gravity. Any clues?
Exanero is offline
sdz
Senior Member
Join Date: Feb 2012
Location: United States
Old 12-28-2018 , 16:45   Re: Execute command from a menu
Reply With Quote #5

https://sm.alliedmods.net/new-api/en...tEntityGravity

depending on your game:
cstrike/csgo https://sm.alliedmods.net/new-api/cs..._RespawnPlayer

tf2 https://sm.alliedmods.net/new-api/tf2/TF2_RespawnPlayer
__________________
lol
sdz is offline
XiLuo
Junior Member
Join Date: Mar 2018
Old 12-28-2018 , 23:13   Re: Execute command from a menu
Reply With Quote #6

If you want to use some commands on server like console command with 'sv_cheats 1'
Such as 'give health' , 'give ammo' and so on.
In l4d2 I usually use the function that is
PHP Code:
stock CheatCommand(clientString:command[], String:argument1[])
{
    new 
userFlags GetUserFlagBits(client);
    
SetUserFlagBits(clientADMFLAG_ROOT);
    new 
flags GetCommandFlags(command);
    
SetCommandFlags(commandflags & ~FCVAR_CHEAT);
    
FakeClientCommand(client"%s %s"commandargument1);
    
SetCommandFlags(commandflags);
    
SetUserFlagBits(clientuserFlags);

This function can execute command on 'sv_cheats 0'
For example,when you want to give 100 health to yourself you can invoke CheatCommand(client,"give health","100");
first argument is a client who will execute command,second and third arguments is the command and arg.
This code use the old grammar,you and update to new if you want.
For respawn and gravity,I also suggest use the method of sidezz.The API is so convenient and easily to use.
Hope to help you and Forgive me for my poor English,thanks

Last edited by XiLuo; 12-29-2018 at 00:04.
XiLuo is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 12-28-2018 , 23:39   Re: Execute command from a menu
Reply With Quote #7

...why you change player admin bits? Not make sense.

*edit
Ok, I maybe figure it out. You want bypass admin commands...

Last edited by Bacardi; 12-28-2018 at 23:40.
Bacardi is offline
XiLuo
Junior Member
Join Date: Mar 2018
Old 12-29-2018 , 00:12   Re: Execute command from a menu
Reply With Quote #8

Quote:
Originally Posted by Bacardi View Post
...why you change player admin bits? Not make sense.

*edit
Ok, I maybe figure it out. You want bypass admin commands...
Yes,some cmds created by RegAdminCmd() can also execute by CheatCommand on general player who is not a admin
XiLuo is offline
backwards
AlliedModders Donor
Join Date: Feb 2014
Location: USA
Old 12-29-2018 , 02:21   Re: Execute command from a menu
Reply With Quote #9

DON'T EVER DO THIS!
Quote:
Originally Posted by sidezz View Post
servercommand("sm_hp %s 100", client);[/php]
Parsing a variable that a user can define into a ServerCommand function without sanitizing the string is very dangerous because it's exploitable. If the user set his name to ";quit" for example, the console command input would be "sm_hp ;quit", which would end the servers process. Or even worse they can have a name like ";rcon_password newpass" and the server will change the password so the attacker could have direct control over the server.
backwards is offline
XiLuo
Junior Member
Join Date: Mar 2018
Old 12-29-2018 , 05:16   Re: Execute command from a menu
Reply With Quote #10

Quote:
Originally Posted by 1337norway View Post
DON'T EVER DO THIS!


Parsing a variable that a user can define into a ServerCommand function without sanitizing the string is very dangerous because it's exploitable. If the user set his name to ";quit" for example, the console command input would be "sm_hp ;quit", which would end the servers process. Or even worse they can have a name like ";rcon_password newpass" and the server will change the password so the attacker could have direct control over the server.

I also ever not notice this problem though I have never use servercommand for client.I learned,thanks for your answer!
XiLuo 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 23:33.


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