Get All Entities

Discussion in 'Plugin Development' started by DestinyEclipse, Jul 28, 2013.

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

    DestinyEclipse

    I'm trying to look for every tamed horse by the current player in all worlds of the server.
    But, once I move a little over 200 blocks away, the horses no longer show up in the returned entities.
    My code I'm using is below:
    Code:
    for (World world : (new HorseGuard().getServer().getWorlds())) {
                for (Entity e : world.getEntities()) {
                    if (e.getType() == EntityType.HORSE) {
                        Horse h = (Horse) e;
                        if (h.isTamed()) {
                            if (h.getOwner() != null) {
                                if (h.getOwner().getName().equalsIgnoreCase(p.getName())) {
                                    //Handle horses
                                }
                            }
                        }
                    }
                }
            }
     
  2. Offline

    CaptainBern Retired Staff

    Try using a scheduler, and also use == only for primitive types like Integers, use .equals() instead.
     
  3. Offline

    DestinyEclipse

    @CaptianBern
    I've tried using a scheduler. Is there anyway to just get all the entites for that one player?
     
  4. Offline

    Alex5657

    CaptainBern == is also used when checking for null and enum values, like here. So everything is fine with the "==".
    DestinyEclipse I think the entites are temporarily removed from the world object when no player is near. You could write them to a list and then remove them if they are despawned by listening for EntityDespawnEvent. What are you trying to do? Maybe I can suggest a better way
     
  5. Offline

    DestinyEclipse

    Alex5657 I'm trying to get all the horses tamed by a player in each world (Normal, Nether, End). But after I did a test of 156 blocks away, the list of the horses tamed by the player return null since the entities no longer are there. I tried storing the Entity in a Serializer but that of course didn't work.
     
  6. Offline

    Alex5657

    Hmm... what you can do is perform that code in onEnable to get all the entities when the server starts to write it all to a list. Then you can track all EntityTameEvent (whatever they are called), check if its a horse and add it to the list. You also will have to track the EntityDespawnEvent, once again check if its a tamed horse and remove it from the list.

    This will actually work fast then getting all entities from the world each time.

    Hope I helped ;)
     
    DestinyEclipse likes this.
  7. Offline

    DestinyEclipse

    It worked! Thank you so much!
    Alex5657


    Scratch that, it didn't work...

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

    Alex5657

    Err... How could it not work?
     
  9. Offline

    DestinyEclipse

    Alex5657
    I think because the entire world isn't loading, only loading chunks of it.
    I tried storing the location of each horse to load chunks of the world at a time but that didn't even work.
    I wish I could just load the Enitiy from a UUID, because I am storing those.
     
Thread Status:
Not open for further replies.

Share This Page