Upgraded the inventory handling API.

The previous API was bad as it was fiddling with memory and the inventory itself,
forcing the coder to remove one item from it whenever one needed info about it.

This was the root cause of objects being removed and added at the end of the item lists,
unsyncing them with the GUI, and provoking a wide range of errors.

Last part of, and fixes #359