tool.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. -- protector placement tool (thanks to Shara for code and idea)
  2. minetest.register_craftitem("protector:tool", {
  3. description = "Protector Placer Tool (stand near protector, face direction and use)",
  4. inventory_image = "protector_display.png^protector_logo.png",
  5. stack_max = 1,
  6. on_use = function(itemstack, user, pointed_thing)
  7. local name = user:get_player_name()
  8. -- check for protector near player (2 block radius)
  9. local pos = user:get_pos()
  10. local pp = minetest.find_nodes_in_area(
  11. vector.subtract(pos, 2), vector.add(pos, 2),
  12. {"protector:protect", "protector:protect2"})
  13. if #pp == 0 then return end -- none found
  14. pos = pp[1] -- take position of first protector found
  15. -- get members on protector
  16. local meta = minetest.get_meta(pos)
  17. local members = meta:get_string("members") or ""
  18. -- get direction player is facing
  19. local dir = minetest.dir_to_facedir( user:get_look_dir() )
  20. local vec = {x = 0, y = 0, z = 0}
  21. local gap = (protector.radius * 2) + 1
  22. local pit = user:get_look_pitch()
  23. -- set placement coords
  24. if pit > 1.2 then
  25. vec.y = gap -- up
  26. elseif pit < -1.2 then
  27. vec.y = -gap -- down
  28. elseif dir == 0 then
  29. vec.z = gap -- north
  30. elseif dir == 1 then
  31. vec.x = gap -- east
  32. elseif dir == 2 then
  33. vec.z = -gap -- south
  34. elseif dir == 3 then
  35. vec.x = -gap -- west
  36. end
  37. -- new position
  38. pos.x = pos.x + vec.x
  39. pos.y = pos.y + vec.y
  40. pos.z = pos.z + vec.z
  41. -- does placing a protector overlap existing area
  42. if not protector.can_dig(protector.radius * 2, pos,
  43. user:get_player_name(), true, 3) then
  44. minetest.chat_send_player(name,
  45. "Overlaps into above players protected area")
  46. return
  47. end
  48. -- does a protector already exist ?
  49. if #minetest.find_nodes_in_area(
  50. vector.subtract(pos, 1), vector.add(pos, 1),
  51. {"protector:protect", "protector:protect2"}) > 0 then
  52. minetest.chat_send_player(name, "Protector already in place!")
  53. return
  54. end
  55. -- do we have protectors to use ?
  56. local nod
  57. local inv = user:get_inventory()
  58. if not inv:contains_item("main", "protector:protect")
  59. and not inv:contains_item("main", "protector:protect2") then
  60. minetest.chat_send_player(name, "No protectors available to place!")
  61. return
  62. end
  63. -- take protector (block first then logo)
  64. if inv:contains_item("main", "protector:protect") then
  65. inv:remove_item("main", "protector:protect")
  66. nod = "protector:protect"
  67. elseif inv:contains_item("main", "protector:protect2") then
  68. inv:remove_item("main", "protector:protect2")
  69. nod = "protector:protect2"
  70. end
  71. -- place protector
  72. minetest.set_node(pos, {name = nod, param2 = 1})
  73. -- set protector metadata
  74. local meta = minetest.get_meta(pos)
  75. meta:set_string("owner", name)
  76. meta:set_string("infotext", "Protection (owned by " .. name .. ")")
  77. -- copy members across if holding sneak when using tool
  78. if user:get_player_control().sneak then
  79. meta:set_string("members", members)
  80. else
  81. meta:set_string("members", "")
  82. end
  83. minetest.chat_send_player(name,
  84. "Protector placed at " .. minetest.pos_to_string(pos))
  85. end,
  86. })
  87. -- tool recipe
  88. minetest.register_craft({
  89. output = "protector:tool",
  90. recipe = {
  91. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  92. {"default:steel_ingot", "protector:protect", "default:steel_ingot"},
  93. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  94. }
  95. })