Author Topic: Custom LUA conflicting with ULX chat commands. Ideas?  (Read 2550 times)

0 Members and 1 Guest are viewing this topic.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Custom LUA conflicting with ULX chat commands. Ideas?
« on: September 18, 2007, 09:48:10 AM »
This LUA was written for my server by a community member to add a few things I suggested, however, when it is run it breaks all of my chat based ULX commands.

anyone know what in this code has a problem with ULX and can you suggest a way to fix it to allow both to run together? I'm thinking it might be the function BTS.PlayerSay( ply, text, public ) or something.. The code used to only show the time played on the server until he added support for the point system which is when the code started conflicting with ULX.

Code: [Select]
// By: ^>:B!G-A:<^

BTS = {}
BTS.CheckedFile = false
BTS.Panel = nil
SetGlobalInt( "BTS_ServerTime", 0 )
BTS.JoinTimes = {}
BTS.LastSave = 0
BTS.LeaveSave = false

AddCSLuaFile( "autorun/big_timespent.lua" )

if SERVER then
function BTS.PlayerLoad( player )
if !BTS.CheckedFile then
BTS.CheckFile()
BTS.CheckedFile = true
end

local times = {}
local myinfo = nil
if file.Exists( "timespent_players.txt" ) then
times = util.KeyValuesToTable(file.Read( "timespent_players.txt" ))
end
player:SendLua("BTS.SendGUI()")
for i, v in pairs(times) do
if string.upper( i ) == player:SteamID( ) then
myinfo = v
break
end
end
if myinfo != nil then
BTS.JoinTimes[player:SteamID()] = {}
BTS.JoinTimes[player:SteamID()].time = GetGlobalInt( "BTS_ServerTime" ) - myinfo.time
BTS.JoinTimes[player:SteamID()].points = myinfo.points
else
BTS.JoinTimes[player:SteamID()] = {}
BTS.JoinTimes[player:SteamID()].time = GetGlobalInt( "BTS_ServerTime" )
BTS.JoinTimes[player:SteamID()].points = 0
end
player:SetNetworkedInt( "BTS_JoinTime", BTS.JoinTimes[player:SteamID()].time )
player:SetNetworkedInt( "BTS_Points", BTS.JoinTimes[player:SteamID()].points )
end
hook.Add( "PlayerInitialSpawn", "BTS_PlayerLoad", BTS.PlayerLoad );

function BTS.PlayerLeave( player )
BTS.LeaveSave = true
BTS.Save()
end
hook.Add( "PlayerDisconnected", "BTS_PlayerLeave", BTS.PlayerLeave );

function BTS.PlayerSpawn( player )
player:SetNetworkedInt( "BTS_JoinTime", BTS.JoinTimes[player:SteamID()].time )
player:SetNetworkedInt( "BTS_Points", BTS.JoinTimes[player:SteamID()].points )
end
hook.Add( "PlayerSpawn", "BTS_PlayerSpawn", BTS.PlayerSpawn );

function BTS.Timer()
local t = GetGlobalInt( "BTS_ServerTime" )
SetGlobalInt( "BTS_ServerTime", t + 1 )
end
timer.Create( "BTS_Timer", 1, 0, BTS.Timer )

function BTS.CheckFile()
local times = {}
local newtimes = {}
if file.Exists( "timespent_players.txt" ) then
times = util.KeyValuesToTable(file.Read( "timespent_players.txt" ))
else
return
end

for i, v in pairs(times) do
if type(v) == "number" then
newtimes[i] = {}
newtimes[i].time = v
newtimes[i].points = 0
else
newtimes[i] = {}
newtimes[i].time = v.time
newtimes[i].points = v.points
end
end

if file.Exists( "timespent_players.txt" ) then
file.Delete( "timespent_players.txt" )
end
file.Write( "timespent_players.txt", util.TableToKeyValues(newtimes) )
end

function BTS.Save()
if !BTS.CheckedFile then
BTS.CheckFile()
BTS.CheckedFile = true
end

local players = player.GetAll( )
local times = {}
local added = ""
if file.Exists( "timespent_players.txt" ) then
times = util.KeyValuesToTable(file.Read( "timespent_players.txt" ))
end

