api.lua 5.1 KB

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