From 5914eab20b44e3be594ce12be6147f8071863e6a Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 20 Apr 2024 13:45:00 +0200 Subject: [PATCH] Abort a delayed operation if positions change fixes #236 --- worldedit_commands/init.lua | 50 +++++++++++++++---- worldedit_commands/locale/template.txt | 1 + .../locale/worldedit_commands.de.tr | 1 + 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index 60aa57f..fc218f0 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -20,6 +20,21 @@ end worldedit.registered_commands = {} +local function copy_state(which, name) + if which == 0 then + return {} + elseif which == 1 then + return { + worldedit.pos1[name] and vector.copy(worldedit.pos1[name]) + } + else + return { + worldedit.pos1[name] and vector.copy(worldedit.pos1[name]), + worldedit.pos2[name] and vector.copy(worldedit.pos2[name]) + } + end +end + local function chatcommand_handler(cmd_name, name, param) local def = assert(worldedit.registered_commands[cmd_name]) @@ -44,21 +59,34 @@ local function chatcommand_handler(cmd_name, name, param) return end - if def.nodes_needed then - local count = def.nodes_needed(name, unpack(parsed)) - safe_region(name, count, function() - local _, msg = def.func(name, unpack(parsed)) - if msg then - minetest.chat_send_player(name, msg) - end - end) - else - -- no "safe region" check + local run = function() local _, msg = def.func(name, unpack(parsed)) if msg then minetest.chat_send_player(name, msg) end end + + if not def.nodes_needed then + -- no safe region check + run() + return + end + + local count = def.nodes_needed(name, unpack(parsed)) + local old_state = copy_state(def.require_pos, name) + safe_region(name, count, function() + local state = copy_state(def.require_pos, name) + local ok = true + for i, v in ipairs(state) do + ok = ok and ( (v == nil and old_state[i] == nil) or vector.equals(v, old_state[i]) ) + end + if not ok then + worldedit.player_notify(name, S("ERROR: the operation was cancelled because the region has changed.")) + return + end + + run() + end) end -- Registers a chatcommand for WorldEdit @@ -66,7 +94,7 @@ end -- def = { -- privs = {}, -- Privileges needed -- params = "", -- Human readable parameter list (optional) --- -- setting params = "" will automatically provide a parse() if not given +-- -- if params = "" then a parse() implementation will automatically be provided -- description = "", -- Description -- require_pos = 0, -- Number of positions required to be set (optional) -- parse = function(param) diff --git a/worldedit_commands/locale/template.txt b/worldedit_commands/locale/template.txt index bdc917a..16889f0 100644 --- a/worldedit_commands/locale/template.txt +++ b/worldedit_commands/locale/template.txt @@ -19,6 +19,7 @@ Can use WorldEdit commands= no region selected= no position 1 selected= invalid usage= +ERROR: the operation was cancelled because the region has changed.= Could not open file "@1"= Invalid file format!= Schematic was created with a newer version of WorldEdit.= diff --git a/worldedit_commands/locale/worldedit_commands.de.tr b/worldedit_commands/locale/worldedit_commands.de.tr index 3c50a1f..4e2822d 100644 --- a/worldedit_commands/locale/worldedit_commands.de.tr +++ b/worldedit_commands/locale/worldedit_commands.de.tr @@ -30,6 +30,7 @@ Can use WorldEdit commands=Kann WorldEdit-Befehle benutzen no region selected=Kein Gebiet ausgewählt no position 1 selected=Keine Position 1 ausgewählt invalid usage=Ungültige Verwendung +ERROR: the operation was cancelled because the region has changed.=FEHLER: Der Vorgang wurde abgebrochen, weil das Gebiet verändert wurde. Could not open file "@1"=Konnte die Datei „@1“ nicht öffnen Invalid file format!=Ungültiges Dateiformat! Schematic was created with a newer version of WorldEdit.=Schematic wurde mit einer neueren Version von WorldEdit erstellt.