Download
(4 Kb)
Download
Updated: 03-08-12 04:50 PM
Updated:03-08-12 04:50 PM
Created:03-01-12 11:35 PM
Downloads:502
Favorites:0
MD5:
LibSlashCmd
Version: 0.3
by: TimeBomb [More]
What is LibSlashCmd?
LibSlashCmd is intended to make handling any and all slash commands simple and worryless. It also has complete slash command clashing protection.


Features:
- Same-name command clashing support.
ex. If you try to register /myaddon but it is already taken, it will (attempt to) register /myaddon2 instead.
LibSlashCmd supports clashing with any and all RIFT slash commands - even with addons that don't use LibSlashCmd.
If you use LibSlashCmd, your slash command will always work, no matter how many addons use your exact slash command. (Allows for up to 9999 same-name slash commands.)

- Simplistic infinite arguments and user input support.

- Your code is (arguably) more organized and easier to read (in comparison to using if/elseif to go through different commands and user input).

- Simple support for fallbacks. If a user enters an unrecognized command, you can (optionally) point them to any function (ex. a message).


How to use it:
With LibSlashCmd, you do not need to worry about registering your command with anything but LibSlashCmd. So no table.insert(Command.Slash.Register("command")...
With LibSlashCmd, you are not required to have a slash command handling function.

First, let's register our command with LibSlashCmd, which will then register it with RIFT.
Code:
LibSlashCmd.Register("myaddon", "MyAddon")
The first parameter is the slash command. The second parameter is your addon name. (If the addon name is incorrect, RIFT will complain.)

The Register method returns the command that you will be using throughout your addon. If your command is already in use by another addon, and clashes, then it may be changed to /command2, /command20, /command999, etc. It will keep trying until it works (from #2 to #9999). (If all 9999 slash command names are taken, the Register method will return nil.)
I suggest creating an addon-wide local to store the Register method return value (the command).
Code:
local command = nil
command = LibSlashCmd.Register("myaddon", "MyAddon")
The LibSlashCmd.Register should be called when your addon is loading/initializing.

LibSlashCmd.RegisterFunc Examples:
For this example here, when the user types /myaddon ANYTHING, it will fallback to the default function, and print "default".
Code:
local command = nil
command = LibSlashCmd.Register("myaddon", "MyAddon")
LibSlashCmd.RegisterFunc(command,
                        "_fallback",
                        function(input)
                            print("default")
                        end)
It's important to note that the fallback function will only be called if no other functions can be found with the user's input.

For the next example, when the user types /myaddon tell me ANYTHING, it will print ANYTHING.
Code:
local command = nil
command = LibSlashCmd.Register("myaddon", "MyAddon")
LibSlashCmd.RegisterFunc(command,
                        "tell me",
                        function(input)
                            message = table.concat(input, " ")
                            print(message)
                        end)
It's important to note that arguments can have spaces in them. It will still work fine, and work as intended.
It's also important to note that the function NEEDS to have one argument. That argument is all of the user's input, in a table array, split by one space per value.
You'll notice that, to get all of the input in one string, I used the table.concat method.
I could have used input[0] if I just wanted the first word.

For organization purposes, you may want to put all the RegisterFunc calls into a separate function, and call that function right after you call LibSlashCmd.Register.


As of version 0.3, LibSlashCmd.InvokeFunc is now available.
It allows the developer to invoke a command function from the code with ease.
Example:
Code:
LibSlashCmd.InvokeFunc("myaddon", "tell me", "This is a dev test.")
This will produce the same result as if the user typed /myaddon tell me This is a dev test.
This makes debugging, and handling certain scenarios, ex. preset configurations, much easier.
Use less code, and keep the code you are using more consistant. This is one factor that makes LibSlashCmd.InvokeFunc so powerful.
V0.2
Fixed major bug where functions with both arguments and user input were not working.
Increased max command clash limit from 99 to 9999.

V0.11
Fixed minor bug that made everything go kaboom.

V0.1
Initial Release
Archived Files (3)
File Name
Version
Size
Author
Date
0.2
4kB
TimeBomb
03-06-12 05:12 AM
0.11
4kB
TimeBomb
03-01-12 11:54 PM
0.1
4kB
TimeBomb
03-01-12 11:35 PM


There have been no comments posted to this file.
Be the first to add one.



Category Jump: