Fix #136 by always running commands as the placer

This commit is contained in:
ShadowNinja 2014-01-09 12:40:38 -05:00
parent 76b9198717
commit cd30aed807

View File

@ -23,14 +23,6 @@ minetest.register_chatcommand("tell", {
end end
}) })
minetest.register_chatcommand("tellme", {
params = "<text>",
description = "Say <text> to yourself",
func = function(name, param)
minetest.chat_send_player(name, param, false)
end
})
minetest.register_chatcommand("hp", { minetest.register_chatcommand("hp", {
params = "<name> <value>", params = "<name> <value>",
description = "Set health of <name> to <value> hitpoints", description = "Set health of <name> to <value> hitpoints",
@ -50,16 +42,15 @@ minetest.register_chatcommand("hp", {
end end
}) })
local initialize_data = function(meta, player, command, param) local function initialize_data(meta)
local command = meta:get_string("command")
local param = meta:get_string("param")
meta:set_string("formspec", meta:set_string("formspec",
"invsize[9,6;]" .. "invsize[9,4;]" ..
"field[1,1;7.5,1;player;Player;" .. player .. "]" .. "field[1,1;2,1;command;Command;" .. command .. "]" ..
"button[1.3,2;2,1;nearest;Nearest]" .. "field[3,1;5.5,1;param;Parameter;" .. param .. "]" ..
"button[3.3,2;2,1;farthest;Farthest]" .. "label[3,1.5;@nearest, @farthest, and @random are replaced by the respective player names]" ..
"button[5.3,2;2,1;random;Random]" .. "button_exit[3.3,3;2,1;submit;Submit]")
"field[1,4;2,1;command;Command;" .. command .. "]" ..
"field[3,4;5.5,1;param;Parameter;" .. param .. "]" ..
"button_exit[3.3,5;2,1;submit;Submit]")
local owner = meta:get_string("owner") local owner = meta:get_string("owner")
if owner == "" then if owner == "" then
owner = "not owned" owner = "not owned"
@ -71,78 +62,63 @@ local initialize_data = function(meta, player, command, param)
"Command: /" .. command .. " " .. param) "Command: /" .. command .. " " .. param)
end end
local construct = function(pos) local function construct(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("player", "@nearest")
meta:set_string("command", "time") meta:set_string("command", "time")
meta:set_string("param", "7000") meta:set_string("param", "7000")
meta:set_string("owner", "") meta:set_string("owner", "")
initialize_data(meta, "@nearest", "time", "7000") initialize_data(meta)
end end
local after_place = function(pos, placer) local function after_place(pos, placer)
if placer then if placer then
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name()) meta:set_string("owner", placer:get_player_name())
initialize_data(meta, "@nearest", "time", "7000") initialize_data(meta)
end end
end end
local receive_fields = function(pos, formname, fields, sender) local function receive_fields(pos, formname, fields, sender)
if fields.quit then if fields.quit then
return return
end end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if fields.nearest then local owner = meta:get_string("owner")
initialize_data(meta, "@nearest", fields.command, fields.param) if owner ~= "" and sender:get_player_name() ~= owner then
elseif fields.farthest then return
initialize_data(meta, "@farthest", fields.command, fields.param) end
elseif fields.random then
initialize_data(meta, "@random", fields.command, fields.param)
else --fields.submit or pressed enter
meta:set_string("player", fields.player)
meta:set_string("command", fields.command) meta:set_string("command", fields.command)
meta:set_string("param", fields.param) meta:set_string("param", fields.param)
initialize_data(meta, fields.player, fields.command, fields.param) initialize_data(meta)
end
end end
local resolve_player = function(name, pos) local function resolve_param(param, pos)
local get_distance = function(pos1, pos2) local nearest, farthest = nil, nil
return math.sqrt((pos1.x - pos2.x) ^ 2 + (pos1.y - pos2.y) ^ 2 + (pos1.z - pos2.z) ^ 2) local min_distance, max_distance = math.huge, -1
end local players = minetest.get_connected_players()
for index, player in pairs(players) do
if name == "@nearest" then local distance = vector.distance(pos, player:getpos())
local min_distance = math.huge
for index, player in ipairs(minetest.get_connected_players()) do
local distance = get_distance(pos, player:getpos())
if distance < min_distance then if distance < min_distance then
min_distance = distance min_distance = distance
name = player:get_player_name() nearest = player:get_player_name()
end end
end
elseif name == "@farthest" then
local max_distance = -1
for index, player in ipairs(minetest.get_connected_players()) do
local distance = get_distance(pos, player:getpos())
if distance > max_distance then if distance > max_distance then
max_distance = distance max_distance = distance
name = player:get_player_name() farthest = player:get_player_name()
end end
end end
elseif name == "@random" then local random = players[math.random(#players)]:get_player_name()
local players = minetest.get_connected_players() param = param:gsub("@nearest", nearest)
local player = players[math.random(#players)] param = param:gsub("@farthest", farthest)
name = player:get_player_name() param = param:gsub("@random", random)
end return param
return name
end end
local commandblock_action_on = function(pos, node) local function commandblock_action_on(pos, node)
if node.name ~= "mesecons_commandblock:commandblock_off" then if node.name ~= "mesecons_commandblock:commandblock_off" then
return return
end end
@ -160,19 +136,26 @@ local commandblock_action_on = function(pos, node)
end end
local has_privs, missing_privs = minetest.check_player_privs(owner, command.privs) local has_privs, missing_privs = minetest.check_player_privs(owner, command.privs)
if not has_privs then if not has_privs then
minetest.chat_send_player(owner, "You don't have permission to run this command (missing privileges: "..table.concat(missing_privs, ", ")..")") minetest.chat_send_player(owner, "You don't have permission "
.."to run this command (missing privileges: "
..table.concat(missing_privs, ", ")..")")
return return
end end
local player = resolve_player(meta:get_string("player"), pos) command.func(owner, resolve_param(meta:get_string("param"), pos))
command.func(player, meta:get_string("param"))
end end
local commandblock_action_off = function(pos, node) local function commandblock_action_off(pos, node)
if node.name == "mesecons_commandblock:commandblock_on" then if node.name == "mesecons_commandblock:commandblock_on" then
minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_off"}) minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_off"})
end end
end end
local function can_dig(pos, player)
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
return owner == "" or owner == player:get_player_name()
end
minetest.register_node("mesecons_commandblock:commandblock_off", { minetest.register_node("mesecons_commandblock:commandblock_off", {
description = "Command Block", description = "Command Block",
tiles = {"jeija_commandblock_off.png"}, tiles = {"jeija_commandblock_off.png"},
@ -181,10 +164,7 @@ minetest.register_node("mesecons_commandblock:commandblock_off", {
on_construct = construct, on_construct = construct,
after_place_node = after_place, after_place_node = after_place,
on_receive_fields = receive_fields, on_receive_fields = receive_fields,
can_dig = function(pos,player) can_dig = can_dig,
local owner = minetest.get_meta(pos):get_string("owner")
return owner == "" or owner == player:get_player_name()
end,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
mesecons = {effector = { mesecons = {effector = {
action_on = commandblock_action_on action_on = commandblock_action_on
@ -199,10 +179,7 @@ minetest.register_node("mesecons_commandblock:commandblock_on", {
on_construct = construct, on_construct = construct,
after_place_node = after_place, after_place_node = after_place,
on_receive_fields = receive_fields, on_receive_fields = receive_fields,
can_dig = function(pos,player) can_dig = can_dig,
local owner = minetest.get_meta(pos):get_string("owner")
return owner == "" or owner == player:get_player_name()
end,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
mesecons = {effector = { mesecons = {effector = {
action_off = commandblock_action_off action_off = commandblock_action_off