Skip to content
View in the app

A better way to browse. Learn more.

Learn more

console-command-browser/addons/amxmodx/scripting/olds_cmdsearch.sma

olds_cmdsearch.sma
289 lines · 7.6 KB · Pawn (AMX Mod X)
Raw
/*
 * Console Command Browser (olds.ro workshop card LIB-1505)
 *
 * Lets admins search all console commands of all plugins by name or
 * description, in the console or in a menu.
 *
 * Admin commands (ADMIN_CFG, flag "h"; amx_cmdsearch also from the server
 * console):
 *   amx_cmdsearch <text>   lists every console command whose name or
 *                          description contains the text (any case), with
 *                          the plugin that registered it
 *   amx_cmdmenu [text]     the same list in a menu, 7 commands per page, in
 *                          the same order and with the same numbers as the
 *                          console list; without text, all commands. Choosing
 *                          a command prints its details to the console.
 * Both stop at olds_cmdsearch_max results and say so.
 *
 * Cvar (saved in configs/plugins/olds_cmdsearch.cfg the first time the
 * plugin starts):
 *   olds_cmdsearch_max  50   most results shown (1 - 500)
 *
 * Commands come from get_concmdsnum / get_concmd; the plugin of each one
 * from get_concmd_plid and get_plugin.
 *
 * Requires AMX Mod X 1.10 or newer. No extra modules and no ReAPI.
 *
 * Copyright (C) 2026 alexit (olds.ro workshop)
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details: <https://www.gnu.org/licenses/>.
 */

#include <amxmodx>
#include <amxmisc>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Console Command Browser";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_cmdsearch.txt";

const MAX_RESULTS = 500;
const ITEMS_PER_PAGE = 7;

new g_maxResults;       // olds_cmdsearch_max

// The last search: the indexes of the commands found.
new g_results[MAX_RESULTS];
new g_resultCount;
new g_matchCount;

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
	register_dictionary(DICTIONARY);

	register_concmd("amx_cmdsearch", "cmd_search", ADMIN_CFG, "OLDS_CS_HELP", -1, true);
	register_concmd("amx_cmdmenu", "cmd_menu", ADMIN_CFG, "OLDS_CS_MENU_HELP", -1, true);

	bind_pcvar_num(create_cvar("olds_cmdsearch_max", "50", FCVAR_NONE,
		"Most results amx_cmdsearch and amx_cmdmenu show",
		true, 1.0, true, float(MAX_RESULTS)), g_maxResults);

	AutoExecConfig(true, "olds_cmdsearch");
}

/**
 * amx_cmdsearch <text>
 */
public cmd_search(id, level, cid)
{
	if (!admin_allowed(id, level))
		return PLUGIN_HANDLED;

	new text[64];
	read_args(text, charsmax(text));
	remove_quotes(text);
	trim(text);

	if (!text[0])
	{
		console_print(id, "%L", id, "OLDS_CS_USAGE");
		return PLUGIN_HANDLED;
	}

	find_commands(id, text);

	if (!g_matchCount)
	{
		console_print(id, "%L", id, "OLDS_CS_NONE", text);
		return PLUGIN_HANDLED;
	}

	console_print(id, "%L", id, "OLDS_CS_HEADER", text, g_matchCount);

	new line[192];

	for (new i = 0; i < g_resultCount; i++)
	{
		describe_command(id, g_results[i], i + 1, line, charsmax(line));
		console_print(id, "%s", line);
	}

	if (g_matchCount > g_resultCount)
		console_print(id, "%L", id, "OLDS_CS_CAPPED", g_resultCount, g_matchCount);

	return PLUGIN_HANDLED;
}

/**
 * amx_cmdmenu [text]
 */
