/*
* Delayed Command Scheduler (olds.ro workshop card LIB-1557)
*
* Lets an admin run a server command later, once or several times, and
* see or cancel what is still waiting.
*
* Admin commands (ADMIN_RCON, flag "l"; also from the server console):
* amx_delay <seconds> <command> runs the command once, after
* the given seconds
* amx_repeat <seconds> <times> <command> runs it <times> times, the first
* time after <seconds> and then
* every <seconds>
* amx_timers lists the pending timers with
* the time left
* amx_timercancel <id> cancels a pending timer
*
* Seconds: 0.1 to 86400 (decimals allowed). Times: 1 to 1000. The command
* runs as a server console command (server_cmd), for example
* amx_delay 60 say Server restarts in one minute
* amx_repeat 300 5 "amx_csay red Visit olds.ro; say Visit olds.ro"
* A command given as one quoted argument is run exactly as written, so it
* may hold several commands separated by ;.
*
* Every timer gets an id (#1, #2, ...). Adding and cancelling timers is
* written to the AMXX log; each run prints a line to the server console.
* Timers do not survive a map change.
*
* Cvar (saved with its description in configs/plugins/olds_timers.cfg the
* first time the plugin starts):
* olds_timer_max 20 Most timers that can wait at the same time (1-64).
*
* Requires AMX Mod X 1.10 or newer. No extra modules; ReAPI is not used.
*
* 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[] = "Delayed Command Scheduler";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const DICTIONARY[] = "olds_timers.txt";
// Hard limit of timer slots; olds_timer_max can only lower it.
const MAX_SLOTS = 64;
// Longest command kept for a timer.
const COMMAND_LENGTH = 190;
// Task ids are TASK_BASE + slot.
const TASK_BASE = 15570;
const Float:MIN_SECONDS = 0.1;
const Float:MAX_SECONDS = 86400.0;
const MAX_TIMES = 1000;
new g_maxTimers; // olds_timer_max
new bool:g_used[MAX_SLOTS];
new g_timerId[MAX_SLOTS];
new g_command[MAX_SLOTS][COMMAND_LENGTH + 1];
new Float:g_interval[MAX_SLOTS];
new g_runsLeft[MAX_SLOTS];
new Float:g_nextRun[MAX_SLOTS];
// Id for the next timer; ids are not reused on the same map.
new g_nextId = 1;
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
register_concmd("amx_delay", "cmd_delay", ADMIN_RCON, "OLDS_TIMER_HELP_DELAY", -1, true);
register_concmd("amx_repeat", "cmd_repeat", ADMIN_RCON, "OLDS_TIMER_HELP_REPEAT", -1, true);
register_concmd("amx_timers", "cmd_timers", ADMIN_RCON, "OLDS_TIMER_HELP_LIST", -1, true);
register_concmd("amx_timercancel", "cmd_cancel", ADMIN_RCON, "OLDS_TIMER_HELP_CANCEL", -1, true);
bind_pcvar_num(create_cvar("olds_timer_max", "20", FCVAR_NONE,
"Most timers (amx_delay / amx_repeat) that can wait at the same time",
true, 1.0, true, float(MAX_SLOTS)), g_maxTimers);
AutoExecConfig(true, "olds_timers");
}
/**
* amx_delay <seconds> <command>
*/
public cmd_delay(id, level, cid)
{
if (!admin_allowed(id, level))
return PLUGIN_HANDLED;
new Float:seconds;
new command[COMMAND_LENGTH + 1];
if (read_argc() < 3)
{
console_print(id, "%L", id, "OLDS_TIMER_USAGE_DELAY");
return PLUGIN_HANDLED;
}
if (!read_seconds(id, 1, seconds))
return PLUGIN_HANDLED;
read_command(2, command, charsmax(command));
if (!command[0])
{
console_print(id, "%L", id, "OLDS_TIMER_USAGE_DELAY");
return PLUGIN_HANDLED;
}
add_timer(id, seconds, 1, command);
return PLUGIN_HANDLED;
}
/**
* amx_repeat <seconds> <times> <command>
*/
public cmd_repeat(id, level, cid)
{
if (!admin_allowed(id, level))
return PLUGIN_HANDLED;
new Float:seconds;
new command[COMMAND_LENGTH + 1];
if (read_argc() < 4)
{
console_print(id, "%L", id, "OLDS_TIMER_USAGE_REPEAT");
return PLUGIN_HANDLED;
}
if (!read_seconds(id, 1, seconds))
return PLUGIN_HANDLED;
new text[16];
read_argv(2, text, charsmax(text));
new times = str_to_num(text);
if (!is_str_num(text) || times < 1 || times > MAX_TIMES)
{
console_print(id, "%L", id, "OLDS_TIMER_BAD_TIMES", MAX_TIMES);
return PLUGIN_HANDLED;
}
read_command(3, command, charsmax(command));
if (!command[0])
{
console_print(id, "%L", id, "OLDS_TIMER_USAGE_REPEAT");
return PLUGIN_HANDLED;
}
add_timer(id, seconds, times, command);
return PLUGIN_HANDLED;
}
/**
* amx_timers
*/
public cmd_timers(id, level, cid)
{
if (!admin_allowed(id, level))
return PLUGIN_HANDLED;
new count = active_count();
if (!count)
{
console_print(id, "%L", id, "OLDS_TIMER_NONE");
return PLUGIN_HANDLED;
}
console_print(id, "%L", id, "OLDS_TIMER_LIST_HEAD", count, g_maxTimers);
new Float:now = get_gametime();
// Oldest timer first: each pass takes the lowest id above the last one.
new lastId;
for (new done = 0; done < count; done++)
{
new slot = -1;
for (new i = 0; i < MAX_SLOTS; i++)
{
if (g_used[i] && g_timerId[i] > lastId && (slot == -1 || g_timerId[i] < g_timerId[slot]))
slot = i;
}
if (slot == -1)
break;
lastId = g_timerId[slot];
new Float:left = g_nextRun[slot] - now;
if (left < 0.0)
left = 0.0;
console_print(id, "%L", id, "OLDS_TIMER_LIST_ROW",
g_timerId[slot], left, g_runsLeft[slot], g_interval[slot], g_command[slot]);
}
return PLUGIN_HANDLED;
}
/**
* amx_timercancel <id>
*/
public cmd_cancel(id, level, cid)
{
if (!admin_allowed(id, level))
return PLUGIN_HANDLED;
if (read_argc() < 2)
{
console_print(id, "%L", id, "OLDS_TIMER_USAGE_CANCEL");
return PLUGIN_HANDLED;
}
new text[16];
read_argv(1, text, charsmax(text));
// "#3" and "3" both work.
new start = (text[0] == '#') ? 1 : 0;
new timerId = str_to_num(text[start]);
new slot = find_slot(timerId);
if (!is_str_num(text[start]) || slot == -1)
{
console_print(id, "%L", id, "OLDS_TIMER_NOT_FOUND", text);
return PLUGIN_HANDLED;
}
remove_task(TASK_BASE + slot);
g_used[slot] = false;
console_print(id, "%L", id, "OLDS_TIMER_CANCELLED", timerId, g_command[slot]);
new who[96];
describe_admin(id, who, charsmax(who));
log_amx("Cmd: %s cancelled timer #%d: %s", who, timerId, g_command[slot]);
return PLUGIN_HANDLED;
}
public task_fire(taskId)
{
new slot = taskId - TASK_BASE;
if (slot < 0 || slot >= MAX_SLOTS || !g_used[slot])
return;
new command[COMMAND_LENGTH + 1];
copy(command, charsmax(command), g_command[slot]);
new timerId = g_timerId[slot];
if (--g_runsLeft[slot] > 0)
{
g_nextRun[slot] = get_gametime() + g_interval[slot];
set_task(g_interval[slot], "task_fire", taskId);
}
else
{
g_used[slot] = false;
}
server_print("[olds_timers] Timer #%d runs: %s", timerId, command);
server_cmd("%s", command);
}
add_timer(id, Float:seconds, times, const command[])
{
new slot = free_slot();
if (active_count() >= g_maxTimers || slot == -1)
{
console_print(id, "%L", id, "OLDS_TIMER_FULL", g_maxTimers);
return;
}
new timerId = g_nextId++;
g_used[slot] = true;
g_timerId[slot] = timerId;
copy(g_command[slot], COMMAND_LENGTH, command);
g_interval[slot] = seconds;
g_runsLeft[slot] = times;
g_nextRun[slot] = get_gametime() + seconds;
set_task(seconds, "task_fire", TASK_BASE + slot);
if (times == 1)
console_print(id, "%L", id, "OLDS_TIMER_ADDED_ONCE", timerId, seconds, command);
else
console_print(id, "%L", id, "OLDS_TIMER_ADDED_REPEAT", timerId, times, seconds, command);
new who[96];
describe_admin(id, who, charsmax(who));
log_amx("Cmd: %s added timer #%d (%d x, every %.1f s): %s", who, timerId, times, seconds, command);
}
/**
* Reads argument arg as seconds. Prints why and returns false if it is not
* a number from MIN_SECONDS to MAX_SECONDS.
*/
bool:read_seconds(id, arg, &Float:seconds)
{
new text[16];
read_argv(arg, text, charsmax(text));
seconds = str_to_float(text);
if (!is_decimal(text) || seconds < MIN_SECONDS || seconds > MAX_SECONDS)
{
console_print(id, "%L", id, "OLDS_TIMER_BAD_SECONDS");
return false;
}
return true;
}
/**
* The command is every argument from firstArg on. One argument is used as it
* is (so a quoted "a; b" stays two commands); several are joined with
* spaces, and an argument that holds a space is quoted again.
*/
read_command(firstArg, command[], maxLength)
{
new count = read_argc();
command[0] = EOS;
if (count - firstArg == 1)
{
read_argv(firstArg, command, maxLength);
trim(command);
return;
}
new part[COMMAND_LENGTH + 1], length;
for (new i = firstArg; i < count && length < maxLength; i++)
{
read_argv(i, part, charsmax(part));
if (length)
length += copy(command[length], maxLength - length, " ");
if (!part[0] || contain(part, " ") != -1)
length += formatex(command[length], maxLength - length, "^"%s^"", part);
else
length += copy(command[length], maxLength - length, part);
}
}
/**
* 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_TIMER_NO_ACCESS");
return false;
}
active_count()
{
new count;
for (new i = 0; i < MAX_SLOTS; i++)
{
if (g_used[i])
count++;
}
return count;
}
free_slot()
{
for (new i = 0; i < MAX_SLOTS; i++)
{
if (!g_used[i])
return i;
}
return -1;
}
find_slot(timerId)
{
for (new i = 0; i < MAX_SLOTS; i++)
{
if (g_used[i] && g_timerId[i] == timerId)
return i;
}
return -1;
}
bool:is_decimal(const text[])
{
new digits, dots;
for (new i = 0; text[i]; i++)
{
if (text[i] == '.')
{
if (++dots > 1)
return false;
}
else if (isdigit(text[i]))
{
digits++;
}
else
{
return false;
}
}
return digits > 0;
}
describe_admin(id, out[], maxLength)
{
if (!id)
{
copy(out, maxLength, "Server");
return;
}
new name[MAX_NAME_LENGTH], authid[44];
get_user_name(id, name, charsmax(name));
get_user_authid(id, authid, charsmax(authid));
formatex(out, maxLength, "^"%s<%d><%s><>^"", name, get_user_userid(id), authid);
}