Rewrite of inventory click handling code

The old code was sloppy and didn't seem to support newer inventory features. Hopefully this will fix most of the pearl-freeing issues that we're having.