Quick question

Post Reply
Infernus
New member
Posts: 9
Joined: Wed Aug 31, 2016 18:10

Quick question

by Infernus » Post

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.
Last edited by Infernus on Wed Sep 07, 2016 02:07, edited 1 time in total.

User avatar
MineYoshi
Member
Posts: 5373
Joined: Wed Jul 08, 2015 13:20
Contact:

Re: Quick question

by MineYoshi » Post

Execute the command in the server? I don't catch it so much....
Have a nice day! :D

Infernus
New member
Posts: 9
Joined: Wed Aug 31, 2016 18:10

Re: Quick question

by Infernus » Post

mod1:

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:

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:

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:

Code: Select all

function execute_home(player)
    gohome(player:get_player_name())
end
So is there a way to execute registered command server sided?

twoelk
Member
Posts: 1482
Joined: Fri Apr 19, 2013 16:19
GitHub: twoelk
IRC: twoelk
In-game: twoelk
Location: northern Germany

Re: Quick question

by twoelk » Post

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?

Infernus
New member
Posts: 9
Joined: Wed Aug 31, 2016 18:10

Re: Quick question

by Infernus » Post

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.

User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

Re: Quick question

by kaeza » Post

Something like this should work:

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.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests