chests.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. default.chest = {}
  2. function default.chest.get_chest_formspec(pos)
  3. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  4. local formspec =
  5. "size[8,9]" ..
  6. "list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
  7. "list[current_player;main;0,4.85;8,1;]" ..
  8. "list[current_player;main;0,6.08;8,3;8]" ..
  9. "listring[nodemeta:" .. spos .. ";main]" ..
  10. "listring[current_player;main]" ..
  11. default.get_hotbar_bg(0,4.85)
  12. return formspec
  13. end
  14. function default.chest.chest_lid_obstructed(pos)
  15. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  16. local def = minetest.registered_nodes[minetest.get_node(above).name]
  17. -- allow ladders, signs, wallmounted things and torches to not obstruct
  18. if def and
  19. (def.drawtype == "airlike" or
  20. def.drawtype == "signlike" or
  21. def.drawtype == "torchlike" or
  22. (def.drawtype == "nodebox" and def.paramtype2 == "wallmounted")) then
  23. return false
  24. end
  25. return true
  26. end
  27. function default.chest.chest_lid_close(pn)
  28. local chest_open_info = default.chest.open_chests[pn]
  29. local pos = chest_open_info.pos
  30. local sound = chest_open_info.sound
  31. local swap = chest_open_info.swap
  32. default.chest.open_chests[pn] = nil
  33. for k, v in pairs(default.chest.open_chests) do
  34. if v.pos.x == pos.x and v.pos.y == pos.y and v.pos.z == pos.z then
  35. return true
  36. end
  37. end
  38. local node = minetest.get_node(pos)
  39. minetest.after(0.2, minetest.swap_node, pos, { name = "default:" .. swap,
  40. param2 = node.param2 })
  41. minetest.sound_play(sound, {gain = 0.3, pos = pos, max_hear_distance = 10})
  42. end
  43. default.chest.open_chests = {}
  44. minetest.register_on_player_receive_fields(function(player, formname, fields)
  45. if formname ~= "default:chest" then
  46. return
  47. end
  48. if not player or not fields.quit then
  49. return
  50. end
  51. local pn = player:get_player_name()
  52. if not default.chest.open_chests[pn] then
  53. return
  54. end
  55. default.chest.chest_lid_close(pn)
  56. return true
  57. end)
  58. minetest.register_on_leaveplayer(function(player)
  59. local pn = player:get_player_name()
  60. if default.chest.open_chests[pn] then
  61. default.chest.chest_lid_close(pn)
  62. end
  63. end)
  64. function default.chest.register_chest(name, d)
  65. local def = table.copy(d)
  66. def.drawtype = "mesh"
  67. def.visual = "mesh"
  68. def.paramtype = "light"
  69. def.paramtype2 = "facedir"
  70. def.legacy_facedir_simple = true
  71. def.is_ground_content = false
  72. if def.protected then
  73. def.on_construct = function(pos)
  74. local meta = minetest.get_meta(pos)
  75. meta:set_string("infotext", "Locked Chest")
  76. meta:set_string("owner", "")
  77. local inv = meta:get_inventory()
  78. inv:set_size("main", 8*4)
  79. end
  80. def.after_place_node = function(pos, placer)
  81. local meta = minetest.get_meta(pos)
  82. meta:set_string("owner", placer:get_player_name() or "")
  83. meta:set_string("infotext", "Locked Chest (owned by " ..
  84. meta:get_string("owner") .. ")")
  85. end
  86. def.can_dig = function(pos,player)
  87. local meta = minetest.get_meta(pos);
  88. local inv = meta:get_inventory()
  89. return inv:is_empty("main") and
  90. default.can_interact_with_node(player, pos)
  91. end
  92. def.allow_metadata_inventory_move = function(pos, from_list, from_index,
  93. to_list, to_index, count, player)
  94. if not default.can_interact_with_node(player, pos) then
  95. return 0
  96. end
  97. return count
  98. end
  99. def.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  100. if not default.can_interact_with_node(player, pos) then
  101. return 0
  102. end
  103. return stack:get_count()
  104. end
  105. def.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  106. if not default.can_interact_with_node(player, pos) then
  107. return 0
  108. end
  109. return stack:get_count()
  110. end
  111. def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  112. if not default.can_interact_with_node(clicker, pos) then
  113. return itemstack
  114. end
  115. minetest.sound_play(def.sound_open, {gain = 0.3,
  116. pos = pos, max_hear_distance = 10})
  117. if not default.chest.chest_lid_obstructed(pos) then
  118. minetest.swap_node(pos,
  119. { name = "default:" .. name .. "_open",
  120. param2 = node.param2 })
  121. end
  122. minetest.after(0.2, minetest.show_formspec,
  123. clicker:get_player_name(),
  124. "default:chest", default.chest.get_chest_formspec(pos))
  125. default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
  126. sound = def.sound_close, swap = name }
  127. end
  128. def.on_blast = function() end
  129. def.on_key_use = function(pos, player)
  130. local secret = minetest.get_meta(pos):get_string("key_lock_secret")
  131. local itemstack = player:get_wielded_item()
  132. local key_meta = itemstack:get_meta()
  133. if itemstack:get_metadata() == "" then
  134. return
  135. end
  136. if key_meta:get_string("secret") == "" then
  137. key_meta:set_string("secret", minetest.parse_json(itemstack:get_metadata()).secret)
  138. itemstack:set_metadata("")
  139. end
  140. if secret ~= key_meta:get_string("secret") then
  141. return
  142. end
  143. minetest.show_formspec(
  144. player:get_player_name(),
  145. "default:chest_locked",
  146. default.chest.get_chest_formspec(pos)
  147. )
  148. end
  149. def.on_skeleton_key_use = function(pos, player, newsecret)
  150. local meta = minetest.get_meta(pos)
  151. local owner = meta:get_string("owner")
  152. local pn = player:get_player_name()
  153. -- verify placer is owner of lockable chest
  154. if owner ~= pn then
  155. minetest.record_protection_violation(pos, pn)
  156. minetest.chat_send_player(pn, "You do not own this chest.")
  157. return nil
  158. end
  159. local secret = meta:get_string("key_lock_secret")
  160. if secret == "" then
  161. secret = newsecret
  162. meta:set_string("key_lock_secret", secret)
  163. end
  164. return secret, "a locked chest", owner
  165. end
  166. else
  167. def.on_construct = function(pos)
  168. local meta = minetest.get_meta(pos)
  169. meta:set_string("infotext", "Chest")
  170. local inv = meta:get_inventory()
  171. inv:set_size("main", 8*4)
  172. end
  173. def.can_dig = function(pos,player)
  174. local meta = minetest.get_meta(pos);
  175. local inv = meta:get_inventory()
  176. return inv:is_empty("main")
  177. end
  178. def.on_rightclick = function(pos, node, clicker)
  179. minetest.sound_play(def.sound_open, {gain = 0.3, pos = pos,
  180. max_hear_distance = 10})
  181. if not default.chest.chest_lid_obstructed(pos) then
  182. minetest.swap_node(pos, {
  183. name = "default:" .. name .. "_open",
  184. param2 = node.param2 })
  185. end
  186. minetest.after(0.2, minetest.show_formspec,
  187. clicker:get_player_name(),
  188. "default:chest", default.chest.get_chest_formspec(pos))
  189. default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
  190. sound = def.sound_close, swap = name }
  191. end
  192. def.on_blast = function(pos)
  193. local drops = {}
  194. default.get_inventory_drops(pos, "main", drops)
  195. drops[#drops+1] = "default:" .. name
  196. minetest.remove_node(pos)
  197. return drops
  198. end
  199. end
  200. def.on_metadata_inventory_move = function(pos, from_list, from_index,
  201. to_list, to_index, count, player)
  202. minetest.log("action", player:get_player_name() ..
  203. " moves stuff in chest at " .. minetest.pos_to_string(pos))
  204. end
  205. def.on_metadata_inventory_put = function(pos, listname, index, stack, player)
  206. minetest.log("action", player:get_player_name() ..
  207. " moves " .. stack:get_name() ..
  208. " to chest at " .. minetest.pos_to_string(pos))
  209. end
  210. def.on_metadata_inventory_take = function(pos, listname, index, stack, player)
  211. minetest.log("action", player:get_player_name() ..
  212. " takes " .. stack:get_name() ..
  213. " from chest at " .. minetest.pos_to_string(pos))
  214. end
  215. local def_opened = table.copy(def)
  216. local def_closed = table.copy(def)
  217. def_opened.mesh = "chest_open.obj"
  218. for i = 1, #def_opened.tiles do
  219. if type(def_opened.tiles[i]) == "string" then
  220. def_opened.tiles[i] = {name = def_opened.tiles[i], backface_culling = true}
  221. elseif def_opened.tiles[i].backface_culling == nil then
  222. def_opened.tiles[i].backface_culling = true
  223. end
  224. end
  225. def_opened.drop = "default:" .. name
  226. def_opened.groups.not_in_creative_inventory = 1
  227. def_opened.selection_box = {
  228. type = "fixed",
  229. fixed = { -1/2, -1/2, -1/2, 1/2, 3/16, 1/2 },
  230. }
  231. def_opened.can_dig = function()
  232. return false
  233. end
  234. def_opened.on_blast = function() end
  235. def_closed.mesh = nil
  236. def_closed.drawtype = nil
  237. def_closed.tiles[6] = def.tiles[5] -- swap textures around for "normal"
  238. def_closed.tiles[5] = def.tiles[3] -- drawtype to make them match the mesh
  239. def_closed.tiles[3] = def.tiles[3].."^[transformFX"
  240. minetest.register_node("default:" .. name, def_closed)
  241. minetest.register_node("default:" .. name .. "_open", def_opened)
  242. -- convert old chests to this new variant
  243. minetest.register_lbm({
  244. label = "update chests to opening chests",
  245. name = "default:upgrade_" .. name .. "_v2",
  246. nodenames = {"default:" .. name},
  247. action = function(pos, node)
  248. local meta = minetest.get_meta(pos)
  249. meta:set_string("formspec", nil)
  250. local inv = meta:get_inventory()
  251. local list = inv:get_list("default:chest")
  252. if list then
  253. inv:set_size("main", 8*4)
  254. inv:set_list("main", list)
  255. inv:set_list("default:chest", nil)
  256. end
  257. end
  258. })
  259. end
  260. default.chest.register_chest("chest", {
  261. description = "Chest",
  262. tiles = {
  263. "default_chest_top.png",
  264. "default_chest_top.png",
  265. "default_chest_side.png",
  266. "default_chest_side.png",
  267. "default_chest_front.png",
  268. "default_chest_inside.png"
  269. },
  270. sounds = default.node_sound_wood_defaults(),
  271. sound_open = "default_chest_open",
  272. sound_close = "default_chest_close",
  273. groups = {choppy = 2, oddly_breakable_by_hand = 2},
  274. })
  275. default.chest.register_chest("chest_locked", {
  276. description = "Locked Chest",
  277. tiles = {
  278. "default_chest_top.png",
  279. "default_chest_top.png",
  280. "default_chest_side.png",
  281. "default_chest_side.png",
  282. "default_chest_lock.png",
  283. "default_chest_inside.png"
  284. },
  285. sounds = default.node_sound_wood_defaults(),
  286. sound_open = "default_chest_open",
  287. sound_close = "default_chest_close",
  288. groups = {choppy = 2, oddly_breakable_by_hand = 2},
  289. protected = true,
  290. })