tool.lua 4.0 KB

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