• Print

Author Topic: Override ULX command  (Read 5971 times)

0 Members and 1 Guest are viewing this topic.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Override ULX command
« on: September 12, 2015, 08:04:16 pm »
Hi,

I'm trying to override a default ulx command (ulx.mute) without changing any ulx files. I tried just creating a new function and calling the ulx.command function but that brings up errors in the console and neither command ends up working. Is there any way to override a command without removing the original command's code?
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Override ULX command
« Reply #1 on: September 12, 2015, 08:31:29 pm »
Nevermind, it seems that overriding isn't my problem. I am however getting this error in console:

Code: [Select]
[ERROR] addons/ulib/lua/ulib/shared/commands.lua:985: attempt to index field 'type' (a nil value)
  1. unknown - addons/ulib/lua/ulib/shared/commands.lua:985

Line 958 of commands.lua is:
Code: Lua
  1. while cmd.args[ hidden_argn ] and (cmd.args[ hidden_argn ].type.invisible or cmd.args[ hidden_argn ].invisible) do
So I think it has something to do with the should_unmute, but I'm not sure what.

Here is my code:

Code: Lua
  1. function ulx.timemute( calling_ply, target_plys, minutes, should_unmute )
  2.         for i=1, #target_plys do
  3.                 local v = target_plys[ i ]
  4.     if timer.Exists( v:SteamID64() .. "UnmuteTimer" ) then
  5.       timer.Remove( v:SteamID64() .. "UnmuteTimer" )
  6.     end
  7.                 if should_unmute then
  8.                         v.gimp = nil
  9.                 else
  10.                         v.gimp = 2
  11.       if minutes < 0 then
  12.         timer.Create( tostring( v:SteamID64() ) .. "UnmuteTimer", minutes * 60, 0, function() v:SetNWBool("ulx_muted", false) end )
  13.       end
  14.                 end
  15.                 v:SetNWBool("ulx_muted", not should_unmute)
  16.         end
  17.  
  18.   local time = "for #i minutes"
  19.   if minutes == 0 then time = "pemanently" end
  20.   local str = "#A muted #T " .. time
  21.  
  22.         if not should_unmute then
  23.                 ulx.fancyLogAdmin( calling_ply, str, target_plys, time )
  24.         else
  25.                 ulx.fancyLogAdmin( calling_ply, "#A unmuted #T", target_plys )
  26.         end
  27. end
  28. local timemute = ulx.command( CATEGORY_NAME, "ulx timemute", ulx.timemute, "!timemute" )
  29. timemute:addParam{ type=ULib.cmds.PlayersArg }
  30. timemute:addParam{ type=ULib.cmds.NumArg, hint="minutes, 0 for perma", ULib.cmds.optional, ULib.cmds.allowTimeString, min=0 }
  31. timemute:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  32. timemute:defaultAccess( ULib.ACCESS_ADMIN )
  33. timemute:help( "Mutes target(s) so they are unable to chat for a certain amount of minutes." )
  34. timemute:setOpposite( "ulx untimemute", {_, _, _, true}, "!untimemute" )

I changed it to timemute temporarily so it's errors wouldn't prevent the normal mute command from working.
« Last Edit: September 12, 2015, 08:33:27 pm by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Caustic Soda-Senpai

  • Sr. Member
  • ****
  • Posts: 469
  • Karma: 54
  • <Insert something clever here>
    • Steam Page
Re: Override ULX command
« Reply #2 on: September 13, 2015, 04:38:24 am »
I'm confused...what exactly are you trying to accomplish?
Once you get to know me, you'll find you'll have never met me at all.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Override ULX command
« Reply #3 on: September 13, 2015, 04:43:26 am »
Well originally I was trying to override the ulx.mute command with my own ulx.mute command, but that worked by simply making a new command. Maybe I should make a new topic for this since the topic name no longer reflects my problem.

Anyways right now I am trying to make this command and it is just giving me this console error related to the structure of the ulx function.
« Last Edit: September 13, 2015, 04:50:37 am by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Override ULX command
« Reply #4 on: September 13, 2015, 05:05:48 am »
Are you running the latest version of ulx and ULib? If you are, I suspect one of your addons is conflicting with ULib. That error seems unrelated to the code of your timemute command.

