Add restrictions for the craftable command block

This commit is contained in:
palige 2015-09-03 12:37:48 +02:00
parent dccf3f45dd
commit ab837d4775
2 changed files with 14 additions and 16 deletions

View File

@ -9,7 +9,7 @@ MoreMesecons is a mod for minetest wich add some mesecons items.
### New items ### New items
* `Adjustable Blinky plant` : Like a mesecons blinky plant, but... adjustable. Right-click to change the interval. * `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. * `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. * `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. * `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.

View File

@ -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 function initialize_data(meta)
local commands = meta:get_string("commands") local commands = meta:get_string("commands")
meta:set_string("formspec", meta:set_string("formspec",
"invsize[9,5;]" .. "invsize[9,5;]" ..
"textarea[0.5,0.5;8.5,4;commands;Commands;"..commands.."]" .. "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]") "button_exit[3.3,4.5;2,1;submit;Submit]")
local owner = meta:get_string("owner") local owner = meta:get_string("owner")
if owner == "" then if owner == "" then
@ -51,8 +52,8 @@ local function receive_fields(pos, formname, fields, sender)
end end
local function resolve_commands(commands, pos) local function resolve_commands(commands, pos)
local nearest, farthest = nil, nil local nearest = nil
local min_distance, max_distance = math.huge, -1 local min_distance = math.huge
local players = minetest.get_connected_players() local players = minetest.get_connected_players()
for index, player in pairs(players) do for index, player in pairs(players) do
local distance = vector.distance(pos, player:getpos()) local distance = vector.distance(pos, player:getpos())
@ -60,16 +61,9 @@ local function resolve_commands(commands, pos)
min_distance = distance min_distance = distance
nearest = player:get_player_name() nearest = player:get_player_name()
end end
if distance > max_distance then
max_distance = distance
farthest = player:get_player_name()
end
end end
local random = players[math.random(#players)]:get_player_name() new_commands = commands:gsub("@nearest", nearest)
commands = commands:gsub("@nearest", nearest) return new_commands, min_distance, new_commands ~= commands
commands = commands:gsub("@farthest", farthest)
commands = commands:gsub("@random", random)
return commands
end end
local function commandblock_action_on(pos, node) local function commandblock_action_on(pos, node)
@ -85,7 +79,11 @@ local function commandblock_action_on(pos, node)
return return
end 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 for _, command in pairs(commands:split("\n")) do
local pos = command:find(" ") local pos = command:find(" ")
local cmd, param = command, "" local cmd, param = command, ""
@ -102,7 +100,7 @@ local function commandblock_action_on(pos, node)
end end
end end
if not is_an_authorized_command and #accepted_commands ~= 0 then 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.") minetest.log("action", "Player "..owner.." tryed to execute an unauthorized command with a craftable command block.")
return return
end end