api.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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:trash", "main"},
  68. {"side", "hopper:trash", "main"},
  69. {"bottom", "hopper:sorter", "main"},
  70. {"side", "hopper:sorter", "main"},
  71. })
  72. if minetest.get_modpath("default") then
  73. hopper:add_container({
  74. {"top", "default:chest", "main"},
  75. {"bottom", "default:chest", "main"},
  76. {"side", "default:chest", "main"},
  77. {"top", "default:chest_open", "main"},
  78. {"bottom", "default:chest_open", "main"},
  79. {"side", "default:chest_open", "main"},
  80. {"top", "default:furnace", "dst"},
  81. {"bottom", "default:furnace", "src"},
  82. {"side", "default:furnace", "fuel"},
  83. {"top", "default:furnace_active", "dst"},
  84. {"bottom", "default:furnace_active", "src"},
  85. {"side", "default:furnace_active", "fuel"},
  86. {"top", "default:chest_locked", "main"},
  87. {"bottom", "default:chest_locked", "main"},
  88. {"side", "default:chest_locked", "main"},
  89. {"top", "default:chest_locked_open", "main"},
  90. {"bottom", "default:chest_locked_open", "main"},
  91. {"side", "default:chest_locked_open", "main"},
  92. {"top", "biofuel:refinery", "dst"},
  93. {"bottom", "biofuel:refinery", "src"},
  94. {"side", "biofuel:refinery", "src"},
  95. {"top", "biofuel:refinery_active", "dst"},
  96. {"bottom", "biofuel:refinery_active", "src"},
  97. {"side", "biofuel:refinery_active", "src"},
  98. })
  99. end
  100. -- protector redo mod support
  101. if minetest.get_modpath("protector") then
  102. hopper:add_container({
  103. {"top", "protector:chest", "main"},
  104. {"bottom", "protector:chest", "main"},
  105. {"side", "protector:chest", "main"},
  106. })
  107. end
  108. -- wine mod support
  109. if minetest.get_modpath("wine") then
  110. hopper:add_container({
  111. {"top", "wine:wine_barrel", "dst"},
  112. {"bottom", "wine:wine_barrel", "src"},
  113. {"side", "wine:wine_barrel", "src"},
  114. })
  115. end