1.16.5 Code isn't printing all the Entities in World

Discussion in 'Spigot Plugin Development' started by CaffeineNYC, Jun 2, 2021.

  1. I'm using the following code to print out all the Entity and LivingEntity for each world. The problem is that it's printing out a different number of entities each time I reboot server. The first time I ran it, I didn't even see any Villagers... just mobs. I've seen as few as 19 Entities printed and as many as 178 (which I still think is a gross under-estimate).

    My *guess* is that Minecraft kicks off a thread for each plugin's onEnable() override, and that my plugin's thread ends before the code finishes, but this is a totally uneducated guess. Printing to the screen (or any kind of I/O) takes a relatively long time compared to processing.

    Why is my code not working as expected?

    Code (Text):

    public class Main extends JavaPlugin
    {
       @Override
       public void onEnable()
       {
           listEntities();
       }
       

       public void listEntities()
       {
           for ( World w : Bukkit.getServer().getWorlds() )
           {
               String worldName = w.getName();
               System.out.println( "\n\n\nWorld: " + worldName + " Entity list:" );

               for ( Entity e : w.getEntities() )
                   System.out.println( e.getName() + ", " + e.getUniqueId() + ", " + e.getCustomName() + ", " + e.getType() + ", " + e.getEntityId() );

               System.out.println( "\n\n\nWorld: " + worldName + " LivingEntity list:" );

               for ( LivingEntity e : w.getLivingEntities() )
                   System.out.println( e.getName() + ", " + e.getUniqueId() + ", " + e.getCustomName() + ", " + e.getType() + ", " + e.getEntityId() );
           }
       }

     
     
  2. World#getEntities() prolly is only returning the loaded entities.
     
  3. Yeah, it's only returning the loaded entities. You could load every chunk and then count the entities, but that is a very very bad idea
     
    • Agree Agree x 1