Author Topic: My cexecall command  (Read 6441 times)

0 Members and 1 Guest are viewing this topic.

Offline Chironex

  • Full Member
  • ***
  • Posts: 197
  • Karma: 11
  • Formerly known as Kyzer
My cexecall command
« on: July 09, 2007, 12:52:11 AM »
Hi,

First sorry if this is not a good place to post it, maybe create a 'Useful commands/code snippets' section?

Anyway, i did that today and i think people can find it useful. For those who don't know what it do, it execute a console command for all players.

Code: [Select]
//CEXECALL
function ulx.cc_cexecall( ply, command, argv, args )

if #argv == 0 then
ULib.tsay( ply, ulx.LOW_ARGS )
return
end

local cmd = args
local allplayers = player.GetAll()

for _, v in ipairs(allplayers) do
if cmd == "r_cleardecals" then
v:ConCommand( cmd )
v:ConCommand( cmd )
else
v:ConCommand( cmd )
end
end

ulx.logServAct( ply, "#A ran command \"" .. cmd .. "\" for all players.", true )
end

ulx.concommand( "cexecall", ulx.cc_cexecall, "<command> - Runs a console command for all players.", ULib.ACCESS_SUPERADMIN, "!cexecall", true, ulx.ID_HELP )

Paste this code in 'addons/ulx/lua/ulx/utils.lua', i suggest under the 'cexec' command so things stay grouped.

Example of usage:

Code: [Select]
ulx cexecall r_cleardecals
ulx cexecall stopsounds

If there are ways to improve this code and make it more efficient, please tell me, i'm beginner. Report bugs if you find any (i didn't find any)
« Last Edit: July 09, 2007, 01:55:12 AM by Kyzer »

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6204
  • Karma: 394
  • Project Lead
Re: My cexecall command
« Reply #1 on: July 09, 2007, 01:20:37 AM »
This function was intended to be able to take multiple players and/or keywords. Here's the fixed version:

Code: Lua
  1. function ulx.cc_cexec( ply, command, argv, args )
  2.         if #argv < 2 then
  3.                 ULib.tsay( ply, ulx.LOW_ARGS )
  4.                 return
  5.         end
  6.  
  7.         local targets, err = ULib.getUsers( argv[ 1 ], _, true, ply ) -- Enable keywords
  8.         if not targets then
  9.                 ULib.tsay( ply, err )
  10.                 return
  11.         end
  12.        
  13.         for _, v in ipairs( targets ) do
  14.                 local cmd = string.gsub( args, "^%S+%s+(.*)$", "%1" )
  15.                 v:ConCommand( cmd )
  16.                 ulx.logUserAct( ply, v, "#A ran \"" .. cmd .. "\" on #T", true )
  17.         end
  18. end
  19. ulx.concommand( "cexec", ulx.cc_cexec, "<user(s)> <command> - Runs a command on the user's console.", ULib.ACCESS_SUPERADMIN, "!cexec", true, ulx.ID_PLAYER_HELP )

Now you can just do "ulx cexec <all> r_cleardecals". This has been fixed for ULX 3.20.
Experiencing God's grace one day at a time.

Offline Chironex

  • Full Member
  • ***
  • Posts: 197
  • Karma: 11
  • Formerly known as Kyzer
Re: My cexecall command
« Reply #2 on: July 09, 2007, 01:32:50 AM »
OK i fixed the bug i had.

Megiddo yes, but it's a pity to type players names to run a command on them. With cexecall you can just bind a key to a command, that's mainly why i did it.

Or i don't understand maybe, with ULib.getUsers and writing <all> as the first argument it will execute the command for all players?

Oh well, i tried ;)

PS: what is the difference between 'in pairs' and 'in ipairs' in for loops?

edit: but what if a player's name is ' <all> ' ? lol
« Last Edit: July 09, 2007, 01:56:05 AM by Kyzer »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 7972
  • Karma: 373
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: My cexecall command
« Reply #3 on: July 26, 2007, 12:16:57 AM »
Or i don't understand maybe, with ULib.getUsers and writing <all> as the first argument it will execute the command for all players?

Yes, using "!cexec <all> [some command]" would preform [some command] on all connected players including yourself unless they were immune (and you weren't superadmin)

Oh well, i tried ;)
And a good try!

PS: what is the difference between 'in pairs' and 'in ipairs' in for loops?
Good tables tutorial here. It explains pairs and ipairs.

edit: but what if a player's name is ' <all> ' ? lol
Then use "<al" and this will prevent the all keyword from being picked up. :P
The price one pays for pursuing any profession or calling is an intimate knowledge of its ugly side. - James Baldwin

Cornflake

  • Guest
Re: My cexecall command
« Reply #4 on: September 26, 2007, 12:36:37 AM »
Wow, this looks like it could be a very useful bit of code!  :) I've wanted a way to set a client-side console command before.

Just a question, will this code you posted, Megiddo, work on ULX v3.11? I just updated to that and I'd like to not have to update to 3.20 now.

-Thanks!

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6204
  • Karma: 394
  • Project Lead
Re: My cexecall command
« Reply #5 on: September 26, 2007, 06:46:23 AM »
Yes Cornflake
Experiencing God's grace one day at a time.

Cornflake

  • Guest
Re: My cexecall command
« Reply #6 on: September 26, 2007, 11:04:54 AM »
Alright, I installed the Lua file, and I tested it out (with only me on the server) and typed in:

Code: [Select]
ulx exec <all> r_dynamic 0
Then I got this message:

Code: [Select]
ConCommand blocked!
Is that normal?

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6204
  • Karma: 394
  • Project Lead
Re: My cexecall command
« Reply #7 on: September 26, 2007, 12:13:48 PM »
Oh right, forgot about garry's blocks. Sorry, a lot of functions won't work for you unless you upgrade (garry likes to take away all usefulness of various functions).
Experiencing God's grace one day at a time.

Cornflake

  • Guest
Re: My cexecall command
« Reply #8 on: September 26, 2007, 12:40:03 PM »
Ah, so ULX v 3.20 finds away around Garry's blocks?

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6204
  • Karma: 394
  • Project Lead
Re: My cexecall command
« Reply #9 on: September 26, 2007, 01:04:11 PM »
Ah, so ULX v 3.20 finds away around Garry's blocks?

Correct.
Experiencing God's grace one day at a time.