I fixed up your command. There were only a few minor mistakes. (:
« Last Edit: September 13, 2015, 05:08:37 am by Timmy »

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Override ULX command
« Reply #5 on: September 13, 2015, 06:25:54 am »
I run the latest release, but not the svn. I have never had any trouble with ULib before, and I'm not sure why you think this problem is caused by another addon.

You did fix a few things like changing the less than sign to a greater than sign, but other changes are useless or detrimental.
 
Code: Lua
  1. function ulx.mute( calling_ply, target_plys, minutes, should_unmute )
  2.         for i=1, #target_plys do
  3.                         local v = target_plys[ i ]
  4.  
  5.                         if timer.Exists( v:SteamID() .. "UnmuteTimer" ) then
  6.                                 timer.Remove( v:SteamID() .. "UnmuteTimer" )
  7.                         end
  8.  
  9.                         if should_unmute then
  10.                                 v.gimp = nil
  11.                                 v:SetNWBool( "ulx_muted", false ) --not sure why you've done this, it can be compressed with v:SetNWBool("ulx_muted", not should_unmute) just fine
  12.                         else
  13.                                 v.gimp = 2
  14.                                 v:SetNWBool( "ulx_muted", true ) --not sure why you've done this, it can be compressed with v:SetNWBool("ulx_muted", not should_unmute) just fine
  15.                                
  16.                                 if minutes > 0 then --you did fix the sign here
  17.                                         timer.Create( v:SteamID() .. "UnmuteTimer", minutes * 60, 0, function()
  18.                                                 RunConsoleCommand( "ulx", "unmute", v:Name() ) -- running this command is time wasting, you could just set v.gimp to nil and v:SetNWBool( "ulx_muted", false ) because that is all the unmute command does
  19.                                         end )
  20.                                 end
  21.                         end
  22.                 end
  23.  
  24.                 local time = "for #i minute(s)"
  25.                 if minutes == 0 then time = "pemanently" end
  26.                 local str = "#A muted #T " .. time
  27.  
  28.                 if not should_unmute then
  29.                         ulx.fancyLogAdmin( calling_ply, str, target_plys, minutes )
  30.                 else
  31.                         ulx.fancyLogAdmin( calling_ply, "#A unmuted #T", target_plys )
  32.                 end
  33. end
  34. local mute = ulx.command( "Utility", "ulx mute", ulx.mute, "!mute" )
  35. mute:addParam{ type=ULib.cmds.PlayersArg }
  36. mute:addParam{ type=ULib.cmds.NumArg, hint="minutes, 0 for perma", ULib.cmds.optional, ULib.cmds.allowTimeString, min=0 }
  37. mute:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  38. mute:defaultAccess( ULib.ACCESS_ADMIN )
  39. mute:help( "Mutes target(s) so tihey are unable to chat for a certain amount of minutes." )
  40. mute:setOpposite( "ulx unmute", {_, _, _, true}, "!unmute" )

Thanks for the help, I'm not exactly sure why I got the error but once I change a few things about this it should work perfectly!
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Override ULX command
« Reply #6 on: September 13, 2015, 07:21:17 am »
One thing you might have missed:
Code: Lua
  1. ulx.fancyLogAdmin( calling_ply, str, target_plys, minutes ) -- Changed "time" to "minutes" here.

--not sure why you've done this, it can be compressed with v:SetNWBool("ulx_muted", not should_unmute) just fine
To be explicit. There's nothing wrong with compressing it though.

-- running this command is time wasting, you could just set v.gimp to nil and v:SetNWBool( "ulx_muted", false ) because that is all the unmute command does
If you want to change the unmute functionality now, you will only have to do it once.

I have never had any trouble with ULib before, and I'm not sure why you think this problem is caused by another addon.
I was suspecting a conflicting addon because I didn't get any ULib-related errors when running/playing with the code you posted. I tested with the latest stable release and the svn version.
« Last Edit: September 13, 2015, 07:44:11 am by Timmy »

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Override ULX command
« Reply #7 on: September 13, 2015, 09:38:23 am »
Yea I realized the time needed to be replaced with minutes, I changed it. Thanks again!
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Override ULX command
« Reply #8 on: September 13, 2015, 01:06:31 pm »
ULib has hooks for when commands are called, including ULX commands.
You could totally stop the normal "ulx mute" from running by returning false after running your own code.
ULibCommandCalled

Code: [Select]
function myfunction ( ply, command, args )
     if command == "ulx mute" then
          ... do stuff with ply(caller) and args (table, including target and minutes related to whatever command)
     end
     return false, nil or error (string if you want to say an error )
end
hook.add ( ULibCommandCalled, check_mute, myfunction )
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

  • Print