Add substitutes
parent
a8bb37a750
commit
3ab7572f3c
64
init.lua
64
init.lua
|
@ -37,6 +37,46 @@ local get_commands = function(itemstack)
|
||||||
return cmds
|
return cmds
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--[[ Takes a command and substitutes placeholders like “@playername” with the actual values.
|
||||||
|
This operation might fail.
|
||||||
|
Returns the substituted command on success.
|
||||||
|
Returns false on failure. ]]
|
||||||
|
local substitute_placeholders = function(command, itemstack, player, pointed_thing)
|
||||||
|
local pos_pt, pos_pl
|
||||||
|
pos_pl = player:get_pos()
|
||||||
|
if pointed_thing.type == "node" then
|
||||||
|
pos_pt = pointed_thing.under
|
||||||
|
|
||||||
|
command = string.gsub(command, "@ptx", pos_pt.x)
|
||||||
|
command = string.gsub(command, "@pty", pos_pt.y)
|
||||||
|
command = string.gsub(command, "@ptz", pos_pt.z)
|
||||||
|
|
||||||
|
local node = minetest.get_node(pos_pt)
|
||||||
|
command = string.gsub(command, "@nodename", node.name)
|
||||||
|
command = string.gsub(command, "@param2", node.param2)
|
||||||
|
else
|
||||||
|
-- If one of the coordinate parameters is in the command, but
|
||||||
|
-- the pointed thing is not a node, we have to fail as the placeholders
|
||||||
|
-- are meaningless.
|
||||||
|
local coord = {"@ptx", "@pty", "@ptz", "@nodename", "@param2", "@light"}
|
||||||
|
for c=1, #coord do
|
||||||
|
if string.find(command, coord[c]) ~= nil then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
command = string.gsub(command, "@plx", pos_pl.x)
|
||||||
|
command = string.gsub(command, "@ply", pos_pl.y)
|
||||||
|
command = string.gsub(command, "@plz", pos_pl.z)
|
||||||
|
|
||||||
|
command = string.gsub(command, "@playername", player:get_player_name())
|
||||||
|
|
||||||
|
command = string.gsub(command, "@@", "@")
|
||||||
|
|
||||||
|
return command
|
||||||
|
end
|
||||||
|
|
||||||
local execute_command = function(itemstack, player, pointed_thing)
|
local execute_command = function(itemstack, player, pointed_thing)
|
||||||
local player_name = player:get_player_name()
|
local player_name = player:get_player_name()
|
||||||
local cmds = get_commands(itemstack)
|
local cmds = get_commands(itemstack)
|
||||||
|
@ -46,6 +86,7 @@ local execute_command = function(itemstack, player, pointed_thing)
|
||||||
local player_privs = minetest.get_player_privs(player_name)
|
local player_privs = minetest.get_player_privs(player_name)
|
||||||
for c=1, #cmds do
|
for c=1, #cmds do
|
||||||
local cmd = cmds[1]
|
local cmd = cmds[1]
|
||||||
|
-- Substitution successful?
|
||||||
-- Split command string into command name and parameters
|
-- Split command string into command name and parameters
|
||||||
local cmd_split = string.split(cmd, " ", false, 1)
|
local cmd_split = string.split(cmd, " ", false, 1)
|
||||||
local cmd_name
|
local cmd_name
|
||||||
|
@ -59,18 +100,25 @@ local execute_command = function(itemstack, player, pointed_thing)
|
||||||
if cmd_split[2] then
|
if cmd_split[2] then
|
||||||
cmd_params = cmd_split[2]
|
cmd_params = cmd_split[2]
|
||||||
end
|
end
|
||||||
|
cmd_params = substitute_placeholders(cmd_params, itemstack, player, pointed_thing)
|
||||||
local def = minetest.registered_chatcommands[cmd_name]
|
local def = minetest.registered_chatcommands[cmd_name]
|
||||||
|
-- Command exists? Placeholder substitution successful?
|
||||||
if def then
|
if def then
|
||||||
local required_privs = def.privs
|
if cmd_params then
|
||||||
for priv, _ in pairs(required_privs) do
|
local required_privs = def.privs
|
||||||
if player_privs[priv] ~= true then
|
for priv, _ in pairs(required_privs) do
|
||||||
minetest.chat_send_player(player_name, "Insufficient privileges for using command “"..cmd_name.."”! You need the “"..priv.."” privilege.")
|
if player_privs[priv] ~= true then
|
||||||
return
|
minetest.chat_send_player(player_name, "Insufficient privileges for using command “"..cmd_name.."”! You need the “"..priv.."” privilege.")
|
||||||
|
return
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
-- All tests survived!
|
||||||
|
-- Call the command
|
||||||
|
def.func(player_name, cmd_params)
|
||||||
|
else
|
||||||
|
-- TODO: Rewrite error msg
|
||||||
|
minetest.chat_send_player(player_name, "Placeholders in command “"..cmd_name.."” do not apply!")
|
||||||
end
|
end
|
||||||
-- All tests survived!
|
|
||||||
-- Call the command
|
|
||||||
def.func(player_name, cmd_params)
|
|
||||||
else
|
else
|
||||||
minetest.chat_send_player(player_name, "The command “"..cmd_name.."” does not exist!")
|
minetest.chat_send_player(player_name, "The command “"..cmd_name.."” does not exist!")
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue