Ulysses

General => Developers Corner => Topic started by: Rice Cooker on July 03, 2019, 06:55:57 PM

Title: Command Prefix
Post by: Rice Cooker on July 03, 2019, 06:55:57 PM
I have been wondering for awhile and I have tried many ways but I can seem to find a way.
Is there a way to have ulx fancylog or command echo start with a prefix? Its not necessary but it looks more elegant.

Ex: If Admin Jails Player
Command Echo: > Admin has jailed Player
Trying To Add: >

I appreciate any help or suggestions that people can offer.
Thanks!
Title: Re: Command Prefix
Post by: Timmy on July 03, 2019, 09:36:20 PM
You could monkey patch (https://en.wikipedia.org/wiki/Monkey_patch) ulx.fancyLogAdmin. It's a little tricky because the function is undocumented and variadic (https://en.wikipedia.org/wiki/Variadic_function).

Steps:

I attached a working example to my post. Download and take a peek at the source if you want to see my implementation.
Title: Re: Command Prefix
Post by: Rice Cooker on July 04, 2019, 12:18:39 AM
Thanks Timmy! I tried your example and it worked. Is there a way to set the prefix to a predetermined color instead of relying on the default chat color?
Title: Re: Command Prefix
Post by: Timmy on July 04, 2019, 11:52:19 AM
Sure.

Internally, ulx.fancyLogAdmin uses ULib.tsayColor to send colorful chat messages to you and your players. (Source: https://github.com/TeamUlysses/ulx/blob/v3.73/lua/ulx/log.lua#L503)

Documentation for ULib.tsayColor: https://ulyssesmod.net/docs/files/lua/ulib/shared/messages-lua.html#tsayColor

You can monkey patch ULib.tsayColor so that it automatically prepends your colored prefix.

Code: [Select]
-- lua/ulx/modules/tsaycolor_patch.lua

local tsayColor = ULib.tsayColor

function ULib.tsayColor( ply, wait, ... )
    return tsayColor( ply, wait, Color( 247, 143, 179 ), "> ", ... )
end

But that will add the prefix every time ULib or ULX send colorful messages. Even for adverts. Perhaps it would be better to temporarily patch tsayColor when fancyLogAdmin runs.

Code: [Select]
-- lua/ulx/modules/fancylogadmin_patch.lua

local fancyLogAdmin = ulx.fancyLogAdmin
local tsayColor = ULib.tsayColor

local function customTsayColor( ply, wait, ... )
    return tsayColor( ply, wait, Color( 247, 143, 179 ), "> ", ... )
end

function ulx.fancyLogAdmin( ... )
    ULib.tsayColor = customTsayColor
    fancyLogAdmin( ... )
    ULib.tsayColor = tsayColor
end