SA-MP Forums Archive
[Include] y_groups - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] y_groups (/showthread.php?tid=571057)



y_groups - dominik523 - 15.04.2015

y_groups

Introduction
What are groups? An admin level is a group of players, a gang is a group of players, a team is a group of players, a faction is group of players - why have different code for each of these things? y_groups is an abstraction of all of these things now supported by most of the YSI libraries to allow you to define exactly who can do what.

Download
This library is a part of YSI, which can be found here. Keep your eye on that topic and your server log for updates.

YSI Download Topic

Use
To demonstrate the use of the group system, this is an example of an admin "/ban" command:
Код:
#include <YSI\y_commands>
#include <YSI\y_groups>

new
	Group:g_grAdmin;

public OnGameModeInit()
{
	// Create the admin group.
	g_grAdmin = Group_Create("admins");
	new
		cmd = Command_GetID("ban");
	// Make normal people unable to use this command.
	Group_SetGlobalCommand(cmd, false);
	// Make admins allowed to use this command.
	Group_SetCommand(g_grAdmin, cmd, true);
}

public OnPlayerLogin(playerid)
{
	// Some sort of admin check.
	if (/* player is an admin */)
	{
		Group_SetPlayer(g_grAdmin, playerid, true);
	}
}

YCMD:ban(playerid, params[], help)
{
	if (help)
	{
		return SendClientMessage(playerid, 0xFF0000AA, "Bans a given player");
	}
	else
	{
		new
			pid;
		if (sscanf(params, "r", pid)) return SendClientMessage(playerid, 0xFF0000AA, "Usage: /ban <id/name>.");
		else if (pid == INVALID_PLAYER_ID) return SendClientMessage(playerid, 0xFF0000AA, "Invalid player to be banned.");
		else
		{
			Ban(pid);
			return SendClientMessage(playerid, 0xFF0000AA, "Player banned.");
		}
	}
}
Notice that there is no permission checking in the command at all - which makes it VERY easy to dynamically update who can and can't use the ban command. You may decide you want to let a normal user use it, in which case the following bit of code could be used:
Код:
Command_SetPlayerNamed("ban", playerid, true);
Players can be in multiple groups - if ANY of the groups they are in can use a feature, they can use a feature. There are plans to add an "exclude" option, so if a player is in a group who are banned from a feature then they can't use it, even if they are in another group which can. This could be useful for making people jailed - just make a "Jailed" group and exclude it from using everything, then people can be easily jailed without loosing all their other information.

Foreach
YSI groups are foreach compatible - just specify which group you would like to loop through the players of:
Код:
new Group:a = Group_Create("group");

Group_SetPlayer(a, 5, true);
Group_SetPlayer(a, 7, true);
Group_SetPlayer(a, 89, true);
Group_SetPlayer(a, 50, true);

foreach (Group(a), playerid)
{
    printf("%d", playerid);
}
That code will give an output of:
Код:
5
7
50
89
Global Group
The global group is the group of all players, and always exists. Everyone is always in the global group, you can't be removed at all, and by default the global group can do everything. If you create a new command, the global group can use it - you have to explicitly remove something from the global group, as shown in the example above.

If you know the old YSI system, this was called the "default" group, but I've had to change it to "global" or functions listed later would be called "SetDefaultDefault".

Distribution

This system uses the YSI master system, which means that if you create a group in one script, you can instantly use it from any other running YSI script. So your nice admin filterscript can define who are admins, and your gamemode can use that information in commands.

Functions

The functions provided by the group system are actually dynamic depending on what other YSI libraries you include. There are the basic functions and library specific functions. Note that if you do not include y_groups, the library specific functions will not exist, neither will they exist if you don't include the library with which they work. Footnote

*These functions are defined automatically - look in "YSI\internal\y_groupsecond.inc" for the code if you're interested.

Note:

This thread was originally written by ******. I'm just reposting it here.