plugins/adminmenu.sp
author Scott Ehlert <ds@alliedmods.net>
Fri Mar 28 06:21:34 2008 +0000 (2008-03-28)
branchsourcemod-1.1.0
changeset 1951 c7b4237fff50
parent 1823 150b81fe27d6
permissions -rw-r--r--
Changed env var used in include paths: SOURCEMM -> SORCEMM16
Removed some unneccessary build configuartations in some project files
     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 new g_maxPlayers;
    49 
    50 /* Forwards */
    51 new Handle:hOnAdminMenuReady = INVALID_HANDLE;
    52 new Handle:hOnAdminMenuCreated = INVALID_HANDLE;
    53 
    54 /* Menus */
    55 new Handle:hAdminMenu = INVALID_HANDLE;
    56 
    57 /* Top menu objects */
    58 new TopMenuObject:obj_playercmds = INVALID_TOPMENUOBJECT;
    59 new TopMenuObject:obj_servercmds = INVALID_TOPMENUOBJECT;
    60 new TopMenuObject:obj_votingcmds = INVALID_TOPMENUOBJECT;
    61 
    62 #include "adminmenu/dynamicmenu.sp"
    63 
    64 public bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max)
    65 {
    66 	CreateNative("GetAdminTopMenu", __GetAdminTopMenu);
    67 	CreateNative("AddTargetsToMenu", __AddTargetsToMenu);
    68 	RegPluginLibrary("adminmenu");
    69 	return true;
    70 }
    71 
    72 public OnPluginStart()
    73 {
    74 	LoadTranslations("common.phrases");
    75 	LoadTranslations("adminmenu.phrases");
    76 	
    77 	hOnAdminMenuCreated = CreateGlobalForward("OnAdminMenuCreated", ET_Ignore, Param_Cell);
    78 	hOnAdminMenuReady = CreateGlobalForward("OnAdminMenuReady", ET_Ignore, Param_Cell);
    79 
    80 	RegAdminCmd("sm_admin", Command_DisplayMenu, ADMFLAG_GENERIC, "Displays the admin menu");
    81 }
    82 
    83 public OnConfigsExecuted()
    84 {
    85 	decl String:path[PLATFORM_MAX_PATH];
    86 	decl String:error[256];
    87 	
    88 	BuildPath(Path_SM, path, sizeof(path), "configs/adminmenu_sorting.txt");
    89 	
    90 	if (!LoadTopMenuConfig(hAdminMenu, path, error, sizeof(error)))
    91 	{
    92 		LogError("Could not load admin menu config (file \"%s\": %s)", path, error);
    93 		return;
    94 	}
    95 }
    96 
    97 public OnMapStart()
    98 {
    99 	g_maxPlayers = GetMaxClients();
   100 	
   101 	ParseConfigs();
   102 }
   103 
   104 public OnAllPluginsLoaded()
   105 {
   106 	hAdminMenu = CreateTopMenu(DefaultCategoryHandler);
   107 	
   108 	obj_playercmds = AddToTopMenu(hAdminMenu, 
   109 		"PlayerCommands",
   110 		TopMenuObject_Category,
   111 		DefaultCategoryHandler,
   112 		INVALID_TOPMENUOBJECT);
   113 
   114 	obj_servercmds = AddToTopMenu(hAdminMenu,
   115 		"ServerCommands",
   116 		TopMenuObject_Category,
   117 		DefaultCategoryHandler,
   118 		INVALID_TOPMENUOBJECT);
   119 
   120 	obj_votingcmds = AddToTopMenu(hAdminMenu,
   121 		"VotingCommands",
   122 		TopMenuObject_Category,
   123 		DefaultCategoryHandler,
   124 		INVALID_TOPMENUOBJECT);
   125 		
   126 	BuildDynamicMenu();
   127 	
   128 	Call_StartForward(hOnAdminMenuCreated);
   129 	Call_PushCell(hAdminMenu);
   130 	Call_Finish();
   131 	
   132 	Call_StartForward(hOnAdminMenuReady);
   133 	Call_PushCell(hAdminMenu);
   134 	Call_Finish();
   135 }
   136 
   137 public DefaultCategoryHandler(Handle:topmenu, 
   138 						TopMenuAction:action,
   139 						TopMenuObject:object_id,
   140 						param,
   141 						String:buffer[],
   142 						maxlength)
   143 {
   144 	if (action == TopMenuAction_DisplayTitle)
   145 	{
   146 		if (object_id == INVALID_TOPMENUOBJECT)
   147 		{
   148 			Format(buffer, maxlength, "%T:", "Admin Menu", param);
   149 		}
   150 		else if (object_id == obj_playercmds)
   151 		{
   152 			Format(buffer, maxlength, "%T:", "Player Commands", param);
   153 		}
   154 		else if (object_id == obj_servercmds)
   155 		{
   156 			Format(buffer, maxlength, "%T:", "Server Commands", param);
   157 		}
   158 		else if (object_id == obj_votingcmds)
   159 		{
   160 			Format(buffer, maxlength, "%T:", "Voting Commands", param);
   161 		}
   162 	}
   163 	else if (action == TopMenuAction_DisplayOption)
   164 	{
   165 		if (object_id == obj_playercmds)
   166 		{
   167 			Format(buffer, maxlength, "%T", "Player Commands", param);
   168 		}
   169 		else if (object_id == obj_servercmds)
   170 		{
   171 			Format(buffer, maxlength, "%T", "Server Commands", param);
   172 		}
   173 		else if (object_id == obj_votingcmds)
   174 		{
   175 			Format(buffer, maxlength, "%T", "Voting Commands", param);
   176 		}
   177 	}
   178 }
   179 
   180 public __GetAdminTopMenu(Handle:plugin, numParams)
   181 {
   182 	return _:hAdminMenu;
   183 }
   184 
   185 public __AddTargetsToMenu(Handle:plugin, numParams)
   186 {
   187 	new bool:alive_only = false;
   188 	
   189 	if (numParams >= 4)
   190 	{
   191 		alive_only = GetNativeCell(4);
   192 	}
   193 	
   194 	return UTIL_AddTargetsToMenu(GetNativeCell(1), GetNativeCell(2), GetNativeCell(3), alive_only);
   195 }
   196 
   197 public Action:Command_DisplayMenu(client, args)
   198 {
   199 	if (client == 0)
   200 	{
   201 		ReplyToCommand(client, "[SM] %t", "Command is in-game only");
   202 		return Plugin_Handled;
   203 	}
   204 	
   205 	DisplayTopMenu(hAdminMenu, client, TopMenuPosition_Start);
   206 	
   207 	return Plugin_Handled;
   208 }
   209 
   210 stock UTIL_AddTargetsToMenu(Handle:menu, source_client, bool:in_game_only, bool:alive_only)
   211 {
   212 	new max_clients = GetMaxClients();
   213 	decl String:user_id[12];
   214 	decl String:name[MAX_NAME_LENGTH];
   215 	decl String:display[MAX_NAME_LENGTH+12];
   216 	
   217 	new num_clients;
   218 	
   219 	for (new i = 1; i <= max_clients; i++)
   220 	{
   221 		if (!IsClientConnected(i) || IsClientInKickQueue(i))
   222 		{
   223 			continue;
   224 		}
   225 		
   226 		if (in_game_only && !IsClientInGame(i))
   227 		{
   228 			continue;
   229 		}
   230 		
   231 		if (alive_only && !IsPlayerAlive(i))
   232 		{
   233 			continue;
   234 		}
   235 		
   236 		if (source_client && !CanUserTarget(source_client, i))
   237 		{
   238 			continue;
   239 		}
   240 		
   241 		IntToString(GetClientUserId(i), user_id, sizeof(user_id));
   242 		GetClientName(i, name, sizeof(name));
   243 		Format(display, sizeof(display), "%s (%s)", name, user_id);
   244 		AddMenuItem(menu, user_id, display);
   245 		num_clients++;
   246 	}
   247 	
   248 	return num_clients;
   249 }
   250