init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. chest_api = chest_api or {}
  2. chest_api.modpath = minetest.get_modpath("chest_api")
  3. dofile(chest_api.modpath .. "/functions.lua")
  4. if not chest_api.run_once then
  5. -- Guard against players leaving the game while a chest is open.
  6. minetest.register_on_leaveplayer(function(...)
  7. return chest_api.on_leaveplayer(...)
  8. end)
  9. -- Or if they die while holding a chest open.
  10. minetest.register_on_dieplayer(function(...)
  11. return chest_api.on_dieplayer(...)
  12. end)
  13. minetest.register_on_player_receive_fields(function(...)
  14. return chest_api.on_player_receive_fields(...)
  15. end)
  16. -- Chest registration function.
  17. function chest_api.register_chest(name, def)
  18. def.drawtype = "mesh"
  19. def.visual = "mesh"
  20. def.paramtype = "light"
  21. def.paramtype2 = "facedir"
  22. def.legacy_facedir_simple = true
  23. def.is_ground_content = false
  24. def._chest_basename = name
  25. def.groups = def.groups or {}
  26. -- Now that node metadata is preserved, we don't need this anymore.
  27. --def.groups.immovable = 1
  28. def.groups.chest_node = 1
  29. local protected = def.protected
  30. if def.protected then
  31. def.on_construct = function(...)
  32. return chest_api.protected_on_construct(...)
  33. end
  34. local original_after_place_node = def.after_place_node
  35. def.after_place_node = function(...)
  36. chest_api.protected_after_place_node(...)
  37. -- Allow registrations to include their own "after_place_node" functionality.
  38. if original_after_place_node then
  39. return original_after_place_node(...)
  40. end
  41. end
  42. def.can_dig = function(...)
  43. return chest_api.protected_can_dig(...)
  44. end
  45. def.allow_metadata_inventory_move = function(...)
  46. return chest_api.protected_allow_metadata_inventory_move(...)
  47. end
  48. def.allow_metadata_inventory_put = function(...)
  49. return chest_api.protected_allow_metadata_inventory_put(...)
  50. end
  51. def.allow_metadata_inventory_take = function(...)
  52. return chest_api.protected_allow_metadata_inventory_take(...)
  53. end
  54. def.on_rightclick = function(...)
  55. return chest_api.protected_on_rightclick(...)
  56. end
  57. -- Another way to open a locked chest.
  58. def.on_key_use = function(...)
  59. return chest_api.protected_on_key_use(...)
  60. end
  61. def.on_skeleton_key_use = function(...)
  62. return chest_api.protected_on_skeleton_key_use(...)
  63. end
  64. -- Called by rename LBM.
  65. def._on_rename_check = function(...)
  66. return chest_api.protected_on_rename_check(...)
  67. end
  68. else
  69. def.on_construct = function(...)
  70. return chest_api.public_on_construct(...)
  71. end
  72. def.can_dig = function(...)
  73. return chest_api.public_can_dig(...)
  74. end
  75. def.on_rightclick = function(...)
  76. return chest_api.public_on_rightclick(...)
  77. end
  78. end
  79. def.on_receive_fields = function(...)
  80. return chest_api.on_receive_fields(...)
  81. end
  82. def.on_metadata_inventory_move = function(...)
  83. return chest_api.on_metadata_inventory_move(...)
  84. end
  85. def.on_metadata_inventory_put = function(...)
  86. return chest_api.on_metadata_inventory_put(...)
  87. end
  88. def.on_metadata_inventory_take = function(...)
  89. return chest_api.on_metadata_inventory_take(...)
  90. end
  91. def.on_blast = function(...)
  92. return chest_api.on_blast(...)
  93. end
  94. local def_opened = table.copy(def)
  95. local def_closed = table.copy(def)
  96. def_opened.mesh = "chest_open.obj"
  97. def_opened.drop = name .. "_closed"
  98. def_opened.groups.not_in_creative_inventory = 1
  99. def_opened.groups.chest_opened = 1
  100. def_opened.selection_box = {
  101. type = "fixed",
  102. fixed = { -1/2, -1/2, -1/2, 1/2, 3/16, 1/2 },
  103. }
  104. -- Chests are not diggable while opened.
  105. def_opened.can_dig = function()
  106. return false
  107. end
  108. def_closed.mesh = "chest_close.obj"
  109. def_closed.groups.chest_closed = 1
  110. minetest.register_node(name .. "_closed", def_closed)
  111. minetest.register_node(name .. "_open", def_opened)
  112. end
  113. local c = "chest_api:core"
  114. local f = chest_api.modpath .. "/init.lua"
  115. reload.register_file(c, f, false)
  116. chest_api.run_once = true
  117. end