Page 1 of 1

Quick question

PostPosted: Wed Sep 07, 2016 01:23
by Infernus
Is it possible to execute a registered command server sided?

For example I have a functionality that exists within a command in a separate mod. Instead of including the file and depend on it, I just execute this command server sided.

Re: Quick question

PostPosted: Wed Sep 07, 2016 01:42
by MineYoshi
Execute the command in the server? I don't catch it so much....

Re: Quick question

PostPosted: Wed Sep 07, 2016 02:45
by Infernus
mod1:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_chatcommand("home", {
    description = SL(("Teleport you to your home point")),
    privs = {home=true},
    func = function (name)
    local player = minetest.get_player_by_name(name)
    if player == nil then
        -- just a check to prevent the server crashing
        return false
    end
    if homepos[player:get_player_name()] then
        player:setpos(homepos[player:get_player_name()])
        minetest.chat_send_player(name, SL("Teleported to home!"))
    else
        minetest.chat_send_player(name, SL("Set a home using /sethome"))
    end
end,
})


mod2:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
function execute_home
    -- call command home
end


Now the situation is that I want to call this command from mod2 instead of copying and moving code around. This should enforce mod compatibility, and reduce unnecessary edits.

I can of course declare the function globally and call it from mod2, but is it efficient solution? I have to touch the sources of mod1, there for, I break the promise.

mod1:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
gohome = function (name)
    local player = minetest.get_player_by_name(name)
    if player == nil then
        -- just a check to prevent the server crashing
        return false
    end
    if homepos[player:get_player_name()] then
        player:setpos(homepos[player:get_player_name()])
        minetest.chat_send_player(name, SL("Teleported to home!"))
    else
        minetest.chat_send_player(name, SL("Set a home using /sethome"))
    end


mod2:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
function execute_home(player)
    gohome(player:get_player_name())
end


So is there a way to execute registered command server sided?

Re: Quick question

PostPosted: Wed Sep 07, 2016 13:46
by twoelk
if the command is registered as chat command, can't you just call the command from any other mod by sending it as if typed in chat?

Re: Quick question

PostPosted: Wed Sep 07, 2016 19:57
by Infernus
twoelk wrote:if the command is registered as chat command, can't you just call the command from any other mod by sending it as if typed in chat?

Seems like client handles it in a different handler. It just displays it on client side.

Re: Quick question

PostPosted: Wed Sep 07, 2016 23:50
by kaeza
Something like this should work:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local command_def = minetest.registered_chatcommands["home"]
command_def.func("playername", "other args")


Be aware that `playername` is the name of the "player" that "sent" the command. Some commands expect to get a "valid" player name (a connected player); for example, in the case of `home`, the "sender" is the one that gets teleported home, so it expects the player to exist. If the command only uses the name in order to provide feedback (e.g. chat messages, which are discouraged BTW), this should pose no problem.

In general, it's far better to see the command's source code to see how to replicate the functionality in your own terms (e.g. fail gracefully if the player doesn't exist, or something), or see if the mod providing the command exposes some public API you can use.

If you want a real world example of calling chat commands programatically, I suggest ShadowNinja's irc_commands mod (relevant code starts here).

Ninja Edit: Fixed link.