TabComplete Engine

Share scripts you wrote for Denizen-Bukkit here!
Post Reply
User avatar
zozer_firehood
User
User
Posts: 9
Joined: August 6th, 2016, 6:50 pm

TabComplete Engine

Post by zozer_firehood » June 11th, 2020, 1:19 pm

Tired of having to figure out how to write a good tab complete script for your new fancy command script? With this procedure script you just have to provide the structure of the command and let the script do the work for you.

As an example, lets set up a warp command as so

Code: Select all

WarpTabComplete:
    type: data
    warps:
        set: end
        remove:
            _warp: end
        goto:
            spawn: end
            _warp: end
where each indent level indicates the allowed argument at that position. adding a _ at the start of a parameter indicates default parameter. this will ignore the actual input and will allow any input (as a result you should only have one _ line per level)

but what if you want more parameters that are dynamic and not just static names? You can define extra procedure scripts to be executed at that parameter.
For instance

Code: Select all

/warps goto (warp)
we know all the valid warps we can go to since its all stored in a server flag so we should add a * in between _ and warp and define a procedure script as such

Code: Select all

WarpTabComplete:
    type: data
    warps:
        set: end
        remove:
            _*warp: end
        goto:
            _*warp: end
WarpTabComplete_warp:
    type: procedure
    script:
        - determine <server.flag[warps]>
And thats it. Only thing to keep in mind the name of the procedure must be NameofContainer_PamaterName.
The script will pass in all the previous arguments the player had typed in already as context to the procedure.
For instance for

Code: Select all

/warp remove (warp)
we never want to allow the player to remove the spawn warp, instead of writing a whole new procedure we can define the procedure as such

Code: Select all

WarpTabComplete_warp:
    type: procedure
    definitions: arg
    script:
        - define warps:|:<server.flag[warps]>
        - if <[arg]> == remove:
            - define warps:<-:spawn
        - determine <[warps]>
Now what about permissions? you may be wondering. Granted you can show off all those fancy options only admins get to use but you do have the option to permission lock branches. For instance we want only users with the warps.admin permission to be able to use /warps set and /warps remove. The result would be as such:

Code: Select all

WarpTabComplete:
    type: data
    warps:
        ?warps.admin set: end
        ?warps.admin remove:
            _*warp: end
        goto:
        _*warp: end
WarpTabComplete_warp:
    type: procedure
    script:
        - determine <server.flag[warps]>
Thats it. All you have to do is insert ?(permission node) at the front of a branch to permission lock it. You are unable to lock default branches (ones that start in _). You can just check for permissions in the procedure script at that point if you need it.
After you have set it up, all you need to type in your tab complete section is

Code: Select all

- determine <proc[TabComplete].context[<list[commandName|ConatinerName].include_single[<context.raw_args>]>]>
for instance with our warp command, this is what the script could look like

Code: Select all

warpcommand:
    type: command
    name: warps
    description: warp to another location
    usage: /warps
    tab complete:
        - determine <proc[TabComplete].context[<list[warps|WarpTabComplete].include_single[<context.raw_args>]>]>
    script:
        #my code
Just drop this script in and you can start using it right away
https://paste.denizenscript.com/View/83717
Note: as a reminder, this is all for tab completion and does not impact the actual script of the command script. You do still need to do that part yourself

edit 1: added permission locking of branches

edit 2: 4/21/2021 updated fallbacks to if_null and a few minor cleanups
Last edited by zozer_firehood on April 25th, 2021, 8:06 am, edited 8 times in total.
I was once a programmer, but then I took a NullPointerException to the knee...

User avatar
zozer_firehood
User
User
Posts: 9
Joined: August 6th, 2016, 6:50 pm

Re: TabComplete Engine

Post by zozer_firehood » June 17th, 2020, 9:58 pm

edit 1: added permission locking of branches
I was once a programmer, but then I took a NullPointerException to the knee...

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests