Keep track of an animal

Discussion in 'Plugin Development' started by Xibalba7, Jul 18, 2014.

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

    Xibalba7

    Hi, I am looking for a good way to allow players to teleport to their horses or the opposite.

    It would be something like : /register_the_horse_i_am_on -> /teleport horse1 on_my_current_location or /teleport me on_horse1_location.

    I am actually able to do this but not in a pretty way, and occasionally a bug happens.

    This is how I am actually doing this :
    1. Every registered horse has its uuid stored along with its location (world, x, y, z)
    2. If a player want to teleport himself or the horse when the horse is spawned, then he/it just teleport.
    3. If a horse's chunk get unloaded, the horse location is stored and the chunk is allowed to unload.
    4. I a player want to teleport to a horse, he just get teleported to the location that was previously stored.
    5. If a player want to teleport his horse to him, the chunk matching the last stored location of this horse is loaded (if the horse is not spawned), the horse get teleported to the player and then the chunk is unloaded.
    Would you have a proper way to do this ?
    Please tell me if my english/explanations are not clear.
    Xibalba7.
     
  2. Offline

    xmarinusx

    Xibalba7 It's better to save the entity's UUID, this is a unique identifier for the entity. You can find the entity by this UUID again too.
     
  3. Offline

    Xibalba7

    xmarinusx

    Well I am already saving its uuid but I could not figure how to get a despawned entity by its uuid :/

    If you could tell me what is this method, it would be really appreciated :p
     
  4. Offline

    xmarinusx

    Use world.getEntities() to get all entities. Loop through that list until you find your entity with your UUID. Then just use entity.despawn() to despawn the entity.
     
  5. Offline

    teej107

  6. Offline

    xmarinusx

    teej107 Yep, if you're looking for a LivingEntity you can better use that. And as you're looking for a horse that's the case.
     
  7. Offline

    Xibalba7

    Oh.. I thought get(Living)Entities only return spawned entities -_-

    Well, thank you^^

    Well I think I was right after all...

    xmarinusx How do you manage to get an Entity that is not spawned with the method world.getEntity() ?

    Maybe have you misunderstood what I was looking for ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  8. Offline

    xmarinusx

    If the horse is not spawned yet, how can you teleport to it? Please be a bit more specific about what you really want.
     
  9. Offline

    Xibalba7

    xmarinusx Well I explained that on the first post but what I want my plugin to do is the following :
    1. A player claim a horse as his own by typing /horse claim
    2. The horse uuid is stored is stored in saves.yml with its owner's uuid
    3. The player loses his horse -> the horse is too far and then despawn when its chunk is unloaded, like any other animal
    4. The player wants his horse back and types /horse teleporthere
    The way I am doing this is by storing the horse's location when it despawn. When a player wants his horse back, the chunk matching the horse's location is loaded and the horse teleported.
     
  10. Offline

    fireblast709

    Aren't horses (and all tamed animals and mobs with nametags) persistent?
     
  11. Offline

    Xibalba7

    If you ask if they are removed when too far away from players, no. But they are despawned when their chunk is unloaded.
     
  12. Offline

    xmarinusx

    Xibalba7 If you know the position of the horse, or at least the chunk it's in, you can load the chunk and the horse should get loaded too I think. This means it should get in the world's (Living)Entities list.
     
  13. Offline

    Xibalba7

    I am already doing this but as I said in my first post :
    What I am looking for is a custom method like getDespawnedEntities() that would be the equivalent of getOfflinePlayers() for entities.
     
  14. Offline

    xmarinusx

    As far as I know that's impossible. I imagine such despawned entities list would have a massive size, so looping over it won't give a better performance I think. Just loading the chunk to teleport the horse and then unloading it again seems the best and easiest option for me.
    What bug happens with it?
     
  15. Offline

    Xibalba7

    It's hard to tell but one time the wanted horse could not be foud on the chunk for some reason, and on two other occasions the horses duplicated : there were two different horse with the same uuid on the same world.
     
  16. Offline

    xmarinusx

    UUIDs are supposed to be unique, it should be impossible to get two horses with the same UUID. I can't see why the horse couldn't be found though.
     
Thread Status:
Not open for further replies.

Share This Page