This mod was designed for detecting when modules are added/removed from crafting machines by construction robots or space platforms, or when modules spoil inside a crafting machine. However, the API works for any type of item and any type of entity.
It is already possible to detect when a player directly adds/removes items from an entity with only a few events:
defines.events.on_player_fast_transferreddefines.events.on_player_dropped_item_into_entitydefines.events.on_player_cursor_stack_changed
There are, however, no built-in events for items being added/removed by construction robots or space platforms. In order to detect these cases, this mod adds lifecycle events for item-request-proxy entities. Using this mod's API, it is possible for a mod to receive events any time an item is added or removed from an entity by construction robots or space platforms. This mod also has an API that allows mods to receive events whenever items of any registered prototype spoil.
Cases this mod does not cover:
- Items added/removed from entities by inserters.
- Items added/removed from entities by logstic robots.
- Items added/removed from entities by script.
- Proxy containers.
Item Request Proxy Events
This API allows mods to receive events for items being added/removed from entities by construction robots and space platforms.
Subscribe to Creation Events
Covers creation of all item-request-proxy entities:
script.on_event("item-request-proxy-created", function(event)
-- Logic to run upon item-request-proxy creation
end)
Subscribe to Destruction Events
Covers both fulfillment and cancellation of all item-request-proxy entities:
script.on_event("item-request-proxy-removed", function(event)
-- Logic to run upon item-request-proxy removal
end)
Subscribe to Update Events
Note: This does have a small on_tick cost. By default, the mod checks (up to) 4 item request proxies per tick. In my local testing, the worst-case update time was ~0.05ms.
Register specific item-request-proxy entities to fire events when updated:
script.on_event("item-request-proxy-created", function(event)
if matches_some_criteria(event) then
remote.call("item-request-proxy-events", "register_item_request_proxy_updated", event.unit_number)
end
end)
Subscribe to the update events:
script.on_event("item-request-proxy-updated", function(event)
-- Logic to run upon item-request-proxy update
end)
EventData
The event data for all three event types is the same:
tick:intThe game tick when theitem-request-proxyentity was created/removed/updated.name:stringThe name of the event.unit_number:intTheLuaEntity.unit_numberof theitem-request-proxyentity; mostly useful for deduplication and callingregister_item_request_proxy_updated.proxy_target:LuaEntityThe entity that theitem-request-proxyis targeting; after being removed, when theitem-request-proxyentity itself is no longer valid, theproxy_targetwill still be valid. If theproxy_targetentity was also removed, this wil benil.
Item Spoiled Events
This API allows mods to receive events for items spoilining inside entities.
Register a Prototype
In the data stage:
local spoilable_items = require("__item-request-proxy-events__.spoilable-items")
spoilable_items.register_item_spoiled_event(data.raw.module["my-module"])
Subscribe to Events
In the runtime stage:
script.on_event("item-spoiled", function(event)
-- Logic to run upon item spoiling
end)
EventData
The event data for this event is:
tick:intThe game tick when the item spoiled.name:stringThe name of the event.entity:LuaEntityThe entity in which the item spoiled (if there is no such entity the event will not be fired).
More Information
Adding to an Existing Save
When a save is loaded after this mod has been added to it, the mod will fire "catch-up" events for all existing item-request-proxy entities.
Issues
This is a new mod, and should still be considered experimental. So far it has only been tested with Rigor Module. Use at your own risk.