plugins/adminmenu.sp
author Scott Ehlert <ds@alliedmods.net>
Sun Mar 30 07:00:22 2008 +0000 (2008-03-30)
changeset 1952 b7aa2b626807
child 1982 26a1e1df3a93
permissions -rw-r--r--
Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Admin Menu Plugin
ds@1952
     5
 * Creates the base admin menu, for plugins to add items to.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
#pragma semicolon 1
ds@1952
    35
ds@1952
    36
#include <sourcemod>
ds@1952
    37
#include <topmenus>
ds@1952
    38
ds@1952
    39
public Plugin:myinfo = 
ds@1952
    40
{
ds@1952
    41
	name = "Admin Menu",
ds@1952
    42
	author = "AlliedModders LLC",
ds@1952
    43
	description = "Administration Menu",
ds@1952
    44
	version = SOURCEMOD_VERSION,
ds@1952
    45
	url = "http://www.sourcemod.net/"
ds@1952
    46
};
ds@1952
    47
ds@1952
    48
new g_maxPlayers;
ds@1952
    49
ds@1952
    50
/* Forwards */
ds@1952
    51
new Handle:hOnAdminMenuReady = INVALID_HANDLE;
ds@1952
    52
new Handle:hOnAdminMenuCreated = INVALID_HANDLE;
ds@1952
    53
ds@1952
    54
/* Menus */
ds@1952
    55
new Handle:hAdminMenu = INVALID_HANDLE;
ds@1952
    56
ds@1952
    57
/* Top menu objects */
ds@1952
    58
new TopMenuObject:obj_playercmds = INVALID_TOPMENUOBJECT;
ds@1952
    59
new TopMenuObject:obj_servercmds = INVALID_TOPMENUOBJECT;
ds@1952
    60
new TopMenuObject:obj_votingcmds = INVALID_TOPMENUOBJECT;
ds@1952
    61
ds@1952
    62
#include "adminmenu/dynamicmenu.sp"
ds@1952
    63
ds@1952
    64
public bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max)
ds@1952
    65
{
ds@1952
    66
	CreateNative("GetAdminTopMenu", __GetAdminTopMenu);
ds@1952
    67
	CreateNative("AddTargetsToMenu", __AddTargetsToMenu);
ds@1952
    68
	CreateNative("AddTargetsToMenu2", __AddTargetsToMenu2);
ds@1952
    69
	RegPluginLibrary("adminmenu");
ds@1952
    70
	return true;
ds@1952
    71
}
ds@1952
    72
ds@1952
    73
public OnPluginStart()
ds@1952
    74
{
ds@1952
    75
	LoadTranslations("common.phrases");
ds@1952
    76
	LoadTranslations("adminmenu.phrases");
ds@1952
    77
	
ds@1952
    78
	hOnAdminMenuCreated = CreateGlobalForward("OnAdminMenuCreated", ET_Ignore, Param_Cell);
ds@1952
    79
	hOnAdminMenuReady = CreateGlobalForward("OnAdminMenuReady", ET_Ignore, Param_Cell);
ds@1952
    80
ds@1952
    81
	RegAdminCmd("sm_admin", Command_DisplayMenu, ADMFLAG_GENERIC, "Displays the admin menu");
ds@1952
    82
}
ds@1952
    83
ds@1952
    84
public OnConfigsExecuted()
ds@1952
    85
{
ds@1952
    86
	decl String:path[PLATFORM_MAX_PATH];
ds@1952
    87
	decl String:error[256];
ds@1952
    88
	
ds@1952
    89
	BuildPath(Path_SM, path, sizeof(path), "configs/adminmenu_sorting.txt");
ds@1952
    90
	
ds@1952
    91
	if (!LoadTopMenuConfig(hAdminMenu, path, error, sizeof(error)))
ds@1952
    92
	{
ds@1952
    93
		LogError("Could not load admin menu config (file \"%s\": %s)", path, error);
ds@1952
    94
		return;
ds@1952
    95
	}
ds@1952
    96
}
ds@1952
    97
ds@1952
    98
public OnMapStart()
ds@1952
    99
{
ds@1952
   100
	g_maxPlayers = GetMaxClients();
ds@1952
   101
	
ds@1952
   102
	ParseConfigs();
ds@1952
   103
}
ds@1952
   104
ds@1952
   105
