api.lua 6.7 KB

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