From 76b9198717e48823caac9c6de7a7e3c486bf096c Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Thu, 9 Jan 2014 12:14:52 -0500 Subject: [PATCH 1/3] Revert "Remove command block until #136 is fixed" This reverts commit 3f76b77001512a7b71a8c81c181f78729c34e0d0. --- mesecons_commandblock/depends.txt | 1 + mesecons_commandblock/init.lua | 211 +++++++++++++++++++++++++++++- 2 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 mesecons_commandblock/depends.txt diff --git a/mesecons_commandblock/depends.txt b/mesecons_commandblock/depends.txt new file mode 100644 index 0000000..acaa924 --- /dev/null +++ b/mesecons_commandblock/depends.txt @@ -0,0 +1 @@ +mesecons diff --git a/mesecons_commandblock/init.lua b/mesecons_commandblock/init.lua index 2b03245..af7034d 100644 --- a/mesecons_commandblock/init.lua +++ b/mesecons_commandblock/init.lua @@ -1 +1,210 @@ --- removed until fixed +minetest.register_chatcommand("say", { + params = "", + description = "Say as the server", + privs = {server=true}, + func = function(name, param) + minetest.chat_send_all(name .. ": " .. param) + end +}) + +minetest.register_chatcommand("tell", { + params = " ", + description = "Say to privately", + func = function(name, param) + local found, _, target, message = param:find("^([^%s]+)%s+(.*)$") + if found == nil then + minetest.chat_send_player(name, "Invalid usage: " .. param) + return + end + if not minetest.get_player_by_name(target) then + minetest.chat_send_player(name, "Invalid target: " .. target) + end + minetest.chat_send_player(target, name .. " whispers: " .. message, false) + end +}) + +minetest.register_chatcommand("tellme", { + params = "", + description = "Say to yourself", + func = function(name, param) + minetest.chat_send_player(name, param, false) + end +}) + +minetest.register_chatcommand("hp", { + params = " ", + description = "Set health of to hitpoints", + privs = {ban=true}, + func = function(name, param) + local found, _, target, value = param:find("^([^%s]+)%s+(%d+)$") + if found == nil then + minetest.chat_send_player(name, "Invalid usage: " .. param) + return + end + local player = minetest.get_player_by_name(target) + if player then + player:set_hp(value) + else + minetest.chat_send_player(name, "Invalid target: " .. target) + end + end +}) + +local initialize_data = function(meta, player, command, param) + meta:set_string("formspec", + "invsize[9,6;]" .. + "field[1,1;7.5,1;player;Player;" .. player .. "]" .. + "button[1.3,2;2,1;nearest;Nearest]" .. + "button[3.3,2;2,1;farthest;Farthest]" .. + "button[5.3,2;2,1;random;Random]" .. + "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") + if owner == "" then + owner = "not owned" + else + owner = "owned by " .. owner + end + meta:set_string("infotext", "Command Block\n" .. + "(" .. owner .. ")\n" .. + "Command: /" .. command .. " " .. param) +end + +local construct = function(pos) + local meta = minetest.get_meta(pos) + + meta:set_string("player", "@nearest") + meta:set_string("command", "time") + meta:set_string("param", "7000") + + meta:set_string("owner", "") + + initialize_data(meta, "@nearest", "time", "7000") +end + +local after_place = function(pos, placer) + if placer then + local meta = minetest.get_meta(pos) + meta:set_string("owner", placer:get_player_name()) + initialize_data(meta, "@nearest", "time", "7000") + end +end + +local receive_fields = function(pos, formname, fields, sender) + if fields.quit then + return + end + local meta = minetest.get_meta(pos) + if fields.nearest then + initialize_data(meta, "@nearest", fields.command, fields.param) + elseif fields.farthest then + initialize_data(meta, "@farthest", fields.command, fields.param) + 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("param", fields.param) + + initialize_data(meta, fields.player, fields.command, fields.param) + end +end + +local resolve_player = function(name, pos) + local get_distance = function(pos1, pos2) + return math.sqrt((pos1.x - pos2.x) ^ 2 + (pos1.y - pos2.y) ^ 2 + (pos1.z - pos2.z) ^ 2) + end + + if name == "@nearest" then + 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 + min_distance = distance + name = player:get_player_name() + 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 + max_distance = distance + name = player:get_player_name() + end + end + elseif name == "@random" then + local players = minetest.get_connected_players() + local player = players[math.random(#players)] + name = player:get_player_name() + end + return name +end + +local commandblock_action_on = function(pos, node) + if node.name ~= "mesecons_commandblock:commandblock_off" then + return + end + + minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_on"}) + + local meta = minetest.get_meta(pos) + local command = minetest.chatcommands[meta:get_string("command")] + if command == nil then + return + end + local owner = meta:get_string("owner") + if owner == "" then + return + end + local has_privs, missing_privs = minetest.check_player_privs(owner, command.privs) + 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, ", ")..")") + return + end + local player = resolve_player(meta:get_string("player"), pos) + command.func(player, meta:get_string("param")) +end + +local commandblock_action_off = function(pos, node) + if node.name == "mesecons_commandblock:commandblock_on" then + minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_off"}) + end +end + +minetest.register_node("mesecons_commandblock:commandblock_off", { + description = "Command Block", + tiles = {"jeija_commandblock_off.png"}, + inventory_image = minetest.inventorycube("jeija_commandblock_off.png"), + groups = {cracky=2, mesecon_effector_off=1}, + on_construct = construct, + after_place_node = after_place, + on_receive_fields = receive_fields, + can_dig = function(pos,player) + local owner = minetest.get_meta(pos):get_string("owner") + return owner == "" or owner == player:get_player_name() + end, + sounds = default.node_sound_stone_defaults(), + mesecons = {effector = { + action_on = commandblock_action_on + }} +}) + +minetest.register_node("mesecons_commandblock:commandblock_on", { + tiles = {"jeija_commandblock_on.png"}, + groups = {cracky=2, mesecon_effector_on=1, not_in_creative_inventory=1}, + light_source = 10, + drop = "mesecons_commandblock:commandblock_off", + on_construct = construct, + after_place_node = after_place, + on_receive_fields = receive_fields, + can_dig = function(pos,player) + local owner = minetest.get_meta(pos):get_string("owner") + return owner == "" or owner == player:get_player_name() + end, + sounds = default.node_sound_stone_defaults(), + mesecons = {effector = { + action_off = commandblock_action_off + }} +}) From cd30aed80787cb56c888f0c5ad40482c4921f31b Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Thu, 9 Jan 2014 12:40:38 -0500 Subject: [PATCH 2/3] Fix #136 by always running commands as the placer --- mesecons_commandblock/init.lua | 125 ++++++++++++++------------------- 1 file changed, 51 insertions(+), 74 deletions(-) diff --git a/mesecons_commandblock/init.lua b/mesecons_commandblock/init.lua index af7034d..1b2df85 100644 --- a/mesecons_commandblock/init.lua +++ b/mesecons_commandblock/init.lua @@ -23,14 +23,6 @@ minetest.register_chatcommand("tell", { end }) -minetest.register_chatcommand("tellme", { - params = "", - description = "Say to yourself", - func = function(name, param) - minetest.chat_send_player(name, param, false) - end -}) - minetest.register_chatcommand("hp", { params = " ", description = "Set health of to hitpoints", @@ -50,16 +42,15 @@ minetest.register_chatcommand("hp", { 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", - "invsize[9,6;]" .. - "field[1,1;7.5,1;player;Player;" .. player .. "]" .. - "button[1.3,2;2,1;nearest;Nearest]" .. - "button[3.3,2;2,1;farthest;Farthest]" .. - "button[5.3,2;2,1;random;Random]" .. - "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]") + "invsize[9,4;]" .. + "field[1,1;2,1;command;Command;" .. command .. "]" .. + "field[3,1;5.5,1;param;Parameter;" .. param .. "]" .. + "label[3,1.5;@nearest, @farthest, and @random are replaced by the respective player names]" .. + "button_exit[3.3,3;2,1;submit;Submit]") local owner = meta:get_string("owner") if owner == "" then owner = "not owned" @@ -71,78 +62,63 @@ local initialize_data = function(meta, player, command, param) "Command: /" .. command .. " " .. param) end -local construct = function(pos) +local function construct(pos) local meta = minetest.get_meta(pos) - meta:set_string("player", "@nearest") meta:set_string("command", "time") meta:set_string("param", "7000") meta:set_string("owner", "") - initialize_data(meta, "@nearest", "time", "7000") + initialize_data(meta) end -local after_place = function(pos, placer) +local function after_place(pos, placer) if placer then local meta = minetest.get_meta(pos) meta:set_string("owner", placer:get_player_name()) - initialize_data(meta, "@nearest", "time", "7000") + initialize_data(meta) end end -local receive_fields = function(pos, formname, fields, sender) +local function receive_fields(pos, formname, fields, sender) if fields.quit then return end local meta = minetest.get_meta(pos) - if fields.nearest then - initialize_data(meta, "@nearest", fields.command, fields.param) - elseif fields.farthest then - initialize_data(meta, "@farthest", fields.command, fields.param) - 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("param", fields.param) - - initialize_data(meta, fields.player, fields.command, fields.param) + local owner = meta:get_string("owner") + if owner ~= "" and sender:get_player_name() ~= owner then + return end + meta:set_string("command", fields.command) + meta:set_string("param", fields.param) + + initialize_data(meta) end -local resolve_player = function(name, pos) - local get_distance = function(pos1, pos2) - return math.sqrt((pos1.x - pos2.x) ^ 2 + (pos1.y - pos2.y) ^ 2 + (pos1.z - pos2.z) ^ 2) - end - - if name == "@nearest" then - 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 - min_distance = distance - name = player:get_player_name() - end +local function resolve_param(param, pos) + local nearest, farthest = nil, nil + local min_distance, max_distance = math.huge, -1 + local players = minetest.get_connected_players() + for index, player in pairs(players) do + local distance = vector.distance(pos, player:getpos()) + if distance < min_distance then + min_distance = distance + nearest = player:get_player_name() 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 - max_distance = distance - name = player:get_player_name() - end + if distance > max_distance then + max_distance = distance + farthest = player:get_player_name() end - elseif name == "@random" then - local players = minetest.get_connected_players() - local player = players[math.random(#players)] - name = player:get_player_name() end - return name + local random = players[math.random(#players)]:get_player_name() + param = param:gsub("@nearest", nearest) + param = param:gsub("@farthest", farthest) + param = param:gsub("@random", random) + return param end -local commandblock_action_on = function(pos, node) +local function commandblock_action_on(pos, node) if node.name ~= "mesecons_commandblock:commandblock_off" then return end @@ -160,19 +136,26 @@ local commandblock_action_on = function(pos, node) end local has_privs, missing_privs = minetest.check_player_privs(owner, command.privs) 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 end - local player = resolve_player(meta:get_string("player"), pos) - command.func(player, meta:get_string("param")) + command.func(owner, resolve_param(meta:get_string("param"), pos)) end -local commandblock_action_off = function(pos, node) +local function commandblock_action_off(pos, node) if node.name == "mesecons_commandblock:commandblock_on" then minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_off"}) 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", { description = "Command Block", tiles = {"jeija_commandblock_off.png"}, @@ -181,10 +164,7 @@ minetest.register_node("mesecons_commandblock:commandblock_off", { on_construct = construct, after_place_node = after_place, on_receive_fields = receive_fields, - can_dig = function(pos,player) - local owner = minetest.get_meta(pos):get_string("owner") - return owner == "" or owner == player:get_player_name() - end, + can_dig = can_dig, sounds = default.node_sound_stone_defaults(), mesecons = {effector = { action_on = commandblock_action_on @@ -199,10 +179,7 @@ minetest.register_node("mesecons_commandblock:commandblock_on", { on_construct = construct, after_place_node = after_place, on_receive_fields = receive_fields, - can_dig = function(pos,player) - local owner = minetest.get_meta(pos):get_string("owner") - return owner == "" or owner == player:get_player_name() - end, + can_dig = can_dig, sounds = default.node_sound_stone_defaults(), mesecons = {effector = { action_off = commandblock_action_off From eea4dbbea8726434e9b607e91875bbb96bcc7eaf Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Sat, 11 Jan 2014 12:15:01 -0500 Subject: [PATCH 3/3] Use a textarea for the commandblock to accept multiple commands --- mesecons_commandblock/init.lua | 64 +++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/mesecons_commandblock/init.lua b/mesecons_commandblock/init.lua index 1b2df85..d163d5c 100644 --- a/mesecons_commandblock/init.lua +++ b/mesecons_commandblock/init.lua @@ -43,14 +43,12 @@ minetest.register_chatcommand("hp", { }) local function initialize_data(meta) - local command = meta:get_string("command") - local param = meta:get_string("param") + local commands = meta:get_string("commands") meta:set_string("formspec", - "invsize[9,4;]" .. - "field[1,1;2,1;command;Command;" .. command .. "]" .. - "field[3,1;5.5,1;param;Parameter;" .. param .. "]" .. - "label[3,1.5;@nearest, @farthest, and @random are replaced by the respective player names]" .. - "button_exit[3.3,3;2,1;submit;Submit]") + "invsize[9,5;]" .. + "textarea[0.5,0.5;8.5,4;commands;Commands;"..commands.."]" .. + "label[1,3.8;@nearest, @farthest, and @random are replaced by the respective player names]" .. + "button_exit[3.3,4.5;2,1;submit;Submit]") local owner = meta:get_string("owner") if owner == "" then owner = "not owned" @@ -59,14 +57,13 @@ local function initialize_data(meta) end meta:set_string("infotext", "Command Block\n" .. "(" .. owner .. ")\n" .. - "Command: /" .. command .. " " .. param) + "Commands: "..commands) end local function construct(pos) local meta = minetest.get_meta(pos) - meta:set_string("command", "time") - meta:set_string("param", "7000") + meta:set_string("commands", "tell @nearest Commandblock unconfigured") meta:set_string("owner", "") @@ -90,13 +87,12 @@ local function receive_fields(pos, formname, fields, sender) if owner ~= "" and sender:get_player_name() ~= owner then return end - meta:set_string("command", fields.command) - meta:set_string("param", fields.param) + meta:set_string("commands", fields.commands) initialize_data(meta) end -local function resolve_param(param, pos) +local function resolve_commands(commands, pos) local nearest, farthest = nil, nil local min_distance, max_distance = math.huge, -1 local players = minetest.get_connected_players() @@ -112,10 +108,10 @@ local function resolve_param(param, pos) end end local random = players[math.random(#players)]:get_player_name() - param = param:gsub("@nearest", nearest) - param = param:gsub("@farthest", farthest) - param = param:gsub("@random", random) - return param + commands = commands:gsub("@nearest", nearest) + commands = commands:gsub("@farthest", farthest) + commands = commands:gsub("@random", random) + return commands end local function commandblock_action_on(pos, node) @@ -126,22 +122,34 @@ local function commandblock_action_on(pos, node) minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_on"}) local meta = minetest.get_meta(pos) - local command = minetest.chatcommands[meta:get_string("command")] - if command == nil then - return - end local owner = meta:get_string("owner") if owner == "" then return end - local has_privs, missing_privs = minetest.check_player_privs(owner, command.privs) - 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, ", ")..")") - return + + local commands = resolve_commands(meta:get_string("commands"), pos) + for _, command in pairs(commands:split("\n")) do + local pos = command:find(" ") + local cmd, param = command, "" + if pos then + cmd = command:sub(1, pos - 1) + param = command:sub(pos + 1) + end + local cmddef = minetest.chatcommands[cmd] + if not cmddef then + minetest.chat_send_player(owner, "The command "..cmd.." does not exist") + return + end + local has_privs, missing_privs = minetest.check_player_privs(owner, cmddef.privs) + if not has_privs then + minetest.chat_send_player(owner, "You don't have permission " + .."to run "..cmd + .." (missing privileges: " + ..table.concat(missing_privs, ", ")..")") + return + end + cmddef.func(owner, param) end - command.func(owner, resolve_param(meta:get_string("param"), pos)) end local function commandblock_action_off(pos, node)