init.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. -- Minetest 0.4 mod: bones
  2. -- See README.txt for licensing and other information.
  3. bones = {}
  4. local share_bones_time = tonumber(minetest.settings:get("share_bones_time") or 1200)
  5. local function is_owner(pos, name)
  6. local owner = minetest.get_meta(pos):get_string("owner")
  7. if owner == "" or owner == name then
  8. return true
  9. end
  10. return false
  11. end
  12. minetest.register_node("bones:bones", {
  13. description = "Bones",
  14. tiles = {
  15. "bones_top.png",
  16. "bones_bottom.png",
  17. "bones_side.png",
  18. "bones_side.png",
  19. "bones_rear.png",
  20. "bones_front.png"
  21. },
  22. paramtype2 = "facedir",
  23. groups = {crumbly=3},
  24. drop = "",
  25. sounds = default.node_sound_dirt_defaults({
  26. footstep = {name="default_gravel_footstep", gain=0.45},
  27. }),
  28. can_dig = function(pos, player)
  29. local inv = minetest.get_meta(pos):get_inventory()
  30. return (is_owner(pos, player:get_player_name()) and inv:is_empty("main")) or inv:is_empty("main")
  31. end,
  32. allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  33. if is_owner(pos, player:get_player_name()) then
  34. return count
  35. end
  36. return 0
  37. end,
  38. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  39. return 0
  40. end,
  41. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  42. if is_owner(pos, player:get_player_name()) then
  43. return stack:get_count()
  44. end
  45. return 0
  46. end,
  47. on_metadata_inventory_take = function(pos, listname, index, stack, player)
  48. local meta = minetest.get_meta(pos)
  49. if meta:get_string("owner") ~= "" and meta:get_inventory():is_empty("main") then
  50. meta:set_string("infotext", meta:get_string("owner").."'s old bones")
  51. meta:set_string("formspec", "")
  52. meta:set_string("owner", "")
  53. end
  54. end,
  55. on_punch = function(pos, node, player)
  56. local owner = minetest.get_meta(pos):get_string("owner")
  57. local infotext = minetest.get_meta(pos):get_string("infotext")
  58. local name = player:get_player_name()
  59. if infotext == "" then return end
  60. if owner ~= "" and owner ~= name then
  61. return
  62. end
  63. local inv = minetest.get_meta(pos):get_inventory()
  64. local player_inv = player:get_inventory()
  65. local has_space = true
  66. for i=1,inv:get_size("main") do
  67. local stk = inv:get_stack("main", i)
  68. if player_inv:room_for_item("main", stk) then
  69. inv:set_stack("main", i, nil)
  70. player_inv:add_item("main", stk)
  71. else
  72. has_space = false
  73. break
  74. end
  75. end
  76. -- Remove bones if player emptied them
  77. if has_space then
  78. minetest.remove_node(pos)
  79. end
  80. end,
  81. on_timer = function(pos, elapsed)
  82. local meta = minetest.get_meta(pos)
  83. local time = meta:get_int("time") + elapsed
  84. if time >= share_bones_time then
  85. meta:set_string("infotext", meta:get_string("owner").."'s old bones")
  86. meta:set_string("owner", "")
  87. else
  88. meta:set_int("time", time)
  89. return true
  90. end
  91. end,
  92. })
  93. local function may_replace(pos, player)
  94. local node_name = minetest.get_node(pos).name
  95. local node_definition = minetest.registered_nodes[node_name]
  96. -- if the node is unknown, we let the protection mod decide
  97. -- this is consistent with when a player could dig or not dig it
  98. -- unknown decoration would often be removed
  99. -- while unknown building materials in use would usually be left
  100. if not node_definition then
  101. -- only replace nodes that are not protected
  102. return not minetest.is_protected(pos, player:get_player_name())
  103. end
  104. -- allow replacing air and liquids
  105. if node_name == "air" or node_definition.liquidtype ~= "none" then
  106. return true
  107. end
  108. -- don't replace filled chests and other nodes that don't allow it
  109. local can_dig_func = node_definition.can_dig
  110. if can_dig_func and not can_dig_func(pos, player) then
  111. return false
  112. end
  113. -- default to each nodes buildable_to; if a placed block would replace it, why shouldn't bones?
  114. -- flowers being squished by bones are more realistical than a squished stone, too
  115. -- exception are of course any protected buildable_to
  116. return node_definition.buildable_to and not minetest.is_protected(pos, player:get_player_name())
  117. end
  118. function bones.place_bones(player)
  119. local pos = player:get_pos()
  120. pos.x = math.floor(pos.x+0.5)
  121. pos.y = math.floor(pos.y+0.5)
  122. pos.z = math.floor(pos.z+0.5)
  123. local param2 = minetest.dir_to_facedir(player:get_look_dir())
  124. local player_name = player:get_player_name()
  125. local player_inv = player:get_inventory()
  126. if (not may_replace(pos, player)) then
  127. if (may_replace({x=pos.x, y=pos.y+1, z=pos.z}, player)) then
  128. -- drop one node above if there's space
  129. -- this should solve most cases of protection related deaths in which players dig straight down
  130. -- yet keeps the bones reachable
  131. pos.y = pos.y+1
  132. else
  133. -- drop items instead of delete
  134. for i=1,player_inv:get_size("main") do
  135. minetest.add_item(pos, player_inv:get_stack("main", i))
  136. end
  137. for i=1,player_inv:get_size("craft") do
  138. minetest.add_item(pos, player_inv:get_stack("craft", i))
  139. end
  140. -- empty lists main and craft
  141. player_inv:set_list("main", {})
  142. player_inv:set_list("craft", {})
  143. return
  144. end
  145. end
  146. minetest.set_node(pos, {name="bones:bones", param2=param2})
  147. local meta = minetest.get_meta(pos)
  148. local inv = meta:get_inventory()
  149. inv:set_size("main", 8*4)
  150. local empty_list = inv:get_list("main")
  151. inv:set_list("main", player_inv:get_list("main"))
  152. player_inv:set_list("main", empty_list)
  153. for i=1,player_inv:get_size("craft") do
  154. inv:add_item("main", player_inv:get_stack("craft", i))
  155. player_inv:set_stack("craft", i, nil)
  156. end
  157. meta:set_string("formspec", "size[8,9;]"..
  158. "list[current_name;main;0,0;8,4;]"..
  159. "list[current_player;main;0,5;8,4;]")
  160. meta:set_string("owner", player_name)
  161. if share_bones_time ~= 0 then
  162. meta:set_string("infotext", player_name.."'s fresh bones")
  163. meta:set_int("time", 0)
  164. minetest.get_node_timer(pos):start(10)
  165. else
  166. meta:set_string("infotext", player_name.."'s bones")
  167. end
  168. end
  169. minetest.register_on_dieplayer(function(player)
  170. if minetest.settings:get_bool("creative_mode") or
  171. minetest.settings:get_bool("disable_bones") then
  172. return
  173. end
  174. bones.place_bones(player)
  175. end)