Getting a players tamed wolf

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

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

    Areoace

    Is there any way to do so?
     
  2. Offline

    GusGold

  3. Offline

    Areoace

    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

    player.getWolf();
     
  4. Offline

    GusGold

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

    Alshain01

    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()

    Code:java
    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.

    Code:java
    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. }
    7.  
    8. if(e instanceof Wolf) {
    9. ((Wolf)e).setSitting(false);
    10. }
    11. ((Tameable)e).setOwner(null);
    12. }
    13. }
     
    NathanWolf likes this.
  6. Offline

    Garris0n


    lol...
     
  7. Offline

    Alshain01

    Garris0n lol, so I guess it's just iterating through them all anyway. Would have thought Bukkit would use a better method /facepalm
     
  8. Offline

    Garris0n

    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.
     
  9. Offline

    Alshain01

    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.
     
  10. Offline

    Garris0n

    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.
     
  11. Offline

    Alshain01

    I'm just so spoiled by LINQ
     
Thread Status:
Not open for further replies.

Share This Page