api.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. local reverse = true
  2. local function destruct_bed(pos, n)
  3. local node = minetest.get_node(pos)
  4. local other
  5. if n == 2 then
  6. local dir = minetest.facedir_to_dir(node.param2)
  7. other = vector.subtract(pos, dir)
  8. elseif n == 1 then
  9. local dir = minetest.facedir_to_dir(node.param2)
  10. other = vector.add(pos, dir)
  11. end
  12. if reverse then
  13. reverse = not reverse
  14. minetest.remove_node(other)
  15. minetest.check_for_falling(other)
  16. beds.remove_spawns_at(pos)
  17. beds.remove_spawns_at(other)
  18. else
  19. reverse = not reverse
  20. end
  21. end
  22. function beds.register_bed(name, def)
  23. minetest.register_node(name .. "_bottom", {
  24. description = def.description,
  25. inventory_image = def.inventory_image,
  26. wield_image = def.wield_image,
  27. drawtype = "nodebox",
  28. tiles = def.tiles.bottom,
  29. use_texture_alpha = "clip",
  30. paramtype = "light",
  31. paramtype2 = "facedir",
  32. is_ground_content = false,
  33. stack_max = 1,
  34. groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 3, bed = 1},
  35. sounds = def.sounds or default.node_sound_wood_defaults(),
  36. node_box = {
  37. type = "fixed",
  38. fixed = def.nodebox.bottom,
  39. },
  40. selection_box = {
  41. type = "fixed",
  42. fixed = def.selectionbox,
  43. },
  44. on_place = function(itemstack, placer, pointed_thing)
  45. local under = pointed_thing.under
  46. local node = minetest.get_node(under)
  47. local udef = minetest.registered_nodes[node.name]
  48. if udef and udef.on_rightclick and
  49. not (placer and placer:is_player() and
  50. placer:get_player_control().sneak) then
  51. return udef.on_rightclick(under, node, placer, itemstack,
  52. pointed_thing) or itemstack
  53. end
  54. local pos
  55. if udef and udef.buildable_to then
  56. pos = under
  57. else
  58. pos = pointed_thing.above
  59. end
  60. local player_name = placer and placer:get_player_name() or ""
  61. if minetest.is_protected(pos, player_name) and
  62. not minetest.check_player_privs(player_name, "protection_bypass") then
  63. minetest.record_protection_violation(pos, player_name)
  64. return itemstack
  65. end
  66. local node_def = minetest.registered_nodes[minetest.get_node(pos).name]
  67. if not node_def or not node_def.buildable_to then
  68. return itemstack
  69. end
  70. local dir = placer and placer:get_look_dir() and
  71. minetest.dir_to_facedir(placer:get_look_dir()) or 0
  72. local botpos = vector.add(pos, minetest.facedir_to_dir(dir))
  73. if minetest.is_protected(botpos, player_name) and
  74. not minetest.check_player_privs(player_name, "protection_bypass") then
  75. minetest.record_protection_violation(botpos, player_name)
  76. return itemstack
  77. end
  78. local botdef = minetest.registered_nodes[minetest.get_node(botpos).name]
  79. if not botdef or not botdef.buildable_to then
  80. return itemstack
  81. end
  82. minetest.set_node(pos, {name = name .. "_bottom", param2 = dir})
  83. minetest.set_node(botpos, {name = name .. "_top", param2 = dir})
  84. if not minetest.is_creative_enabled(player_name) then
  85. itemstack:take_item()
  86. end
  87. return itemstack
  88. end,
  89. on_destruct = function(pos)
  90. destruct_bed(pos, 1)
  91. end,
  92. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  93. beds.on_rightclick(pos, clicker)
  94. return itemstack
  95. end,
  96. on_rotate = function(pos, node, user, _, new_param2)
  97. local dir = minetest.facedir_to_dir(node.param2)
  98. local p = vector.add(pos, dir)
  99. local node2 = minetest.get_node_or_nil(p)
  100. if not node2 or not minetest.get_item_group(node2.name, "bed") == 2 or
  101. not node.param2 == node2.param2 then
  102. return false
  103. end
  104. if minetest.is_protected(p, user:get_player_name()) then
  105. minetest.record_protection_violation(p, user:get_player_name())
  106. return false
  107. end
  108. if new_param2 % 32 > 3 then
  109. return false
  110. end
  111. local newp = vector.add(pos, minetest.facedir_to_dir(new_param2))
  112. local node3 = minetest.get_node_or_nil(newp)
  113. local node_def = node3 and minetest.registered_nodes[node3.name]
  114. if not node_def or not node_def.buildable_to then
  115. return false
  116. end
  117. if minetest.is_protected(newp, user:get_player_name()) then
  118. minetest.record_protection_violation(newp, user:get_player_name())
  119. return false
  120. end
  121. node.param2 = new_param2
  122. -- do not remove_node here - it will trigger destroy_bed()
  123. minetest.set_node(p, {name = "air"})
  124. minetest.set_node(pos, node)
  125. minetest.set_node(newp, {name = name .. "_top", param2 = new_param2})
  126. return true
  127. end,
  128. can_dig = function(pos, player)
  129. return beds.can_dig(pos)
  130. end,
  131. })
  132. minetest.register_node(name .. "_top", {
  133. drawtype = "nodebox",
  134. tiles = def.tiles.top,
  135. use_texture_alpha = "clip",
  136. paramtype = "light",
  137. paramtype2 = "facedir",
  138. is_ground_content = false,
  139. pointable = false,
  140. groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 3, bed = 2,
  141. not_in_creative_inventory = 1},
  142. sounds = def.sounds or default.node_sound_wood_defaults(),
  143. drop = name .. "_bottom",
  144. node_box = {
  145. type = "fixed",
  146. fixed = def.nodebox.top,
  147. },
  148. on_destruct = function(pos)
  149. destruct_bed(pos, 2)
  150. end,
  151. can_dig = function(pos, player)
  152. local node = minetest.get_node(pos)
  153. local dir = minetest.facedir_to_dir(node.param2)
  154. local p = vector.add(pos, dir)
  155. return beds.can_dig(p)
  156. end,
  157. })
  158. minetest.register_alias(name, name .. "_bottom")
  159. minetest.register_craft({
  160. output = name,
  161. recipe = def.recipe
  162. })
  163. end