api.lua 5.3 KB

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