base.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. -- NOTE: `require` is not allowed with mod security on
  2. -- `dofile` with a return on the module is used instead
  3. generate_formspec_string = dofile(minetest.get_modpath("more_chests").."/utils/formspec.lua")
  4. local actions = dofile(minetest.get_modpath("more_chests").."/utils/actions.lua")
  5. local S = minetest.get_translator("more_chests")
  6. local function parse_action(value, default_getter)
  7. if value == false then -- model disabled this attribute
  8. return nil
  9. elseif value == nil then -- model wants the default attribute
  10. return default_getter()
  11. else -- model provided its own attribute
  12. return value
  13. end
  14. end
  15. function generate_chest_def(def)
  16. -- TODO assert def.size in ("big", "small")
  17. local out = {
  18. description = def.description,
  19. tiles = {
  20. def.tiles.top or def.tiles.side,
  21. def.tiles.top or def.tiles.side,
  22. def.tiles.side,
  23. def.tiles.side,
  24. def.tiles.side,
  25. def.tiles.front
  26. },
  27. paramtype2 = "facedir",
  28. legacy_facedir_simple = true,
  29. groups = {
  30. snappy=2,
  31. choppy=2,
  32. oddly_breakable_by_hand=2
  33. },
  34. sounds = def.sounds or default.node_sound_wood_defaults(),
  35. after_place_node = function(pos, placer)
  36. local meta = minetest.get_meta(pos)
  37. meta:set_string("owner", placer:get_player_name() or "")
  38. meta:set_string("infotext", S("@1 (owned by @2)", def.description, meta:get_string("owner")))
  39. end,
  40. on_construct = function(pos)
  41. local meta = minetest.get_meta(pos)
  42. local formspec_str = def.formspec or generate_formspec_string(def.size, def.inventory_name or nil)
  43. meta:set_string("formspec", formspec_str)
  44. meta:set_string("infotext", def.description)
  45. meta:set_string("owner", "")
  46. if def.inventory_name == nil or def.inventory_name == "main" then
  47. local inv = meta:get_inventory()
  48. local chest_size = def.size == "big" and 14*5 or 8*4
  49. inv:set_size("main", chest_size)
  50. end
  51. end,
  52. can_dig = function(pos,player)
  53. local meta = minetest.get_meta(pos)
  54. local inv = meta:get_inventory()
  55. return inv:is_empty("main")
  56. end,
  57. }
  58. -- register log actions, NOTE passing an anonymous function to avoid getting the default if not necessary
  59. out.allow_metadata_inventory_move = parse_action(def.allow_metadata_inventory_move, function() actions.get_allow_metadata_inventory_move{def.type} end)
  60. out.allow_metadata_inventory_put = parse_action(def.allow_metadata_inventory_put, function() actions.get_allow_metadata_inventory_put{def.type} end)
  61. out.allow_metadata_inventory_take = parse_action(def.allow_metadata_inventory_take, function() actions.get_allow_metadata_inventory_take{def.type} end)
  62. out.on_metadata_inventory_move = parse_action(def.on_metadata_inventory_move, function() actions.get_on_metadata_inventory_move(def.type) end)
  63. out.on_metadata_inventory_put = parse_action(def.on_metadata_inventory_put, function() actions.get_on_metadata_inventory_put(def.type) end)
  64. out.on_metadata_inventory_take = parse_action(def.on_metadata_inventory_take, function() actions.get_on_metadata_inventory_take(def.type) end)
  65. -- if model is not a simple block handle node_box attribute
  66. if def.node_box then
  67. out.drawtype = "nodebox"
  68. out.node_box = {
  69. type = "fixed",
  70. fixed = def.node_box,
  71. }
  72. end
  73. -- add pipeworks compatibility, TODO needs proper testing
  74. if def.pipeworks_enabled == true then
  75. out.groups.tubedevice = 1
  76. out.groups.tubedevice_receiver = 1
  77. out.tube = {
  78. insert_object = function(pos, node, stack, direction)
  79. local meta = minetest.get_meta(pos)
  80. local inv = meta:get_inventory()
  81. return inv:add_item("main", stack)
  82. end,
  83. can_insert = function(pos, node, stack, direction)
  84. local meta = minetest.get_meta(pos)
  85. local inv = meta:get_inventory()
  86. return inv:room_for_item("main", stack)
  87. end,
  88. input_inventory = "main",
  89. connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}
  90. }
  91. end
  92. return out
  93. end
  94. return generate_chest_def