public OnAllPluginsLoaded()
ds@1952
   106
{
ds@1952
   107
	hAdminMenu = CreateTopMenu(DefaultCategoryHandler);
ds@1952
   108
	
ds@1952
   109
	obj_playercmds = AddToTopMenu(hAdminMenu, 
ds@1952
   110
		"PlayerCommands",
ds@1952
   111
		TopMenuObject_Category,
ds@1952
   112
		DefaultCategoryHandler,
ds@1952
   113
		INVALID_TOPMENUOBJECT);
ds@1952
   114
ds@1952
   115
	obj_servercmds = AddToTopMenu(hAdminMenu,
ds@1952
   116
		"ServerCommands",
ds@1952
   117
		TopMenuObject_Category,
ds@1952
   118
		DefaultCategoryHandler,
ds@1952
   119
		INVALID_TOPMENUOBJECT);
ds@1952
   120
ds@1952
   121
	obj_votingcmds = AddToTopMenu(hAdminMenu,
ds@1952
   122
		"VotingCommands",
ds@1952
   123
		TopMenuObject_Category,
ds@1952
   124
		DefaultCategoryHandler,
ds@1952
   125
		INVALID_TOPMENUOBJECT);
ds@1952
   126
		
ds@1952
   127
	BuildDynamicMenu();
ds@1952
   128
	
ds@1952
   129
	Call_StartForward(hOnAdminMenuCreated);
ds@1952
   130
	Call_PushCell(hAdminMenu);
ds@1952
   131
	Call_Finish();
ds@1952
   132
	
ds@1952
   133
	Call_StartForward(hOnAdminMenuReady);
ds@1952
   134
	Call_PushCell(hAdminMenu);
ds@1952
   135
	Call_Finish();
ds@1952
   136
}
ds@1952
   137
ds@1952
   138
public DefaultCategoryHandler(Handle:topmenu, 
ds@1952
   139
						TopMenuAction:action,
ds@1952
   140
						TopMenuObject:object_id,
ds@1952
   141
						param,
ds@1952
   142
						String:buffer[],
ds@1952
   143
						maxlength)
ds@1952
   144
{
ds@1952
   145
	if (action == TopMenuAction_DisplayTitle)
ds@1952
   146
	{
ds@1952
   147
		if (object_id == INVALID_TOPMENUOBJECT)
ds@1952
   148
		{
ds@1952
   149
			Format(buffer, maxlength, "%T:", "Admin Menu", param);
ds@1952
   150
		}
ds@1952
   151
		else if (object_id == obj_playercmds)
ds@1952
   152
		{
ds@1952
   153
			Format(buffer, maxlength, "%T:", "Player Commands", param);
ds@1952
   154
		}
ds@1952
   155
		else if (object_id == obj_servercmds)
ds@1952
   156
		{
ds@1952
   157
			Format(buffer, maxlength, "%T:", "Server Commands", param);
ds@1952
   158
		}
ds@1952
   159
		else if (object_id == obj_votingcmds)
ds@1952
   160
		{
ds@1952
   161
			Format(buffer, maxlength, "%T:", "Voting Commands", param);
ds@1952
   162
		}
ds@1952
   163
	}
ds@1952
   164
	else if (action == TopMenuAction_DisplayOption)
ds@1952
   165
	{
ds@1952
   166
		if (object_id == obj_playercmds)
ds@1952
   167
		{
ds@1952
   168
			Format(buffer, maxlength, "%T", "Player Commands", param);
ds@1952
   169
		}
ds@1952
   170
		else if (object_id == obj_servercmds)
ds@1952
   171
		{
ds@1952
   172
			Format(buffer, maxlength, "%T", "Server Commands", param);
ds@1952
   173
		}
ds@1952
   174
		else if (object_id == obj_votingcmds)
ds@1952
   175
		{
ds@1952
   176
			Format(buffer, maxlength, "%T", "Voting Commands", param);
ds@1952
   177
		}
ds@1952
   178
	}
ds@1952
   179
}
ds@1952
   180
ds@1952
   181
public __GetAdminTopMenu(Handle:plugin, numParams)
ds@1952
   182
{
ds@1952
   183
	return _:hAdminMenu;
ds@1952
   184
}
ds@1952
   185
ds@1952
   186
public __AddTargetsToMenu(Handle:plugin, numParams)
ds@1952
   187
{
ds@1952
   188
	new bool:alive_only = false;
ds@1952
   189
	
ds@1952
   190
	if (numParams >= 4)
ds@1952
   191
	{
ds@1952
   192
		alive_only = GetNativeCell(4);
ds@1952
   193
	}
ds@1952
   194
	
ds@1952
   195
	return UTIL_AddTargetsToMenu(GetNativeCell(1), GetNativeCell(2), GetNativeCell(3), alive_only);
ds@1952
   196
}
ds@1952
   197
ds@1952
   198
