Trying to make a mod that allows filtering chest inventories
Posted: Tue Oct 19, 2021 12:52 am
Hi. I'm trying to make chests whose slots can be filtered to only allow some kind of items just like cargo wagons, and it's my first time modding so I have no idea what I'm doing. I have several potential approaches in mind, and frequently switch whenever the current one hits a roadblock:
1) Use some metatable magic change the inventory of chests to some other inventory that allows filters: if this worked, it would have been really easy but it seems to be impossible because metatables are protected.
2) Somehow merge the chest and cargo wagon definitions of data.raw, something like a cargo wagon with the bounding boxes and sprites of the chests. I feel like this is unlikely to work, and even if it did, it would most likely have weird unexpected behaviors.
3) Do it like you would without modding (i.e. with a constant combinator for the allowed context, with a small circuit that tells the inserters to only inserts when doing so respects the filter), possibly with some things hidden via custom versions of combinators etc. added to data.raw. This looks like a very unstable way of doing this.
4) Hijack both the the behavior of inserters, and the gui that are opened when opening chests to open the gui of a cargo wagon whose content is synchronized with the chest.
The inserter part looks fairly easy: replace stack inserter by filter stack inserters that have the skin of stack inserters, and whose filter is automatically recomputed at each tick (iterating through the items that can be inserted without going over the limit).
The gui part felt harder: I found how to prevent de default gui from opening, and how to add other gui elements, but not how to reuse the "normal" ui. Is there some way, given a chest entity, to open the corresponding gui? Or does one have to recreate the whole ui? I would have expected GuiStyles to be for that purpose, but it seems to only define the style of the layers of the normal gui, but to not have anything to build a part of the gui with all its descendants.
Is there a simpler method I'm missing? Which method to you think is best?
1) Use some metatable magic change the inventory of chests to some other inventory that allows filters: if this worked, it would have been really easy but it seems to be impossible because metatables are protected.
2) Somehow merge the chest and cargo wagon definitions of data.raw, something like a cargo wagon with the bounding boxes and sprites of the chests. I feel like this is unlikely to work, and even if it did, it would most likely have weird unexpected behaviors.
3) Do it like you would without modding (i.e. with a constant combinator for the allowed context, with a small circuit that tells the inserters to only inserts when doing so respects the filter), possibly with some things hidden via custom versions of combinators etc. added to data.raw. This looks like a very unstable way of doing this.
4) Hijack both the the behavior of inserters, and the gui that are opened when opening chests to open the gui of a cargo wagon whose content is synchronized with the chest.
The inserter part looks fairly easy: replace stack inserter by filter stack inserters that have the skin of stack inserters, and whose filter is automatically recomputed at each tick (iterating through the items that can be inserted without going over the limit).
The gui part felt harder: I found how to prevent de default gui from opening, and how to add other gui elements, but not how to reuse the "normal" ui. Is there some way, given a chest entity, to open the corresponding gui? Or does one have to recreate the whole ui? I would have expected GuiStyles to be for that purpose, but it seems to only define the style of the layers of the normal gui, but to not have anything to build a part of the gui with all its descendants.
Is there a simpler method I'm missing? Which method to you think is best?