Skip to content

Instantly share code, notes, and snippets.

@MyNameIsTimb
Last active October 7, 2019 01:41
Show Gist options
  • Save MyNameIsTimb/1220337e26a64f7a06bfc463d17da6ac to your computer and use it in GitHub Desktop.
Save MyNameIsTimb/1220337e26a64f7a06bfc463d17da6ac to your computer and use it in GitHub Desktop.
TimB's Hostname Switcher
timb = timb or {}
local timshs = {
atLeastOneLoad = false,
-- Attention crusty man/gril: don't edit them here, put them in data/timb/titles.txt!
-- Or add them in game with `timb_addtitle "Your Title Here"`
titles = {}
}
-- For logging
local getNiceTimeStamp = function()
return os.date( "%H:%M:%S - %d/%m/%Y", os.time() )
end
local writeLog = function( text )
if not file.Exists( "timb/title_log.txt", "DATA" ) then
file.Write( "timb/title_log.txt", getNiceTimeStamp() .. " | Log started" )
end
file.Append( "timb/title_log.txt", getNiceTimeStamp() .. " | " .. ( text or "Nothing happened" ) )
end
timshs.AddTitle = function( tit )
table.insert( timshs.titles, tit )
timshs.SaveTitles()
end
timshs.SetPrefix = function( pref )
timshs.prefix = pref
timshs.SavePrefix()
end
-- Sets the prefix
concommand.Add( "timb_setprefix", function( ply, cmd, args, argStr )
local should_set = not IsValid( ply ) -- true if console, false if player
if not should_set then -- if ply is a player and not console
should_set = ply:IsSuperAdmin()
end
-- intentionally disjoint
if should_set then
local new_prefix = table.concat( args, " " )
if new_prefix ~= "" then
-- cool, let's set it
timshs.SetPrefix( new_prefix )
writeLog( ( IsValid( ply ) and ( ply:Nick() .. " (" .. ply:SteamID() .. ")" ) or "[CONSOLE]" ) .. " set the prefix to \"" .. new_prefix .. "\"" )
end
end
end )
-- Adds a title
concommand.Add( "timb_addtitle", function( ply, cmd, args, argStr )
local should_set = not IsValid( ply ) -- true if console, false if player
if not should_set then -- if ply is a player and not console
should_set = ply:IsSuperAdmin()
end
-- intentionally disjoint
if should_set then
local new_title = table.concat( args, " " )
if new_title ~= "" then
-- cool, let's insert into the table and save it
timshs.AddTitle( new_title )
writeLog( ( IsValid( ply ) and ( ply:Nick() .. " (" .. ply:SteamID() .. ")" ) or "[CONSOLE]" ) .. " added title \"" .. new_title .. "\"" )
end
end
end )
-- Saves prefix from Lua to the data folder
timshs.SavePrefix = function()
if timshs.prefix then
file.Write( "timb/hostname_prefix.txt", timshs.prefix )
end
end
-- Saves titles from Lua to the data folder
timshs.SaveTitles = function()
if timshs.atLeastOneLoad then
file.Write( "timb/titles.txt", table.concat( timshs.titles, "\r\n" ) )
end
end
-- Loads titles from the data folder to Lua
timshs.LoadTitles = function()
if not file.IsDir( "timb", "DATA" ) then
file.CreateDir( "timb", "DATA" )
end
if not file.Exists( "timb/titles.txt", "DATA" ) then
file.Write( "timb/titles.txt", "" )
timshs.atLeastOneLoad = true
end
readtext = file.Read( "timb/titles.txt", "DATA" )
if readtext then
-- Please be patient i have windows
readtext = string.gsub( readtext, "\r", "" ) -- a \n b \n c
exploded = string.Explode( "\n", readtext, false )
timshs.titles = {}
for _,v in next, exploded, nil do
if v ~= "" then
table.insert( timshs.titles, v )
end
end
timshs.atLeastOneLoad = true
end
end
-- Loads prefix from the data folder to Lua
timshs.LoadPrefix = function()
if not file.IsDir( "timb", "DATA" ) then
file.CreateDir( "timb", "DATA" )
end
if file.Exists( "timb/hostname_prefix.txt", "DATA" ) then
readtext = file.Read( "timb/hostname_prefix.txt", "DATA" )
if readtext then
-- Please be patient i have windows
readtext = string.gsub( readtext, "\r", "" ) -- a \n b \n c
exploded = string.Explode( "\n", readtext, false )
if exploded then
if #( exploded ) >= 1 then
timshs.prefix = exploded[1]
end
end
end
end
end
-- Run everything after IPE
hook.Add( "InitPostEntity", "timshs_InitPostEntity", function()
-- Resolve the prefix first
timshs.LoadPrefix()
-- Then load in all the suffixes
timshs.LoadTitles()
-- Start the timers
timer.Create( "timb_titles_rotate", 2.1, 0, function()
-- Every 2.1 seconds, set the title
if #timshs.titles > 0 then
RunConsoleCommand( "hostname",
( timshs.prefix or "A GMod Server | " ) ..
timshs.titles[ math.random( #timshs.titles ) ]
)
end
end )
timer.Create( "timb_titles_reload", 60, 0, function()
-- Reload titles every now and then just to be safe
timshs.LoadTitles()
-- Also reload the prefix
timshs.LoadPrefix()
end )
end )
-- Point
timb.HostnameSwitcher = timshs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment