Allow merging undo/redo actions within create/destroy event handlers
Posted: Mon Dec 23, 2024 12:25 pm
When calling LuaSurface.create_entity or LuaEntity.destroy, we are able to pass the arguments player and optionally item_index to have the actions added to the undo/redo stack. However, in the common case where these methods are being called within the various on_built or on_mined event handlers, these actions will not be merged into the main UndoRedoItem, as that item does not get added until after the event handlers have finished. As a result, you get two different items, and there is no way to merge these.
There are two solutions I can see for this. The first would be to change the internal code to add the item to the stack before event handlers are called. This way there will already be an action on the stack that can be added to. Alternatively, when the action does get added afterwards, have it merge automatically with any item that was created during event handling. Or possibly, use the first option for create events, and the second for destroy, to preserve ordering.
There are two solutions I can see for this. The first would be to change the internal code to add the item to the stack before event handlers are called. This way there will already be an action on the stack that can be added to. Alternatively, when the action does get added afterwards, have it merge automatically with any item that was created during event handling. Or possibly, use the first option for create events, and the second for destroy, to preserve ordering.