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.
Quick question
Quick question
Last edited by Infernus on Wed Sep 07, 2016 02:07, edited 1 time in total.
Re: Quick question
Execute the command in the server? I don't catch it so much....
Have a nice day! :D
Re: Quick question
mod1:
mod2:
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:
mod2:
So is there a way to execute registered command server sided?
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,
})
Code: Select all
function execute_home
-- call command home
end
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
Code: Select all
function execute_home(player)
gohome(player:get_player_name())
end
-
- Member
- Posts: 1482
- Joined: Fri Apr 19, 2013 16:19
- GitHub: twoelk
- IRC: twoelk
- In-game: twoelk
- Location: northern Germany
Re: Quick question
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
Seems like client handles it in a different handler. It just displays it on client side.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?
- 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
Something like this should work:
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.
Code: Select all
local command_def = minetest.registered_chatcommands["home"]
command_def.func("playername", "other args")
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
Check out my stuff! | Donations greatly appreciated! PayPal
Who is online
Users browsing this forum: No registered users and 3 guests