Item Request Proxy Events


Utility mod for item-request-proxy events.

Internal
12 days ago
2.0 - 2.1
4.04K
Owner:
thremtopod
Source:
https://gitlab.com/jfletcher94/factorio-mods
Homepage:
N/A
License:
GNU LGPLv3
Created:
a month ago
Latest Version:
0.3.3 (12 days ago)
Factorio version:
2.0 - 2.1
Downloaded by:
4.04K users

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:

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: int The game tick when the item-request-proxy entity was created/removed/updated.
  • name: string The name of the event.
  • unit_number: int The LuaEntity.unit_number of the item-request-proxy entity; mostly useful for deduplication and calling register_item_request_proxy_updated.
  • proxy_target: LuaEntity The entity that the item-request-proxy is targeting; after being removed, when the item-request-proxy entity itself is no longer valid, the proxy_target will still be valid. If the proxy_target entity was also removed, this wil be nil.

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: int The game tick when the item spoiled.
  • name: string The name of the event.
  • entity: LuaEntity The 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.