plugins/adminmenu.sp
author Borja Ferrer <faluco@alliedmods.net>
Mon Oct 01 21:20:30 2007 +0000 (2007-10-01)
changeset 1515 3e112467aacd
parent 1510 de71b8774afc
child 1518 5c2bbd2506fc
permissions -rw-r--r--
fixed amb991, dispatchkeyvalue natives had wrong offsets
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Anti-Flood Plugin
     5  * Protects against chat flooding.
     6  *
     7  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    10  * This program is free software; you can redistribute it and/or modify it under
    11  * the terms of the GNU General Public License, version 3.0, as published by the
    12  * Free Software Foundation.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but WITHOUT
    15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    17  * details.
    18  *
    19  * You should have received a copy of the GNU General Public License along with
    20  * this program.  If not, see <http://www.gnu.org/licenses/>.
    21  *
    22  * As a special exception, AlliedModders LLC gives you permission to link the
    23  * code of this program (as well as its derivative works) to "Half-Life 2," the
    24  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    25  * by the Valve Corporation.  You must obey the GNU General Public License in
    26  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    27  * this exception to all derivative works.  AlliedModders LLC defines further
    28  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    29  * or <http://www.sourcemod.net/license.php>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #pragma semicolon 1
    35 
    36 #include <sourcemod>
    37 #include <topmenus>
    38 
    39 public Plugin:myinfo = 
    40 {
    41 	name = "Admin Menu",
    42 	author = "AlliedModders LLC",
    43 	description = "Administration Menu",
    44 	version = SOURCEMOD_VERSION,
    45 	url = "http://www.sourcemod.net/"
    46 };
    47 
    48 /* Forwards */
    49 new Handle:hOnAdminMenuReady = INVALID_HANDLE;
    50 new Handle:hOnAdminMenuCreated = INVALID_HANDLE;
    51 
    52 /* Menus */
    53 new Handle:hAdminMenu = INVALID_HANDLE;
    54 
    55 /* Top menu objects */
    56 new TopMenuObject:obj_playercmds = INVALID_TOPMENUOBJECT;
    57 
    58 public bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max)
    59 {
    60 	CreateNative("GetAdminTopMenu", __GetAdminTopMenu);
    61 	CreateNative("AddTargetsToMenu", __AddTargetsToMenu);
    62 	RegPluginLibrary("adminmenu");
    63 	return true;
    64 }
    65 
    66 public OnPluginStart()
    67 {
    68 	LoadTranslations("common.phrases");
    69 	
    70 	hOnAdminMenuCreated = CreateGlobalForward("OnAdminMenuCreated", ET_Ignore, Param_Cell);
    71 	hOnAdminMenuReady = CreateGlobalForward("OnAdminMenuReady", ET_Ignore, Param_Cell);
    72 
    73 	RegAdminCmd("sm_admin", Command_DisplayMenu, ADMFLAG_GENERIC, "sm_admin");
    74 }
    75 
    76 public OnAllPluginsLoaded()
    77 {
    78 	hAdminMenu = CreateTopMenu(CategoryHandler);
    79 	
    80 	obj_playercmds = AddToTopMenu(hAdminMenu, 
    81 		"PlayerCommands",
    82 		TopMenuObject_Category,
    83 		CategoryHandler,
    84 		INVALID_TOPMENUOBJECT);
    85 	
    86 	Call_StartForward(hOnAdminMenuCreated);
    87 	Call_PushCell(hAdminMenu);
    88 	Call_Finish();
    89 	
    90 	Call_StartForward(hOnAdminMenuReady);
    91 	Call_PushCell(hAdminMenu);
    92 	Call_Finish();
    93 }
    94 
    95 public CategoryHandler(Handle:topmenu, 
    96 						TopMenuAction:action,
    97 						TopMenuObject:object_id,
    98 						param,
    99 						String:buffer[],
   100 						maxlength)
   101 {
   102 	if (action == TopMenuAction_DrawTitle)
   103 	{
   104 		if (object_id == INVALID_TOPMENUOBJECT)
   105 		{
   106 			Format(buffer, maxlength, "%T:", "Admin Menu", param);
   107 		}
   108 		else if (object_id == obj_playercmds)
   109 		{
   110 			Format(buffer, maxlength, "%T:", "Player Commands", param);
   111 		}
   112 	}
   113 	else if (action == TopMenuAction_DrawOption)
   114 	{
   115 		if (object_id == obj_playercmds)
   116 		{
   117 			Format(buffer, maxlength, "%T", "Player Commands", param);
   118 		}
   119 		return ITEMDRAW_DEFAULT;
   120 	}
   121 	
   122 	return 0;
   123 }
   124 
   125 public __GetAdminTopMenu(Handle:plugin, numParams)
   126 {
   127 	return _:hAdminMenu;
   128 }
   129 
   130 public __AddTargetsToMenu(Handle:plugin, numParams)
   131 {
   132 	return UTIL_AddTargetsToMenu(GetNativeCell(1), GetNativeCell(2), GetNativeCell(3));
   133 }
   134 
   135 public Action:Command_DisplayMenu(client, args)
   136 {
   137 	DisplayTopMenu(hAdminMenu, client, TopMenuPosition_Start);
   138 	
   139 	return Plugin_Handled;
   140 }
   141 
   142 stock UTIL_AddTargetsToMenu(Handle:menu, source_client, bool:in_game_only)
   143 {
   144 	new max_clients = GetMaxClients();
   145 	decl String:user_id[12];
   146 	decl String:name[MAX_NAME_LENGTH];
   147 	decl String:display[MAX_NAME_LENGTH+12];
   148 	
   149 	new num_clients;
   150 	
   151 	for (new i = 1; i <= max_clients; i++)
   152 	{
   153 		if (!IsClientConnected(i) || IsClientInKickQueue(i))
   154 		{
   155 			continue;
   156 		}
   157 		
   158 		if (in_game_only && !IsClientInGame(i))
   159 		{
   160 			continue;
   161 		}
   162 		
   163 		if (source_client && !CanUserTarget(source_client, i))
   164 		{
   165 			continue;
   166 		}
   167 		
   168 		IntToString(GetClientUserId(i), user_id, sizeof(user_id));
   169 		GetClientName(i, name, sizeof(name));
   170 		Format(display, sizeof(display), "%s (%s)", name, user_id);
   171 		AddMenuItem(menu, user_id, display);
   172 		num_clients++;
   173 	}
   174 	
   175 	return num_clients;
   176 }