diff --git a/.luacheckrc b/.luacheckrc index a7bd5d89..41422cae 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -22,6 +22,7 @@ globals = { "worldeditadditions", "worldeditadditions_commands", "worldeditadditions_core", + "worldeditadditions_tools" } -- Read-only globals read_globals = { diff --git a/CHANGELOG.md b/CHANGELOG.md index d8dd4d70..68735500 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,14 +12,16 @@ Note to self: See the bottom of this file for the release template text. - Added [`//ngroups`](https://worldeditadditions.mooncarrot.space/Reference/#ngroups), which lists the groups that a given node is a member of. Useful when paired with [`//nodeapply`](https://worldeditadditions.mooncarrot.space/Reference/#nodeapply)! - Added [`//rotate+`](https://worldeditadditions.mooncarrot.space/Reference/#rotate) to rotate regions through arbitrary series of potentially non-axis-aligned rotations. **Does not support slabs/stairs yet,** but this is on the todo list! - Added [`//speed`](https://worldeditadditions.mooncarrot.space/Reference/#speed) to adjust your own movement speed - - Also added an associated [movement speed adjustment tool](https://worldeditadditions.mooncarrot.space/Reference/#movement), which looks like this: ![A picture of the move speed adjustment tool. It looks like a monarch butterfly.](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/dev/worldeditadditions_farwand/textures/worldeditadditions_movement.png) + - Also added an associated [movement speed adjustment tool](https://worldeditadditions.mooncarrot.space/Reference/#movement), which looks like this: ![A picture of the move speed adjustment tool. It looks like a monarch butterfly.](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/dev/worldeditadditions_tools/textures/worldeditadditions_movement.png) - Added [`//set+`](https://worldeditadditions.mooncarrot.space/Reference/#set) for setting nodes and param2/light levels quickly. - NOTE TO SELF: Setting light values doesn't appear to be working very well for some reason - Added [`//ndef`](https://worldeditadditions.mooncarrot.space/Reference/#ndef) to print a given node's definition table. This is for debugging and development purposes. - Added `//sgrow` and `//sshrink` commands to enlarge and shrink selection regions and aliased them over WorldEdit equivalents (`//expand`, `//outset` and `//contract`, `//inset` respectively). - Added Unified Axis Syntax (UAS) parser. - Implementation by @VorTechnix - - See [UAS System reference] for details. (Note to self hook up hyperlink) + - See [UAS System reference](https://worldeditadditions.mooncarrot.space/Reference/#unified-axis-syntax-uas) for details. - Added `//uasparse` command to show the vectors produced by a given UAS expression. - Implementation by @VorTechnix +- Added `//tool` command to list available tools and give tools to players. (Requires players to have the `weatool` privilege) - Implementation by @VorTechnix +- Added `weatool` privilege. - Implementation by @VorTechnix ### Bugfixes and changes - When commands produce an error, the name of the command that produced the error is now also printed. Useful when using e.g. [`//multi`](https://worldeditadditions.mooncarrot.space/Reference/#multi) etc. @@ -36,6 +38,7 @@ Note to self: See the bottom of this file for the release template text. ### Lua API changes - Add `core.pos.get12(player_name, sort=false)` +- Add `tools.register_tool(name, tool_def)` - Implementation by @VorTechnix ## v1.14.5: The multipoint update, hotfix 5 (1st August 2023) @@ -63,7 +66,7 @@ Note to self: See the bottom of this file for the release template text. - Add `//dome+`, which allows you to change the direction the dome is pointing in, and also create multiple domes at once - Add `//metaball`, which renders 2 or more [metaballs](https://en.wikipedia.org/wiki/Metaballs) in Minetest - Significant backend refactoring to tidy things up - - Add new multi-point selection wand ![A picture of the multi-point wand](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/main/worldeditadditions_farwand/textures/worldeditadditions_multiwand.png) to select many points at once. + - Add new multi-point selection wand ![A picture of the multi-point wand](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/main/worldeditadditions_tools/textures/worldeditadditions_multiwand.png) to select many points at once. - 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 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 791815c1..a72417e8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,7 +11,7 @@ Name | Description --------------------------------|------------------------ `worldeditadditions` | The main mod. Core world manipulation implementations (backed by the `*_core` mod ref core and utility functions) should go in here. `worldeditadditions_commands` | Chat commands. These interact with the core manipulators in `worldeditadditions` mod. -`worldeditadditions_farwand` | Everything to do with the far wand tool, and now other tools like the cloud wand, multi-point wand, etc. It's different enough to everything else that it warrants it's own separate mod to avoid muddling things. +`worldeditadditions_tools` | Everything to do with the far wand tool, and now other tools like the cloud wand, multi-point wand, etc. It's different enough to everything else that it warrants it's own separate mod to avoid muddling things. `worldeditadditions_core` | Core components such as the positioning system (`worldeditadditions_core.pos`), the command registration function, and utility functions go in here. Additionally, every command should be implemented in its own file. This helps keep things organised and files short. diff --git a/Chat-Command-Reference.md b/Chat-Command-Reference.md index d608396c..a66fa717 100644 --- a/Chat-Command-Reference.md +++ b/Chat-Command-Reference.md @@ -1896,8 +1896,19 @@ Prevents the execution of a command if it could potentially affect a large numbe ██ ██████ ██████ ███████ ███████ --> +### `//tool list || give|exists ` +> Added in v1.15 + +Lists available tools, allowes player to check if a specific tool exists and gives tools on demand as long as the player doesn't already have them, has room in their inventory, and has the `weatool` privilege. + +```weacmd +//tool list +//tool give cloudwand +//tool exists multiwand +``` + ### Movement speed adjustment tool -The movement speed adjustment tool, as the name suggests, adjusts your local player movement speed. It looks like this: ![A picture of the move speed adjustment tool. It looks like a monarch butterfly.](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/dev/worldeditadditions_farwand/textures/worldeditadditions_movement.png). +The movement speed adjustment tool, as the name suggests, adjusts your local player movement speed. It looks like this: ![A picture of the move speed adjustment tool. It looks like a monarch butterfly.](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/dev/worldeditadditions_tools/textures/worldeditadditions_movement.png). Primary (left) clicking will increase your movement speed, and secondary (right) clicking will decrease it in x0.5 increments, down to a minimum of x0.5. @@ -1908,7 +1919,7 @@ See also [`//speed`](#speed). ### Far Wand > Added in v1.7 -The far wand (`worldeditadditions:farwand`) is a variant on the traditional WorldEdit wand (`worldedit:wand`). It looks like this: ![A picture of the far wand](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/main/worldeditadditions_farwand/textures/worldeditadditions_farwand.png) +The far wand (`worldeditadditions:farwand`) is a variant on the traditional WorldEdit wand (`worldedit:wand`). It looks like this: ![A picture of the far wand](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/main/worldeditadditions_tools/textures/worldeditadditions_farwand.png) It functions very similarly to the regular WorldEdit wand, except that it has a _much_ longer range - which can be very useful for working on large-scale terrain for example. It also comes with an associated command to control it. @@ -1941,7 +1952,7 @@ Note that the number there isn't in blocks (because hard maths). It is however p ### Cloud Wand > Added in v1.11 -The cloud wand (`worldeditadditions:cloudwand`) is a another variant the above _Far Wand_. It looks like this: ![A picture of the far wand](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/main/worldeditadditions_farwand/textures/worldeditadditions_cloudwand.png) +The cloud wand (`worldeditadditions:cloudwand`) is a another variant the above _Far Wand_. It looks like this: ![A picture of the far wand](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/main/worldeditadditions_tools/textures/worldeditadditions_cloudwand.png) Unlike the other 2 wands, this wand functions in an additive manner. Left-click on a node to expand the currently defined region (creating a new one if one isn't defined already) to include that node. Right click to clear the currently defined region. @@ -1953,7 +1964,7 @@ Note that punching out the positions **does not unset them**. Use `//reset` to r ### MultiPoint Wand > Added in v1.14 -The third type of wand provided by WorldEditAdditions is completely different, in that it allows you to select up to **999 points** at once! It looks like this: ![A picture of the multi-point wand](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/main/worldeditadditions_farwand/textures/worldeditadditions_multiwand.png) +The third type of wand provided by WorldEditAdditions is completely different, in that it allows you to select up to **999 points** at once! It looks like this: ![A picture of the multi-point wand](https://raw.githubusercontent.com/sbrl/Minetest-WorldEditAdditions/main/worldeditadditions_tools/textures/worldeditadditions_multiwand.png) It is important to note that (at present) the points selected by this wand **are not compatible with normal points**. This will change in the future, but requires a lot of work to implement. diff --git a/README.md b/README.md index 56f1244f..fe0380a3 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,7 @@ The detailed explanations have moved! Check them out [here](https://worldeditadd - [WorldEditAdditions Far Wand](https://github.com/sbrl/Minetest-WorldEditAdditions/blob/main/Chat-Command-Reference.md#far-wand) - [WorldEditAdditions Cloud Wand](https://github.com/sbrl/Minetest-WorldEditAdditions/blob/main/Chat-Command-Reference.md#cloud-wand) - [`//farwand skip_liquid (true|false) | maxdist `](https://github.com/sbrl/Minetest-WorldEditAdditions/blob/main/Chat-Command-Reference.md#farwand-skip_liquid-truefalse--maxdist-number) + - [`//tool list || give|exists `](https://github.com/sbrl/Minetest-WorldEditAdditions/blob/main/Chat-Command-Reference.md#tool-list-giveexists-tool-name) _(new in v1.15)_ ## Installation diff --git a/worldeditadditions_core/core/pos.lua b/worldeditadditions_core/core/pos.lua index 77728d62..73e13cf9 100644 --- a/worldeditadditions_core/core/pos.lua +++ b/worldeditadditions_core/core/pos.lua @@ -84,6 +84,7 @@ local function compat_worldedit_pos2_get(player_name) end --- Sets pos1/pos2 in worldedit for compatibility. +-- @internal -- @param player_name string The name of the player to set the position for. -- @param i number The index of the position to set. -- @param pos Vector3 The position to set. @@ -106,6 +107,7 @@ local function compat_worldedit_set(player_name, i, pos, do_update) end --- Fetches pos1/pos2 from WorldEdit (if available) and sets them in WorldEditAdditions' postional subsystem +-- @internal -- @param player_name string The name of the player to sync the positions for. local function compat_worldedit_get(player_name) compat_worldedit_pos1_get(player_name) @@ -334,7 +336,6 @@ anchor = wea_c.EventEmitter.new({ set_all = set_all, unmark = unmark, mark = mark, - compat_worldedit_get = compat_worldedit_get, }) anchor.debug = false diff --git a/worldeditadditions_farwand/init.lua b/worldeditadditions_farwand/init.lua deleted file mode 100644 index d5da5e9f..00000000 --- a/worldeditadditions_farwand/init.lua +++ /dev/null @@ -1,14 +0,0 @@ -worldeditadditions.farwand = { - player_data = {} -} - -local modpath = minetest.get_modpath("worldeditadditions_farwand") - -dofile(modpath.."/lib/do_raycast.lua") -dofile(modpath.."/lib/farwand.lua") -dofile(modpath.."/lib/cloudwand.lua") -dofile(modpath.."/lib/multiwand.lua") -dofile(modpath.."/lib/movetool.lua") - -dofile(modpath.."/lib/chatcommand.lua") -dofile(modpath.."/lib/settings.lua") diff --git a/worldeditadditions_farwand/lib/do_raycast.lua b/worldeditadditions_farwand/lib/do_raycast.lua deleted file mode 100644 index ea99022d..00000000 --- a/worldeditadditions_farwand/lib/do_raycast.lua +++ /dev/null @@ -1,17 +0,0 @@ -local wea_c = worldeditadditions_core ---- worldeditadditions.raycast() wrapper -function worldeditadditions.farwand.do_raycast(player) - if player == nil then return nil end - local player_name = player:get_player_name() - - if worldeditadditions.farwand.player_data[player_name] == nil then - worldeditadditions.farwand.player_data[player_name] = { maxdist = 1000, skip_liquid = true } - end - - local looking_pos, node_id = wea_c.raycast( - player, - worldeditadditions.farwand.player_data[player_name].maxdist, - worldeditadditions.farwand.player_data[player_name].skip_liquid - ) - return looking_pos, node_id -end diff --git a/worldeditadditions_farwand/mod.conf b/worldeditadditions_farwand/mod.conf deleted file mode 100644 index 6a00c479..00000000 --- a/worldeditadditions_farwand/mod.conf +++ /dev/null @@ -1,3 +0,0 @@ -name = worldeditadditions_farwand -description = worldeditadditions: convenient tool items -depends = worldedit, worldeditadditions diff --git a/worldeditadditions_farwand/edit/brush.piskel b/worldeditadditions_tools/.edit/brush.piskel similarity index 100% rename from worldeditadditions_farwand/edit/brush.piskel rename to worldeditadditions_tools/.edit/brush.piskel diff --git a/worldeditadditions_farwand/edit/worldedit_wand.piskel b/worldeditadditions_tools/.edit/worldedit_wand.piskel similarity index 100% rename from worldeditadditions_farwand/edit/worldedit_wand.piskel rename to worldeditadditions_tools/.edit/worldedit_wand.piskel diff --git a/worldeditadditions_farwand/edit/worldeditadditions_movement.piskel b/worldeditadditions_tools/.edit/worldeditadditions_movement.piskel similarity index 100% rename from worldeditadditions_farwand/edit/worldeditadditions_movement.piskel rename to worldeditadditions_tools/.edit/worldeditadditions_movement.piskel diff --git a/worldeditadditions_farwand/lib/chatcommand.lua b/worldeditadditions_tools/commands/farwand_config.lua similarity index 96% rename from worldeditadditions_farwand/lib/chatcommand.lua rename to worldeditadditions_tools/commands/farwand_config.lua index 4df3a083..5d71c46c 100644 --- a/worldeditadditions_farwand/lib/chatcommand.lua +++ b/worldeditadditions_tools/commands/farwand_config.lua @@ -1,5 +1,5 @@ local wea_c = worldeditadditions_core -local farwand = worldeditadditions.farwand -- Convenience shurtcut +local farwand = worldeditadditions_tools -- Convenience shurtcut local function parse_params_farwand(params_text) if params_text == nil then diff --git a/worldeditadditions_tools/commands/weatool.lua b/worldeditadditions_tools/commands/weatool.lua new file mode 100644 index 00000000..7565e980 --- /dev/null +++ b/worldeditadditions_tools/commands/weatool.lua @@ -0,0 +1,64 @@ +-- ██ ██ ███████ █████ ████████ ██████ ██████ ██ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ██ █ ██ █████ ███████ ██ ██ ██ ██ ██ ██ +-- ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ███ ███ ███████ ██ ██ ██ ██████ ██████ ███████ + +local wea_c = worldeditadditions_core +local wea_t = worldeditadditions_tools + +worldeditadditions_core.register_command("tool", { + params = "list || give|exists ", + description = "Give WEA tools by name to the calling player or list available tools.", + privs = { worldedit = true, weatool = true }, + require_pos = 0, + parse = function(params_text) + local ret = wea_c.split(params_text) + if #ret < 1 then return false, "Error: No params found!" end + + local commands = {list = true, give = true, exists = true} + if not commands[ret[1]] then + return false, "Invalid command: "..ret[1] + end + + if wea_t.registered_tools[ret[2]] or ret[1] == "list" then + return true, ret + else return false, "No such WEA tool: "..ret[2] end + end, + func = function(name, params_text) + if params_text[1] == "list" then + -- Return a list of available tools in rows of 7 + local ret = "WEA Registered Tools:\n" + local i = 0 + for k, _ in pairs(wea_t.registered_tools) do + i = i + 1 + if i % 7 == 0 then ret = ret..k.."\n" + else ret = ret..k.." " end + end + return true, ret + elseif params_text[1] == "exists" then + -- NOTE: If tool did not exist it would be flagged by parse function + return true, "WEA tool \""..params_text[2].."\" exists" + else + -- Initiate player variable and check if it is a valid player + local player = minetest.get_player_by_name(name) + if not player or not player:is_player() then + return false, "\""..name.."\" is not a valid player." + end + -- Create inventory and item instances + local inv = player:get_inventory() + local item = "worldeditadditions:"..params_text[2] + -- Make sure the player doesn't already have the item and has room for it + if inv:contains_item("main", item) then + return false, name.." already has (a) \""..params_text[2].."\"." + elseif not inv:room_for_item("main", item) then + return false, name.." does not have room for (a) \""..params_text[2].."\"." + else + -- Give the player the item + inv:add_item("main", item) + return true, "Gave \""..params_text[2].."\" to "..name + end + + end + end, +}) \ No newline at end of file diff --git a/worldeditadditions_tools/init.lua b/worldeditadditions_tools/init.lua new file mode 100644 index 00000000..876a863f --- /dev/null +++ b/worldeditadditions_tools/init.lua @@ -0,0 +1,30 @@ +---@diagnostic disable-next-line: lowercase-global +worldeditadditions_tools = { + player_data = {}, + registered_tools = {}, +} +local wea_t = worldeditadditions_tools + +minetest.register_privilege("weatool", { + description = "Allows players to use WEA tools.", + give_to_singleplayer = true, + give_to_admin = true, +}) +local modpath = minetest.get_modpath("worldeditadditions_tools") + + +-- Libraries +dofile(modpath.."/lib/do_raycast.lua") +dofile(modpath.."/lib/settings.lua") + +wea_t.register_tool = dofile(modpath.."/lib/register_tool.lua") + +-- Items +dofile(modpath.."/items/farwand.lua") +dofile(modpath.."/items/cloudwand.lua") +dofile(modpath.."/items/multiwand.lua") +dofile(modpath.."/items/movetool.lua") + +-- Chat commands +dofile(modpath.."/commands/farwand_config.lua") +dofile(modpath.."/commands/weatool.lua") diff --git a/worldeditadditions_farwand/lib/cloudwand.lua b/worldeditadditions_tools/items/cloudwand.lua similarity index 88% rename from worldeditadditions_farwand/lib/cloudwand.lua rename to worldeditadditions_tools/items/cloudwand.lua index a880aea0..9182de4e 100644 --- a/worldeditadditions_farwand/lib/cloudwand.lua +++ b/worldeditadditions_tools/items/cloudwand.lua @@ -1,7 +1,8 @@ local wea = worldeditadditions local wea_c = worldeditadditions_core +local wea_t = worldeditadditions_tools -minetest.register_tool(":worldeditadditions:cloudwand", { +wea_t.register_tool("cloudwand", { description = "WorldEditAdditions far-reaching additive selector wand", inventory_image = "worldeditadditions_cloudwand.png", @@ -16,7 +17,7 @@ minetest.register_tool(":worldeditadditions:cloudwand", { on_use = function(itemstack, player, pointed_thing) local name = player:get_player_name() -- print("[farwand] on_use", name) - local looking_pos, node_id = worldeditadditions.farwand.do_raycast(player) + local looking_pos, node_id = wea_t.do_raycast(player) wea.selection.add_point(name, looking_pos) -- Left click when pointing at something or nothing end, diff --git a/worldeditadditions_farwand/lib/farwand.lua b/worldeditadditions_tools/items/farwand.lua similarity index 82% rename from worldeditadditions_farwand/lib/farwand.lua rename to worldeditadditions_tools/items/farwand.lua index 56a9a2ac..1b174eb0 100644 --- a/worldeditadditions_farwand/lib/farwand.lua +++ b/worldeditadditions_tools/items/farwand.lua @@ -1,3 +1,5 @@ +local wea_t = worldeditadditions_tools + local function set_pos1(name, pos) if pos ~= nil then -- print("[set_pos1]", name, "("..pos.x..", "..pos.y..", "..pos.z..")") @@ -21,23 +23,23 @@ local function set_pos2(name, pos) end end -minetest.register_tool(":worldeditadditions:farwand", { +wea_t.register_tool("farwand", { description = "WorldEditAdditions far-reaching wand", - inventory_image = "worldeditadditions_farwand.png", + inventory_image = "worldeditadditions_tools.png", on_place = function(itemstack, player, pointed_thing) local name = player:get_player_name() -- print("[farwand] on_place", name) -- Right click when pointing at something -- Pointed thing: https://rubenwardy.com/minetest_modding_book/lua_api.html#pointed_thing - local looking_pos, node_id = worldeditadditions.farwand.do_raycast(player) + local looking_pos, node_id = wea_t.do_raycast(player) set_pos2(name, looking_pos) end, on_use = function(itemstack, player, pointed_thing) local name = player:get_player_name() -- print("[farwand] on_use", name) - local looking_pos, node_id = worldeditadditions.farwand.do_raycast(player) + local looking_pos, node_id = wea_t.do_raycast(player) set_pos1(name, looking_pos) -- Left click when pointing at something or nothing end, @@ -47,7 +49,7 @@ minetest.register_tool(":worldeditadditions:farwand", { -- Right click when pointing at nothing -- print("[farwand] on_secondary_use", name) - local looking_pos, node_id = worldeditadditions.farwand.do_raycast(player) + local looking_pos, node_id = wea_t.do_raycast(player) set_pos2(name, looking_pos) end }) diff --git a/worldeditadditions_farwand/lib/movetool.lua b/worldeditadditions_tools/items/movetool.lua similarity index 83% rename from worldeditadditions_farwand/lib/movetool.lua rename to worldeditadditions_tools/items/movetool.lua index d91528cd..8d1b241c 100644 --- a/worldeditadditions_farwand/lib/movetool.lua +++ b/worldeditadditions_tools/items/movetool.lua @@ -1,7 +1,8 @@ -local weac = worldeditadditions_core +local wea_c = worldeditadditions_core +local wea_t = worldeditadditions_tools local function adjust_speed_relative(player, offset) - local overrides = weac.player_get_physics_override( + local overrides = wea_c.player_get_physics_override( player, "worldeditadditions_movetool" ) if overrides == nil then overrides = {} end @@ -10,7 +11,7 @@ local function adjust_speed_relative(player, offset) local src_speed = overrides.speed or 1 local src_climb_speed = overrides.climb_speed or 1 - weac.player_set_physics_override( + wea_c.player_set_physics_override( player, "worldeditadditions_movetool", { speed = math.max(src_speed + offset, 0.5), climb_speed = math.max(src_climb_speed + offset, 0.5) @@ -20,7 +21,7 @@ local function adjust_speed_relative(player, offset) -- }) -- Completely paranoid is me - local overrides_after = weac.player_get_physics_override( + local overrides_after = wea_c.player_get_physics_override( player, "worldeditadditions_movetool" ) worldedit.player_notify(player:get_player_name(), "Movement speed is now x" .. tostring(overrides_after.speed)) @@ -36,7 +37,7 @@ local function use_secondary(player) end -minetest.register_tool(":worldeditadditions:movetool", { +wea_t.register_tool("movetool", { description = "WorldEditAdditions movement speed adjustment tool", inventory_image = "worldeditadditions_movement.png", diff --git a/worldeditadditions_farwand/lib/multiwand.lua b/worldeditadditions_tools/items/multiwand.lua similarity index 85% rename from worldeditadditions_farwand/lib/multiwand.lua rename to worldeditadditions_tools/items/multiwand.lua index 09cb1e1c..c246c18a 100644 --- a/worldeditadditions_farwand/lib/multiwand.lua +++ b/worldeditadditions_tools/items/multiwand.lua @@ -1,4 +1,5 @@ local wea_c = worldeditadditions_core +local wea_t = worldeditadditions_tools local wea = worldeditadditions local Vector3 = wea_c.Vector3 @@ -25,7 +26,7 @@ local function pop_pos(player_name) end -minetest.register_tool(":worldeditadditions:multiwand", { +wea_t.register_tool("multiwand", { description = "WorldEditAdditions multi-point wand", inventory_image = "worldeditadditions_multiwand.png", @@ -33,7 +34,6 @@ minetest.register_tool(":worldeditadditions:multiwand", { -- Right click when pointing at something -- Pointed thing: https://rubenwardy.com/minetest_modding_book/lua_api.html#pointed_thing local player_name = player:get_player_name() - wea_c.pos.compat_worldedit_get(player_name) -- print("[farwand] on_place", player_name) pop_pos(player_name) end, @@ -41,9 +41,8 @@ minetest.register_tool(":worldeditadditions:multiwand", { on_use = function(itemstack, player, pointed_thing) -- Left click when pointing at something or nothing local player_name = player:get_player_name() - wea_c.pos.compat_worldedit_get(player_name) -- print("[farwand] on_use", player_name) - local looking_pos, node_id = wea.farwand.do_raycast(player) + local looking_pos, node_id = wea_t.do_raycast(player) push_pos(player_name, looking_pos) end, @@ -51,8 +50,6 @@ minetest.register_tool(":worldeditadditions:multiwand", { -- Right click when pointing at nothing local player_name = player:get_player_name() -- print("[farwand] on_secondary_use", player_name) - wea_c.pos.compat_worldedit_get(player_name) - -- local looking_pos, node_id = do_raycast(player) pop_pos(player_name) end }) diff --git a/worldeditadditions_tools/lib/do_raycast.lua b/worldeditadditions_tools/lib/do_raycast.lua new file mode 100644 index 00000000..bb08a76a --- /dev/null +++ b/worldeditadditions_tools/lib/do_raycast.lua @@ -0,0 +1,17 @@ +local wea_c = worldeditadditions_core +--- worldeditadditions.raycast() wrapper +function worldeditadditions_tools.do_raycast(player) + if player == nil then return nil end + local player_name = player:get_player_name() + + if worldeditadditions_tools.player_data[player_name] == nil then + worldeditadditions_tools.player_data[player_name] = { maxdist = 1000, skip_liquid = true } + end + + local looking_pos, node_id = wea_c.raycast( + player, + worldeditadditions_tools.player_data[player_name].maxdist, + worldeditadditions_tools.player_data[player_name].skip_liquid + ) + return looking_pos, node_id +end diff --git a/worldeditadditions_tools/lib/register_tool.lua b/worldeditadditions_tools/lib/register_tool.lua new file mode 100644 index 00000000..5ce61c9d --- /dev/null +++ b/worldeditadditions_tools/lib/register_tool.lua @@ -0,0 +1,100 @@ +-- ██████ ███████ ██████ ██ ███████ ████████ ███████ ██████ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ██████ █████ ██ ███ ██ ███████ ██ █████ ██████ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ██ ██ ███████ ██████ ██ ███████ ██ ███████ ██ ██ +-- +-- ████████ ██████ ██████ ██ +-- ██ ██ ██ ██ ██ ██ +-- ██ ██ ██ ██ ██ ██ +-- ██ ██ ██ ██ ██ ██ +-- ██ ██████ ██████ ███████ + +--- WorldEditAdditions tool registration +-- A wrapper for registering tools for WorldEditAdditions. +-- @module worldeditadditions_core + +local wea_t = worldeditadditions_tools + +-- Helper functions -- +local function log_error(toolname, error_message) + minetest.log("error", "register_tool("..toolname..") error: "..error_message) +end + +local function log_warn(toolname, error_message) + minetest.log("warning", "register_tool("..toolname..") warning: "..error_message) +end + + +--- Registers a new WorldEditAdditions tool. +-- @param tool string The name of the tool to register (sans the ":worldeditadditions:" prefix). +--[[ + === WARNING === + ":worldeditadditions:" will be prepended to the `tool` param so + calling `register_tool(":worldeditadditions:", def_table)` + may cause errors. +--]] +-- @param options table A table of options for the tool: +-- - `description` (string) A description of the tool. +-- - `inventory_image` (string) The path to the image for the tool. +-- - `on_use` (function) A function that is called when right click while pointing at something. +-- - `on_place` (function) A function that is called when left click while pointing at something. +-- - `on_secondary_use` (function) A function that is called when left click while pointing at nothing. +-- - `stack_max` (number) The maximum number of the items that can be put in an ItemStack. +-- - `groups` (table) A table of groups that the tool belongs to. +local function register_tool(tool, options) + + --- + -- 1: Validation + --- + if type(options.description) ~= "string" then + log_error(tool, "The description option is not a string.") + return false + end + if type(options.inventory_image) ~= "string" then + log_error(tool, "The inventory_image option is not a string.") + return false + end + if not options.inventory_image:match("^.+%.png$") then + log_error(tool, "The inventory_image option is not a valid image path.") + return false + end + if type(options.on_use) ~= "function" then + if options.on_use == nil then + log_warn(tool, "The on_use option is nil.") + else + log_error(tool, "The on_use option is not a function.") + return false + end + end + if type(options.on_place) ~= "function" then + if options.on_place == nil then + log_warn(tool, "The on_place option is nil.") + else + log_error(tool, "The on_place option is not a function.") + return false + end + end + if type(options.on_secondary_use) ~= "function" then + if options.on_secondary_use == nil then + log_warn(tool, "The on_secondary_use option is nil.") + else + log_error(tool, "The on_secondary_use option is not a function.") + return false + end + end + + --- + -- 2: Normalisation + --- + if not options.stack_max then options.stack_max = 1 end + if not options.groups then options.groups = {wea = 1, wand = 1} end + + --- + -- 3: Registration + --- + minetest.register_tool(":worldeditadditions:" .. tool, options) + wea_t.registered_tools[tool] = options +end + +return register_tool \ No newline at end of file diff --git a/worldeditadditions_farwand/lib/settings.lua b/worldeditadditions_tools/lib/settings.lua similarity index 71% rename from worldeditadditions_farwand/lib/settings.lua rename to worldeditadditions_tools/lib/settings.lua index 8e1cddbd..d6dd939f 100644 --- a/worldeditadditions_farwand/lib/settings.lua +++ b/worldeditadditions_tools/lib/settings.lua @@ -1,9 +1,9 @@ --- If the settings object for the given player name doesn't exist, it is created. -- @param name The name of the player to ensure has a settings object. local function settings_init(name) - if worldeditadditions.farwand.player_data[name] == nil then + if worldeditadditions_tools.player_data[name] == nil then minetest.log("INFO", "Initialising settings for "..name) - worldeditadditions.farwand.player_data[name] = { + worldeditadditions_tools.player_data[name] = { maxdist = 1000, skip_liquid = true } @@ -14,10 +14,10 @@ end -- @param string name The name of the player to get the setting for. -- @param string setting_name The name of the setting to fetch. -- @return any The value of the setting. -function worldeditadditions.farwand.setting_get(name, setting_name) +function worldeditadditions_tools.setting_get(name, setting_name) if setting_name == nil then return nil end settings_init(name) - return worldeditadditions.farwand.player_data[name][setting_name] + return worldeditadditions_tools.player_data[name][setting_name] end --- Sets a given farwand setting for the given player name to the given value. @@ -25,9 +25,9 @@ end -- @param string setting_name The name of the setting to set. -- @param any setting_value The value to set the setting to. -- @return bool Whether setting the setting was successful or not. -function worldeditadditions.farwand.setting_set(name, setting_name, setting_value) +function worldeditadditions_tools.setting_set(name, setting_name, setting_value) if setting_name == nil then return false end settings_init(name) - worldeditadditions.farwand.player_data[name][setting_name] = setting_value + worldeditadditions_tools.player_data[name][setting_name] = setting_value return true end diff --git a/worldeditadditions_tools/mod.conf b/worldeditadditions_tools/mod.conf new file mode 100644 index 00000000..c5ce9faf --- /dev/null +++ b/worldeditadditions_tools/mod.conf @@ -0,0 +1,3 @@ +name = worldeditadditions_tools +description = worldeditadditions: convenient tool items +depends = worldedit, worldeditadditions, worldeditadditions_core diff --git a/worldeditadditions_farwand/textures/worldeditadditions_chisel.png b/worldeditadditions_tools/textures/worldeditadditions_chisel.png similarity index 100% rename from worldeditadditions_farwand/textures/worldeditadditions_chisel.png rename to worldeditadditions_tools/textures/worldeditadditions_chisel.png diff --git a/worldeditadditions_farwand/textures/worldeditadditions_cloudwand.png b/worldeditadditions_tools/textures/worldeditadditions_cloudwand.png similarity index 100% rename from worldeditadditions_farwand/textures/worldeditadditions_cloudwand.png rename to worldeditadditions_tools/textures/worldeditadditions_cloudwand.png diff --git a/worldeditadditions_farwand/textures/worldeditadditions_farwand.png b/worldeditadditions_tools/textures/worldeditadditions_farwand.png similarity index 100% rename from worldeditadditions_farwand/textures/worldeditadditions_farwand.png rename to worldeditadditions_tools/textures/worldeditadditions_farwand.png diff --git a/worldeditadditions_farwand/textures/worldeditadditions_movement.png b/worldeditadditions_tools/textures/worldeditadditions_movement.png similarity index 100% rename from worldeditadditions_farwand/textures/worldeditadditions_movement.png rename to worldeditadditions_tools/textures/worldeditadditions_movement.png diff --git a/worldeditadditions_farwand/textures/worldeditadditions_multiwand.png b/worldeditadditions_tools/textures/worldeditadditions_multiwand.png similarity index 100% rename from worldeditadditions_farwand/textures/worldeditadditions_multiwand.png rename to worldeditadditions_tools/textures/worldeditadditions_multiwand.png