wand.lua 2.1 KB

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