api.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. stack_max = 1,
  27. tiles = def.tiles.bottom,
  28. paramtype = "light",
  29. paramtype2 = "facedir",
  30. is_ground_content = false,
  31. stack_max = 1,
  32. groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 3, bed = 1},
  33. sounds = def.sounds or default.node_sound_wood_defaults(),
  34. node_box = {
  35. type = "fixed",
  36. fixed = def.nodebox.bottom,
  37. },
  38. selection_box = {
  39. type = "fixed",
  40. fixed = def.selectionbox,
  41. },
  42. on_place = function(itemstack, placer, pointed_thing)
  43. local under = pointed_thing.under
  44. local node = minetest.get_node(under)
  45. local udef = minetest.registered_nodes[node.name]
  46. if udef and udef.on_rightclick and
  47. not (placer and placer:is_player() and
  48. placer:get_player_control().sneak) then
  49. return udef.on_rightclick(under, node, placer, itemstack,
  50. pointed_thing) or itemstack
  51. end
  52. local pos
  53. if udef and udef.buildable_to then
  54. pos = under
  55. else
  56. pos = pointed_thing.above
  57. end
  58. local player_name = placer and placer:get_player_name() or ""
  59. if minetest.is_protected(pos, player_name) and
  60. not minetest.check_player_privs(player_name, "protection_bypass") then
  61. minetest.record_protection_violation(pos, player_name)
  62. return itemstack
  63. end
  64. local node_def = minetest.registered_nodes[minetest.get_node(pos).name]
  65. if not node_def or not node_def.buildable_to then
  66. return itemstack
  67. end
  68. local dir = placer and placer:get_look_dir() and
  69. minetest.dir_to_facedir(placer:get_look_dir()) or 0
  70. local botpos = vector.add(pos, minetest.facedir_to_dir(dir))
  71. if minetest.is_protected(botpos, player_name) and
  72. not minetest.check_player_privs(player_name, "protection_bypass") then
  73. minetest.record_protection_violation(botpos, player_name)
  74. return itemstack
  75. end
  76. local botdef = minetest.registered_nodes[minetest.get_node(botpos).name]
  77. if not botdef or not botdef.buildable_to then
  78. return itemstack
  79. end
  80. minetest.set_node(pos, {name = name .. "_bottom", param2 = dir})
  81. minetest.set_node(botpos, {name = name .. "_top", param2 = dir})
  82. if not (creative and creative.is_enabled_for
  83. and creative.is_enabled_for(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, mode, 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 not minetest.get_item_group(node2.name, "bed") == 2 or
  100. not 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 mode ~= screwdriver.ROTATE_FACE 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. })
  128. minetest.register_node(name .. "_top", {
  129. drawtype = "nodebox",
  130. tiles = def.tiles.top,
  131. paramtype = "light",
  132. paramtype2 = "facedir",
  133. is_ground_content = false,
  134. pointable = false,
  135. groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 3, bed = 2},
  136. sounds = def.sounds or default.node_sound_wood_defaults(),
  137. drop = name .. "_bottom",
  138. node_box = {
  139. type = "fixed",
  140. fixed = def.nodebox.top,
  141. },
  142. on_destruct = function(pos)
  143. destruct_bed(pos, 2)
  144. end,
  145. })
  146. minetest.register_alias(name, name .. "_bottom")
  147. minetest.register_craft({
  148. output = name,
  149. recipe = def.recipe
  150. })
  151. end