Quick question

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

Quick question

by Infernus » Wed Sep 07, 2016 01:23

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: 4267
Joined: Wed Jul 08, 2015 13:20
GitHub: MineYosh
IRC: MineYoshi
In-game: Kirby_Retro

Re: Quick question

by MineYoshi » Wed Sep 07, 2016 01:42

Execute the command in the server? I don't catch it so much....
People talk about freedom of speech, so i'll say that God exists.
Open your eyes!! See The big unicorn conspiracy.!! :D The government has been lying to us about unicorns!!
"I've learned there are three things you don't discuss with people: religion, politics and the Great Pumpkin" - Linus Van Pelt
I'm the Officially 1st ABJist in the world ( ͡° ͜ʖ ͡°)
 

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

Re: Quick question

by Infernus » Wed Sep 07, 2016 02:45

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?
 

twoelk
Member
 
Posts: 1092
Joined: Fri Apr 19, 2013 16:19

Re: Quick question

by twoelk » Wed Sep 07, 2016 13:46

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 » Wed Sep 07, 2016 19:57

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
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: Quick question

by kaeza » Wed Sep 07, 2016 23:50

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.
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 | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 3 guests

cron