Getting a players tamed wolf

Discussion in 'Plugin Development' started by Areoace, Nov 20, 2013.

Thread Status:
Not open for further replies.
  1. Offline


    Is there any way to do so?
  2. Offline


    I want to check if the player has any wolves, I have not got them defined, how would i call it out of the blue e.g

  3. Areoace
    An (ugly) solution would be to get all the entities in the world into a list
    1. List<Entity> entities = world.getEntities();
    and check each one if they are a wolf
    1. for (Entitiy entity : entities){
    2. if (entity instanceof Wolf){
    if so, check if they are tamed
    1. if (((Tameable) entity).isTamed()){
    and then check if the player is the owner
    1. if (((Tameable) entity).getOwner().getName().equals(player.getName())){
    2. //Player owns this wolf
    and then finish off by closing our ifs and fors
    1. }
    2. }
    3. }
    4. }
    to give us
    1. List<Entity> entities = world.getEntities();
    2. for (Entitiy entity : entities){
    3. if (entity instanceof Wolf){
    4. if (((Tameable) entity).isTamed()){
    5. if (((Tameable) entity).getOwner().getName().equals(player.getName())){
    6. //Player owns this wolf
    7. }
    8. }
    9. }
    10. }

    Note: I didn't test the code, nor did I use an IDE, so if there are errors, let me know and i'll try to fix them. Also, don't forget to tahg me with GusGold if you want a reply :)
    NathanWolf likes this.
  4. GusGold is on the right track but cycling through every entity on the server is time consuming. I actually did this recently in and found you can trim that down significantly using World.getEntitiesByClasses()

    1. for(World w : Bukkit.getWorlds()) {
    2. for(Entity e : w.getEntitiesByClasses(Wolf.class)) {
    3. // do stuff
    4. }
    5. }

    Doing this you can also safely cast e to Wolf or Tameable without testing because all the entities are wolves and all wolves are tameable.

    The purpose of my plugin was to remove all tamed animals when a player dies (a hardcore functionality). So if you want a bigger example, this was my entire loop verbatim.

    1. for(Entity e : w.getEntitiesByClasses(Ocelot.class, Wolf.class)) {
    2. if(((Tameable)e).isTamed() && ((Tameable)e).getOwner().getName().equals(player.getName())) {
    3. if(e instanceof Ocelot) {
    4. ((Ocelot)e).setCatType(Type.WILD_OCELOT);
    5. ((Ocelot)e).setSitting(false);
    6. }
    8. if(e instanceof Wolf) {
    9. ((Wolf)e).setSitting(false);
    10. }
    11. ((Tameable)e).setOwner(null);
    12. }
    13. }
    NathanWolf likes this.

  5. lol...
  6. Garris0n lol, so I guess it's just iterating through them all anyway. Would have thought Bukkit would use a better method /facepalm
  7. There's literally no better way to do it. Also, your code is technically more expensive than iterating yourself because you're iterating through and checking the instances, then checking the instances again.
  8. Well, there is actually, but it would mean storing them in a better way too which would require minecraft editing, they don't want to do that I'm sure.
  9. Yes, you could make a bunch of lists and store each type of entity separately, which would probably end in more resource usage than just iterating through them.
  10. I'm just so spoiled by LINQ
Thread Status:
Not open for further replies.

Share This Page