public cmd_menu(id, level, cid)
{
	if (!admin_allowed(id, level))
		return PLUGIN_HANDLED;

	if (!id)
	{
		console_print(id, "%L", id, "OLDS_CS_MENU_PLAYERS");
		return PLUGIN_HANDLED;
	}

	new text[64];
	read_args(text, charsmax(text));
	remove_quotes(text);
	trim(text);

	find_commands(id, text);

	if (!g_matchCount)
	{
		console_print(id, "%L", id, "OLDS_CS_NONE", text);
		return PLUGIN_HANDLED;
	}

	new title[128];

	if (text[0])
		formatex(title, charsmax(title), "%L", id, "OLDS_CS_MENU_TITLE", text, g_matchCount);
	else
		formatex(title, charsmax(title), "%L", id, "OLDS_CS_MENU_ALL", g_matchCount);

	if (g_matchCount > g_resultCount)
	{
		new capped[64];
		formatex(capped, charsmax(capped), "^n%L", id, "OLDS_CS_MENU_CAPPED", g_resultCount);
		add(title, charsmax(title), capped);
	}

	new menu = menu_create(title, "menu_handler");
	new name[64], info[8];

	for (new i = 0; i < g_resultCount; i++)
	{
		menu_label(g_results[i], i + 1, name, charsmax(name));
		num_to_str(g_results[i], info, charsmax(info));
		menu_additem(menu, name, info);
	}

	menu_setprop(menu, MPROP_PERPAGE, ITEMS_PER_PAGE);
	menu_display(id, menu, 0);
	return PLUGIN_HANDLED;
}

public menu_handler(id, menu, item)
{
	if (item < 0)
	{
		menu_destroy(menu);
		return PLUGIN_HANDLED;
	}

	new info[8], name[64], access, callback;
	menu_item_getinfo(menu, item, access, info, charsmax(info), name, charsmax(name), callback);

	new line[192];
	describe_command(id, str_to_num(info), item + 1, line, charsmax(line));
	console_print(id, "%s", line);
	client_print_color(id, print_team_default, "^1%L", id, "OLDS_CS_SEE_CONSOLE", name);

	// Stay on the same page.
	menu_display(id, menu, item / ITEMS_PER_PAGE);
	return PLUGIN_HANDLED;
}

/**
 * Fills g_results with the indexes of the matching commands (all commands
 * when text is empty), at most olds_cmdsearch_max; g_matchCount counts all.
 */
find_commands(id, const text[])
{
	new limit = clamp(g_maxResults, 1, MAX_RESULTS);
	new total = get_concmdsnum(-1, -1);

	new command[64], info[128], description[128], flags, bool:infoMl;

	g_resultCount = 0;
	g_matchCount = 0;

	for (new i = 0; i < total; i++)
	{
		if (!get_concmd(i, command, charsmax(command), flags, info, charsmax(info), -1, -1, infoMl))
			continue;

		if (text[0])
		{
			if (infoMl)
				formatex(description, charsmax(description), "%L", id, info);
			else
				copy(description, charsmax(description), info);

			if (containi(command, text) == -1 && containi(description, text) == -1)
				continue;
		}

		g_matchCount++;

		if (g_resultCount < limit)
			g_results[g_resultCount++] = i;
	}
}

/**
 * "12. amx_slap [admincmd.amxx] <name> [power] - slaps player with given power"
 */
describe_command(id, index, number, out[], maxLength)
{
	new command[64], info[128], description[128], flags, bool:infoMl, plugin[64];

	get_concmd(index, command, charsmax(command), flags, info, charsmax(info), -1, -1, infoMl);

	if (infoMl)
		formatex(description, charsmax(description), "%L", id, info);
	else
		copy(description, charsmax(description), info);

	plugin_file(index, plugin, charsmax(plugin));
	formatex(out, maxLength, "%L", id, "OLDS_CS_LINE", number, command, plugin, description);
}

menu_label(index, number, out[], maxLength)
{
	new command[64], info[4], flags, plugin[64];

	get_concmd(index, command, charsmax(command), flags, info, charsmax(info), -1, -1);
	plugin_file(index, plugin, charsmax(plugin));
	formatex(out, maxLength, "%d. %s \d[%s]", number, command, plugin);
}

plugin_file(index, out[], maxLength)
{
	new pluginId = get_concmd_plid(index, -1, -1);

	if (pluginId < 0)
	{
		copy(out, maxLength, "?");
		return;
	}

	get_plugin(pluginId, out, maxLength);
}

/**
 * The same rules as cmd_access: the server console (and the host of a listen
 * server) may always; ADMIN_ALL = everyone; ADMIN_ADMIN = any admin flag.
 * The level can be changed in configs/cmdaccess.ini.
 */
bool:admin_allowed(id, level)
{
	if (id == (is_dedicated_server() ? 0 : 1) || level == ADMIN_ALL)
		return true;

	if ((level == ADMIN_ADMIN && is_user_admin(id)) || (get_user_flags(id) & level))
		return true;

	console_print(id, "%L", id, "OLDS_CS_NO_ACCESS");
	return false;
}

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.