From 3a859a941dd87e3338342c38f899b1f5119bd298 Mon Sep 17 00:00:00 2001 From: Wyrrrd Date: Mon, 24 Feb 2020 18:56:01 +0100 Subject: [PATCH] Added check for empty filter and whitelist mode Avoids overwriting filter on replacing preexisting inserters/ghosts --- changelog.txt | 1 + control.lua | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/changelog.txt b/changelog.txt index 538c5d1..15f4db6 100644 --- a/changelog.txt +++ b/changelog.txt @@ -3,6 +3,7 @@ Version: 0.18.5 Date: 24. 2. 2020 Changes: - Added hidden dependency to Bob's Adjustable Inserters (fixes setting filter from wrong inventory when pickup location is rotated) + - Added check for empty filter and whitelist mode to avoid overwriting filter on replacing preexisting inserters/ghosts Locale: - Added translated mod description diff --git a/control.lua b/control.lua index aff2c44..c3544ef 100644 --- a/control.lua +++ b/control.lua @@ -1,6 +1,15 @@ --control.lua --functions definitions +local function is_filter_empty(inserter) + for slot = 1,inserter.filter_slot_count do + if inserter.get_filter(slot) then + return false + end + return true + end +end + local function get_items_by_content(inserter,inventory) local items = {} for item,_ in pairs(inventory.get_contents()) do @@ -25,18 +34,20 @@ local function on_built_entity(event) local inserter = event.created_entity if inserter.type == "inserter" then if inserter.filter_slot_count then - local pickup = inserter.surface.find_entities_filtered({position = inserter.pickup_position, limit = 1}) - if #pickup > 0 then - local inventory = pickup[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) + if is_filter_empty(inserter) and inserter.inserter_filter_mode == "whitelist" then + local pickup = inserter.surface.find_entities_filtered({position = inserter.pickup_position, limit = 1}) + if #pickup > 0 then + local inventory = pickup[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