for i, v in pairs(times) do
local ply = nil
for e, k in ipairs(player.GetAll( )) do
if string.upper( i ) == k:SteamID( ) then
ply = k
break
end
end
if ply != nil then
if BTS.LeaveSave then
times[i].time = v.time + (GetGlobalInt( "BTS_ServerTime" ) - BTS.LastSave)
else
times[i].time = v.time + GetConVarNumber( "BTS_SaveFreq" )
end
times[i].points = ply:GetNetworkedInt( "BTS_Points" )
added = added .. i
end
end

for i, v in ipairs(player.GetAll( )) do
if string.find(added,v:SteamID( )) == nil then
times[v:SteamID( )] = {}
times[v:SteamID( )].time = GetGlobalInt("BTS_ServerTime")-v:GetNetworkedInt( "BTS_JoinTime" )
times[v:SteamID( )].points = v:GetNetworkedInt( "BTS_Points" )
end
end

if file.Exists( "timespent_players.txt" ) then
file.Delete( "timespent_players.txt" )
end
file.Write( "timespent_players.txt", util.TableToKeyValues(times) )

BTS.LastSave = GetGlobalInt( "BTS_ServerTime" )
if BTS.LeaveSave then
BTS.LeaveSave = false
end
end
timer.Create( "BTS_Save", 120, 0, BTS.Save )

function BTS.PlayerSay( ply, text, public )
if !ply:IsAdmin( ) and !ply:IsSuperAdmin( ) then return text end
local given = nil
local x = 0

local args = string.Explode( " ", text )
if table.getn(args) < 4 then return text end

if string.upper(args[1]..args[2]) == "/PPADD" then
x = 1
elseif string.upper(args[1]..args[2]) == "/PPREM" then
x = -1
else
return text
end
if x == 0 then return text end

if string.len(args[3]) < 3 then
ply:PrintMessage( HUD_PRINTTALK, "You must enter atleast 3 characters for a name!" ) 
return ""
end

for i, v in ipairs(player.GetAll( )) do
if string.find(string.upper(v:GetName()),string.upper(args[3])) != nil then
given = v
break
end
end
if given == nil then
ply:PrintMessage( HUD_PRINTTALK, "Player not found!" ) 
return ""
end

if BTS.JoinTimes[given:SteamID()] == nil or BTS.JoinTimes[given:SteamID()].points == nil then
ply:PrintMessage( HUD_PRINTTALK, "### There was an internal scripting variable error and the players data has been lost," ) 
ply:PrintMessage( HUD_PRINTTALK, "    have the player reconnect to reload his/her data." ) 
return ""
end

if x == 1 then
BTS.JoinTimes[given:SteamID()].points = BTS.JoinTimes[given:SteamID()].points + math.abs(tonumber(args[4]))
given:SetNetworkedInt( "BTS_Points", BTS.JoinTimes[given:SteamID()].points )
for i, v in ipairs(player.GetAll( )) do
v:PrintMessage( HUD_PRINTTALK, ply:GetName( ).." has given "..given:GetName( ).." "..math.abs(tonumber(args[4])).." Presidential Point(s)." ) 
end
else
BTS.JoinTimes[given:SteamID()].points = BTS.JoinTimes[given:SteamID()].points - math.abs(tonumber(args[4]))
given:SetNetworkedInt( "BTS_Points", BTS.JoinTimes[given:SteamID()].points )
for i, v in ipairs(player.GetAll( )) do
v:PrintMessage( HUD_PRINTTALK, ply:GetName( ).." has taken "..math.abs(tonumber(args[4])).." Presidential Point(s) from "..given:GetName( ) ) 
end
end
BTS.Save()

return ""
end
hook.Add( "PlayerSay", "BTS_PlayerSay", BTS.PlayerSay );
end

if CLIENT then
BTS.Scale = 0.50
BTS.Aim = nil
BTS.AimLock = -1

function BTS.ToYearsDaysHoursMinutesSeconds( i )
local input = i
local temp = 0
local output = ""

temp = math.floor(input / 31536000)
input = input - (temp * 31536000)
if temp < 10 then
output = output.."0"..tostring(temp)..":"
else
output = output..tostring(temp)..":"
end

temp = math.floor(input / 86400)
input = input - (temp * 86400)
if temp < 10 then
output = output.."0"..tostring(temp)..":"
else
output = output..tostring(temp)..":"
end

