modify LuaItemStack in on_player_mined_item?

Place to get help with not working mods / modding interface.
Post Reply
devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

Hi,

I don't suppose it's possible to modify the event.item_stack that's returned in the on_player_mined_item event, so that the resources returned to the player after it's mined is different from what the mined entity usually return?

I tried event.item_stack.set_stack() and event.item_stack.clear() but both return an error saying that this doesn't exist..

shound't it be called a SimpleItemStack instead of a LuaItemStack since it's not working as a normal LuaItemStack?

too bad the API doesn't have any way to do events cancellation :\

thanks!

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by aubergine18 »

I've not tried this, but can't you just set the 'result' property of the 'minable' table in the entity / resource prototype?

viewtopic.php?f=25&t=31277

Also, see if any of this is of use:

viewtopic.php?f=25&t=31511
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

aubergine18 wrote:I've not tried this, but can't you just set the 'result' property of the 'minable' table in the entity / resource prototype?

viewtopic.php?f=25&t=31277
It's that I don't want to change the result for every entity of that kind, just for that one time when you mine something you shouldn't have been mining but that I couldn't make non mineable because of reasons.

It's for my chainsaw mod, when you mine something that you sound't have been mining like a stone-rock.. I was thinking maybe I could hack some sort of even cancellation by recording the entity being mined in the premined event and then in the mined event I place the rock back on the ground like nothing happened. I menage to made it work but now I'm faced with 2 immediate problem and 1 annoyance :

problem 1 : there don't seem to be a way to get and set the variant when placing a simple-entity. It's not like the tree where they are named (tree-1-red, etc.) I get a different stone than the one I had each time I replace the stone. Even if I copy the direction or orientation over.

problem 2 : There no way to cancel the player getting the resources, so I have to code something to look for it in the diverse inventory, surface, etc.. was it automatically send to trash by the auto-trash thing?

annoyance : That wouldn't work well with complex entity like assemblers, cars, factorissimo, etc
Last edited by devilwarriors on Mon Aug 29, 2016 11:21 pm, edited 1 time in total.

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

But I thought of something while writhing this..

