plugins/adminmenu.sp
author David Anderson <dvander@alliedmods.net>
Sat Dec 22 08:57:08 2007 +0000 (2007-12-22)
changeset 1813 372939c88bc7
parent 1632 3ebff7c63316
child 1782 aa53ee1d9db1
child 1832 1f97873475e8
permissions -rw-r--r--
fixed amb1305 by making OnClientPostAdminCheck() always occur after OnClientPutInServer(). this is now GUARANTEED. it was undefined before, however the usual case was opposite. if plugins were relying on this, they were written poorly and shouldn't have worked in the first place, especially with the threaded SQL plugin.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Admin Menu Plugin
     5  * Creates the base admin menu, for plugins to add items to.
     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 new TopMenuObject:obj_servercmds = INVALID_TOPMENUOBJECT;
    58 new TopMenuObject:obj_votingcmds = INVALID_TOPMENUOBJECT;
    59 
    60 public bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max)
    61 {
    62 	CreateNative("GetAdminTopMenu", __GetAdminTopMenu);
    63 	CreateNative("AddTargetsToMenu", __AddTargetsToMenu);
    64 	RegPluginLibrary("adminmenu");
    65 	return true;
    66 }
    67 
    68 public OnPluginStart()
    69 {
    70 	LoadTranslations("common.phrases");
    71 	LoadTranslations("adminmenu.phrases");
    72 	
    73 	hOnAdminMenuCreated = CreateGlobalForward("OnAdminMenuCreated", ET_Ignore, Param_Cell);
    74 	hOnAdminMenuReady = CreateGlobalForward("OnAdminMenuReady", ET_Ignore, Param_Cell);
    75 
    76 	RegAdminCmd("sm_admin", Command_DisplayMenu, ADMFLAG_GENERIC, "Displays the admin menu");
    77 }
    78 
    79 public OnConfigsExecuted()
    80 {
    81 	decl String:path[PLATFORM_MAX_PATH];
    82 	decl String:error[256];
    83 	
    84 	BuildPath(Path_SM, path, sizeof(path), "configs/adminmenu_sorting.txt");
    85 	
    86 	if (!LoadTopMenuConfig(hAdminMenu, path, error, sizeof(error)))
    87 	{
    88 		LogError("Could not load admin menu config (file \"%s\": %s)", path, error);
    89 		return;
    90 	}
    91 }
    92 
    93 public OnAllPluginsLoaded()
    94 {
    95 	hAdminMenu = CreateTopMenu(CategoryHandler);
    96 	
    97 	obj_playercmds = AddToTopMenu(hAdminMenu, 
    98 		"PlayerCommands",
    99 		TopMenuObject_Category,
   100 		CategoryHandler,
   101 		INVALID_TOPMENUOBJECT);
   102 
   103 	obj_servercmds = AddToTopMenu(hAdminMenu,
   104 		"ServerCommands",
   105 		TopMenuObject_Category,
   106 		CategoryHandler,
   107 		INVALID_TOPMENUOBJECT);
   108 
   109 	obj_votingcmds = AddToTopMenu(hAdminMenu,
   110 		"VotingCommands",
   111 		TopMenuObject_Category,
   112 		CategoryHandler,
   113 		INVALID_TOPMENUOBJECT);
   114 	
   115 	Call_StartForward(hOnAdminMenuCreated);
   116 	Call_PushCell(hAdminMenu);
   117 	Call_Finish();
   118 	
   119 	Call_StartForward(hOnAdminMenuReady);
   120 	Call_PushCell(hAdminMenu);
   121 	Call_Finish();
   122 }
   123 
   124 public CategoryHandler(Handle:topmenu, 
   125 						TopMenuAction:action,
   126 						TopMenuObject:object_id,
   127 						param,
   128 						String:buffer[],
   129 						maxlength)
   130 {
   131 	if (action == TopMenuAction_DisplayTitle)
   132 	{
   133 		if (object_id == INVALID_TOPMENUOBJECT)
   134 		{
   135 			Format(buffer, maxlength, "%T:", "Admin Menu", param);
   136 		}
   137 		else if (object_id == obj_playercmds)
   138 		{
   139 			Format(buffer, maxlength, "%T:", "Player Commands", param);
   140 		}
   141 		else if (object_id == obj_servercmds)
   142 		{
   143 			Format(buffer, maxlength, "%T:", "Server Commands", param);
   144 		}
   145 		else if (object_id == obj_votingcmds)
   146 		{
   147 			Format(buffer, maxlength, "%T:", "Voting Commands", param);
   148 		}
   149 	}
   150 	else if (action == TopMenuAction_DisplayOption)
   151 	{
   152 		if (object_id == obj_playercmds)
   153 		{
   154 			Format(buffer, maxlength, "%T", "Player Commands", param);
   155 		}
   156 		else if (object_id == obj_servercmds)
   157 		{
   158 			Format(buffer, maxlength, "%T", "Server Commands", param);
   159 		}
   160 		else if (object_id == obj_votingcmds)
   161 		{
   162 			Format(buffer, maxlength, "%T", "Voting Commands", param);
   163 		}
   164 	}
   165 }
   166 
   167 public __GetAdminTopMenu(Handle:plugin, numParams)
   168 {
   169 	return _:hAdminMenu;
   170 }
   171 
   172 public __AddTargetsToMenu(Handle:plugin, numParams)
   173 {
   174 	new bool:alive_only = false;
   175 	
   176 	if (numParams >= 4)
   177 	{
   178 		alive_only = GetNativeCell(4);
   179 	}
   180 	
   181 	return UTIL_AddTargetsToMenu(GetNativeCell(1), GetNativeCell(2), GetNativeCell(3), alive_only);
   182 }
   183 
   184 public Action:Command_DisplayMenu(client, args)
   185 {
   186 	if (client == 0)
   187 	{
   188 		ReplyToCommand(client, "[SM] %t", "Command is in-game only");
   189 		return Plugin_Handled;
   190 	}
   191 	
   192 	DisplayTopMenu(hAdminMenu, client, TopMenuPosition_Start);
   193 	
   194 	return Plugin_Handled;
   195 }
   196 
   197 stock UTIL_AddTargetsToMenu(Handle:menu, source_client, bool:in_game_only, bool:alive_only)
   198 {
   199 	new max_clients = GetMaxClients();
   200 	decl String:user_id[12];
   201 	decl String:name[MAX_NAME_LENGTH];
   202 	decl String:display[MAX_NAME_LENGTH+12];
   203 	
   204 	new num_clients;
   205 	
   206 	for (new i = 1; i <= max_clients; i++)
   207 	{
   208 		if (!IsClientConnected(i) || IsClientInKickQueue(i))
   209 		{
   210 			continue;
   211 		}
   212 		
   213 		if (in_game_only && !IsClientInGame(i))
   214 		{
   215 			continue;
   216 		}
   217 		
   218 		if (alive_only && !IsPlayerAlive(i))
   219 		{
   220 			continue;
   221 		}
   222 		
   223 		if (source_client && !CanUserTarget(source_client, i))
   224 		{
   225 			continue;
   226 		}
   227 		
   228 		IntToString(GetClientUserId(i), user_id, sizeof(user_id));
   229 		GetClientName(i, name, sizeof(name));
   230 		Format(display, sizeof(display), "%s (%s)", name, user_id);
   231 		AddMenuItem(menu, user_id, display);
   232 		num_clients++;
   233 	}
   234 	
   235 	return num_clients;
   236 }
   237