temp = math.floor(input / 3600)
input = input - (temp * 3600)
if temp < 10 then
output = output.."0"..tostring(temp)..":"
else
output = output..tostring(temp)..":"
end

temp = math.floor(input / 60)
input = input - (temp * 60)
if temp < 10 then
output = output.."0"..tostring(temp)..":"
else
output = output..tostring(temp)..":"
end

if input < 10 then
output = output.."0"..tostring(input)
else
output = output..tostring(input)
end

return output
end

function BTS.SendGUI()
BTS.Panel = vgui.Create( "Panel" )
BTS.Panel:SetName("vgui_timespent")
BTS.Panel:SetPos(ScrW()-170,10)
BTS.Panel:SetSize(160,84)

function BTS.Panel:Paint()
draw.RoundedBox( 8, 0, 0, self:GetWide(), self:GetTall()*BTS.Scale, Color(50,50,75,160) )
surface.SetFont( "Default" )
surface.SetTextColor( 255, 255, 255, 220 )
surface.SetTextPos( 10, 9 )
surface.DrawText( "Time Spent: " .. BTS.ToYearsDaysHoursMinutesSeconds(GetGlobalInt("BTS_ServerTime")-LocalPlayer( ):GetNetworkedInt( "BTS_JoinTime" )) )
surface.SetTextPos( 10, 21 )
surface.DrawText( "Points: " .. LocalPlayer( ):GetNetworkedInt( "BTS_Points" ) )
if ValidEntity( BTS.Aim ) then
if BTS.Scale > 0.50 then
draw.RoundedBox( 8, 3, 37, self:GetWide()-6, 44*((BTS.Scale-0.50)/0.50), Color(50,50,135,160*((BTS.Scale-0.50)/0.50)) )
surface.SetTextColor( 255, 255, 255, 220*((BTS.Scale-0.50)/0.50) )
surface.SetTextPos( 10, 40 )
surface.DrawText( "Name: " .. BTS.Aim:GetName( ) )
surface.SetTextPos( 10, 52 )
surface.DrawText( "Time Spent: " .. BTS.ToYearsDaysHoursMinutesSeconds(GetGlobalInt("BTS_ServerTime")-BTS.Aim:GetNetworkedInt( "BTS_JoinTime" )) )
surface.SetTextPos( 10, 64 )
surface.DrawText( "Points: " .. BTS.Aim:GetNetworkedInt( "BTS_Points" ) )
end
end
end

BTS.Panel:SetVisible( true )
end

function BTS.CheckAim()
local p = LocalPlayer()
local a = nil
local traceRes = p:GetEyeTrace()
if traceRes.Entity != nil then
a = player.GetByID(traceRes.Entity:EntIndex())
end

if (ValidEntity( a ) || ValidEntity( BTS.Aim )) && BTS.Scale < 1.00 then
BTS.Scale = BTS.Scale + 0.01
if BTS.AimLock == -1 && ValidEntity( a ) then
BTS.Aim = a
BTS.AimLock = GetGlobalInt("BTS_ServerTime")
end
end
if ( (!ValidEntity( a ) && !ValidEntity( BTS.Aim )) || GetGlobalInt("BTS_ServerTime") - BTS.AimLock >= 2 ) && BTS.Scale > 0.50 then
BTS.Scale = BTS.Scale - 0.01
BTS.Aim = nil
BTS.AimLock = -1
end
end
hook.Add( "Think", "BTS_CheckAim", BTS.CheckAim );
end

Please.. any help would be appreciated. Thanks.

Offline spbogie

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 456
  • Karma: 41
Re: Custom LUA conflicting with ULX chat commands. Ideas?
« Reply #1 on: September 18, 2007, 11:10:24 AM »
In the PlayerSay hook, replace anywhere it says "return text" with just "return" (w/o quotes of course).
I have not failed. I've just found 10,000 ways that won't work. - Thomas A. Edison
I reject your reality and substitute my own. - Adam Savage

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Custom LUA conflicting with ULX chat commands. Ideas?
« Reply #2 on: September 18, 2007, 11:18:04 AM »
Thanks for your reply. I think this is what he did, but he fixed it (the author) and it runs fine now. =)-