maybe I could make the entity that are not tree, around the player (in it's data.raw.player.player.reach_distance), temporarely non minable ..
entity.mineable is a write/read so that should work.

It won't work well with multiplayer and the true-long-reach mod, but in single player it would work great :\

Supercheese
Filter Inserter
Filter Inserter
Posts: 841
Joined: Mon Sep 14, 2015 7:40 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by Supercheese »

You could use on_preplayer_mined_item, then check what the player is holding, then check the entity, and if the entity is not suitable, temporarily set it non-minable, maybe even print a message to the player. You can flip it back to minable sometime later.

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by aubergine18 »

And you could possibly do that on the on_preplayer_mined_item event (triggered when player starts to mine something)... Might be quicker way to temporarily disable mining on specific object rather than having to constantly iterate nearby objects. You could add such entities to a list and then after few ticks revert back to being minable. Although I'm not sure if changing minable after mining has started will stop the current mining. You could also add in damage to the chainsaw and maybe emit some particles to visualise the damage.

EDIT: Damn you supercheese, you beat me to it lolol!
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

Supercheese
Filter Inserter
Filter Inserter
Posts: 841
Joined: Mon Sep 14, 2015 7:40 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by Supercheese »

aubergine18 wrote:EDIT: Damn you supercheese, you beat me to it lolol!
Great minds think alike :D

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

on_preplayer_mined_item is actually the whole problem, it's not what everyone thing it is..

It's not triggered when you start mining something, it's triggered when you finish mining right before the entity is pulled from the ground, so it's triggered probably in the same tick as on_preplayer_mined_item and is completly useless.

one event return the entity and the other return the the result of mining it, so you end up with having to read 2 events to get the whole picture. it's weird..

I don't know if it's working as intended or bugged but I remember trying to use it to set mineable to false and it wasn't working.

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

devilwarriors wrote:I don't know if it's working as intended or bugged but I remember trying to use it to set mineable to false and it wasn't working.
Just tryed again and yeah it's not working, the entity is mined regardless.

And I logged the tick id and it's the same for both call, so they are executed one after the other.
One before destroying the entity and the other just after.

Useless :(

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by aubergine18 »

IMO it would be worth raising that as a bug to let the devs know - it would certainly be more useful if it gets called when mining starts, so mods could stop the mining. (obviously setting minable false after starting mining should stop the mining, so if it doesn't already that would need fixing too)

Note: If I understand events properly, it might be useless anyway - even if the event was fired at start of mining, by time it reaches your event handler the mining might already be complete :/

The only other alternative seems to be like you mentioned earlier, constant scanning of reach distance around player for stuff they can't mine with current tool... but that is crazy slow, especially in MP games where you could be scanning multiple players at same time.

Another option would be the ability to define which categories/groups of things a mining tool can mine. I don't know if there's already some way to do this?
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

aubergine18 wrote:Another option would be the ability to define which categories/groups of things a mining tool can mine. I don't know if there's already some way to do this?
nope haven't found anything like that

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by Nexela »

Use on_put_item if you are holding an item, but chances are you have it set as a mining-tool and if thats the cause then you could try to change the cursor_stack portion to get the weapon inventory. Not sure if on put item triggers in this case though

Code: Select all

script.on_event(defines.events.on_put_item, function(event)
local player = game.players[event.player_index]
if player.cursor_stack and player.cursor_stack.valid_for_read and player.cursor_stack.name=="chainsaw" then
 if player.selected and not player.selected.name == "tree" then
  player.clean_cursor()
  player.print("You dropped your chainsaw on your toes while trying to mine something not wooden!")
end
end
end)

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

Nexela wrote:Use on_put_item if you are holding an item, but chances are you have it set as a mining-tool and if thats the cause then you could try to change the cursor_stack portion to get the weapon inventory. Not sure if on put item triggers in this case though

Code: Select all

script.on_event(defines.events.on_put_item, function(event)
local player = game.players[event.player_index]
if player.cursor_stack and player.cursor_stack.valid_for_read and player.cursor_stack.name=="chainsaw" then
 if player.selected and not player.selected.name == "tree" then
  player.clean_cursor()
  player.print("You dropped your chainsaw on your toes while trying to mine something not wooden!")
end
end
end)
Yeah no it's mining-tool.. but maybe I should give a try to the do it your way, with an item that you hold..

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by Adil »

aubergine18 wrote:INote: If I understand events properly, it might be useless anyway - even if the event was fired at start of mining, by time it reaches your event handler the mining might already be complete :/
Nope, that's not how events work. Handlers are run on the same tick the event runs, and only after that game proceeds.
aubergine18 wrote: The only other alternative seems to be like you mentioned earlier, constant scanning of reach distance around player for stuff they can't mine with current tool... but that is crazy slow, especially in MP games where you could be scanning multiple players at same time.
Or you could check the `player.selected` entity only, store its reference and make it minable again once player removes cursor from it.
Still would be weird in multiplayer, where one person could prevent the other from mining stuff by looking at the same thing.
aubergine18 wrote: Another option would be the ability to define which categories/groups of things a mining tool can mine. I don't know if there's already some way to do this?
There's `hardness` property of the resources, if tool is not hard enough, then it can't mine that resource. Trees and other default entities have the value of 0.5 iirc, and vanilla resources have hardness of 0.9.
Not a finely grained control, but it's there.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

Adil wrote:
aubergine18 wrote: Another option would be the ability to define which categories/groups of things a mining tool can mine. I don't know if there's already some way to do this?
There's `hardness` property of the resources, if tool is not hard enough, then it can't mine that resource. Trees and other default entities have the value of 0.5 iirc, and vanilla resources have hardness of 0.9.
Not a finely grained control, but it's there.
yeah rseding just told me the same thing, this should do the trick, thank!

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

How do to set what the max hardness a mining-tool can mine? Can't seem to find anything on that in the doc.

also building are like 0.2 hardness so that wouldn't really work either way..

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

Adil wrote: Or you could check the `player.selected` entity only, store its reference and make it minable again once player removes cursor from it.
Still would be weird in multiplayer, where one person could prevent the other from mining stuff by looking at the same thing.
That might actually work really well tho.. the multiplayer impact is still pretty minor compared to my other solutions.

devilwarriors
Filter Inserter
Filter Inserter
Posts: 311
Joined: Sat Jan 09, 2016 1:11 am
Contact:

Re: modify LuaItemStack in on_player_mined_item?

Post by devilwarriors »

turn out the hardness won't do it, since I would have to reduce the speed of the mining tool to close to nothing for this to work and that would defeat the purpose of my mod.

Post Reply

Return to “Modding help”