y_groups permissions trick

Let's suppose you have this simple code

//main.pwn
#include <a_samp>
#include <YSI\y_commands>

main() {}

new  
    Group:LoggedPlayers
;

#include "submodule/impl"

public OnGameModeInit()  
{
    LoggedPlayers = Group_Create("logged");

    return 1;
}


YCMD:login(plyerid, params[], help)  
{
    if (help) return SendClientMessage(playerid, -1, "Login command");

    Group_SetPlayer(LoggedPlayers, playerid, true);
    return SendClientMessage(playerid, -1, "Logged in!");
}
//submodule/impl.inc
#include <YSI\y_hooks>

hook OnGameModeInit()  
{
    GROUP_ADD<LoggedPlayers> 
    { 
        @YCMD:foo;
    }

    return 1;
}

YCMD:foo(plyerid, params[], help)  
{
    if (help) return SendClientMessage(playerid, -1, "Foo command");

    return SendClientMessage(playerid, -1, "Only for logged in players!");
}

Idea is simple, the new module should contain a command available only for players from LoggedPlayers group. You start up your server, connect, and... "Command not found". Why is that?

Due to y_hooks works, hooks are called before the public they are hooking to - so your command permission system fails, as the group doesn't exist when it's called.

How to fix that?

We need to create custom public function first

//main.pwn
forward OnGroupPermissions();  
public OnGroupPermissions() {}  

Then hook it from our module

//submodule/impl.inc
hook OnGroupPermissions()  
{
    GROUP_ADD<LoggedPlayers> 
    { 
        @YCMD:foo;
    }
}

And finally call it after the group was created

//main.pwn
public OnGameModeInit()  
{
    LoggedPlayers = Group_Create("logged");

    CallLocalFunction("OnGroupPermissions", "");

    return 1;
}

Now every new module can hook OnGroupPermissions and assign group permissions to its commands.