From 2d16152188bea4b71051d11d65eb91eed89bf1c3 Mon Sep 17 00:00:00 2001 From: Anthony Zhang Date: Sun, 28 Oct 2012 16:31:03 -0400 Subject: [PATCH] Add command block: * not obtainable legitimately in Survival mode * found in Creative inventory or using /giveme mesecons_commandblock:commandblock_off * glows slightly when turned on * runs a command in the context of the targeted player * player can be targeted by name, or using the special forms (@nearest, @farthest, @random) * when powered, runs its command with its parameters * right click to bring up the configuration screen * diggable and modifiable only by its owner, the person who placed it --- mesecons_commandblock/depends.txt | 1 + mesecons_commandblock/init.lua | 157 ++++++++++++++++++ .../textures/jeija_commandblock_off.png | Bin 0 -> 282 bytes .../textures/jeija_commandblock_on.png | Bin 0 -> 278 bytes 4 files changed, 158 insertions(+) create mode 100644 mesecons_commandblock/depends.txt create mode 100644 mesecons_commandblock/init.lua create mode 100644 mesecons_textures/textures/jeija_commandblock_off.png create mode 100644 mesecons_textures/textures/jeija_commandblock_on.png 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 new file mode 100644 index 0000000..345ca7c --- /dev/null +++ b/mesecons_commandblock/init.lua @@ -0,0 +1,157 @@ +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.env: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.env: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) + local meta = minetest.env: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 + +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, mesecon=2}, + on_construct = construct, + after_place_node = after_place, + on_receive_fields = receive_fields, + can_dig = function(pos,player) + local owner = minetest.env:get_meta(pos):get_string("owner") + return owner == "" or owner == player:get_player_name() + end, +}) + +minetest.register_node("mesecons_commandblock:commandblock_on", { + tiles = {"jeija_commandblock_on.png"}, + groups = {cracky=2, mesecon_effector_on=1, mesecon=2, 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.env:get_meta(pos):get_string("owner") + return owner == "" or owner == player:get_player_name() + end, +}) + +mesecon:register_effector("mesecons_commandblock:commandblock_on", "mesecons_commandblock:commandblock_off") + +local swap_node = function(pos, name) + local node = minetest.env:get_node(pos) + local data = minetest.env:get_meta(pos):to_table() + node.name = name + minetest.env:add_node(pos, node) + minetest.env:get_meta(pos):from_table(data) +end + +mesecon:register_on_signal_on(function(pos, node) + if node.name ~= "mesecons_commandblock:commandblock_off" then + return + end + + swap_node(pos, "mesecons_commandblock:commandblock_on") + + local meta = minetest.env: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) + +mesecon:register_on_signal_off(function(pos, node) + if node.name == "mesecons_commandblock:commandblock_on" then + swap_node(pos, "mesecons_commandblock:commandblock_off") + end +end) \ No newline at end of file diff --git a/mesecons_textures/textures/jeija_commandblock_off.png b/mesecons_textures/textures/jeija_commandblock_off.png new file mode 100644 index 0000000000000000000000000000000000000000..c05b616a9f69c7bcc162820a8510ce2409bb00d1 GIT binary patch literal 282 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|oCO|{#S9GG!XV7ZFl&wkQ1FbW zi(`n!`QD&|yoU^UY!-R^Tu`&iNj;HOPC@KJzr+IO4Ghi(%&ClF8v1S>BB7lXEU|OV zT7L=7O}4g+-&3_!TF30ZJMV$@iw(INzDq7REq$GpRpIEehNDJHE;AonkzPJ4Ice?M z)yE{B3Yb_kRc!g5bTF@8;o79CO>aN?)a;IH>-6mW(^a{}#q;9N^(*@srj*pBFL$3_ zCUM&8)q$N)4Q8&{F<-}RSx>_5tb6bO+iq8R0SCh+ d&&AsB?GLhdgtPkYXa)L=~c-*cp@m(ml zs5s~N^X~BT#mz^b{a(TN!`H-(@q-<6N#)~R3m80dxFY7XZLvw{*;TgOQtjHUU0J<> ze2$xy8y+u=DX3r5S?U%0=G#5t_uHl?CIu<&Pm=T1(6st*`c<&OYwrD