Macro - Code after body -
Programie - 20.02.2012
Hi,
I'm using zcmd for the player commands in my script.
Now I want to add some admin commands. In every script I have to check if the player has the required permission.
Currently I'm doing it this way:
Код:
CMD:makeadmin(playerid, params[])
{
if (IsAdmin(playerid, PERMISSION_COMMAND_MAKEADMIN))
{
// Some code executed if the player has the permission
}
else
{
return COMMAND_PERMISSION_DENIED;
}
}
That's unsafe because I can forget to add the IsAdmin check.
So I want to convert this code into a macro. I want to use something like this to write my admin commands:
Код:
ACMD:makeadmin(playerid, params[])
{
// Some code executed if the player has the permission
}
IsAdmin should be called in the macro and use the constant "PERMISSION_COMMAND_{command}" as the second parameter.
Is that possible?
Re: Macro - Code after body -
MP2 - 20.02.2012
I believe ******' y_commands has built-in support for stuff like this, check it out.
Re: Macro - Code after body -
Programie - 20.02.2012
Quote:
Originally Posted by MP2
I believe ******' y_commands has built-in support for stuff like this, check it out.
|
Wooow that was fast!
I will try y_commands.
Re: Macro - Code after body -
Cameltoe - 20.02.2012
Zcmd also allows the user to do this.
pawn Код:
public OnPlayerCommandPerformed(playerid, cmdtext[], success)
{
if (IsAdmin(playerid, PERMISSION_COMMAND_MAKEADMIN))
{
return 1;
}
else
{
return 0;
}
}
Re: Macro - Code after body -
Programie - 20.02.2012
Quote:
Originally Posted by Cameltoe
Zcmd also allows the user to do this.
pawn Код:
public OnPlayerCommandPerformed(playerid, cmdtext[], success) { if (IsAdmin(playerid, PERMISSION_COMMAND_MAKEADMIN)) { return 1; } else { return 0; } }
|
I know, but then I still have to manually add the check for every command. And that is not what I wanted.
What I want is to decrease the script size of my commands so I don't have to add the IsAdmin check every time.
Re: Macro - Code after body -
Programie - 20.02.2012
Quote:
Originally Posted by ******
Yes and no. You can't make "makeadmin" upper-case, so you can only generate code with "PERMISSION_COMMAND_makeadmin". If that will do you have:
pawn Код:
#define ACMD:%0(%1) CMD:%0(%1) if (!IsAdmin(playerid, PERMISSION_COMMAND_%0)) return COMMAND_PERMISSION_DENIED; else
|
The other solution is to write every command in uppercase.
Код:
ACMD:MAKEADMIN(playerid, params[])
Hmm, or another way:
Код:
#define ACMD:%0(%1,%2,%3) CMD:%0(%1, %2) if (!IsAdmin(playerid, %3)) return COMMAND_PERMISSION_DENIED; else
Pro: I can use permission groups like PERMISSION_SERVERADMIN to set one permission for multiple commands.
Contra: I have to add the required permission constant to each command.
//EDIT: Ok, that works.
But this will crash the compiler:
Код:
#define ACMD:%0(%1,%2,%3) COMMAND:%0(%1, %2) if (!HasPermission(%1, PERMISSION_%3)) return COMMAND_PERMISSION_DENIED; else
I've changed IsAdmin to HasPermission and CMD to COMMAND. CMD is just an alias of COMMAND in zcmd.
If I change "PERMISSION_%3" to "%3" it works...
// EDIT 2: I found the problem. There is a space before %3... Is there a way to remove it without removing the space everywhere?
Re: Macro - Code after body -
Programie - 20.02.2012
OK, I've done it this way:
Writing PERMISSION_{permission} everytime in the command definition.
Код:
ACMD:makeadmin(playerid, params[], PERMISSION_COMMAND_MAKEADMIN)
Hey, that's shorter then writing the following everytime:
Код:
CMD:makeadmin(playerid, params[])
{
if (!HasPermission(playerid, PERMISSION_COMMAND_MAKEADMIN)) return COMMAND_PERMISSION_DENIED;
// My code
}
And it's flexible enough to easily change the permission check code in the future.
Thanks for the helpful replies!
Re: Macro - Code after body -
Programie - 20.02.2012
Quote:
Originally Posted by ******
You could even do it another way by changing the syntax. Currently "PERMISSION_COMMAND_MAKEADMIN" looks like a parameter to the command, which it isn't:
pawn Код:
#define ACMD:%0[%3](%1,%2) COMMAND:%0(%1, %2) if (!HasPermission(%1, PERMISSION_%3)) return COMMAND_PERMISSION_DENIED; else
pawn Код:
ACMD:makeadmin[MAKEADMIN](playerid, params[])
I'm a big fan of new syntax for new features, but others aren't.
|
Yeah, that looks cool. I will do it this way.
*changes code to new syntax*