Author Topic: A new option for PlayerArg and PlayersArg  (Read 405 times)

0 Members and 1 Guest are viewing this topic.

Offline markusmarkusz

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
A new option for PlayerArg and PlayersArg
« on: November 13, 2016, 06:26:59 AM »
It would be very nice to have a option that can be used to restrict PlayerArg and PlayersArg to players which fit some things.
Example:
I have a respawn Command. Let's called it ulx respawn.
And I want it that you can only target players that are dead.
So that users where Player:Alive() returns false aren't displayed.
Something like:
Code: [Select]
local function targets()
local pl = {}
for k,v in pairs(player.GetAll()) do
if not v:Alive() then table.insert(pl, v) end
end
return pl
end
function ulx.respawn( calling_ply, target_ply)
target_ply:Spawn()
ulx.fancyLogAdmin( calling_ply, "#A respawned #T", affected_plys, dmg )
end
local respawn = ulx.command("Util", "ulx respawn", ulx.respawn, "!respawn" )
respawn:addParam{ type=ULib.cmds.PlayerArg, autocomplete_fn=targets }
respawn:defaultAccess(ULib.ACCESS_ADMIN)
respawn:help( "Respawns target." )

If this is already possible and I just missed something, then tell me what.
Thanks.

Online Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 720
  • Karma: 112
  • :)
    • View Profile
    • My Homepage
Re: A new option for PlayerArg and PlayersArg
« Reply #1 on: November 13, 2016, 09:24:22 AM »
Well, for starters, it looks like you've copied a template for another command without modifying it to suit your needs.

Your call to fancyLogAdmin uses two undefined variables, affected_plys and dmg (which looks similar to ulx.slap). Instead of those, just use target_ply.

Second off, using autocomplete to hide targets doesn't mean they can't go outside of the bounds you set (IIRC). You would still need to have checks in your code which determine if the targeted player is dead, which rather defeats the purpose.
bw81@ulysses-forums ~ % whoami
Homepage

I have 10 more Keybase invites! PM me for more information or to request one!
An easy intro to PGP and public-key cryptography

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6154
  • Karma: 387
  • Project Lead
    • View Profile
Re: A new option for PlayerArg and PlayersArg
« Reply #2 on: November 13, 2016, 02:32:13 PM »
Bytewave is correct that autocompletes aren't necessarily tied to permissions (and neither is necessarily a superset of the other).

I agree with you, filtering arguments in such a way allows for much more flexibility and keeps the code less cluttered with checks at the beginning of each command functions. All of this will be coming in ULX4. :)

Here's an example of what we want to accomplish with the new restriction structure in ULX4.

Code: Lua
  1. cmd = plugin:Command("command", ulx.command)
  2. cmd:Hint("Hint text")
  3. cmd:Access("admin")
  4. cmd:Args({
  5.   ArgPlayer(),
  6.   ArgTime():Optional():min(0),
  7.   ArgString():Optional():Greedy():RestrictToCompletes():Completes(tbl)
  8. })
  9. cmd:Restrictions({
  10.   RestrictToOnceEvery("1 hour"),
  11.   RestrictToTime("1500-1700"),
  12.   RestrictToAbsenceOf("%superadmin"),
  13.   RestrictToPresenceOf("#user"),
  14.   RestrictToCustom(function(ply, ...)
  15.     if not ply:IsAlive() then
  16.       return false, "you must be alive to run this command"
  17.     else
  18.       return true
  19.     end
  20.   end)
  21. })

And for the deathly curious, ULX4 will be Moonscript-based (but the above Lua will work perfectly fine as well) -- the Moonscript from which the above was generated is...

