ULX

Author Topic: Command Prefix  (Read 2948 times)

0 Members and 1 Guest are viewing this topic.

Offline Rice Cooker

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Command Prefix
« 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!

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Command Prefix
« Reply #1 on: July 03, 2019, 09:36:20 PM »
You could monkey patch ulx.fancyLogAdmin. It's a little tricky because the function is undocumented and variadic.

Steps:
  • Hijack ulx.fancyLogAdmin.
    • Find out at which argument holds the formatting string.
    • Customize the formatting string.
    • Call the original ulx.fancyLogAdmin with the customized formatting string.

I attached a working example to my post. Download and take a peek at the source if you want to see my implementation.
« Last Edit: July 03, 2019, 09:46:38 PM by Timmy »

Offline Rice Cooker

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Command Prefix
« Reply #2 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?

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Command Prefix
« Reply #3 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