r173: Register/bind commands
#1
A new feature in the upcoming release r173 is that you can register commands, so you don't have to parse the chat messages yourself anymore.

This feature was requested twice:
http://www.mc-server.org/support/index.p...task_id=71
http://www.mc-server.org/support/index.p...task_id=24

So you created a function that needs to be called when you type /god, this is the function:
Code:
function HandleGodCommand( Split, Player )
    if( not Player:HasPermission( "myplugin.god" ) ) then -- Check permissions first
        return false
    end
    MakePlayerGod( Player ) -- Do something to make the player invincible
    return true
end

It's really easy to bind this function to a command, you bind a command on a plugin. Like so:
Code:
function MyPlugin:Initialize()
    self:AddCommand("/god", " - Makes you invincible", "myplugin.god") -- This only adds the command to the commandlist for /help
    self:BindCommand("/god", HandleGodCommand) -- This actually binds the /god command to the HandleGodCommand function
    return true -- Make sure you return true, otherwise the plugin is not added
end

The function HandleGodCommand is called each time a player types /god

UPDATE:
In r174 you can also specify the permissions of a command, so you don't have to manually check it.
Code:
self:BindCommand("/god", HandleGodCommand) -- This still works, but you have to manually check permissions
self:BindCommand( "/home", "myplugin.god", HandleGodCommand) -- New version where permissions are checked for you
Both methods work.
Reply
Thanks given by:
#2
Sounds good.

But why do self:AddCommand takes as third argument the permission when I have to check for permission in function HandleGodCommand Huh ?

greets,
Tybor
Reply
Thanks given by:
#3
It's for the /help plugin or any other plugin that might be curious about the commands a certain plugin uses. The /help plugin needs to be aware of the permissions, otherwise it can't filter out commands the player can't use.
Reply
Thanks given by:
#4
(02-24-2011, 05:39 AM)FakeTruth Wrote: It's for the /help plugin or any other plugin that might be curious about the commands a certain plugin uses. The /help plugin needs to be aware of the permissions, otherwise it can't filter out commands the player can't use.
So why not just let the server check permission if it already gets this information?
This would ease the use of commands a little bit more.
If no permission is provided, you can still check them by yourself.

greets,
Tybor
Reply
Thanks given by:
#5
Good call, I did not think of only checking manually if you don't specify a permission.
I'll see what I can do, and if you're lucky it won't even break plugins created for r173 because I might be able to overload the function.
Reply
Thanks given by:
#6
In r174 you can now also specify the permissions of a command, so you don't have to manually check it.
Code:
self:BindCommand("/god", HandleGodCommand) -- This still works, but you have to manually check permissions
self:BindCommand( "/god", "myplugin.god", HandleGodCommand) -- New version where permissions are checked for you
Both methods work.

You still need to use the AddCommand function for the /help plugin. This is because you might have several command aliases ( /item and /i ) but you don't want all aliases to show in the command list.
Reply
Thanks given by:
#7
Thumbs Up 
Cool
Reply
Thanks given by:
#8
(02-24-2011, 10:54 PM)FakeTruth Wrote: In r174 you can now also specify the permissions of a command, so you don't have to manually check it.
Code:
self:BindCommand("/god", HandleGodCommand) -- This still works, but you have to manually check permissions
self:BindCommand( "/god", "myplugin.god", HandleGodCommand) -- New version where permissions are checked for you

Heart
I love it when i get to type less.
:P
Reply
Thanks given by:




Users browsing this thread: 2 Guest(s)