From dd380963ce60afa7c9512c0422f5d917ccc4b7bc Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Wed, 16 May 2018 17:31:01 +0200 Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=9C@@=E2=80=9D=20placeholder=20to?= =?UTF-8?q?=20cmd=20blocks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mods/ITEMS/REDSTONE/mesecons_commandblock/init.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mods/ITEMS/REDSTONE/mesecons_commandblock/init.lua b/mods/ITEMS/REDSTONE/mesecons_commandblock/init.lua index b41f20d908..6e80d8e2ce 100644 --- a/mods/ITEMS/REDSTONE/mesecons_commandblock/init.lua +++ b/mods/ITEMS/REDSTONE/mesecons_commandblock/init.lua @@ -19,14 +19,19 @@ local function resolve_commands(commands, pos) local meta = minetest.get_meta(pos) local commander = meta:get_string("commander") + -- A non-printable character used while replacing “@@”. + local SUBSTITUTE_CHARACTER = '\26' -- ASCII SUB + -- No players online: remove all commands containing - -- “@” placeholders + -- problematic placeholders. if #players == 0 then commands = commands:gsub("[^\r\n]+", function (line) + line = line:gsub("@@", SUBSTITUTE_CHARACTER) if line:find("@nearest") then return "" end if line:find("@farthest") then return "" end if line:find("@random") then return "" end - if line:find("@commander") then return commander end + line = line:gsub("@commander", commander) + line = line:gsub(SUBSTITUTE_CHARACTER, "@") return line end) return commands @@ -46,10 +51,12 @@ local function resolve_commands(commands, pos) end end local random = players[math.random(#players)]:get_player_name() + commands = commands:gsub("@@", SUBSTITUTE_CHARACTER) commands = commands:gsub("@nearest", nearest) commands = commands:gsub("@farthest", farthest) commands = commands:gsub("@random", random) commands = commands:gsub("@commander", commander) + commands = commands:gsub(SUBSTITUTE_CHARACTER, "@") return commands end @@ -185,6 +192,7 @@ Command blocks support placeholders, insert one of these placerholders and they • “@nearest”: nearest player from the command block • “@farthest” farthest player from the command block • “@random”: random player currently in the world +• “@@”: literal “@” sign Example 1: time 12000