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