api.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. hopper.containers = {}
  2. hopper.groups = {}
  3. hopper.neighbors = {}
  4. -- global function to add new containers
  5. function hopper:add_container(list)
  6. for _, entry in pairs(list) do
  7. local target_node = entry[2]
  8. local neighbor_node
  9. if string.sub(target_node, 1, 6) == "group:" then
  10. local group_identifier, group_number
  11. local equals_index = string.find(target_node, "=")
  12. if equals_index ~= nil then
  13. group_identifier = string.sub(target_node, 7, equals_index-1)
  14. -- it's possible that the string was of the form "group:blah = 1", in which case we want to trim spaces off the end of the group identifier
  15. local space_index = string.find(group_identifier, " ")
  16. if space_index ~= nil then
  17. group_identifier = string.sub(group_identifier, 1, space_index-1)
  18. end
  19. group_number = tonumber(string.sub(target_node, equals_index+1, -1))
  20. else
  21. group_identifier = string.sub(target_node, 7, -1)
  22. group_number = "all" -- special value to indicate no number was provided
  23. end
  24. local group_info = hopper.groups[group_identifier]
  25. if group_info == nil then
  26. group_info = {}
  27. end
  28. if group_info[group_number] == nil then
  29. group_info[group_number] = {}
  30. end
  31. group_info[group_number][entry[1]] = entry[3]
  32. hopper.groups[group_identifier] = group_info
  33. neighbor_node = "group:"..group_identifier
  34. -- result is a table of the form groups[group_identifier][group_number][relative_position][inventory_name]
  35. else
  36. local node_info = hopper.containers[target_node]
  37. if node_info == nil then
  38. node_info = {}
  39. end
  40. node_info[entry[1]] = entry[3]
  41. hopper.containers[target_node] = node_info
  42. neighbor_node = target_node
  43. -- result is a table of the form containers[target_node_name][relative_position][inventory_name]
  44. end
  45. local already_in_neighbors = false
  46. for _, value in pairs(hopper.neighbors) do
  47. if value == neighbor_node then
  48. already_in_neighbors = true
  49. break
  50. end
  51. end
  52. if not already_in_neighbors then
  53. table.insert(hopper.neighbors, neighbor_node)
  54. end
  55. end
  56. end
  57. -- "top" indicates what inventory the hopper will take items from if this node is located at the hopper's wide end
  58. -- "side" indicates what inventory the hopper will put items into if this node is located at the hopper's narrow end and at the same height as the hopper
  59. -- "bottom" indicates what inventory the hopper will put items into if this node is located at the hopper's narrow end and either above or below the hopper.
  60. hopper:add_container({
  61. {"top", "hopper:hopper", "main"},
  62. {"bottom", "hopper:hopper", "main"},
  63. {"side", "hopper:hopper", "main"},
  64. {"side", "hopper:hopper_side", "main"},
  65. {"bottom", "hopper:chute", "main"},
  66. {"side", "hopper:chute", "main"},
  67. {"bottom", "hopper:sorter", "main"},
  68. {"side", "hopper:sorter", "main"},
  69. })
  70. if minetest.get_modpath("default") then
  71. hopper:add_container({
  72. {"top", "default:chest", "main"},
  73. {"bottom", "default:chest", "main"},
  74. {"side", "default:chest", "main"},
  75. {"top", "default:furnace", "dst"},
  76. {"bottom", "default:furnace", "src"},
  77. {"side", "default:furnace", "fuel"},
  78. {"top", "default:furnace_active", "dst"},
  79. {"bottom", "default:furnace_active", "src"},
  80. {"side", "default:furnace_active", "fuel"},
  81. {"top", "default:chest_locked", "main"},
  82. {"bottom", "default:chest_locked", "main"},
  83. {"side", "default:chest_locked", "main"},
  84. })
  85. end
  86. -- protector redo mod support
  87. if minetest.get_modpath("protector") then
  88. hopper:add_container({
  89. {"top", "protector:chest", "main"},
  90. {"bottom", "protector:chest", "main"},
  91. {"side", "protector:chest", "main"},
  92. })
  93. end
  94. -- wine mod support
  95. if minetest.get_modpath("wine") then
  96. hopper:add_container({
  97. {"top", "wine:wine_barrel", "dst"},
  98. {"bottom", "wine:wine_barrel", "src"},
  99. {"side", "wine:wine_barrel", "src"},
  100. })
  101. end