[0.16.51] on_player_crafted_item event description wrong or event execution order bug

This subforum contains all the issues which we already resolved.
Post Reply
User avatar
Muppet9010
Filter Inserter
Filter Inserter
Posts: 278
Joined: Sat Dec 09, 2017 6:01 pm
Contact:

[0.16.51] on_player_crafted_item event description wrong or event execution order bug

Post by Muppet9010 »

The on_player_crafted_item event states
Called when the player crafts an item (upon inserting into player's inventory, not clicking the button to craft)
However, if you check the player's inventory for the item upon the event it isn't present. It will be found 1 tick later. This applies to items that go to the main inventory player tools inventory and quickbar.

So I don't know if the API description is wrong or its a bug with the timing of the event firing within the Factorio core code?

Code run to test in game

Code: Select all

/c
itemName = "iron-chest"

ItemCrafted = function(event, text)
	script.on_nth_tick(1, nil)
	local player = game.players[event.player_index]
	if event.item_stack.name ~= itemName then return end
	
	local toolSearch = player.get_inventory(defines.inventory.player_tools).find_item_stack(itemName)
	if toolSearch ~= nil then
		game.print(text .. " : player tools : " .. itemName .. " found")
	else
		game.print(text .. " : player tools : " .. itemName .. " NOT found")
	end
	
	local inventorySearch = player.get_inventory(defines.inventory.player_main).find_item_stack(itemName)
	if inventorySearch ~= nil then
		game.print(text .. " : player main inventory : " .. itemName .. " found")
	else
		game.print(text .. " : player main inventory : " .. itemName .. " NOT found")
	end
	
	local quickbarSearch = player.get_inventory(defines.inventory.player_quickbar).find_item_stack(itemName)
	if quickbarSearch ~= nil then
		game.print(text .. " : player quickbar : " .. itemName .. " found")
	else
		game.print(text .. " : player quickbar : " .. itemName .. " NOT found")
	end
end

QueueItemCrafted = function(event)
	ItemCrafted(event, "instant")
	script.on_nth_tick(1, function() ItemCrafted(event, "delayed") end)
end

script.on_event(defines.events.on_player_crafted_item, QueueItemCrafted)
Reproduction Steps:
  1. Started a new game
  2. Ran the above code
  3. Crafted an iron chest and it appears in my quickbar.
  4. logging on screen shows "instant" doesn't find the item in any of the inventories, the "delayed" does find it where it is.
This test can be repeated for an iron-axe that goes to the player tools inventory. just update the first line of code from "iron-chest" to "iron-axe".

Rseding91
Factorio Staff
Factorio Staff
Posts: 13198
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.16.51] on_player_crafted_item event description wrong or event execution order bug

Post by Rseding91 »

Thanks for the report. The description of the event is wrong. It's called *just* before the item is put into the players inventory. I'll fix the description and change the event while I'm at it so you can modify the stack during the event (before it's put into the player).
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Resolved Problems and Bugs”