From 0c7471e8d5e2723a49f9eb7312629b1fd424657d Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 7 Jan 2017 02:52:27 +0100 Subject: [PATCH] Add basic command block --- mods/redstone/mesecons_commandblock.rar | Bin 1866 -> 0 bytes .../mesecons_commandblock/depends.txt | 3 + mods/redstone/mesecons_commandblock/init.lua | 207 ++++++++++++++++++ 3 files changed, 210 insertions(+) delete mode 100644 mods/redstone/mesecons_commandblock.rar create mode 100644 mods/redstone/mesecons_commandblock/depends.txt create mode 100644 mods/redstone/mesecons_commandblock/init.lua diff --git a/mods/redstone/mesecons_commandblock.rar b/mods/redstone/mesecons_commandblock.rar deleted file mode 100644 index 34a4c1057698b811d9c677860396910798b4b090..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1866 zcmaKtdpHvcAIC=`_ggNZ3}@~na!w{oE-|$|YjP+wRavC80z5R&zK{p=kWCPCmu$3MF2m}D4LSsWiqH(c-A<%N%8y)hSKROg2 zibKa*65{aSaQhYeFZ#Vj z93AiRo~^m4&z>G5c(2V?3>a&4Wms&!L!rs%1nw1`6HXTLcAcZk9n)T;$7x9Nr# zFdR(J*u2tWq{@BCi;%z4mjF8I4=K_-dh9Iy=#y0own(&9iMg2>7J9)>A;QqP?i@7Q zBd46dqfAcLm^c+AvQ?!y?OZwF)Bv-a<}%mPwKEH^d))%f&o%P=@BI-vTI|P0t-u|X zY#mbP%E*7rr&_I#jccwJr(sZpUbs3jSCLmO-Wy6*XuLOr-a*=EMWE@igLMUsj{M7V zgCHU1;l&q`s|h=~gys7CAH1$sM&EUkh`I;p8}t7V@4E5qmu;V*^^2>cpDqwj85uFm zVkY#4%$q+Od|5Z@F)s05b)rnrG^0spLax7Sh~Hq77fo5?2|R<%NGk) z(R)Dqc^qpS9WUqvb4Owy%L})ma*;!dk>K%e3vqlY4I5d*Ag_q&yh2FbfGXAkG-kKs zAsnH8SK%D2)Y3AGe*0+r=#1j3;`?3YaryE@YHhLgrhdku;oGubo9q~|XZ%l2?n_Ku zaVv&GB9?0$l@6-39NfBec801eNe%d=1-TqW-0D@fwA>Jtp3;I%QN|8YkB8rt$2SW} z)J^9;Z86r9-vp=p@cC-|-8OZ(*ZUXG$9>eFxSq4v)JpQ z?)?vqd6FQBg3y$;hi4@OHf5Z`wf&GW>EYwEBeG17Nw#y6h?#hmY!aa+P=dW4u^P%} zN7oL$W#~^a24bdcQp#L2w5ru+f&?F3BQ&5cbk0HrF+I89Zk)>7o!KeNuoT-F9|T6 zvb^1bQl&wGRYEDv%I2W$a}@=<>#0s|83X6xxXq*k?|>hh;zO9o0^UmVIq)ze7kRKPOs1}+s^IIO z3!ZsLSLQ3+b1)#RI*YGHS*N0d^_uV7|5|YGI=q%Do-k(*4pZka!y6pza98_?FYkQX zyV1}bu>r|65fCvE9rYVIDm-}M&kAYQJ-R;AunD0-?&2ugqUM%RrsH!}ia?yt%J7J! z5jlRgCl(Bh&WNn-Xm#qsd-*z@+W+Q+sYQjy|Y7-1SJ+o(pbLD-_x4+&(%p4O1%6OvTvDgj)1|Q!q6V|53uIdW-%)wl4^?fg)i9p8c4#C``ejCKInj#oYt(dF}JSv z4qCNJFnOxHpHiA`&=Ao`|+@?v>>PQHt$97W`qpAKwpO9kfydC;|VE`8pgD0gh6D{{Sc$fNlT) diff --git a/mods/redstone/mesecons_commandblock/depends.txt b/mods/redstone/mesecons_commandblock/depends.txt new file mode 100644 index 00000000..99bc4787 --- /dev/null +++ b/mods/redstone/mesecons_commandblock/depends.txt @@ -0,0 +1,3 @@ +mesecons +doc? +doc_items? \ No newline at end of file diff --git a/mods/redstone/mesecons_commandblock/init.lua b/mods/redstone/mesecons_commandblock/init.lua new file mode 100644 index 00000000..5819946a --- /dev/null +++ b/mods/redstone/mesecons_commandblock/init.lua @@ -0,0 +1,207 @@ +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("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 function initialize_data(meta) + local commands = minetest.formspec_escape(meta:get_string("commands")) + meta:set_string("formspec", + "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" + else + owner = "owned by " .. owner + end + meta:set_string("infotext", "Command Block\n" .. + "(" .. owner .. ")\n" .. + "Commands: "..commands) +end + +local function construct(pos) + local meta = minetest.get_meta(pos) + + meta:set_string("commands", "tell @nearest Commandblock unconfigured") + + meta:set_string("owner", "") + + initialize_data(meta) +end + +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) + end +end + +local function receive_fields(pos, formname, fields, sender) + if not fields.submit then + return + end + local meta = minetest.get_meta(pos) + local owner = meta:get_string("owner") + if owner ~= "" and sender:get_player_name() ~= owner then + return + end + meta:set_string("commands", fields.commands) + + initialize_data(meta) +end + +local function resolve_commands(commands, pos) + local players = minetest.get_connected_players() + + -- No players online: remove all commands containing + -- @nearest, @farthest and @random + if #players == 0 then + commands = commands:gsub("[^\r\n]+", function (line) + if line:find("@nearest") then return "" end + if line:find("@farthest") then return "" end + if line:find("@random") then return "" end + return line + end) + return commands + end + + local nearest, farthest = nil, nil + local min_distance, max_distance = math.huge, -1 + 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 + if distance > max_distance then + max_distance = distance + farthest = player:get_player_name() + end + end + local random = players[math.random(#players)]:get_player_name() + 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) + 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 owner = meta:get_string("owner") + if owner == "" then + return + end + + 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 +end + +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"}, + 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 = can_dig, + 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 = {mesecon_effector_on=1, not_in_creative_inventory=1}, + drop = "mesecons_commandblock:commandblock_off", + on_construct = construct, + after_place_node = after_place, + on_receive_fields = receive_fields, + can_dig = can_dig, + sounds = default.node_sound_stone_defaults(), + mesecons = {effector = { + action_off = commandblock_action_off + }} +})