Code: Text
  1. with plugin\Command( "command", ulx.command ) -- Chat alias is automatically assumed, can override with .ChatAlias = ...
  2.         \Hint "Hint text"
  3.         \Access "admin"
  4.         --\Category "fun" -- Only need to specify if differing from plugin category
  5.         --\ChatAlias "command" or {"cmd", "command"} for multiple - if unspecified, "command" from first line is assumed
  6.         --\ConsoleAlias "cmd" or {"cmd1", "cmd2"} for multiple
  7.         \Args{
  8.                 ArgPlayer! -- help text for arguments automatically contains "player to <command name>"
  9.                 ArgTime!\Optional!\min 0 -- help text "time (in minutes) to <command name>"
  10.                 ArgString!\Optional!\Greedy!\RestrictToCompletes!\Completes tbl
  11.         }
  12.         \Restrictions{
  13.                 RestrictToOnceEvery "1 hour"
  14.                 RestrictToTime "1500-1700"
  15.                 RestrictToAbsenceOf "%superadmin"
  16.                 RestrictToPresenceOf "#user"
  17.                 RestrictToCustom (ply, ...) -> if not ply\IsAlive() then return false, "you must be alive to run this command" else return true
  18.         }
« Last Edit: November 13, 2016, 02:38:11 PM by Megiddo »
Experiencing God's grace one day at a time.

Offline markusmarkusz

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: A new option for PlayerArg and PlayersArg
« Reply #3 on: November 14, 2016, 07:14:12 AM »
Well, for starters, it looks like you've copied a template for another command without modifying it to suit your needs.

Your call to fancyLogAdmin uses two undefined variables, affected_plys and dmg (which looks similar to ulx.slap). Instead of those, just use target_ply.
I have to admit that this was just copied and a bit edited. It was just an example. ;)
(But how did you know that I copied it from ulx.slap :o?)

The only reason why I asked this was, that I don't like these checks. They're a bit too much in some cases.

Online Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 720
  • Karma: 112
  • :)
    • View Profile
    • My Homepage
Re: A new option for PlayerArg and PlayersArg
« Reply #4 on: November 14, 2016, 09:07:12 AM »
I have to admit that this was just copied and a bit edited. It was just an example. ;)
(But how did you know that I copied it from ulx.slap :o?)
I was looking for ulx.fancyLogAdmin examples so I could remember what the tags mean, and just happened to use ulx.slap as my example. :)

The only reason why I asked this was, that I don't like these checks. They're a bit too much in some cases.
What do you mean?
You've already written the checks in your code, you'd just have to move it. Just make sure target_ply isn't IsAlive and isn't IsSpec (sic), and respawn if the test matches, else error.
bw81@ulysses-forums ~ % whoami
Homepage

I have 10 more Keybase invites! PM me for more information or to request one!
An easy intro to PGP and public-key cryptography

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 7610
  • Karma: 343
  • Sertafide Ulysses Jenius
    • View Profile
    • Team Ulysses [ULib/ULX, other fine releases]
Re: A new option for PlayerArg and PlayersArg
« Reply #5 on: November 14, 2016, 03:17:37 PM »
markusmarkusz, over time, we've honed and developed a robust framework for each new major revision of ULX and ULib.
It seems we have a cycle of about ~5 years on average where we release a major update to ULX in such a way that everything known must be relearned.
To the end user, rarely noticed.
To the ULX developer, perhaps a pain, but shiny and new and grand once studied.
Though you may not like the checks now, I humbly refer you a post I made in 2010, right about the time we were about to release ULX 3.5.
https://forums.ulyssesmod.net/index.php/topic,4464.msg17838.html

At that time, our code base for ULX functions significantly changed for the good, becoming less, as each command object became where many checks and information were done and setup before ever reaching the actual function code.

Though I appreciate your opinion, I tend to agree with others in that though nice, they aren't really required.
Who knows though, perhaps in addition to the picker targets like we have now (@, !, %, *, ^), we could one day add #dead and #alive to the targeting system.

And as for how one knew where you were grabbing example from - see my signature.
Sometimes we, and our community, know our code too well. :)




The price one pays for pursuing any profession or calling is an intimate knowledge of its ugly side. - James Baldwin