plugins/adminmenu.sp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Oct 11 19:47:20 2011 -0400 (2011-10-11)
changeset 3399 c81e3ec1123a
parent 2920 1d5645ab7ae0
permissions -rw-r--r--
Added better Nuclear Dawn compatibility to base plugins (bug 5117, r=fyren).
     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-2008 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 #include "adminmenu/dynamicmenu.sp"
    61 
    62 public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
    63 {
    64 	CreateNative("GetAdminTopMenu", __GetAdminTopMenu);
    65 	CreateNative("AddTargetsToMenu", __AddTargetsToMenu);
    66 	CreateNative("AddTargetsToMenu2", __AddTargetsToMenu2);
    67 	RegPluginLibrary("adminmenu");
    68 	return APLRes_Success;
    69 }
    70 
    71 public OnPluginStart()
    72 {
    73 	LoadTranslations("common.phrases");
    74 	LoadTranslations("adminmenu.phrases");
    75 	
    76 	hOnAdminMenuCreated = CreateGlobalForward("OnAdminMenuCreated", ET_Ignore, Param_Cell);
    77 	hOnAdminMenuReady = CreateGlobalForward("OnAdminMenuReady", ET_Ignore, Param_Cell);
    78 
    79 	RegAdminCmd("sm_admin", Command_DisplayMenu, ADMFLAG_GENERIC, "Displays the admin menu");
    80 }
    81 
    82 public OnConfigsExecuted()
    83 {
    84 	decl String:path[PLATFORM_MAX_PATH];
    85 	decl String:error[256];
    86 	
    87 	BuildPath(Path_SM, path, sizeof(path), "configs/adminmenu_sorting.txt");
    88 	
    89 	if (!LoadTopMenuConfig(hAdminMenu, path, error, sizeof(error)))
    90 	{
    91 		LogError("Could not load admin menu config (file \"%s\": %s)", path, error);
    92 		return;
    93 	}
    94 }
    95 
    96 public OnMapStart()
    97 {
    98 	ParseConfigs();
    99 }
   100 
   101 public OnAllPluginsLoaded()
   102 {
   103 	hAdminMenu = CreateTopMenu(DefaultCategoryHandler);
   104 	
   105 	obj_playercmds = AddToTopMenu(hAdminMenu, 
   106 		"PlayerCommands",
   107 		TopMenuObject_Category,
   108 		DefaultCategoryHandler,
   109 		INVALID_TOPMENUOBJECT);
   110 
   111 	obj_servercmds = AddToTopMenu(hAdminMenu,
   112 		"ServerCommands",
   113 		TopMenuObject_Category,
   114 		DefaultCategoryHandler,
   115 		INVALID_TOPMENUOBJECT);
   116 
   117 	obj_votingcmds = AddToTopMenu(hAdminMenu,
   118 		"VotingCommands",
   119 		TopMenuObject_Category,
   120 		DefaultCategoryHandler,
   121 		INVALID_TOPMENUOBJECT);
   122 		
   123 	BuildDynamicMenu();
   124 	
   125 	Call_StartForward(hOnAdminMenuCreated);
   126 	Call_PushCell(hAdminMenu);
   127 	Call_Finish();
   128 	
   129 	Call_StartForward(hOnAdminMenuReady);
   130 	Call_PushCell(hAdminMenu);
   131 	Call_Finish();
   132 }
   133 
   134 public DefaultCategoryHandler(Handle:topmenu, 
   135 						TopMenuAction:action,
   136 						TopMenuObject:object_id,
   137 						param,
   138 						String:buffer[],
   139 						maxlength)
   140 {
   141 	if (action == TopMenuAction_DisplayTitle)
   142 	{
   143 		if (object_id == INVALID_TOPMENUOBJECT)
   144 		{
   145 			Format(buffer, maxlength, "%T:", "Admin Menu", param);
   146 		}
   147 		else if (object_id == obj_playercmds)
   148 		{
   149 			Format(buffer, maxlength, "%T:", "Player Commands", param);
   150 		}
   151 		else if (object_id == obj_servercmds)
   152 		{
   153 			Format(buffer, maxlength, "%T:", "Server Commands", param);
   154 		}
   155 		else if (object_id == obj_votingcmds)
   156 		{
   157 			Format(buffer, maxlength, "%T:", "Voting Commands", param);
   158 		}
   159 	}
   160 	else if (action == TopMenuAction_DisplayOption)
   161 	{
   162 		if (object_id == obj_playercmds)
   163 		{
   164 			Format(buffer, maxlength, "%T", "Player Commands", param);
   165 		}
   166 		else if (object_id == obj_servercmds)
   167 		{
   168 			Format(buffer, maxlength, "%T", "Server Commands", param);
   169 		}
   170 		else if (object_id == obj_votingcmds)
   171 		{
   172 			Format(buffer, maxlength, "%T", "Voting Commands", param);
   173 		}
   174 	}
   175 }
   176 
   177 public __GetAdminTopMenu(Handle:plugin, numParams)
   178 {
   179 	return _:hAdminMenu;
   180 }
   181 
   182 public __AddTargetsToMenu(Handle:plugin, numParams)
   183 {
   184 	new bool:alive_only = false;
   185 	
   186 	if (numParams >= 4)
   187 	{
   188 		alive_only = GetNativeCell(4);
   189 	}
   190 	
   191 	return UTIL_AddTargetsToMenu(GetNativeCell(1), GetNativeCell(2), GetNativeCell(3), alive_only);
   192 }
   193 
   194 public __AddTargetsToMenu2(Handle:plugin, numParams)
   195 {
   196 	return UTIL_AddTargetsToMenu2(GetNativeCell(1), GetNativeCell(2), GetNativeCell(3));
   197 }
   198 
   199 public Action:Command_DisplayMenu(client, args)
   200 {
   201 	if (client == 0)
   202 	{
   203 		ReplyToCommand(client, "[SM] %t", "Command is in-game only");
   204 		return Plugin_Handled;
   205 	}
   206 	
   207 	DisplayTopMenu(hAdminMenu, client, TopMenuPosition_Start);
   208 	
   209 	return Plugin_Handled;
   210 }
   211 
   212 stock UTIL_AddTargetsToMenu2(Handle:menu, source_client, flags)
   213 {
   214 	decl String:user_id[12];
   215 	decl String:name[MAX_NAME_LENGTH];
   216 	decl String:display[MAX_NAME_LENGTH+12];
   217 	
   218 	new num_clients;
   219 	
   220 	for (new i = 1; i <= MaxClients; i++)
   221 	{
   222 		if (!IsClientConnected(i) || IsClientInKickQueue(i))
   223 		{
   224 			continue;
   225 		}
   226 		
   227 		if (((flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS)
   228 			&& IsFakeClient(i))
   229 		{
   230 			continue;
   231 		}
   232 		
   233 		if (((flags & COMMAND_FILTER_CONNECTED) != COMMAND_FILTER_CONNECTED)
   234 			&& !IsClientInGame(i))
   235 		{
   236 			continue;
   237 		}
   238 		
   239 		if (((flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE) 
   240 			&& !IsPlayerAlive(i))
   241 		{
   242 			continue;
   243 		}
   244 		
   245 		if (((flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD)
   246 			&& IsPlayerAlive(i))
   247 		{
   248 			continue;
   249 		}
   250 		
   251 		if ((source_client && ((flags & COMMAND_FILTER_NO_IMMUNITY) != COMMAND_FILTER_NO_IMMUNITY))
   252 			&& !CanUserTarget(source_client, i))
   253 		{
   254 			continue;
   255 		}
   256 		
   257 		IntToString(GetClientUserId(i), user_id, sizeof(user_id));
   258 		GetClientName(i, name, sizeof(name));
   259 		Format(display, sizeof(display), "%s (%s)", name, user_id);
   260 		AddMenuItem(menu, user_id, display);
   261 		num_clients++;
   262 	}
   263 	
   264 	return num_clients;
   265 }
   266 
   267 stock UTIL_AddTargetsToMenu(Handle:menu, source_client, bool:in_game_only, bool:alive_only)
   268 {
   269 	new flags = 0;
   270 	
   271 	if (!in_game_only)
   272 	{
   273 		flags |= COMMAND_FILTER_CONNECTED;
   274 	}
   275 	
   276 	if (alive_only)
   277 	{
   278 		flags |= COMMAND_FILTER_ALIVE;
   279 	}
   280 	
   281 	return UTIL_AddTargetsToMenu2(menu, source_client, flags);
   282 }
   283