From ab837d4775203b9a7cce4ca6b1a383493ce1c6ac Mon Sep 17 00:00:00 2001 From: palige Date: Thu, 3 Sep 2015 12:37:48 +0200 Subject: [PATCH] Add restrictions for the craftable command block --- README.md | 2 +- moremesecons_commandblock/init.lua | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 74a72c1..e925ede 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ MoreMesecons is a mod for minetest wich add some mesecons items. ### New items * `Adjustable Blinky plant` : Like a mesecons blinky plant, but... adjustable. Right-click to change the interval. -* `Craftable Command Block` : A command block with just some commands accepted. The admin can change the accepted command (first line of the init.lua), default "say" and "tell". +* `Craftable Command Block` : A command block with just some commands accepted. The admin can change the accepted command (first line of the init.lua), default "tell". Only "@nearest" can be used in the commands, and the admin can change the maximum distance of "@nearest" (default 8 blocks). * `Dual Delayer` : If it receives a mesecons signal, port 1 turns on immediatly and port 2 turns on 0.4 seconds later. At the end of the signal, port 2 turns off immediatly and port 1 turns off 0.4 secondes later. For example, this is useful for double extenders. * `Player Killer` : This block kills the nearest player (with a maximal distance of 8 blocks by default) (if this player isn't its owner) when it receives a mesecons signal. * `Signal Changer` : If it receives a signal on its pin "F", it turns on. If ti receives a signal on its pin "O", it turns off. diff --git a/moremesecons_commandblock/init.lua b/moremesecons_commandblock/init.lua index de1fe1b..7d647c0 100644 --- a/moremesecons_commandblock/init.lua +++ b/moremesecons_commandblock/init.lua @@ -1,11 +1,12 @@ -local accepted_commands = {"say", "tell"} -- Authorized commands. Any to accept all. +local accepted_commands = {"tell"} -- Authorized commands. Any to accept all. +local NEAREST_MAX_DISTANCE = 8 local function initialize_data(meta) local commands = 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]" .. + "label[1,3.8;@nearest is replaced by the nearest player name ("..tostring(NEAREST_MAX_DISTANCE).." nodes max for the nearest distance)".."]" .. "button_exit[3.3,4.5;2,1;submit;Submit]") local owner = meta:get_string("owner") if owner == "" then @@ -51,8 +52,8 @@ local function receive_fields(pos, formname, fields, sender) end local function resolve_commands(commands, pos) - local nearest, farthest = nil, nil - local min_distance, max_distance = math.huge, -1 + local nearest = nil + local min_distance = math.huge local players = minetest.get_connected_players() for index, player in pairs(players) do local distance = vector.distance(pos, player:getpos()) @@ -60,16 +61,9 @@ local function resolve_commands(commands, pos) 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 + new_commands = commands:gsub("@nearest", nearest) + return new_commands, min_distance, new_commands ~= commands end local function commandblock_action_on(pos, node) @@ -85,7 +79,11 @@ local function commandblock_action_on(pos, node) return end - local commands = resolve_commands(meta:get_string("commands"), pos) + local commands, distance, nearest_in_commands = resolve_commands(meta:get_string("commands"), pos) + if distance > NEAREST_MAX_DISTANCE and nearest_in_commands then + minetest.chat_send_player(owner, "The nearest player is too far to use his name in the commands of a craftable command block.") + return + end for _, command in pairs(commands:split("\n")) do local pos = command:find(" ") local cmd, param = command, "" @@ -102,7 +100,7 @@ local function commandblock_action_on(pos, node) end end if not is_an_authorized_command and #accepted_commands ~= 0 then - minetest.chat_send_player(owner, "You can not execute this command with a craftable command block ! This event will be reported.") + minetest.chat_send_player(owner, "You can not execute the command "..cmd.." with a craftable command block ! This event will be reported.") minetest.log("action", "Player "..owner.." tryed to execute an unauthorized command with a craftable command block.") return end