Faster
A config file must be created in configs folder called : facts.txt
One fact par line. And that's it !
Also, color code support :
this {green}is {red}a {orange}fact {purple}!
PHP Code:
#include <sourcemod>
#include <sdktools>
#include <multicolors>
#define PLUGIN_AUTHOR "Arkarr"
#define PLUGIN_VERSION "1.00"
#define PLUGIN_TAG "{green}[FACT]{default}"
#pragma newdecls required
Handle ARRAY_Facts;
Handle TMR_TimerNextFact;
Handle CVAR_FactsInterval;
int LastFactID;
public Plugin myinfo =
{
name = "[ANY] Facts!",
author = PLUGIN_AUTHOR,
description = "Display a line picked up from a text file every X minutes",
version = PLUGIN_VERSION,
url = "http://www.sourcemod.net"
};
public void OnPluginStart()
{
CVAR_FactsInterval = CreateConVar("sm_fact_interval", "5", "Display a fact every X minutes", _, true, 1.0);
RegAdminCmd("sm_ffact", CMD_PrintNextFact, ADMFLAG_CHAT, "Force to print the next fact.");
RegAdminCmd("sm_forcefact", CMD_PrintNextFact, ADMFLAG_CHAT, "Force to print the next fact.");
}
public void OnPluginEnd()
{
KillTimer(TMR_TimerNextFact);
}
public void OnConfigsExecuted()
{
LoadFacts();
TMR_TimerNextFact = CreateTimer(GetConVarFloat(CVAR_FactsInterval) * 60, TMR_PrintRandomFact, _, TIMER_REPEAT);
}
public Action CMD_PrintNextFact(int client, int args)
{
PrintFact();
return Plugin_Handled;
}
public Action TMR_PrintRandomFact(Handle tmr)
{
PrintFact();
}
public void PrintFact()
{
char fact[255];
int nextFactID = GetRandomInt(0, GetArraySize(ARRAY_Facts)-1);
while(nextFactID == LastFactID && GetArraySize(ARRAY_Facts)-1 > 0)
nextFactID = GetRandomInt(0, GetArraySize(ARRAY_Facts)-1);
LastFactID = nextFactID;
GetArrayString(ARRAY_Facts, nextFactID, fact, sizeof(fact));
CPrintToChatAll("%s %s", PLUGIN_TAG, fact);
}
public void LoadFacts()
{
char path[PLATFORM_MAX_PATH];
char line[255];
ARRAY_Facts = CreateArray(sizeof(line));
BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "configs/facts.txt");
if(FileExists(path))
{
Handle fileHandle = OpenFile(path, "r");
while(!IsEndOfFile(fileHandle)&&ReadFileLine(fileHandle, line, sizeof(line)))
PushArrayString(ARRAY_Facts, line);
CloseHandle(fileHandle);
}
else
{
PushArrayString(ARRAY_Facts, "Config file facts.txt missing or unable to read from !");
}
}