Cuberite Forum
r173: Register/bind commands - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html)
+--- Thread: r173: Register/bind commands (/thread-9.html)



r173: Register/bind commands - FakeTruth - 02-24-2011

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.php?do=details&task_id=71
http://www.mc-server.org/support/index.php?do=details&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.


RE: r173: Register/bind commands - Tybor - 02-24-2011

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


RE: r173: Register/bind commands - FakeTruth - 02-24-2011

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.


RE: r173: Register/bind commands - Tybor - 02-24-2011

(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



RE: r173: Register/bind commands - FakeTruth - 02-24-2011

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.


RE: r173: Register/bind commands - FakeTruth - 02-24-2011

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.


RE: r173: Register/bind commands - Tybor - 02-25-2011

Cool


RE: r173: Register/bind commands - Tim - 02-25-2011

(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