For years, tourists visiting the picturesque Uluwatu temple in Bali have been warned about a local gang of marauding macaques who expertly snatch loose items you might have about your person, and then barter them back for food.

Sunglasses, flip-flops, hats, and even jewellery - everything is fair game, and temple staff have become experts at 'buying' the valuables back with pieces of food. But this behaviour is actually quite unusual in wild animals, and now for the first time researchers have some answers on what drives these long-tailed macaques to become thugs.

The fact that primates understand bartering is not surprising. In captive settings, researchers actually use token exchange to study various behaviours in monkeys.

In a famous example, primatologist Frans de Waal once discovered that capuchin monkeys have a strong reaction against inequality when taught to barter rocks for cucumbers or grapes. The outrage is palpable:

But humans teaching the concept of bartering to captive animals is one thing, whereas it looks like the macaques in Bali have arrived at the lucrative racket by themselves, and might be the only wild animals in the world to do so.

To figure out what's going on, a team of researchers went to Uluwatu in 2010, and spent four months observing and recording the behaviour of these monkey thieves.

The scientists identified four distinct social groups of macaques living around the temple, and made note of stealing attempts as they watched all of them interacting with tourists and temple staff.

The team recorded 201 'robbing and bartering' events, noting down the identity of the thief, which of the four groups the monkey belonged to, what object it tried to steal (glasses were the most popular) and whether it got a successful barter for it.

Below you can watch an extended episode of extortion, captured by one of the researchers, Jean-Baptiste Leca. The macaque swipes a pair of prescription glasses, refuses several food items until it gets a lump of crunchy fruit, then finally surrenders the loot:

The scientists found that these monkey jerks are actually a very special case of canny adaptation to an environment frequented by humans.

The monkeys learn the robbing and bartering techniques from each other and even pass them down to offspring. And the more time they spend around humans, the better thieves they become.

"[O]ur findings indicate that robbing and bartering is a good candidate for a new behavioural tradition defined as a group-/population-specific practice, socially transmitted among at least some group members, persistent over several generations, and possibly locally adaptive," the team writes in their paper.

Of the four groups living near the temple, two were hanging around the tourist-dense areas more often and were also the best at stealing. Another correlation - if your social group has more young male macaques, there will be more stealing.

In an interesting twist, a fifth group of monkeys who previously weren't exposed to humans, started hanging near the temple during the time of the original study.

A follow-up trip revealed that a few years later these new monkeys had also picked up tips from the local mafia and were successfully trading valuables for food (that particular data is yet to be published).

All this points to a fascinating case of social and cultural learning, and the scientists who published the small, preliminary study are hoping to do future work with larger datasets (more monkeys, more stealing).

What's crazy is that the behaviour appears to be population-specific despite other places in Bali providing other monkeys with very similar opportunities.

For example, monkeys in Ubud Monkey Forest in central Bali also live near troves of tourists and temple staff that they could be roping into shady bartering deals, but for now those macaques seem to be taking the high road.

It sure sucks to have your glasses broken by a monkey, but we're definitely hoping to learn more about primate social learning from these amazing little thieves in the future.

And if you happen to visit Uluwatu, make sure to hold onto your GoPro.

The study was published in Primates.