mirror of
https://github.com/Wyrrrd/Autofilter.git
synced 2026-06-03 06:42:26 +02:00
Initial release
This commit is contained in:
+44
@@ -0,0 +1,44 @@
|
||||
--control.lua
|
||||
|
||||
--functions definitions
|
||||
local function get_items_by_content(inserter,inventory)
|
||||
local items = {}
|
||||
for item,count in pairs(inventory.get_contents()) do
|
||||
if #items < inserter.filter_slot_count then
|
||||
items[#items+1] = item
|
||||
end
|
||||
end
|
||||
return items
|
||||
end
|
||||
|
||||
local function get_items_by_filter(inserter,inventory)
|
||||
local items = {}
|
||||
for slot = 1,#inventory,1 do
|
||||
if #items < inserter.filter_slot_count and inventory.get_filter(slot) then
|
||||
items[#items+1] = inventory.get_filter(slot)
|
||||
end
|
||||
end
|
||||
return items
|
||||
end
|
||||
|
||||
local function on_built_entity(event)
|
||||
local inserter = event.created_entity
|
||||
if inserter.filter_slot_count then
|
||||
local inventory = inserter.surface.find_entities_filtered({position = inserter.pickup_position, limit = 1})[1].get_output_inventory()
|
||||
if inventory then
|
||||
local mode = game.players[event.player_index].mod_settings["Autofilter_Mode"].value
|
||||
if mode == "content" and not inventory.is_empty() then
|
||||
for slot,item in pairs(get_items_by_content(inserter,inventory)) do
|
||||
inserter.set_filter(slot,item)
|
||||
end
|
||||
elseif mode == "filter" and inventory.is_filtered() then
|
||||
for slot,item in pairs(get_items_by_filter(inserter,inventory)) do
|
||||
inserter.set_filter(slot,item)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--event handling
|
||||
script.on_event(defines.events.on_built_entity, on_built_entity,{{filter="type", type = "inserter"}})
|
||||
Reference in New Issue
Block a user