From 5c632df658ebd834f3c810487816dd7a2606ef0e Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Mon, 10 Jul 2023 20:17:28 +0100 Subject: [PATCH] Override basic selection commands //mark //unmark //pos1 //1 //pos2 //2 //reset Also add //pos --- CHANGELOG.md | 1 + Chat-Command-Reference.md | 68 +++++++++++++++++++ .../commands/selectors/init.lua | 38 +++++++---- .../commands/selectors/mark.lua | 23 +++++++ .../commands/selectors/pos1-2.lua | 63 +++++++++++++++++ .../commands/selectors/reset.lua | 32 +++++++++ worldeditadditions_core/core/pos.lua | 17 +++++ .../core/pos_marker_manage.lua | 37 +++++++--- .../core/pos_marker_wall_manage.lua | 2 + 9 files changed, 255 insertions(+), 26 deletions(-) create mode 100644 worldeditadditions_commands/commands/selectors/mark.lua create mode 100644 worldeditadditions_commands/commands/selectors/pos1-2.lua create mode 100644 worldeditadditions_commands/commands/selectors/reset.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 20a752d..1702fce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Note to self: See the bottom of this file for the release template text. - Implement custom region boxing UI, which replaces the WorldEdit region box when using WorldEditAdditions wands. - Is backwards compatible with regular WorldEdit wands and tools, as WorldEditAdditions keeps the new positioning system in sync with WorldEdit's. - The new multipoint wand required this as a prerequisite + - Add [`//pos`](https://worldeditadditions.mooncarrot.space/Reference/#pos), for setting any numbered point (i.e. not just pos1 and pos2, but pos3 and beyond) - Add [`//spline`](https://worldeditadditions.mooncarrot.space/Reference/#spline), for drawing curved lines with an arbitrary number of points **(uses the new multi-point wand)** - Add [`//revolve`](https://worldeditadditions.mooncarrot.space/Reference/#revolve), which makes multiple evenly-spaced rotated copies of the defined region **(uses the new multi-point wand)** - [`//copy+`](https://worldeditadditions.mooncarrot.space/Reference/#copy), [`//move+`](https://worldeditadditions.mooncarrot.space/Reference/#move): Added support for integrated `airapply` mode, which replaces nodes at the target only if they are air - append `airapply`/`aa` to the command to use diff --git a/Chat-Command-Reference.md b/Chat-Command-Reference.md index bd68a97..cf4c109 100644 --- a/Chat-Command-Reference.md +++ b/Chat-Command-Reference.md @@ -868,7 +868,75 @@ Returns the absolute canonical name of a node, given an alias or partial node na ███████ ███████ ███████ ███████ ██████ ██ ██ ██████ ██ ████ --> +### `//unmark` +Hides the in-game UI that indicates where the current positions and region are located. +This hides both the WorldEditAdditions *and* the WorldEdit UI if displayed, but does **not** change or remove any points that are registered. + +Should more than 2 points be defined, they are all hidden. + +```weacmd +//unmark +``` + +### `//mark` +Shows the in-game UI that indicates where the current positions and region are located once more. + +Should more than 2 points be defined, they are all shown once more. + +Often used after calling [`//unmark`](#unmark) + +```weacmd +//mark +``` + + +### `//pos1` +Sets pos1 to the location of the calling player. + +This is, as with all other WorldEditAdditions commands, seamlessly synchronised with WorldEdit, allowing you to use any combination of WorldEditAdditions and WorldEdit commands and tools without them desynchronising from one another. + +**Aliases:** `//1` + +```weacmd +//pos2 +``` + +### `//pos2` +Sets pos1 to the location of the calling player. + +This is, as with all other WorldEditAdditions commands, seamlessly synchronised with WorldEdit, allowing you to use any combination of WorldEditAdditions and WorldEdit commands and tools without them desynchronising from one another. + +**Aliases:** `//2` + +```weacmd +//pos2 +``` + +### `//pos ` +Sets position with the given index `` to the location of the calling player. + +Should the index be less than or equal to 2, then as with all other WorldEditAdditions commands, seamlessly synchronised with WorldEdit, allowing you to use any combination of WorldEditAdditions and WorldEdit commands and tools without them desynchronising from one another. + +Should the index be greater than 2, the position will only be registered in WorldEditAdditions, as WorldEdit does not support defining more than 2 points. + +If no index is specified, an error is returned and nothing is done. + +```weacmd +//pos 1 +//pos 3 +//pos 45 +//pos 2 +``` + +### `//reset` +Clears all positions defined and the defined region. + +This also synchronises with WorldEdit, as all other WorldEditAdditions commands do. + +```weacmd +//reset +``` ### `//scol [ ] ` diff --git a/worldeditadditions_commands/commands/selectors/init.lua b/worldeditadditions_commands/commands/selectors/init.lua index 6b81f1a..00d5685 100644 --- a/worldeditadditions_commands/commands/selectors/init.lua +++ b/worldeditadditions_commands/commands/selectors/init.lua @@ -6,21 +6,29 @@ -- Chat commands that operate on selections. -local we_cmdpath = worldeditadditions_commands.modpath .. "/commands/selectors/" +local wea_cmdpath = worldeditadditions_commands.modpath .. "/commands/selectors/" +local weac = worldeditadditions_core -dofile(we_cmdpath.."srel.lua") -dofile(we_cmdpath.."scentre.lua") -dofile(we_cmdpath.."scloud.lua") -dofile(we_cmdpath.."scol.lua") -dofile(we_cmdpath.."scube.lua") -dofile(we_cmdpath.."sfactor.lua") -dofile(we_cmdpath.."smake.lua") -dofile(we_cmdpath.."spop.lua") -dofile(we_cmdpath.."spush.lua") -dofile(we_cmdpath.."srect.lua") -dofile(we_cmdpath.."sshift.lua") -dofile(we_cmdpath.."sstack.lua") -dofile(we_cmdpath.."unmark.lua") +dofile(wea_cmdpath.."srel.lua") +dofile(wea_cmdpath.."scentre.lua") +dofile(wea_cmdpath.."scloud.lua") +dofile(wea_cmdpath.."scol.lua") +dofile(wea_cmdpath.."scube.lua") +dofile(wea_cmdpath.."sfactor.lua") +dofile(wea_cmdpath.."smake.lua") +dofile(wea_cmdpath.."spop.lua") +dofile(wea_cmdpath.."spush.lua") +dofile(wea_cmdpath.."srect.lua") +dofile(wea_cmdpath.."sshift.lua") +dofile(wea_cmdpath.."sstack.lua") + +dofile(wea_cmdpath.."unmark.lua") +dofile(wea_cmdpath.."mark.lua") +dofile(wea_cmdpath.."pos1-2.lua") +dofile(wea_cmdpath.."reset.lua") -- Aliases -worldedit.alias_command("sfac", "sfactor") +weac.register_alias("sfac", "sfactor") + +weac.register_alias("1", "pos1", true) -- true = override target +weac.register_alias("2", "pos2", true) -- true = override target diff --git a/worldeditadditions_commands/commands/selectors/mark.lua b/worldeditadditions_commands/commands/selectors/mark.lua new file mode 100644 index 0000000..675a2cf --- /dev/null +++ b/worldeditadditions_commands/commands/selectors/mark.lua @@ -0,0 +1,23 @@ +local weac = worldeditadditions_core + + +local function do_mark(name, params_text) + -- TODO: Decide whether we need to hided the worldeditadditions marker here or not. + -- Show the WorldEditAdditions marker + weac.pos.mark(name) +end + +if minetest.registered_chatcommands["/mark"] then + minetest.override_chatcommand("/mark", { + params = "", + description = "Show the markers for the defined region (and any other positions) once more.", + func = do_mark + }) +else + minetest.register_chatcommand("/mark", { + params = "", + description = "Show the markers for the defined region (and any other positions) once more.", + privs = { worldedit = true }, + func = do_mark + }) +end \ No newline at end of file diff --git a/worldeditadditions_commands/commands/selectors/pos1-2.lua b/worldeditadditions_commands/commands/selectors/pos1-2.lua new file mode 100644 index 0000000..9227615 --- /dev/null +++ b/worldeditadditions_commands/commands/selectors/pos1-2.lua @@ -0,0 +1,63 @@ +local weac = worldeditadditions_core +local Vector3 = weac.Vector3 + + +local function do_set(name, i) + local player = minetest.get_player_by_name(name) + weac.pos.set(name, i, Vector3.clone(player:get_pos())) +end + +local function do_set1(name, params_text) + do_set(name, 1) +end +local function do_set2(name, params_text) + do_set(name, 2) +end + +if minetest.registered_chatcommands["/pos1"] then + minetest.override_chatcommand("/pos1", { + params = "", + description = + "Sets pos1 to the current position of the calling player.", + func = do_set1 + }) +else + minetest.register_chatcommand("/pos1", { + params = "", + description = + "Sets pos1 to the current position of the calling player.", + privs = { worldedit = true }, + func = do_set1 + }) +end +if minetest.registered_chatcommands["/pos2"] then + minetest.override_chatcommand("/pos2", { + params = "", + description = "Sets pos2 to the current position of the calling player.", + func = do_set2 + }) +else + minetest.register_chatcommand("/pos2", { + params = "", + description = "Sets pos2 to the current position of the calling player.", + privs = { worldedit = true }, + func = do_set2 + }) +end + + +minetest.register_chatcommand("//pos", { + params = "", + description = "Sets position to the current position of the calling player.", + privs = { worldedit = true }, + func = function(name, params_text) + local i = tonumber(params_text) + if type(i) ~= "number" then + worldedit.player_notify(name, "Error: Invalid index number given.") + return + end + i = math.floor(i) + + do_set(name, i) + end +}) \ No newline at end of file diff --git a/worldeditadditions_commands/commands/selectors/reset.lua b/worldeditadditions_commands/commands/selectors/reset.lua new file mode 100644 index 0000000..54c82f5 --- /dev/null +++ b/worldeditadditions_commands/commands/selectors/reset.lua @@ -0,0 +1,32 @@ +local weac = worldeditadditions_core + + +local worldedit_reset +if minetest.registered_chatcommands["/reset"] then + worldedit_reset = minetest.registered_chatcommands["/reset"].func +end + +local function do_reset(name, params_text) + -- Hide the WorldEdit marker, if appropriate + if type(worldedit_reset) == "function" then + worldedit_reset(name, params_text) + end + + -- Hide the WorldEditAdditions marker + weac.pos.clear(name) +end + +if minetest.registered_chatcommands["/reset"] then + minetest.override_chatcommand("/reset", { + params = "", + description = "Clears all defined points and the currently defined region.", + func = do_reset + }) +else + minetest.register_chatcommand("/reset", { + params = "", + description = "Clears all defined points and the currently defined region.", + privs = { worldedit = true }, + func = do_reset + }) +end \ No newline at end of file diff --git a/worldeditadditions_core/core/pos.lua b/worldeditadditions_core/core/pos.lua index 88d5940..9fe7abe 100644 --- a/worldeditadditions_core/core/pos.lua +++ b/worldeditadditions_core/core/pos.lua @@ -31,6 +31,14 @@ local anchor = nil -- @event clear -- @format { player_name: string } +--- It is requested that all position/region marker UI elements be hidden for the given player. +-- @event unmark +-- @format { player_name: string } + +--- It is requested that all position/region marker UI elements be shown once more for the given player. +-- @event mark +-- @format { player_name: string } + --- Ensures that a table exists for the given player. -- @param player_name string The name of the player to check. local function ensure_player(player_name) @@ -266,6 +274,14 @@ local function unmark(player_name) }) end +--- Shows the visual markers for the given player's positions and defined region once more. +-- Often used some time after calling worldeditadditions_core.pos.unmark(). +-- @param player_name string The name of the player to operate on. +local function mark(player_name) + anchor:emit("mark", { + player_name = player_name + }) +end anchor = wea_c.EventEmitter.new({ get = get, @@ -282,6 +298,7 @@ anchor = wea_c.EventEmitter.new({ set2 = set2, set_all = set_all, unmark = unmark, + mark = mark, compat_worldedit_get = compat_worldedit_get, }) anchor.debug = false diff --git a/worldeditadditions_core/core/pos_marker_manage.lua b/worldeditadditions_core/core/pos_marker_manage.lua index d9999f9..8272292 100644 --- a/worldeditadditions_core/core/pos_marker_manage.lua +++ b/worldeditadditions_core/core/pos_marker_manage.lua @@ -43,6 +43,15 @@ local function do_create(event) position_entities[event.player_name][event.i] = new_entity end +local function do_delete_all(player_name) + if #position_entities[player_name] > 0 then + for _, entity in pairs(position_entities[player_name]) do + wea_c.entities.pos_marker.delete(entity) + end + end + position_entities[player_name] = nil +end + wea_c.pos:addEventListener("push", function(event) do_create(event) end) @@ -72,26 +81,32 @@ end) wea_c.pos:addEventListener("clear", function(event) ensure_player(event.player_name) - if #position_entities[event.player_name] > 0 then - for _, entity in pairs(position_entities[event.player_name]) do - wea_c.entities.pos_marker.delete(entity) - end - end + do_delete_all(event.player_name) + -- For compatibility, ensure that we also clear the legacy worldedit region too if worldedit and worldedit.marker_update then worldedit.marker_update(event.player_name) end - position_entities[event.player_name] = nil end) wea_c.pos:addEventListener("unmark", function(event) ensure_player(event.player_name) - if #position_entities[event.player_name] > 0 then - for _, entity in pairs(position_entities[event.player_name]) do - wea_c.entities.pos_marker.delete(entity) - end - end + do_delete_all(event.player_name) -- Note that this function is NOT WorldEdit compatible, because it is only called through our override of WorldEdit's `//unmark`, and WorldEdit doesn't have an API function to call to unmark and everything is complicated. +end) + +wea_c.pos:addEventListener("mark", function(event) + ensure_player(event.player_name) + + do_delete_all(event.player_name) + + for i, pos in pairs(wea_c.pos.get_all(event.player_name)) do + do_create({ + player_name = event.player_name, + i = i, + pos = pos + }) + end end) \ No newline at end of file diff --git a/worldeditadditions_core/core/pos_marker_wall_manage.lua b/worldeditadditions_core/core/pos_marker_wall_manage.lua index f30c5aa..4e6c5da 100644 --- a/worldeditadditions_core/core/pos_marker_wall_manage.lua +++ b/worldeditadditions_core/core/pos_marker_wall_manage.lua @@ -70,4 +70,6 @@ weac.pos:addEventListener("set", handle_event) weac.pos:addEventListener("pop", handle_event) weac.pos:addEventListener("push", handle_event) weac.pos:addEventListener("clear", do_delete) + weac.pos:addEventListener("unmark", do_delete) +weac.pos:addEventListener("mark", do_update)