diff --git a/changelog.txt b/changelog.txt index 9333907..cc5a6e5 100644 --- a/changelog.txt +++ b/changelog.txt @@ -13,6 +13,7 @@ Date: ??? - Add compatibility patch for Teleportation Equipment mod - All flora collectors now have a fake fluid box - Fixed incompatibility with Extended Vanilla. Resolves https://github.com/pyanodon/pybugreports/issues/1265 + - Added py.find_latest_undo_action to find the latest undo action referencing a supplied entity --------------------------------------------------------------------------------------------------- Version: 3.0.41 Date: 2025-12-28 diff --git a/lib/control-stage.lua b/lib/control-stage.lua index 8bc055a..f98895d 100644 --- a/lib/control-stage.lua +++ b/lib/control-stage.lua @@ -235,3 +235,27 @@ py.get_planet_property = function(planet, property) if planet.surface then return planet.surface.get_property(property) end return planet.prototype.surface_properties[property] end + +---Returns the undo item and action associated with this entity +---@param player_index integer +---@param entity LuaEntity +---@param type string UndoRedoAction.type +---@return uint32? item_index, uint32? action_index +py.find_latest_undo_action = function(player_index, entity, type) + if not player_index or not entity or not entity.valid then return end + local stack = game.get_player(player_index).undo_redo_stack + local name = entity.name == "entity-ghost" and entity.ghost_name or entity.name + for i = 1, stack.get_undo_item_count() do + for a, action in pairs(stack.get_undo_item(i)) do + if action.type == type and + action.surface_index == entity.surface_index and + action.target.name == name and + (not action.target.quality or action.target.quality == entity.quality) and + action.target.position.x == entity.position.x and + action.target.position.y == entity.position.y and + (not action.target.direction or action.target.direction == entity.direction) then + return i, a + end + end + end +end \ No newline at end of file