functions.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. --
  2. -- Sounds
  3. --
  4. function default.node_sound_defaults(table)
  5. table = table or {}
  6. table.footstep = table.footstep or
  7. {name = "", gain = 1.0}
  8. table.dug = table.dug or
  9. {name = "default_dug_node", gain = 0.25}
  10. table.place = table.place or
  11. {name = "default_place_node_hard", gain = 1.0}
  12. return table
  13. end
  14. function default.node_sound_stone_defaults(table)
  15. table = table or {}
  16. table.footstep = table.footstep or
  17. {name = "default_hard_footstep", gain = 0.3}
  18. table.dug = table.dug or
  19. {name = "default_hard_footstep", gain = 1.0}
  20. default.node_sound_defaults(table)
  21. return table
  22. end
  23. function default.node_sound_dirt_defaults(table)
  24. table = table or {}
  25. table.footstep = table.footstep or
  26. {name = "default_dirt_footstep", gain = 0.4}
  27. table.dug = table.dug or
  28. {name = "default_dirt_footstep", gain = 1.0}
  29. table.place = table.place or
  30. {name = "default_place_node", gain = 1.0}
  31. default.node_sound_defaults(table)
  32. return table
  33. end
  34. function default.node_sound_sand_defaults(table)
  35. table = table or {}
  36. table.footstep = table.footstep or
  37. {name = "default_sand_footstep", gain = 0.12}
  38. table.dug = table.dug or
  39. {name = "default_sand_footstep", gain = 0.24}
  40. table.place = table.place or
  41. {name = "default_place_node", gain = 1.0}
  42. default.node_sound_defaults(table)
  43. return table
  44. end
  45. function default.node_sound_gravel_defaults(table)
  46. table = table or {}
  47. table.footstep = table.footstep or
  48. {name = "default_gravel_footstep", gain = 0.4}
  49. table.dug = table.dug or
  50. {name = "default_gravel_footstep", gain = 1.0}
  51. table.place = table.place or
  52. {name = "default_place_node", gain = 1.0}
  53. default.node_sound_defaults(table)
  54. return table
  55. end
  56. function default.node_sound_wood_defaults(table)
  57. table = table or {}
  58. table.footstep = table.footstep or
  59. {name = "default_wood_footstep", gain = 0.3}
  60. table.dug = table.dug or
  61. {name = "default_wood_footstep", gain = 1.0}
  62. default.node_sound_defaults(table)
  63. return table
  64. end
  65. function default.node_sound_leaves_defaults(table)
  66. table = table or {}
  67. table.footstep = table.footstep or
  68. {name = "default_grass_footstep", gain = 0.45}
  69. table.dug = table.dug or
  70. {name = "default_grass_footstep", gain = 0.7}
  71. table.place = table.place or
  72. {name = "default_place_node", gain = 1.0}
  73. default.node_sound_defaults(table)
  74. return table
  75. end
  76. function default.node_sound_glass_defaults(table)
  77. table = table or {}
  78. table.footstep = table.footstep or
  79. {name = "default_glass_footstep", gain = 0.3}
  80. table.dig = table.dig or
  81. {name = "default_glass_footstep", gain = 0.5}
  82. table.dug = table.dug or
  83. {name = "default_break_glass", gain = 1.0}
  84. default.node_sound_defaults(table)
  85. return table
  86. end
  87. function default.node_sound_metal_defaults(table)
  88. table = table or {}
  89. table.footstep = table.footstep or
  90. {name = "default_metal_footstep", gain = 0.4}
  91. table.dig = table.dig or
  92. {name = "default_dig_metal", gain = 0.5}
  93. table.dug = table.dug or
  94. {name = "default_dug_metal", gain = 0.5}
  95. table.place = table.place or
  96. {name = "default_place_node_metal", gain = 0.5}
  97. default.node_sound_defaults(table)
  98. return table
  99. end
  100. function default.node_sound_water_defaults(table)
  101. table = table or {}
  102. table.footstep = table.footstep or
  103. {name = "default_water_footstep", gain = 0.2}
  104. default.node_sound_defaults(table)
  105. return table
  106. end
  107. function default.node_sound_snow_defaults(table)
  108. table = table or {}
  109. table.footstep = table.footstep or
  110. {name = "default_snow_footstep", gain = 0.2}
  111. table.dig = table.dig or
  112. {name = "default_snow_footstep", gain = 0.3}
  113. table.dug = table.dug or
  114. {name = "default_snow_footstep", gain = 0.3}
  115. table.place = table.place or
  116. {name = "default_place_node", gain = 1.0}
  117. default.node_sound_defaults(table)
  118. return table
  119. end
  120. --
  121. -- Lavacooling
  122. --
  123. default.cool_lava = function(pos, node)
  124. if node.name == "default:lava_source" then
  125. minetest.set_node(pos, {name = "default:obsidian"})
  126. else -- Lava flowing
  127. minetest.set_node(pos, {name = "default:stone"})
  128. end
  129. minetest.sound_play("default_cool_lava",
  130. {pos = pos, max_hear_distance = 16, gain = 0.25}, true)
  131. end
  132. if minetest.settings:get_bool("enable_lavacooling") ~= false then
  133. minetest.register_abm({
  134. label = "Lava cooling",
  135. nodenames = {"default:lava_source", "default:lava_flowing"},
  136. neighbors = {"group:cools_lava", "group:water"},
  137. interval = 2,
  138. chance = 2,
  139. catch_up = false,
  140. action = function(...)
  141. default.cool_lava(...)
  142. end,
  143. })
  144. end
  145. --
  146. -- Optimized helper to put all items in an inventory into a drops list
  147. --
  148. function default.get_inventory_drops(pos, inventory, drops)
  149. local inv = minetest.get_meta(pos):get_inventory()
  150. local n = #drops
  151. for i = 1, inv:get_size(inventory) do
  152. local stack = inv:get_stack(inventory, i)
  153. if stack:get_count() > 0 then
  154. drops[n+1] = stack:to_table()
  155. n = n + 1
  156. end
  157. end
  158. end
  159. --
  160. -- Reed and cactus growing
  161. --
  162. -- Wrapping the functions in ABM action is necessary to make overriding them possible
  163. function default.grow_cactus(pos, node)
  164. if node.param2 >= 4 then
  165. return
  166. end
  167. pos.y = pos.y - 1
  168. if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
  169. return
  170. end
  171. pos.y = pos.y + 1
  172. local height = 0
  173. while node.name == "default:cactus" and height < 4 do
  174. height = height + 1
  175. pos.y = pos.y + 1
  176. node = minetest.get_node(pos)
  177. end
  178. if height == 4 or node.name ~= "air" then
  179. return
  180. end
  181. if minetest.get_node_light(pos) < 13 then
  182. return
  183. end
  184. minetest.set_node(pos, {name = "default:cactus"})
  185. return true
  186. end
  187. function default.grow_reed(pos, node)
  188. pos.y = pos.y - 1
  189. local name = minetest.get_node(pos).name
  190. if name ~= "default:dirt" and
  191. name ~= "default:dirt_with_grass" and
  192. name ~= "default:dirt_with_dry_grass" and
  193. name ~= "default:dirt_with_rainforest_litter" and
  194. name ~= "default:dry_dirt" and
  195. name ~= "default:dry_dirt_with_dry_grass" then
  196. return
  197. end
  198. if not minetest.find_node_near(pos, 3, {"group:water"}) then
  199. return
  200. end
  201. pos.y = pos.y + 1
  202. local height = 0
  203. while node.name == "default:reed" and height < 4 do
  204. height = height + 1
  205. pos.y = pos.y + 1
  206. node = minetest.get_node(pos)
  207. end
  208. if height == 4 or node.name ~= "air" then
  209. return
  210. end
  211. if minetest.get_node_light(pos) < 13 then
  212. return
  213. end
  214. minetest.set_node(pos, {name = "default:reed"})
  215. return true
  216. end
  217. minetest.register_abm({
  218. label = "Grow cactus",
  219. nodenames = {"default:cactus"},
  220. neighbors = {"group:sand"},
  221. interval = 12,
  222. chance = 83,
  223. action = function(...)
  224. default.grow_cactus(...)
  225. end
  226. })
  227. minetest.register_abm({
  228. nodenames = {"default:reed"},
  229. label = "Grow papyrus",
  230. nodenames = {"default:papyrus"},
  231. -- Grows on the dirt and surface dirt nodes of the biomes papyrus appears in,
  232. -- including the old savanna nodes.
  233. -- 'default:dirt_with_grass' is here only because it was allowed before.
  234. neighbors = {
  235. "default:dirt",
  236. "default:dirt_with_grass",
  237. "default:dirt_with_dry_grass",
  238. "default:dirt_with_rainforest_litter",
  239. "default:dry_dirt",
  240. "default:dry_dirt_with_dry_grass",
  241. },
  242. interval = 14,
  243. chance = 71,
  244. action = function(...)
  245. default.grow_reed(...)
  246. end
  247. })
  248. --
  249. -- Dig upwards
  250. --
  251. function default.dig_up(pos, node, digger)
  252. if digger == nil then return end
  253. local np = {x = pos.x, y = pos.y + 1, z = pos.z}
  254. local nn = minetest.get_node(np)
  255. if nn.name == node.name then
  256. minetest.node_dig(np, nn, digger)
  257. end
  258. end
  259. --
  260. -- Fence registration helper
  261. --
  262. local fence_collision_extra = minetest.settings:get_bool("enable_fence_tall") and 3/8 or 0
  263. function default.register_fence(name, def)
  264. minetest.register_craft({
  265. output = name .. " 4",
  266. recipe = {
  267. { def.material, 'group:stick', def.material },
  268. { def.material, 'group:stick', def.material },
  269. }
  270. })
  271. local fence_texture = "default_fence_overlay.png^" .. def.texture ..
  272. "^default_fence_overlay.png^[makealpha:255,126,126"
  273. -- Allow almost everything to be overridden
  274. local default_fields = {
  275. paramtype = "light",
  276. drawtype = "nodebox",
  277. node_box = {
  278. type = "connected",
  279. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2, 1/8},
  280. -- connect_top =
  281. -- connect_bottom =
  282. connect_front = {{-1/16, 3/16, -1/2, 1/16, 5/16, -1/8 },
  283. {-1/16, -5/16, -1/2, 1/16, -3/16, -1/8 }},
  284. connect_left = {{-1/2, 3/16, -1/16, -1/8, 5/16, 1/16},
  285. {-1/2, -5/16, -1/16, -1/8, -3/16, 1/16}},
  286. connect_back = {{-1/16, 3/16, 1/8, 1/16, 5/16, 1/2 },
  287. {-1/16, -5/16, 1/8, 1/16, -3/16, 1/2 }},
  288. connect_right = {{ 1/8, 3/16, -1/16, 1/2, 5/16, 1/16},
  289. { 1/8, -5/16, -1/16, 1/2, -3/16, 1/16}}
  290. },
  291. collision_box = {
  292. type = "connected",
  293. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2 + fence_collision_extra, 1/8},
  294. -- connect_top =
  295. -- connect_bottom =
  296. connect_front = {-1/8, -1/2, -1/2, 1/8, 1/2 + fence_collision_extra, -1/8},
  297. connect_left = {-1/2, -1/2, -1/8, -1/8, 1/2 + fence_collision_extra, 1/8},
  298. connect_back = {-1/8, -1/2, 1/8, 1/8, 1/2 + fence_collision_extra, 1/2},
  299. connect_right = { 1/8, -1/2, -1/8, 1/2, 1/2 + fence_collision_extra, 1/8}
  300. },
  301. connects_to = {"group:fence", "group:wood", "group:tree", "group:wall", "group:stone", "group:cracky"},
  302. inventory_image = fence_texture,
  303. wield_image = fence_texture,
  304. tiles = {def.texture},
  305. sunlight_propagates = true,
  306. is_ground_content = false,
  307. groups = {},
  308. }
  309. for k, v in pairs(default_fields) do
  310. if def[k] == nil then
  311. def[k] = v
  312. end
  313. end
  314. -- Always add to the fence group, even if no group provided
  315. def.groups.fence = 1
  316. def.texture = nil
  317. def.material = nil
  318. minetest.register_node(name, def)
  319. end
  320. --
  321. -- Fence rail registration helper
  322. --
  323. function default.register_fence_rail(name, def)
  324. minetest.register_craft({
  325. output = name .. " 16",
  326. recipe = {
  327. { def.material, def.material },
  328. { "", ""},
  329. { def.material, def.material },
  330. }
  331. })
  332. local fence_rail_texture = "default_fence_rail_overlay.png^" .. def.texture ..
  333. "^default_fence_rail_overlay.png^[makealpha:255,126,126"
  334. -- Allow almost everything to be overridden
  335. local default_fields = {
  336. paramtype = "light",
  337. drawtype = "nodebox",
  338. node_box = {
  339. type = "connected",
  340. fixed = {{-1/16, 3/16, -1/16, 1/16, 5/16, 1/16},
  341. {-1/16, -3/16, -1/16, 1/16, -5/16, 1/16}},
  342. -- connect_top =
  343. -- connect_bottom =
  344. connect_front = {{-1/16, 3/16, -1/2, 1/16, 5/16, -1/16},
  345. {-1/16, -5/16, -1/2, 1/16, -3/16, -1/16}},
  346. connect_left = {{-1/2, 3/16, -1/16, -1/16, 5/16, 1/16},
  347. {-1/2, -5/16, -1/16, -1/16, -3/16, 1/16}},
  348. connect_back = {{-1/16, 3/16, 1/16, 1/16, 5/16, 1/2 },
  349. {-1/16, -5/16, 1/16, 1/16, -3/16, 1/2 }},
  350. connect_right = {{ 1/16, 3/16, -1/16, 1/2, 5/16, 1/16},
  351. { 1/16, -5/16, -1/16, 1/2, -3/16, 1/16}}
  352. },
  353. collision_box = {
  354. type = "connected",
  355. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2 + fence_collision_extra, 1/8},
  356. -- connect_top =
  357. -- connect_bottom =
  358. connect_front = {-1/8, -1/2, -1/2, 1/8, 1/2 + fence_collision_extra, -1/8},
  359. connect_left = {-1/2, -1/2, -1/8, -1/8, 1/2 + fence_collision_extra, 1/8},
  360. connect_back = {-1/8, -1/2, 1/8, 1/8, 1/2 + fence_collision_extra, 1/2},
  361. connect_right = { 1/8, -1/2, -1/8, 1/2, 1/2 + fence_collision_extra, 1/8}
  362. },
  363. connects_to = {"group:fence", "group:wall"},
  364. inventory_image = fence_rail_texture,
  365. wield_image = fence_rail_texture,
  366. tiles = {def.texture},
  367. sunlight_propagates = true,
  368. is_ground_content = false,
  369. groups = {},
  370. }
  371. for k, v in pairs(default_fields) do
  372. if def[k] == nil then
  373. def[k] = v
  374. end
  375. end
  376. -- Always add to the fence group, even if no group provided
  377. def.groups.fence = 1
  378. def.texture = nil
  379. def.material = nil
  380. minetest.register_node(name, def)
  381. end
  382. --
  383. -- Leafdecay
  384. --
  385. -- Prevent decay of placed leaves
  386. default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
  387. if placer and placer:is_player() then
  388. local node = minetest.get_node(pos)
  389. node.param2 = 1
  390. minetest.set_node(pos, node)
  391. end
  392. end
  393. -- Leafdecay
  394. local function leafdecay_after_destruct(pos, oldnode, def)
  395. for _, v in pairs(minetest.find_nodes_in_area(vector.subtract(pos, def.radius),
  396. vector.add(pos, def.radius), def.leaves)) do
  397. local node = minetest.get_node(v)
  398. local timer = minetest.get_node_timer(v)
  399. if node.param2 ~= 1 and not timer:is_started() then
  400. timer:start(math.random(20, 120) / 10)
  401. end
  402. end
  403. end
  404. local movement_gravity = tonumber(
  405. minetest.settings:get("movement_gravity")) or 9.81
  406. local function leafdecay_on_timer(pos, def)
  407. if minetest.find_node_near(pos, def.radius, def.trunks) then
  408. return false
  409. end
  410. local node = minetest.get_node(pos)
  411. local drops = minetest.get_node_drops(node.name)
  412. for _, item in ipairs(drops) do
  413. local is_leaf
  414. for _, v in pairs(def.leaves) do
  415. if v == item then
  416. is_leaf = true
  417. end
  418. end
  419. if minetest.get_item_group(item, "leafdecay_drop") ~= 0 or
  420. not is_leaf then
  421. minetest.add_item({
  422. x = pos.x - 0.5 + math.random(),
  423. y = pos.y - 0.5 + math.random(),
  424. z = pos.z - 0.5 + math.random(),
  425. }, item)
  426. end
  427. end
  428. minetest.remove_node(pos)
  429. minetest.check_for_falling(pos)
  430. -- spawn a few particles for the removed node
  431. minetest.add_particlespawner({
  432. amount = 8,
  433. time = 0.001,
  434. minpos = vector.subtract(pos, {x=0.5, y=0.5, z=0.5}),
  435. maxpos = vector.add(pos, {x=0.5, y=0.5, z=0.5}),
  436. minvel = vector.new(-0.5, -1, -0.5),
  437. maxvel = vector.new(0.5, 0, 0.5),
  438. minacc = vector.new(0, -movement_gravity, 0),
  439. maxacc = vector.new(0, -movement_gravity, 0),
  440. minsize = 0,
  441. maxsize = 0,
  442. node = node,
  443. })
  444. end
  445. function default.register_leafdecay(def)
  446. assert(def.leaves)
  447. assert(def.trunks)
  448. assert(def.radius)
  449. for _, v in pairs(def.trunks) do
  450. minetest.override_item(v, {
  451. after_destruct = function(pos, oldnode)
  452. leafdecay_after_destruct(pos, oldnode, def)
  453. end,
  454. })
  455. end
  456. for _, v in pairs(def.leaves) do
  457. minetest.override_item(v, {
  458. on_timer = function(pos)
  459. leafdecay_on_timer(pos, def)
  460. end,
  461. })
  462. end
  463. end
  464. --
  465. -- Convert default:dirt to something that fits the environment
  466. --
  467. minetest.register_abm({
  468. label = "Grass spread",
  469. nodenames = {"default:dirt"},
  470. neighbors = {
  471. "air",
  472. "group:grass",
  473. "group:dry_grass",
  474. "default:snow",
  475. },
  476. interval = 6,
  477. chance = 50,
  478. catch_up = false,
  479. action = function(pos, node)
  480. -- Check for darkness: night, shadow or under a light-blocking node
  481. -- Returns if ignore above
  482. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  483. if (minetest.get_node_light(above) or 0) < 13 then
  484. return
  485. end
  486. -- Look for spreading dirt-type neighbours
  487. local p2 = minetest.find_node_near(pos, 1, "group:spreading_dirt_type")
  488. if p2 then
  489. local n3 = minetest.get_node(p2)
  490. minetest.set_node(pos, {name = n3.name})
  491. return
  492. end
  493. -- Else, any seeding nodes on top?
  494. local name = minetest.get_node(above).name
  495. -- Snow check is cheapest, so comes first
  496. if name == "default:snow" then
  497. minetest.set_node(pos, {name = "default:dirt_with_snow"})
  498. elseif minetest.get_item_group(name, "grass") ~= 0 then
  499. minetest.set_node(pos, {name = "default:dirt_with_grass"})
  500. elseif minetest.get_item_group(name, "dry_grass") ~= 0 then
  501. minetest.set_node(pos, {name = "default:dirt_with_dry_grass"})
  502. end
  503. end
  504. })
  505. --
  506. -- Grass and dry grass removed in darkness
  507. --
  508. minetest.register_abm({
  509. label = "Grass covered",
  510. nodenames = {"group:spreading_dirt_type", "default:dry_dirt_with_dry_grass"},
  511. interval = 8,
  512. chance = 50,
  513. catch_up = false,
  514. action = function(pos, node)
  515. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  516. local name = minetest.get_node(above).name
  517. local nodedef = minetest.registered_nodes[name]
  518. if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
  519. nodedef.paramtype == "light") and
  520. nodedef.liquidtype == "none") then
  521. if node.name == "default:dry_dirt_with_dry_grass" then
  522. minetest.set_node(pos, {name = "default:dry_dirt"})
  523. else
  524. minetest.set_node(pos, {name = "default:dirt"})
  525. end
  526. end
  527. end
  528. })
  529. --
  530. -- Moss growth on cobble near water
  531. --
  532. local moss_correspondences = {
  533. ["default:cobble"] = "default:mossycobble",
  534. ["stairs:slab_cobble"] = "stairs:slab_mossycobble",
  535. ["stairs:stair_cobble"] = "stairs:stair_mossycobble",
  536. ["stairs:stair_inner_cobble"] = "stairs:stair_inner_mossycobble",
  537. ["stairs:stair_outer_cobble"] = "stairs:stair_outer_mossycobble",
  538. ["walls:cobble"] = "walls:mossycobble",
  539. }
  540. minetest.register_abm({
  541. label = "Moss growth",
  542. nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble",
  543. "stairs:stair_inner_cobble", "stairs:stair_outer_cobble",
  544. "walls:cobble"},
  545. neighbors = {"group:water"},
  546. interval = 16,
  547. chance = 200,
  548. catch_up = false,
  549. action = function(pos, node)
  550. node.name = moss_correspondences[node.name]
  551. if node.name then
  552. minetest.set_node(pos, node)
  553. end
  554. end
  555. })
  556. --
  557. -- Register a craft to copy the metadata of items
  558. --
  559. function default.register_craft_metadata_copy(ingredient, result)
  560. minetest.register_craft({
  561. type = "shapeless",
  562. output = result,
  563. recipe = {ingredient, result}
  564. })
  565. minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
  566. if itemstack:get_name() ~= result then
  567. return
  568. end
  569. local original
  570. local index
  571. for i = 1, #old_craft_grid do
  572. if old_craft_grid[i]:get_name() == result then
  573. original = old_craft_grid[i]
  574. index = i
  575. end
  576. end
  577. if not original then
  578. return
  579. end
  580. local copymeta = original:get_meta():to_table()
  581. itemstack:get_meta():from_table(copymeta)
  582. -- put the book with metadata back in the craft grid
  583. craft_inv:set_stack("craft", index, original)
  584. end)
  585. end
  586. --
  587. -- NOTICE: This method is not an official part of the API yet.
  588. -- This method may change in future.
  589. --
  590. function default.can_interact_with_node(player, pos)
  591. if player and player:is_player() then
  592. if minetest.check_player_privs(player, "protection_bypass") then
  593. return true
  594. end
  595. else
  596. return false
  597. end
  598. local meta = minetest.get_meta(pos)
  599. local owner = meta:get_string("owner")
  600. if not owner or owner == "" or owner == player:get_player_name() then
  601. return true
  602. end
  603. -- Is player wielding the right key?
  604. local item = player:get_wielded_item()
  605. if minetest.get_item_group(item:get_name(), "key") == 1 then
  606. local key_meta = item:get_meta()
  607. if key_meta:get_string("secret") == "" then
  608. local key_oldmeta = item:get_metadata()
  609. if key_oldmeta == "" or not minetest.parse_json(key_oldmeta) then
  610. return false
  611. end
  612. key_meta:set_string("secret", minetest.parse_json(key_oldmeta).secret)
  613. item:set_metadata("")
  614. end
  615. return meta:get_string("key_lock_secret") == key_meta:get_string("secret")
  616. end
  617. return false
  618. end