This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
areuz's profile picture

[Solved] How to properly use the "turtle_inventory" event

Started by areuz, 25 August 2019 - 01:06 PM
areuz #1
Posted 25 August 2019 - 03:06 PM
EDIT: Please move this into the proper category, I have no idea when I clicked on "tutorials" but this is definitely the wrong section! sorry

I'm using the latest CC: Tweaked 1.83.1
To optimize miners, I want to map the entire inventory into a table, and keep track of it during mining without manually looping over all of the slots with 'turtle.getItemDetail([slot])' as I don't know what impact that may have on server performance with 20+ turtles running.

The official Wiki page for this event shows a very basic unrealistic use case of this event, but does tell us, that it has no return values.
The in-game help message is a little bit more useful, suggesting that we use "comparison operations" to see what changes have been made.
Events fired by the Turtle API: "turtle_inventory" when any of the items in the inventory are changed. Use comparison operations to inspect the changes.

What "comparison operations" does this have in mind? It can't be 'turtle.compareTo(number slot)', or any of the other 'turtle.compare' methods. The same goes for all of the other API methods. If this just goes back "keep the old state of the inventory and on this event, iterate through the whole thing again and compare the slots"… then that's dissapoiting.


Note:
If this can't be done within the current API, could anyone willing and knowledgeable enough do a quick test if a return value (slot number / table slots that changed / etc) can be added to the event?
From a quick glance at the source, it seems plausible enough assuming the 'm_previousInventory' has not changed yet.

The calling function in 'main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java' does have access to the 'm_previousInventory', and 'm_inventory' - I just couldn't find where this function is called from and don't know if the inventories weren't synced by this point.

@Override
	public void update()
	{
		super.update();
		m_brain.update();
		if( !getWorld().isRemote && m_inventoryChanged )
		{
			ServerComputer computer = getServerComputer();
			if( computer != null ) computer.queueEvent( "turtle_inventory" );
			m_inventoryChanged = false;
			for( int n = 0; n < getSizeInventory(); n++ )
			{
				m_previousInventory.set( n, InventoryUtil.copyItem( getStackInSlot( n ) ) );
			}
		}
	}
Edited on 31 August 2019 - 10:08 PM
EveryOS #2
Posted 26 August 2019 - 07:53 PM
From the wiki, it looks like you could do comparisons using the `getItemDetail` method of `turtle` (and that is the only way).
https://www.computer...e.getItemDetail

As far as server impact, it should be near-0 for any server not running Windows 95 (:

Trust me, the code executes uber-fast
Edited on 26 August 2019 - 05:58 PM
Lyqyd #3
Posted 27 August 2019 - 11:28 PM
Moved to Ask a Pro.
areuz #4
Posted 01 September 2019 - 12:05 AM
From the wiki, it looks like you could do comparisons using the `getItemDetail` method of `turtle` (and that is the only way).
https://www.computer...e.getItemDetail

As far as server impact, it should be near-0 for any server not running Windows 95 (:

Trust me, the code executes uber-fast

Yep, thought about that, and might do some testing. Though the event still looks very unfinished without any return values. It's as if Windows didn't send info about a file change, but sent out a "Yeah something on the disk changed, figure it out".

Anyway, thanks for the confirmation!