]> git.lizzy.rs Git - Crafter.git/blob - mods/hopper/api.lua
Add in jetpacks
[Crafter.git] / mods / hopper / api.lua
1 hopper.containers = {}
2 hopper.groups = {}
3 hopper.neighbors = {}
4
5 -- global function to add new containers
6 function hopper:add_container(list)
7         for _, entry in pairs(list) do
8         
9                 local target_node = entry[2]
10                 local neighbor_node
11                 
12                 if string.sub(target_node, 1, 6) == "group:" then
13                         local group_identifier, group_number
14                         local equals_index = string.find(target_node, "=")
15                         if equals_index ~= nil then
16                                 group_identifier = string.sub(target_node, 7, equals_index-1)
17                                 -- 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
18                                 local space_index = string.find(group_identifier, " ") 
19                                 if space_index ~= nil then
20                                         group_identifier = string.sub(group_identifier, 1, space_index-1)
21                                 end
22                                 group_number = tonumber(string.sub(target_node, equals_index+1, -1))
23                         else
24                                 group_identifier = string.sub(target_node, 7, -1)
25                                 group_number = "all" -- special value to indicate no number was provided
26                         end
27                         
28                         local group_info = hopper.groups[group_identifier]
29                         if group_info == nil then
30                                 group_info = {}
31                         end
32                         if group_info[group_number] == nil then
33                                 group_info[group_number] = {}
34                         end
35                         group_info[group_number][entry[1]] = entry[3]
36                         hopper.groups[group_identifier] = group_info
37                         neighbor_node = "group:"..group_identifier
38                         -- result is a table of the form groups[group_identifier][group_number][relative_position][inventory_name]
39                 else
40                         local node_info = hopper.containers[target_node]
41                         if node_info == nil then
42                                 node_info = {}
43                         end
44                         node_info[entry[1]] = entry[3]
45                         hopper.containers[target_node] = node_info
46                         neighbor_node = target_node
47                         -- result is a table of the form containers[target_node_name][relative_position][inventory_name]
48                 end
49                 
50                 local already_in_neighbors = false
51                 for _, value in pairs(hopper.neighbors) do
52                         if value == neighbor_node then
53                                 already_in_neighbors = true
54                                 break
55                         end
56                 end
57                 if not already_in_neighbors then
58                         table.insert(hopper.neighbors, neighbor_node)
59                 end
60         end
61 end
62
63 -- "top" indicates what inventory the hopper will take items from if this node is located at the hopper's wide end
64 -- "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
65 -- "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.
66
67 hopper:add_container({
68         {"top", "hopper:hopper", "main"},
69         {"bottom", "hopper:hopper", "main"},
70         {"side", "hopper:hopper", "main"},
71         {"side", "hopper:hopper_side", "main"},
72         
73         {"bottom", "hopper:chute", "main"},
74         {"side", "hopper:chute", "main"},
75         
76         {"bottom", "hopper:sorter", "main"},
77         {"side", "hopper:sorter", "main"},
78 })
79
80 hopper:add_container({
81         {"top", "utility:chest", "main"},
82         {"bottom", "utility:chest", "main"},
83         {"side", "utility:chest", "main"},
84         
85         {"top", "utility:chest_open", "main"},
86         {"bottom", "utility:chest_open", "main"},
87         {"side", "utility:chest_open", "main"},
88
89         {"top", "utility:furnace", "dst"},
90         {"bottom", "utility:furnace", "src"},
91         {"side", "utility:furnace", "fuel"},
92
93         {"top", "utility:furnace_active", "dst"},
94         {"bottom", "utility:furnace_active", "src"},
95         {"side", "utility:furnace_active", "fuel"},
96 })
97