]> git.lizzy.rs Git - worldedit.git/blob - worldedit_commands/wand.lua
Allow easily setting pos1 + 2 to the same node using the wand
[worldedit.git] / worldedit_commands / wand.lua
1 local function above_or_under(placer, pointed_thing)
2         if placer:get_player_control().sneak then
3                 return pointed_thing.above
4         else
5                 return pointed_thing.under
6         end
7 end
8
9 local punched_air_time = {}
10
11 minetest.register_tool(":worldedit:wand", {
12         description = "WorldEdit Wand tool\nLeft-click to set 1st position, right-click to set 2nd",
13         inventory_image = "worldedit_wand.png",
14         stack_max = 1, -- there is no need to have more than one
15         liquids_pointable = true, -- ground with only water on can be selected as well
16
17         on_use = function(itemstack, placer, pointed_thing)
18                 if placer == nil or pointed_thing == nil then return itemstack end
19                 local name = placer:get_player_name()
20                 if pointed_thing.type == "node" then
21                         -- set and mark pos1
22                         worldedit.pos1[name] = above_or_under(placer, pointed_thing)
23                         worldedit.mark_pos1(name)
24                 elseif pointed_thing.type == "nothing" then
25                         local now = minetest.get_us_time()
26                         if now - (punched_air_time[name] or 0) < 1000 * 1000 then
27                                 -- reset markers
28                                 minetest.registered_chatcommands["/reset"].func(name, "")
29                         end
30                         punched_air_time[name] = now
31                 elseif pointed_thing.type == "object" then
32                         local entity = pointed_thing.ref:get_luaentity()
33                         if entity and entity.name == "worldedit:pos2" then
34                                 -- set pos1 = pos2
35                                 worldedit.pos1[name] = worldedit.pos2[name]
36                                 worldedit.mark_pos1(name)
37                         end
38                 end
39                 return itemstack -- nothing consumed, nothing changed
40         end,
41
42         on_place = function(itemstack, placer, pointed_thing)
43                 if placer == nil or pointed_thing == nil then return itemstack end
44                 local name = placer:get_player_name()
45                 if pointed_thing.type == "node" then
46                         -- set and mark pos2
47                         worldedit.pos2[name] = above_or_under(placer, pointed_thing)
48                         worldedit.mark_pos2(name)
49                 elseif pointed_thing.type == "object" then
50                         local entity = pointed_thing.ref:get_luaentity()
51                         if entity and entity.name == "worldedit:pos1" then
52                                 -- set pos2 = pos1
53                                 worldedit.pos2[name] = worldedit.pos1[name]
54                                 worldedit.mark_pos2(name)
55                         end
56                 end
57                 return itemstack -- nothing consumed, nothing changed
58         end,
59 })