Skip to content

Instantly share code, notes, and snippets.

@JoinedSenses
Last active September 26, 2020 19:06
Show Gist options
  • Save JoinedSenses/79f3fb38848e1062f7acc0a3cce0943a to your computer and use it in GitHub Desktop.
Save JoinedSenses/79f3fb38848e1062f7acc0a3cce0943a to your computer and use it in GitHub Desktop.
Example sourcemod plugin to display time
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
Handle g_Timers[MAXPLAYERS+1];
public void OnPluginStart() {
RegAdminCmd("sm_showtime", cmdShowTime, ADMFLAG_ROOT);
RegAdminCmd("sm_stoptime", cmdStopTime, ADMFLAG_ROOT);
LoadTranslations("common.phrases");
}
public Action cmdShowTime(int client, int args) {
// if no args
if (!args) {
// if client isnt console and if they dont yet have a timer
if (client && g_Timers[client] == null) {
// create a timer and store it to the array
g_Timers[client] = CreateTimer(1.0, timerPrintMessage, GetClientUserId(client), TIMER_REPEAT);
ReplyToCommand(client, "Now displaying current time");
}
return Plugin_Handled;
}
// get their arg
char arg[32];
GetCmdArg(1, arg, sizeof arg);
// try to find a target
int target = FindTarget(client, arg, true, false);
if (target == -1) {
return Plugin_Handled;
}
// target already has a timer
if (g_Timers[target] != null) {
ReplyToCommand(client, "%N is already being shown the time", target);
return Plugin_Handled;
}
// create a timer and store it to the array
g_Timers[target] = CreateTimer(1.0, timerPrintMessage, GetClientUserId(target), TIMER_REPEAT);
ReplyToCommand(client, "Now showing time to %N", target);
return Plugin_Handled;
}
public Action cmdStopTime(int client, int args) {
// if no args
if (!args) {
// if client isnt console
if (client) {
// attempt to remove own timer
delete g_Timers[client];
}
return Plugin_Handled;
}
// get their arg
char arg[32];
GetCmdArg(1, arg, sizeof arg);
// try to find a target
int target = FindTarget(client, arg, true, false);
if (target == -1) {
return Plugin_Handled;
}
// not using delete here so we can add a message during timer close
if (g_Timers[target] != null) {
CloseHandle(g_Timers[target]);
g_Timers[target] = null;
ReplyToCommand(client, "Stopped showing time to %N", target);
}
else {
ReplyToCommand(client, "%N is not being shown the current time", target);
}
return Plugin_Handled;
}
public Action timerPrintMessage(Handle timer, int userid) {
// try to get client index
int client = GetClientOfUserId(userid);
// if bad data, stop the timer
if (!client) {
return Plugin_Stop;
}
// format and print time to target
char curTime[9];
FormatTime(curTime, sizeof curTime, "%H:%M:%S");
PrintHintText(client, "%s", curTime);
return Plugin_Continue;
}
// @note: this is also called on map changes. if you only want the
// real disconnect, hook the disconnect event
public void OnClientDisconnect(int client) {
// if they have an active timer, dispose of it
delete g_Timers[client];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment