• Print

Author Topic: ULX Admin Chat  (Read 13604 times)

0 Members and 1 Guest are viewing this topic.

Offline Darkblizzard

  • Newbie
  • *
  • Posts: 39
  • Karma: 1
ULX Admin Chat
« on: July 15, 2017, 12:46:40 am »
Hello!
I'm currently trying to edit a small portion of the admin chat though unsure the correct way of editing a line of code. The code with format =  "[Admin Request] #P: #s" is what I'm trying to edit. I'm trying to make the part the says "[Admin Request]" have a specific color to it while the rest stays the same.
 
Code: [Select]
function ulx.asay( calling_ply, message )
    local format
    local me = "/me "
    if message:sub( 1, me:len() ) == me then
        format = "(ADMINS) *** #P #s"
        message = message:sub( me:len() + 1 )
    else
        format =  "[Admin Request] #P: #s"
    end

    local players = player.GetAll()
    for i=#players, 1, -1 do
        local v = players[ i ]
        if not ULib.ucl.query( v, seeasayAccess ) and v ~= calling_ply then -- Calling player always gets to see the echo
            table.remove( players, i )
        end
    end

    ulx.fancyLog( players, format, calling_ply, message )
end

I've attempted of doing this, but of course it doesn't work. Would anyone have the solution of getting what I want to work?
Thanks

Code: [Select]
format =  Color( 0, 255, 0, 255 ), "[Admin Request] ", .. "#P: #s"

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: ULX Admin Chat
« Reply #1 on: July 15, 2017, 01:05:33 am »
I do not believe fancyLogAdmin accepts colours as an argument, so if anything you'd need to use ULib.tsayColor in order to send it to the players.

As a side note, it's never really a good idea to edit core files of any addon, because you might screw something up and then the whole addon breaks, and even if you don't you'll have to edit it the next time an update comes out as your files will be overwritten.

With that being said, you could always use the ULibCommandCalled hook to stop the default action from being taken and to execute your own code.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Darkblizzard

  • Newbie
  • *
  • Posts: 39
  • Karma: 1
Re: ULX Admin Chat
« Reply #2 on: July 15, 2017, 10:05:38 am »
I may need some help finishing this code. I'm not quite sure how to grab the message once it found that command is used.

Code: [Select]
hook.Add( ULibCommandCalled, admin chat replace, function( ply, commandName, args)
local col = Color(255, 255, 255, 255), "[Admin Request] "
if commandName == "ulx asay" then

return col .. ply .. " "

end
end )
« Last Edit: July 15, 2017, 10:09:28 am by Darkblizzard »

Offline monkeymacman

  • Newbie
  • *
  • Posts: 44
  • Karma: 13
Re: ULX Admin Chat
« Reply #3 on: July 15, 2017, 11:44:08 am »
I may need some help finishing this code. I'm not quite sure how to grab the message once it found that command is used.

Code: [Select]
hook.Add( ULibCommandCalled, admin chat replace, function( ply, commandName, args)
   local col = Color(255, 255, 255, 255), "[Admin Request] "
   if commandName == "ulx asay" then
   
   return col .. ply .. " "

   end
end )
You did the hook.Add wrong, look more into it on the wiki. As for for the actual command, message = args[1] (probably), and mostly just follow the actual code that ULX has for the command, but replace the fancylog with whatever you want to use to send it, and add whatever thing

Offline Darkblizzard

  • Newbie
  • *
  • Posts: 39
  • Karma: 1
Re: ULX Admin Chat
« Reply #4 on: July 15, 2017, 06:15:52 pm »
Sorry, slightly new to lua so still learning. I fixed the hook finding the problem already. Now as for the "message = args[1]", I'd assume that's a global variable, but not quite sure what the "args[1]" would define? I'm also thinking ahead to wondering how I could send the message to only those with the permission to see asay.
« Last Edit: July 15, 2017, 06:33:57 pm by Darkblizzard »

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: ULX Admin Chat
« Reply #5 on: July 15, 2017, 09:19:52 pm »
This is how hookAdd works:
Code: Lua
  1. hook.Add( "ULibCommandCalled", "admin chat replace", function( ply, cmd, args )
  2.     -- your code
  3. end
  4.  
So this adds the ULibCommandCalled hook that currently does nothing. Notice how the first two variables to the function are in quotes; they need to be strings.

Then you want to check if the command is the away command, like you did:
Code: Lua
  1. if cmd == "ulx asay" then
  2.     -- your code
  3. end
  4.  

Then, you'll want to stop the default asay function from being called. The hook's docs page says "return false to override and not allow, true to stop executing callbacks and allow.". So, after your call you will want to have a "return false" in there, so that the default ulx.asay function isn't called.

I wrote it up for you (sorry) because it wasn't super difficult, however I left some comments in there so you'd be able to figure out things later on if you'd like to :)

Code: Lua
  1. hook.Add( "ULibCommandCalled", "admin chat replace", function( ply, cmd, args )
  2.     if cmd == "ulx asay" then
  3.         local admins = {} -- Setup an empty table so our new code knows who to send it to
  4.         for _, player in pairs( player.GetAll() ) do -- Loop through each player on the server...
  5.             if ULib.ucl.query( player, "ulx seeasay" ) then -- ...Then check if the player has the ability to see asay chat...
  6.                 table.insert( admins, player ) -- ...If they do, insert them into our table.
  7.             end
  8.         end
  9.         for _, player in pairs( admins ) do -- Now we want to loop through each player in our 'admins' table.
  10.             ULib.tsayColor( player, "", Color( 0, 255, 0 ), "[Admin Request] ", Color( 255, 255, 255 ), ply:Nick() .. ": " .. args[ 1 ] ) -- The first argument for tsayColor is a player. Since we're going through each player in the "admins" table, this will be sent to all of them. I add in an empty string ("") because this function doesn't change colour until at least one string is finished, not sure why but that's how I get around it. Next we add in your colour for the "[Admin Request]" part of the string, which is 0, 255, 0. We leave a space so we don't have to concatenate a space ( "some string" .. " " .. "some other string" ). Then we reset back to white text ( 255, 255, 255 ), add the person's name, then paste in the rest of the message.
  11.         end
  12.         return false -- Most important part, so only this code is called, nothing else.
  13.     end
  14. end )
  15.  
So this should work, keep in mind I just wrote this up so I MAY have forgotten something, but that should work.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Darkblizzard

  • Newbie
  • *
  • Posts: 39
  • Karma: 1
Re: ULX Admin Chat
« Reply #6 on: July 15, 2017, 09:28:56 pm »
Thanks for the help! I'll be surely to learn along the way as I attempt to code more minor scripts for servers to come. Though I was curious about what "_," meant?

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: ULX Admin Chat
« Reply #7 on: July 15, 2017, 09:53:35 pm »
Ah. A "pairs" loop loops through the keys and values of a table. In the first case, the player.GetAll returns a table, where the key is the number specified to the player (I.E. 2 or 7), while the value is the actual player entity, which is the part we need. Adding a "_" is just a variable so I can remind myself it isn't needed. In the past, I'd use
Code: Lua
  1.  for k,v in pairs( player.GetAll() ) do
and just use the "v".

You can read up on loops here: https://www.lua.org/pil/7.3.html
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Darkblizzard

  • Newbie
  • *
  • Posts: 39
  • Karma: 1
Re: ULX Admin Chat
« Reply #8 on: July 15, 2017, 10:01:39 pm »
So for "_ , player" the k would be the "_" and the v would be "player" while the "," just separates the two?

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: ULX Admin Chat
« Reply #9 on: July 15, 2017, 10:04:35 pm »
That would be correct. Simply naming it "player" lets me know exactly what I'm looking at.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Darkblizzard

  • Newbie
  • *
  • Posts: 39
  • Karma: 1
Re: ULX Admin Chat
« Reply #10 on: July 15, 2017, 11:35:30 pm »
Alright! I understand now. After testing it work rather fine! Though looking closely at it, I noticed that return would only be either true or false. Would I have to make another hook if I wanted the command to disappear from chat?

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: ULX Admin Chat
« Reply #11 on: July 15, 2017, 11:50:05 pm »
Hm... never thought of that actually. I'll do some digging and let you know if I can find how ULX/ULib hides command calls.

EDIT: I think you may need to make another hook, yes. Most specifically, a GM:PlayerSay hook. I found this section in the source of ULib. ULib calls the command, then checks if the command is hidden, and returns nothing if it is. So I think you'd need to check if any message starts with @, and if it does to return nothing:

Code: Lua
  1. hook.Add( "PlayerSay", "asay overwrite fix", function( ply, text, team )
  2.     if string.StartWith( text, "@" ) then
  3.         return ""
  4.     end
  5. end )
  6.  
« Last Edit: July 15, 2017, 11:55:19 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Darkblizzard

  • Newbie
  • *
  • Posts: 39
  • Karma: 1
Re: ULX Admin Chat
« Reply #12 on: July 16, 2017, 12:04:52 am »
I tested out the small hook, and it would seem that if you space out any text in asay, it would be deleted only grabbing the non-spaced string.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: ULX Admin Chat
« Reply #13 on: July 16, 2017, 01:09:57 am »
Ah, so they're all placed as separate args, then.

Easy fix:

Code: Lua
  1. hook.Add( "ULibCommandCalled", "admin chat replace", function( ply, cmd, args )
  2.     if cmd == "ulx asay" then
  3.         local admins = {}
  4.         local final_str = ""
  5.         for _, player in pairs( player.GetAll() ) do
  6.             if ULib.ucl.query( player, "ulx seeasay" ) then
  7.                 table.insert( admins, player )
  8.             end
  9.         end
  10.         for _, arg in pairs( args ) do
  11.             final_str = final_str .. arg -- Goes through each arg and concatenates it to "final_str"
  12.         end
  13.         for _, player in pairs( admins ) do
  14.             ULib.tsayColor( player, "", Color( 0, 255, 0 ), "[Admin Request] ", Color( 255, 255, 255 ), ply:Nick() .. ": " .. final_str )
  15.         end
  16.         return false
  17.     end
  18. end )
  19.  
  20.  
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline monkeymacman

  • Newbie
  • *
  • Posts: 44
  • Karma: 13
Re: ULX Admin Chat
« Reply #14 on: July 16, 2017, 07:11:25 am »
With your current code I think you forget to allow the person sending the message to see it, which would be easy, just replace the
Code: Lua
  1. if ULib.ucl.query( player, "ulx seeasay" ) then
with
Code: Lua
  1. if ULib.ucl.query( player, "ulx seeasay" ) or player == ply then

Alternatively, you could just add a
Code: Lua
  1. table.insert( admins, ply )
after the loop, which might be more efficient (though neither are what the ulx asay command normally does)

  • Print