api.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. 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 (creative and creative.is_enabled_for
  84. and creative.is_enabled_for(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. 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. sounds = def.sounds or default.node_sound_wood_defaults(),
  141. drop = name .. "_bottom",
  142. node_box = {
  143. type = "fixed",
  144. fixed = def.nodebox.top,
  145. },
  146. on_destruct = function(pos)
  147. destruct_bed(pos, 2)
  148. end,
  149. can_dig = function(pos, player)
  150. local node = minetest.get_node(pos)
  151. local dir = minetest.facedir_to_dir(node.param2)
  152. local p = vector.add(pos, dir)
  153. return beds.can_dig(p)
  154. end,
  155. })
  156. minetest.register_alias(name, name .. "_bottom")
  157. -- minetest.register_craft({
  158. -- output = name,
  159. -- recipe = def.recipe
  160. -- })
  161. end