diff --git a/README.md b/README.md new file mode 100644 index 0000000..a73c8ad --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Autofilter +![mod thumbnail](https://raw.githubusercontent.com/Wyrrrd/Autofilter/master/thumbnail.png) + +Description +- When you place a filter inserter, it reads either the inventory contents or inventory filters on the input side and sets it's filter to those items. +- Works only until the inserter filter is full (of course). +- The mode can be changed or disabled in player mod settings on the fly as needed. \ No newline at end of file diff --git a/changelog.txt b/changelog.txt new file mode 100644 index 0000000..7ac0ab6 --- /dev/null +++ b/changelog.txt @@ -0,0 +1,6 @@ +--------------------------------------------------------------------------------------------------- +Version: 0.18.0 +Date: 22. 2. 2020 + Changes: + - Initial release +--------------------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/control.lua b/control.lua new file mode 100644 index 0000000..3fff0c9 --- /dev/null +++ b/control.lua @@ -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"}}) \ No newline at end of file diff --git a/info.json b/info.json new file mode 100644 index 0000000..ab38230 --- /dev/null +++ b/info.json @@ -0,0 +1,9 @@ +{ + "name": "Autofilter", + "version": "0.18.0", + "title": "Autofilter", + "author": "Wyrrrd", + "dependencies": ["base >= 0.18.0"], + "description": "Automatically set inserter filters based on adjacent inventory.", + "factorio_version": "0.18" +} \ No newline at end of file diff --git a/locale/de/strings.cfg b/locale/de/strings.cfg new file mode 100644 index 0000000..a3da81c --- /dev/null +++ b/locale/de/strings.cfg @@ -0,0 +1,10 @@ +[mod-setting-name] +Autofilter_Mode=Autofilter-Modus + +[mod-setting-description] +Autofilter_Mode=Ändere, ob Greifarmfilter durch Inventarinhalt, Inventarfilter oder gar nicht gesetzt werden sollen. + +[string-mod-setting] +Autofilter_Mode-content=Inhalt +Autofilter_Mode-filter=Filter +Autofilter_Mode-none=Keiner \ No newline at end of file diff --git a/locale/en/strings.cfg b/locale/en/strings.cfg new file mode 100644 index 0000000..ac95deb --- /dev/null +++ b/locale/en/strings.cfg @@ -0,0 +1,10 @@ +[mod-setting-name] +Autofilter_Mode=Autofilter Mode + +[mod-setting-description] +Autofilter_Mode=Change if inserter filters should be set from inventory items, inventory filter or not at all. + +[string-mod-setting] +Autofilter_Mode-content=Content +Autofilter_Mode-filter=Filter +Autofilter_Mode-none=None \ No newline at end of file diff --git a/settings.lua b/settings.lua new file mode 100644 index 0000000..689da93 --- /dev/null +++ b/settings.lua @@ -0,0 +1,10 @@ +data:extend({ + { + type = "string-setting", + name = "Autofilter_Mode", + default_value = "content", + allowed_values = {"content","filter","none"}, + setting_type = "runtime-per-user", + order = "a", + }, +}) \ No newline at end of file diff --git a/thumbnail.png b/thumbnail.png new file mode 100644 index 0000000..c7a22fe Binary files /dev/null and b/thumbnail.png differ