]> git.lizzy.rs Git - worldedit.git/blob - worldedit_commands/wand.lua
fc74214f6dadd89da239f02aae08bea8b5cbab7c
[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 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         end,
40
41         on_place = function(itemstack, placer, pointed_thing)
42                 if placer == nil or (pointed_thing or {}).type ~= "node" then
43                         return itemstack
44                 end
45                 local name = placer:get_player_name()
46                 -- set and mark pos2
47                 worldedit.pos2[name] = above_or_under(placer, pointed_thing)
48                 worldedit.mark_pos2(name)
49                 return itemstack -- nothing consumed, nothing changed
50         end,
51
52         on_secondary_use = function(itemstack, user, pointed_thing)
53                 if user == nil or (pointed_thing or {}).type ~= "object" then
54                         return itemstack
55                 end
56                 local name = user:get_player_name()
57                 local entity = pointed_thing.ref:get_luaentity()
58                 if entity and entity.name == "worldedit:pos1" then
59                         -- set pos2 = pos1
60                         worldedit.pos2[name] = worldedit.pos1[name]
61                         worldedit.mark_pos2(name)
62                 end
63                 return itemstack -- nothing consumed, nothing changed
64         end,
65 })