Skip to content

Commit 085f7a1

Browse files
authored
chore: Merge pull request #2 from noodlebox/issue-1
Fix caching of limbo containers
2 parents 1113c63 + cc384ea commit 085f7a1

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

migrations/limbo-cull.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Cull limbo chests that were created due to a bug in inventory-selector <=1.0.0
2+
-- See: https://github.com/noodlebox/factorio-inventory-selector/issues/1
3+
4+
local limbo = require("script.limbo")
5+
6+
-- First, reassign any proxies to properly placed limbo containers, if necessary
7+
for _, selector in pairs(storage.selectors) do
8+
local proxy = selector.proxy
9+
if proxy and proxy.valid then
10+
local target = proxy.proxy_target_entity
11+
if target and target.valid and target.name == "inventory-selector-limbo" then
12+
proxy.proxy_target_entity = limbo[proxy.surface_index]
13+
end
14+
end
15+
end
16+
17+
-- Then, remove the misplaced limbo containers
18+
for _, surface in pairs(game.surfaces) do
19+
for _, entity in pairs(surface.find_entities_filtered{ name="inventory-selector-limbo", position={0.5,0.5} }) do
20+
entity.destroy()
21+
end
22+
end

script/limbo.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,19 @@ local origin = {0, 0}
3535
-- issues because of the change to the behavior of entity prototype flags concerning automated interaction.
3636
-- TODO: Open a new bug report for these issues once explored a bit more.
3737
local limbo = setmetatable({}, {
38-
---@param surface LuaSurface
38+
---@param surface_index uint
3939
---@return LuaEntity?
40-
__index = function(self, surface)
40+
__index = function(self, surface_index)
41+
local surface = game.surfaces[surface_index]
42+
if not surface then return nil end
4143
local entity = surface.find_entity("inventory-selector-limbo", origin)
4244
if not entity then
4345
-- Any and all flags that can be set to make this entity as invisible and uninteractable as possible
4446
-- should be present here.
4547
entity = surface.create_entity{
4648
name = "inventory-selector-limbo",
4749
position = origin,
50+
snap_to_grid = false,
4851
force = game.forces.neutral,
4952
create_build_effect_smoke = false,
5053
preserve_ghosts_and_corpses = true,
@@ -55,10 +58,9 @@ local limbo = setmetatable({}, {
5558
entity.active = false
5659
end
5760
-- Cache the reference for future lookups
58-
self[surface] = entity
61+
self[surface_index] = entity
5962
return entity
6063
end,
61-
__mode = "k",
6264
})
6365

6466
return limbo

script/selector.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function selector:update_proxy_inventory()
265265

266266
::limbo::
267267
-- The target wasn't valid for some reason, so just prepare a limbo container
268-
target = limbo[proxy.surface]
268+
target = limbo[proxy.surface_index]
269269
index = inventory.get_inventory_info(target, "main") or defines.inventory.chest -- 1
270270

271271
::assign::
@@ -826,6 +826,12 @@ local library = {
826826
on_load = register_tick_handlers,
827827
---@type table<defines.events, function>
828828
events = {
829+
[defines.events.on_surface_cleared] = function (event)
830+
limbo[event.surface_index] = nil
831+
end,
832+
[defines.events.on_surface_deleted] = function (event)
833+
limbo[event.surface_index] = nil
834+
end,
829835
[defines.events.on_object_destroyed] = on_object_destroyed,
830836
[defines.events.on_player_rotated_entity] = on_entity_modified,
831837
[defines.events.on_player_flipped_entity] = on_entity_modified,

0 commit comments

Comments
 (0)