Improved ping command

This commit is contained in:
WillConker 2024-07-03 16:58:08 +01:00
parent 44093cdd6f
commit df198fc9a2
1 changed files with 20 additions and 7 deletions

View File

@ -1,22 +1,35 @@
vl_client_interface.register_capability("ping", 1)
minetest.register_chatcommand("ping", {
params = "<player>:<message>",
params = "<player> <message>",
description = "pings a client from the server",
func = function(name, param)
minetest.debug("Pinging...")
local player_name, content = unpack(string.split(param, ":"))
local matched, _, player_name, content = param:find("^([^%s]+)%s+(.+)$")
if matched == nil then
return false, "Invalid arguments, see /help ping"
end
vl_client_interface.send_message(player_name, "ping", {type="request", content=content})
return true
return true, "Server: pinged client " .. player_name .. ": " .. content
end,
})
vl_client_interface.register_on_message("ping", function(player_name, message)
minetest.debug("pingmessage", dump(message))
minetest.debug(dump(message))
if message.type == "request" then
minetest.debug("Server received ping from " .. player_name .. " with content:", message.content)
if type(message.content) ~= "string" then
minetest.chat_send_player(player_name, "Server: got malformed ping (content) from " .. player_name)
return
end
minetest.debug("content", message.content)
minetest.chat_send_player(player_name, "Server: got ping from " .. player_name .. ": " .. message.content)
vl_client_interface.send_message(player_name, "ping", {type="response", content=message.content})
elseif message.type == "response" then
if type(message.content) ~= "string" then
minetest.chat_send_player(player_name, "Server: got malformed ping response (content) from " .. player_name)
return
end
minetest.chat_send_player(player_name, "Server got response to ping from " .. player_name .. ": " .. message.content)
else
minetest.debug("Server received client response from " .. player_name .. " to ping with content:", message.content)
minetest.chat_send_player(player_name, "Server: got malformed ping (message) from " .. player_name)
end
end)