Create inventory information through an existing table

This will greatly facilitate API support and improve code readability.
This commit is contained in:
Ajrat Makhmutov 2024-08-12 23:27:58 +03:00 committed by SmallJoker
parent e2f4aec5c9
commit 4c46073621

45
api.lua

@ -36,46 +36,41 @@ end
function hopper:add_container(list) function hopper:add_container(list)
for _, entry in pairs(list) do for _, entry in pairs(list) do
local relative_position = entry[1]
local target_node = entry[2] local target_node = entry[2]
local neighbor_node
local inventory_info = entry
inventory_info.inventory_name = entry[3]
table.remove(inventory_info, 1)
table.remove(inventory_info, 1)
table.remove(inventory_info, 1)
local neighbor_node
if string.sub(target_node, 1, 6) == "group:" then if string.sub(target_node, 1, 6) == "group:" then
local group_identifier, group_number = parse_group(target_node) local group_identifier, group_number = parse_group(target_node)
if hopper.groups[group_identifier] == nil then if hopper.groups[group_identifier] == nil then
hopper.groups[group_identifier] = {} hopper.groups[group_identifier] = {}
end end
local group = hopper.groups[group_identifier][group_number] if hopper.groups[group_identifier][group_number] == nil then
if group == nil then hopper.groups[group_identifier][group_number] = {}
group = {}
end end
if group[entry[1]] == nil then if hopper.groups[group_identifier][group_number].extra == nil then
group[entry[1]] = {} hopper.groups[group_identifier][group_number].extra = {}
end end
group[entry[1]]["inventory_name"] = entry[3]
if entry["get_inventory"] then hopper.groups[group_identifier][group_number][relative_position] = inventory_info
group[entry[1]]["get_inventory"] = entry["get_inventory"]
end
hopper.groups[group_identifier][group_number] = group
neighbor_node = "group:"..group_identifier neighbor_node = "group:"..group_identifier
-- result is a table of the form:
-- groups[group_identifier][group_number][relative_position]{"inventory_name","get_inventory"}
else else
local node_info = hopper.containers[target_node] if hopper.containers[target_node] == nil then
if node_info == nil then hopper.containers[target_node] = {}
node_info = {}
end end
if node_info[entry[1]] == nil then if hopper.containers[target_node].extra == nil then
node_info[entry[1]] = {} hopper.containers[target_node].extra = {}
end end
node_info[entry[1]]["inventory_name"] = entry[3]
if entry["get_inventory"] then hopper.containers[target_node][relative_position] = inventory_info
node_info[entry[1]]["get_inventory"] = entry["get_inventory"]
end
hopper.containers[target_node] = node_info
neighbor_node = target_node neighbor_node = target_node
-- result is a table of the form:
-- containers[target_node_name][relative_position]{"inventory_name","get_inventory"}
end end
if not is_already_in_neighbors(neighbor_node) then if not is_already_in_neighbors(neighbor_node) then