From af4549c511c24c2c74b3e53714cc091eb3281599 Mon Sep 17 00:00:00 2001 From: Alexamakans Date: Wed, 12 Jul 2023 14:24:51 +0200 Subject: [PATCH 1/3] Updated changelog, bump v1.0.5 --- changelog.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/changelog.txt b/changelog.txt index 924594d..b80c1f5 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,9 @@ --------------------------------------------------------------------------------------------------- +Version: 1.0.5 +Date: 2023-07-12 + Fixes: + - Fixed filter not being pasted properly from blueprints and copy-pasting. +--------------------------------------------------------------------------------------------------- Version: 1.0.4 Date: 2023-07-03 Fixes: From de18d6e969e9feaef94f62920309c2aced71a4c8 Mon Sep 17 00:00:00 2001 From: Alexamakans Date: Wed, 12 Jul 2023 15:36:25 +0200 Subject: [PATCH 2/3] info.json version v1.0.5, remove unnecessary code?? --- info.json | 2 +- scripts/main.lua | 52 ------------------------------------------------ 2 files changed, 1 insertion(+), 53 deletions(-) diff --git a/info.json b/info.json index 6108577..e4ffef4 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "Unichest", - "version": "1.0.4", + "version": "1.0.5", "title": "Unichest", "author": "zysnarch", "factorio_version": "1.1", diff --git a/scripts/main.lua b/scripts/main.lua index 583fe2f..bee8151 100644 --- a/scripts/main.lua +++ b/scripts/main.lua @@ -39,58 +39,6 @@ script.on_event(defines.events.on_player_setup_blueprint, function(event) end end) -function rotateAndFlip(pos, dir, flipH, flipV) - if flipH then pos.x = -pos.x end - if flipV then pos.y = -pos.y end - if dir == defines.direction.north then return pos end - if dir == defines.direction.west then return Position.construct(pos.y, -pos.x) end - if dir == defines.direction.south then return Position.construct(-pos.x, -pos.y) end - if dir == defines.direction.east then return Position.construct(-pos.y, pos.x) end - game.print("Warning: unexpected blueprint rotation " .. dir ". Chest filters will be incorrect.") - return pos -end - -script.on_event(defines.events.on_pre_build, function(event) - local player = game.players[event.player_index] - if not player.is_cursor_blueprint() or global.lastPreBuildTick == event.tick then return end - if not player.get_blueprint_entities() then return end -- might be all tiles - global.lastPreBuildTick = event.tick - - -- Find the blueprint's bounding box. - local positions = {} - table.each(player.get_blueprint_entities(), function(v) table.insert(positions, Position.new(v.position)) end) - local leftTop = Position.min_xy(positions) - local rightBottom = Position.max_xy(positions) - local bbox = Area.new{leftTop, rightBottom} - local negCenter = bbox:center():flip() - local bboxSize = bbox:offset(negCenter) -- `bbox - bbox.center` - -- Maybe rotate the bbox. - local area = bboxSize:offset(event.position):ceil() - if event.direction == defines.direction.east or event.direction == defines.direction.west then - area = area:flip() - end - - -- Find the positions where the blueprint *would* place the relevant entities. - local bpEntityPositions = {} - table.each(player.get_blueprint_entities(), function(v) - if v.name == Config.CHEST_NAME then - local bpPos = rotateAndFlip(Position.new(v.position):add(negCenter), event.direction, event.flip_horizontal, event.flip_vertical) - local pos = bpPos:add(event.position):center() - table.insert(bpEntityPositions, pos) - end - end) - - -- Destroy any existing entities where the blueprint would overwrite them. - table.each(player.surface.find_entities_filtered {name = 'entity-ghost', area = area}, function(v) - if v.ghost_name == Config.CHEST_NAME then - local center = Position.center(v.position) - if table.any(bpEntityPositions, function(p) return center:equals(p) end) then - v.destroy() - end - end - end) -end) - function onBuiltEntity(event) local entity = event.created_entity if entity and entity.valid then From 7626422a9c8983ec388073289abe60c9ff4c2217 Mon Sep 17 00:00:00 2001 From: Alexamakans Date: Wed, 12 Jul 2023 17:35:39 +0200 Subject: [PATCH 3/3] fix pasting filter, and copying from chest with no items in it --- scripts/chestutil.lua | 5 +++++ scripts/unichest.lua | 33 +++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/scripts/chestutil.lua b/scripts/chestutil.lua index d3394e8..f6f6f0b 100644 --- a/scripts/chestutil.lua +++ b/scripts/chestutil.lua @@ -105,6 +105,11 @@ function Chest.setItemFilterFromSource(dest, source, isOutput) if source == nil or dest == nil or not source.valid or not dest.valid then return end if dest.name ~= Config.CHEST_NAME then return end + if dest.name == source.name then + Chest.setItemFilter(dest, Chest.getNameFromId(source.link_id)) + return + end + local itemCycle = {} if not isOutput then -- Burner fuel and labs are input-only diff --git a/scripts/unichest.lua b/scripts/unichest.lua index e2c0997..db0de32 100644 --- a/scripts/unichest.lua +++ b/scripts/unichest.lua @@ -9,13 +9,30 @@ function Chest.onBuiltEntity(event, entity) if entity.name ~= Config.CHEST_NAME then return end local tagFilter = event.tags and event.tags["filter"] -- Extract filter from a blueprint tag + global.updateFilterQueue = global.updateFilterQueue or {} if tagFilter then - Chest.setItemFilter(entity, tagFilter) - elseif entity.link_id == 0 then - Chest.setItemFilter(entity, global.lastItemFilter or Chest.getNameFromId(entity.link_id)) + table.insert(global.updateFilterQueue, {entity, tagFilter}) + else + table.insert(global.updateFilterQueue, {entity, Chest.getNameFromId(entity.link_id) or global.lastItemFilter}) end end +function tick_event_handler_gui(player, guiEntity, guiFilter) + -- Reset any changes via the GUIs we can't control (e.g. Link bitmask and manual filtering). + if not guiEntity.valid then return end + player.gui.relative.unichestFrame.itemFilter.elem_value = guiFilter + Chest.setItemFilter(guiEntity, guiFilter) +end + +function tick_event_handler_update_filter(event) + global.updateFilterQueue = global.updateFilterQueue or {} + for i, val in pairs(global.updateFilterQueue) do + Chest.setItemFilter(val[1], val[2]) + end + + global.updateFilterQueue = {} +end + function Chest.openGui(player, entity) local guiEntity = entity local guiFilter = Chest.getNameFromId(guiEntity.link_id) @@ -23,10 +40,8 @@ function Chest.openGui(player, entity) Chest.setItemFilter(guiEntity, guiFilter) script.on_event(defines.events.on_tick, function(event) - -- Reset any changes via the GUIs we can't control (e.g. Link bitmask and manual filtering). - if not guiEntity.valid then return end - player.gui.relative.unichestFrame.itemFilter.elem_value = guiFilter - Chest.setItemFilter(guiEntity, guiFilter) + tick_event_handler_gui(player, guiEntity, guiFilter) + tick_event_handler_update_filter(event) end) script.on_event(defines.events.on_gui_elem_changed, function(event) @@ -40,7 +55,9 @@ function Chest.openGui(player, entity) end) script.on_event(defines.events.on_gui_closed, function(event) - script.on_event(defines.events.on_tick, nil) + script.on_event(defines.events.on_tick, function(event) + tick_event_handler_update_filter(event) + end) script.on_event(defines.events.on_gui_elem_changed, nil) end) end