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

Post your bugs and problems so we can fix them.
Post Reply
User avatar
Muppet9010
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Sat Dec 09, 2017 6:01 pm

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

Post by Muppet9010 » Sun Nov 18, 2018 5:47 pm

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".

Post Reply

Return to “Bug Reports”