api.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. -- Localize for performance.
  2. local vector_round = vector.round
  3. local reverse = true
  4. local function destruct_bed(pos, n)
  5. local node = minetest.get_node(pos)
  6. local other
  7. if n == 2 then
  8. local dir = minetest.facedir_to_dir(node.param2)
  9. other = vector.subtract(pos, dir)
  10. elseif n == 1 then
  11. local meta = minetest.get_meta(pos)
  12. local owner = meta:get_string("owner") or ""
  13. if owner ~= "" and owner ~= "server" then
  14. if beds.spawn[owner] then
  15. local p1 = vector_round(pos)
  16. local p2 = vector_round(beds.spawn[owner])
  17. if vector.equals(p1, p2) then
  18. beds.spawn[owner] = nil
  19. beds.save_spawns()
  20. beds.storage:set_int(owner .. ":count", 0)
  21. chat_core.alert_player_sound(owner)
  22. local RED = core.get_color_escape_sequence("#ff0000")
  23. minetest.chat_send_player(owner, RED .. "# Server: Your primary bed is destroyed. Your home position is LOST!")
  24. else
  25. minetest.chat_send_player(owner, "# Server: A secondary bed owned by you was destroyed.")
  26. end
  27. else
  28. minetest.chat_send_player(owner, "# Server: A bed owned by you was destroyed, but you had no home position set.")
  29. end
  30. end
  31. local dir = minetest.facedir_to_dir(node.param2)
  32. other = vector.add(pos, dir)
  33. end
  34. if reverse then
  35. reverse = not reverse
  36. minetest.remove_node(other)
  37. minetest.check_for_falling(other)
  38. else
  39. reverse = not reverse
  40. end
  41. end
  42. function beds.register_bed(name, def)
  43. minetest.register_node(name .. "_bottom", {
  44. description = def.description,
  45. inventory_image = def.inventory_image,
  46. wield_image = def.wield_image,
  47. drawtype = "nodebox",
  48. tiles = def.tiles.bottom,
  49. paramtype = "light",
  50. paramtype2 = "facedir",
  51. is_ground_content = false,
  52. stack_max = 1,
  53. groups = utility.dig_groups("furniture", {flammable = 3, bed = 1, immovable = 1}),
  54. sounds = def.sounds or default.node_sound_wood_defaults(),
  55. node_box = {
  56. type = "fixed",
  57. fixed = def.nodebox.bottom,
  58. },
  59. selection_box = {
  60. type = "fixed",
  61. fixed = def.selectionbox,
  62. },
  63. on_place = function(itemstack, placer, pointed_thing)
  64. if not placer or not placer:is_player() then
  65. return itemstack
  66. end
  67. local under = pointed_thing.under
  68. local node = minetest.get_node(under)
  69. local udef = minetest.reg_ns_nodes[node.name]
  70. if udef and udef.on_rightclick and
  71. not (placer and placer:get_player_control().sneak) then
  72. return udef.on_rightclick(under, node, placer, itemstack,
  73. pointed_thing) or itemstack
  74. end
  75. local pos
  76. if minetest.registered_items[minetest.get_node(under).name].buildable_to then
  77. pos = under
  78. else
  79. pos = pointed_thing.above
  80. end
  81. if minetest.is_protected(pos, placer:get_player_name()) then
  82. minetest.record_protection_violation(pos, placer:get_player_name())
  83. return itemstack
  84. end
  85. local node_def = minetest.reg_ns_nodes[
  86. minetest.get_node(pos).name]
  87. if not node_def or not node_def.buildable_to then
  88. return itemstack
  89. end
  90. local dir = minetest.dir_to_facedir(placer:get_look_dir())
  91. local botpos = vector.add(pos, minetest.facedir_to_dir(dir))
  92. if minetest.is_protected(botpos, placer:get_player_name()) then
  93. minetest.record_protection_violation(botpos, placer:get_player_name())
  94. return itemstack
  95. end
  96. local botdef = minetest.reg_ns_nodes[
  97. minetest.get_node(botpos).name]
  98. if not botdef or not botdef.buildable_to then
  99. return itemstack
  100. end
  101. minetest.add_node(pos, {name = name .. "_bottom", param2 = dir})
  102. minetest.add_node(botpos, {name = name .. "_top", param2 = dir})
  103. -- If player is holding 'E', then bed shall be public.
  104. do
  105. local control = placer:get_player_control()
  106. if control.aux1 then
  107. local meta = minetest.get_meta(pos)
  108. meta:set_string("owner", "server") -- "server" is a reserved name.
  109. meta:mark_as_private("owner")
  110. meta:set_string("infotext", "Public Bed")
  111. end
  112. end
  113. if not minetest.setting_getbool("creative_mode") then
  114. itemstack:take_item()
  115. end
  116. return itemstack
  117. end,
  118. on_destruct = function(pos)
  119. destruct_bed(pos, 1)
  120. end,
  121. -- TNT+beds=problems.
  122. on_blast = function(pos) end,
  123. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  124. beds.on_rightclick(pos, clicker)
  125. return itemstack
  126. end,
  127. on_rotate = function(pos, node, user, mode, new_param2)
  128. local dir = minetest.facedir_to_dir(node.param2)
  129. local p = vector.add(pos, dir)
  130. local node2 = minetest.get_node_or_nil(p)
  131. if not node2 or not minetest.get_item_group(node2.name, "bed") == 2 or
  132. not node.param2 == node2.param2 then
  133. return false
  134. end
  135. if minetest.is_protected(p, user:get_player_name()) then
  136. minetest.record_protection_violation(p, user:get_player_name())
  137. return false
  138. end
  139. if mode ~= screwdriver.ROTATE_FACE then
  140. return false
  141. end
  142. local newp = vector.add(pos, minetest.facedir_to_dir(new_param2))
  143. local node3 = minetest.get_node_or_nil(newp)
  144. local node_def = node3 and minetest.reg_ns_nodes[node3.name]
  145. if not node_def or not node_def.buildable_to then
  146. return false
  147. end
  148. if minetest.is_protected(newp, user:get_player_name()) then
  149. minetest.record_protection_violation(newp, user:get_player_name())
  150. return false
  151. end
  152. node.param2 = new_param2
  153. -- do not remove_node here - it will trigger destroy_bed()
  154. minetest.swap_node(p, {name = "air"})
  155. minetest.swap_node(pos, node) -- Do not remove meta.
  156. minetest.add_node(newp, {name = name .. "_top", param2 = new_param2})
  157. return true
  158. end,
  159. -- Called by rename LBM.
  160. _on_rename_check = function(pos)
  161. local meta = minetest.get_meta(pos)
  162. local owner = meta:get_string("owner")
  163. -- Nobody placed this block.
  164. if owner == "" or owner == "server" then
  165. return
  166. end
  167. local dname = rename.gpn(owner)
  168. meta:set_string("rename", dname)
  169. meta:mark_as_private("rename")
  170. meta:set_string("infotext", "Bed (Owned by <" .. dname .. ">!)")
  171. end,
  172. })
  173. minetest.register_node(name .. "_top", {
  174. drawtype = "nodebox",
  175. tiles = def.tiles.top,
  176. paramtype = "light",
  177. paramtype2 = "facedir",
  178. is_ground_content = false,
  179. pointable = false,
  180. groups = utility.dig_groups("furniture", {oddly_breakable_by_hand = 1, flammable = 3, bed = 2, immovable = 1}),
  181. sounds = def.sounds or default.node_sound_wood_defaults(),
  182. drop = name .. "_bottom",
  183. node_box = {
  184. type = "fixed",
  185. fixed = def.nodebox.top,
  186. },
  187. on_destruct = function(pos)
  188. destruct_bed(pos, 2)
  189. end,
  190. -- TNT+beds=problems.
  191. on_blast = function(pos) end,
  192. })
  193. minetest.register_alias(name, name .. "_bottom")
  194. minetest.register_craft({
  195. output = name,
  196. recipe = def.recipe
  197. })
  198. end