public __AddTargetsToMenu2(Handle:plugin, numParams)
ds@1952
   199
{
ds@1952
   200
	return UTIL_AddTargetsToMenu2(GetNativeCell(1), GetNativeCell(2), GetNativeCell(3));
ds@1952
   201
}
ds@1952
   202
ds@1952
   203
public Action:Command_DisplayMenu(client, args)
ds@1952
   204
{
ds@1952
   205
	if (client == 0)
ds@1952
   206
	{
ds@1952
   207
		ReplyToCommand(client, "[SM] %t", "Command is in-game only");
ds@1952
   208
		return Plugin_Handled;
ds@1952
   209
	}
ds@1952
   210
	
ds@1952
   211
	DisplayTopMenu(hAdminMenu, client, TopMenuPosition_Start);
ds@1952
   212
	
ds@1952
   213
	return Plugin_Handled;
ds@1952
   214
}
ds@1952
   215
ds@1952
   216
stock UTIL_AddTargetsToMenu2(Handle:menu, source_client, flags)
ds@1952
   217
{
ds@1952
   218
	new max_clients = GetMaxClients();
ds@1952
   219
	decl String:user_id[12];
ds@1952
   220
	decl String:name[MAX_NAME_LENGTH];
ds@1952
   221
	decl String:display[MAX_NAME_LENGTH+12];
ds@1952
   222
	
ds@1952
   223
	new num_clients;
ds@1952
   224
	
ds@1952
   225
	for (new i = 1; i <= max_clients; i++)
ds@1952
   226
	{
ds@1952
   227
		if (!IsClientConnected(i) || IsClientInKickQueue(i))
ds@1952
   228
		{
ds@1952
   229
			continue;
ds@1952
   230
		}
ds@1952
   231
		
ds@1952
   232
		PrintToServer("%d, %d, %d", i, flags, IsFakeClient(i));
ds@1952
   233
		
ds@1952
   234
		if (((flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS)
ds@1952
   235
			&& IsFakeClient(i))
ds@1952
   236
		{
ds@1952
   237
			continue;
ds@1952
   238
		}
ds@1952
   239
		
ds@1952
   240
		if (((flags & COMMAND_FILTER_CONNECTED) != COMMAND_FILTER_CONNECTED)
ds@1952
   241
			&& !IsClientInGame(i))
ds@1952
   242
		{
ds@1952
   243
			continue;
ds@1952
   244
		}
ds@1952
   245
		
ds@1952
   246
		if (((flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE) 
ds@1952
   247
			&& !IsPlayerAlive(i))
ds@1952
   248
		{
ds@1952
   249
			continue;
ds@1952
   250
		}
ds@1952
   251
		
ds@1952
   252
		if (((flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD)
ds@1952
   253
			&& IsPlayerAlive(i))
ds@1952
   254
		{
ds@1952
   255
			continue;
ds@1952
   256
		}
ds@1952
   257
		
ds@1952
   258
		if ((source_client && ((flags & COMMAND_FILTER_NO_IMMUNITY) != COMMAND_FILTER_NO_IMMUNITY))
ds@1952
   259
			&& !CanUserTarget(source_client, i))
ds@1952
   260
		{
ds@1952
   261
			continue;
ds@1952
   262
		}
ds@1952
   263
		
ds@1952
   264
		IntToString(GetClientUserId(i), user_id, sizeof(user_id));
ds@1952
   265
		GetClientName(i, name, sizeof(name));
ds@1952
   266
		Format(display, sizeof(display), "%s (%s)", name, user_id);
ds@1952
   267
		AddMenuItem(menu, user_id, display);
ds@1952
   268
		num_clients++;
ds@1952
   269
	}
ds@1952
   270
	
ds@1952
   271
	return num_clients;
ds@1952
   272
}
ds@1952
   273
ds@1952
   274
stock UTIL_AddTargetsToMenu(Handle:menu, source_client, bool:in_game_only, bool:alive_only)
ds@1952
   275
{
ds@1952
   276
	new flags = 0;
ds@1952
   277
	
ds@1952
   278
	if (!in_game_only)
ds@1952
   279
	{
ds@1952
   280
		flags |= COMMAND_FILTER_CONNECTED;
ds@1952
   281
	}
ds@1952
   282
	
ds@1952
   283
	if (alive_only)
ds@1952
   284
	{
ds@1952
   285
		flags |= COMMAND_FILTER_ALIVE;
ds@1952
   286
	}
ds@1952
   287
	
ds@1952
   288
	return UTIL_AddTargetsToMenu2(menu, source_client, flags);
ds@1952
   289
}
ds@1952
   290