Page 1 of 1

make on_player_mined_entity raisable by script

Posted: Sun May 14, 2017 8:37 pm
by gheift
The event on_player_mined_entity is called just before an entity is mined and allows mods to change the resulting items. My problem: if I want to support this behavior in my mod, which emulates mining by user, I am not able to create the buffer inventory:

Code: Select all

-- mod a
script.on_event(defines.events.on_player_mined_entity, function (event)
    event.buffer.insert{name = "coal"}
end)

Code: Select all

-- mod b

-- raise event for an entity which will be destroyed
script.raise_event(defines.events.on_player_mined_entity, {
    tick = game.tick,
    player_index = player.index,
    buffer = {
        { name = "raw-wood" }
    },
)
-- insert modified buffer
for _, stack in pairs(buffer) do
    player.insert(stack)
end
My problem is: I can deliver the list of stacks as a table, but the other mod expects a LuaInventory.
Therefore I need either a method, with which I can create a buffer inventory myself or the C++ side which delivers the raised event to other mods, has to convert the buffer to a LuaInventory and back.

Re: make on_player_mined_entity raisable by script

Posted: Sun May 14, 2017 8:52 pm
by Nexela
This would be wonderful. I also asked for a method for "getting" the buffer
viewtopic.php?f=28&t=44993&p=260127&hil ... er#p260127


It is not perfect but right now I fake it similar to this
buffer = {}
setmetatable(buffer, {valid=true, valid_for_read=true})

raise mined event

insert buffer (don't forget to check that count > 0 before inserting!)

Re: make on_player_mined_entity raisable by script

Posted: Mon May 15, 2017 9:20 pm
by Rseding91
I've added a mine_entity() method for 0.15.11 that will do the same logic as if the player mined the given entity.

Re: make on_player_mined_entity raisable by script

Posted: Tue May 16, 2017 1:39 am
by Mylon
Rseding91 wrote:I've added a mine_entity() method for 0.15.11 that will do the same logic as if the player mined the given entity.
Finally! This will make my bluebuild mod so much smoother and more reliable.