Getting the owner of a tamed wolf

Hello,
I’m trying to make a port of my plugin to Sponge and I’m trying to count mobs killed by a player.

Since players can tame wolves and those wolves can kill mobs, it’d be nice to count their kills as player’s kills.
On Bukkit the Wolf entity has isTamed method and getOwner method that allow this.

I tried to find the answer on Google, but I mostly got wikipedia pages about wolf taming and washing wolves with sponges - so I’m asking here. One Sponge answer was from 2016 stating that AI was not yet implemented - I’m not sure if that is still the case.

TL;DR:
When I have the Wolf entity, how do I get the UUID of the Player that has tamed the wolf?

Edit: Wolf seems to have getCreator() method that returns an UUID, I’ll try that

Google might be nice, but it’s no developer. The place to look is the Sponge JavaDocs, which contains everything documented in the API in the standard JavaDoc format.

Finding Wolf was easy enough, and by looking at it’s inherited methods getCreator is inherited from Entity and returns the UUID of the player that spawned the entity, not necessarily tamed it. A quick search for tame immediately brought up TameableData, which has an owner method returning the optional UUID value. For good measure, I checked the data implementation issue to make sure it’s been implemented, which it is (most of them are in now, but it was a lot more spotty when I started and it’s become habbit).

The javadoc for TameableData also notes Keys.TAMED_OWNER, which is the final answer to your question. Once you have the entity, you can get Keys.TAMED_OWNER from it which will return an Optional<UUID>. The rest should be simple enough to piece together.

2 Likes

Thank you for the answer :slight_smile:

I’m still a bit new to the data api so I appreciate that you wrote down the process of finding the keys related to the data from the JavaDocs - That’s something I wouldn’t have figured on my own.