• Print

Author Topic: Hook Priority  (Read 4869 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Hook Priority
« on: April 09, 2017, 12:06:11 am »
I have a question about hook priorities. I have two hooks currently, a PlayerSay hook that checks if the player has PData that wouldn't allow them to speak, and a PlayerCanSeePlayersChat hook that checks the same thing. The purpose of the PlayerSay hook is to warn the player they cannot speak and to let an Admin know using Admin Say. The PlayerCanSeePlayersChat hook should be obvious, but it checks if they have the PData and if they have it, return false. Here's what I have:


Code: Lua
  1.  
  2. function pmuteHook( text, team, listener, speaker )
  3.  
  4.  
  5.         if speaker:IsValid() and speaker:GetPData( "permmuted" ) and speaker:GetPData( "permmuted" ) == "true" then
  6.        
  7.                 -- ULib.tsayError( speaker, "You are permanently muted and cannot speak, please ask an Admin (@) to unpmute you." )
  8.                 return false
  9.                
  10.         else
  11.  
  12.  
  13.                 return true
  14.        
  15. end
  16. hook.Add( "PlayerCanSeePlayersChat", "pdatamute", pmuteHook )
  17.  
  18.  
  19. function pmuteWarnPlayer( ply, text, bTeamOnly )
  20.         local players = player.GetAll()
  21.         local randTarg = players[ math.floor( math.random( 1, #player.GetAll() ) ) ]
  22.         if not pmuteHook( text, bTeamOnly, randTarg, ply ) then
  23.                 ULib.tsayError( ply, "You are permanently muted. Please contact an Admin (using @<message>) for more information." )
  24.                 return ""
  25.         end
  26. end
  27. hook.Add( "PlayerSay", "pmuteWarnPlayer", pmuteWarnPlayer )
  28.  


So I was wondering if it was possible to call one of the hooks before the other, because I think I'd need to call the PlayerCanSeePlayersChat hook first. This may be stupid, though, as can't I just put that PlayerSay hook inside the PlayerCanSeePlayersChat hook? Not the actual hook, obviously, but the hook inside to let them know? Something like what I have but with the tsayError statement uncommented?


Also, another question, is it possible to do
Code: Lua
  1. player.GetAll()[ math.floor( math.random( 1, #player.GetAll() ) ) ]
?



I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Hook Priority
« Reply #1 on: April 09, 2017, 04:23:54 am »
On your second question, yes that would work, but you don't need the "math.floor", and you should probably store the results of "player.GetAll()" so you don't have to evaluate it twice. For our more advanced users on the forum, yes, I realize that LuaJIT may optimize that out anyways, but you might as well get it right the first time.

For your first question, it's unclear why you are using both these hooks at all. For example, ULX's mute only uses PlayerSay and accomplishes its task fine. But to more directly answer your question, ULib's hook priorities are only useful within the same type of hook -- they do not designate priorities between different hook types. As an additional note, "return true" in the first hook is a faux pas. By returning a value unnecessarily, you block other hooks from running after you.
Experiencing God's grace one day at a time.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Hook Priority
« Reply #2 on: April 10, 2017, 06:54:18 am »
I originally didn't have the 'return true' but I put that in when I put it on here... not sure why.

So, really, I could just have this:

Code: Lua
  1. function pmuteHook( text, team, listener, speaker )
  2.  
  3.  
  4.         if speaker:IsValid() and speaker:GetPData( "permmuted" ) and speaker:GetPData( "permmuted" ) == "true" then
  5.        
  6.                 ULib.tsayError( speaker, "You are permanently muted and cannot speak, please ask an Admin (@) to unpmute you." )
  7.                 return false
  8.        
  9. end
  10. hook.Add( "PlayerCanSeePlayersChat", "pdatamute", pmuteHook )
  11.  
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Hook Priority
« Reply #3 on: April 10, 2017, 03:57:03 pm »
Dejavu!

Sometimes I feel like
I'm walking in circles.

By no means do I recommend stop improving because the wheel already exists.
I simply recommend reviewing wheels that have already been built and may even still spin.
:)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Hook Priority
« Reply #4 on: April 10, 2017, 04:41:10 pm »
I know I created this one, I'm just trying to improve upon it because I've received a lot of messages about people complaining about it, so I wanted something that warned them in-case they didn't know.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print