AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved First time creating natives (https://forums.alliedmods.net/showthread.php?t=331667)

LiveAndLetDie 03-31-2021 22:51

First time creating natives
 
Hey, I am learning how to create natives and after reading the article on wiki I came up with this. Does it seem correct? At least it works but it doesn't necessarily mean that's it's 'smartly' done.

.inc file

Code:

/**
 * Check if a client has a certain admin flag.
 *
 * @param num1    a client.
 * @param num2    a flag.
 * @return        True if the client has the flag, false otherwise.
 */
 
 native bool CheckClientFlag(int client, char[] flag);

.sp file

Code:

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
        CreateNative("CheckClientFlag", Native_FlagHelper);
        return APLRes_Success;
}

public any Native_FlagHelper(Handle plugin, int numParams)
{
        int client = GetNativeCell(1);
        char flag = view_as<int>(GetNativeCell(2));
        return(CheckCommandAccess(client, "", flag, true));
}

example

Code:

CheckClientFlag(client, "b");

LiveAndLetDie 03-31-2021 23:08

Re: First time creating natives
 
And yes, I know that a native like that doesn't make much sense but it's just an example.

Cruze 04-01-2021 01:31

Re: First time creating natives
 
PHP Code:

public any Native_FlagHelper(Handle pluginint numParams)
{
    
int client GetNativeCell(1);
    
char flag[5];
        
GetNativeString(2flag5);
    return(
CheckCommandAccess(client""ReadFlagString(flag), true));


Rest seem correct to me

asherkin 04-01-2021 19:45

Re: First time creating natives
 
FYI, it isn't safe (or sane) to pass an empty command string to CheckCommandAccess.

LiveAndLetDie 04-01-2021 21:59

Re: First time creating natives
 
Quote:

Originally Posted by Cruze (Post 2742595)
PHP Code:

public any Native_FlagHelper(Handle pluginint numParams)
{
    
int client GetNativeCell(1);
    
char flag[5];
        
GetNativeString(2flag5);
    return(
CheckCommandAccess(client""ReadFlagString(flag), true));


Rest seem correct to me

Thanks!

Quote:

Originally Posted by asherkin (Post 2742691)
FYI, it isn't safe (or sane) to pass an empty command string to CheckCommandAccess.

Why is that? Especially if one doesn't have a need to check overrides or commands.

ThatKidWhoGames 04-01-2021 22:01

Re: First time creating natives
 
Quote:

Originally Posted by LiveAndLetDie (Post 2742696)
Thanks!



Why is that? Especially if one doesn't have a need to check overrides or commands.

You can directly check if they have a certain flag through GetUserAdmin and checking if they have access to a certain flag.


All times are GMT -4. The time now is 21:39.

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