Get Full List of Mobs on the server

Discussion in 'Spigot Plugin Development' started by emanon, Apr 20, 2017.

  1. Im attemping to remove some mobs, to do so i need to get a full list of the LivingEntitys and apply some conditions...

    the World class has some smart methods like
    getEntities and
    getLivingEntities

    so this is my code:

    Code (Text):
    for (int i = 0; i < Bukkit.getWorlds().size(); i++){
               
                List<LivingEntity> l = Bukkit.getWorlds().get(i).getLivingEntities();
                System.out.println("world: "+Bukkit.getWorlds().get(i).getName());

                for (int j = 0; j < l.size() ;j++){
                    System.out.println("examining "+l.get(j).getType());
                    //some conditions
                    System.out.println("removed "+l.get(j).getType());
                    l.get(j).remove();
                }
            }
           
            for (int i = 0; i < Bukkit.getWorlds().size(); i++){
                List<Entity> l = Bukkit.getWorlds().get(i).getEntities();
                System.out.println("world: "+Bukkit.getWorlds().get(i).getName());

                for (int j = 0; j < l.size() ;j++){
                    if (!(l.get(j) instanceof LivingEntity))
                         break;
                    System.out.println("examining "+l.get(j).getType());
                    //some conditions
                    System.out.println("removed "+l.get(j).getType());
                    l.get(j).remove();
                }
            }
    and its not worlking properly, it doesn't catch all the living entity, just some (no exceptions no errors)

    should i use a different method?
     
  2. minoneer

    Benefactor

    It will only return the currently loaded entities. Those in unloaded chunks will not be returned until the chunks are loaded.
     
    • Informative Informative x 1
  3. then how can i get a full list of entities even on unloaded chunks?
     
  4. Listen for when a chunk loads, and clear the mobs inside of said chunk.
     
  5. i need to do this once, when the server starts, chunks may be loaded and unloaded multiple times
     
  6. minoneer

    Benefactor

    There is no way to get entities from unloaded chunks, as they are saved within the chunk and only loaded into memory when the chunk itself is loaded. You can either remove them when they are loaded by the game (as @undead_guppy suggested), maybe marking the ones you already handled to avoid processing multiples times, or you have to load each generated chunk manually to do it all in one batch. That could potentially be a very expensive operation and it is not trivial to find all already generated chunks.
     
  7. i just need to do it once when the server restart so i don't really care if it need a couple of mins (or even more) to be done, i would like to find generated chunks but honestly i don't know where to start
     
  8. since its for my custom server which has a premade map i could know which area has to be checked and select chunks inside it, still im pretty sure thats not the right way to do it so im open to suggestions
     
  9. well what you could do is,
    Code (Text):

    for (Entity ent : // get entities list code)
    {
        ent.remove();

         // Yeah! It works! ;)
    }
     
    or use/get entities from net.minecraft World class by

    Code (Text):

    ((CraftWorld) world).getHandle() // <---- returns net.minecraft World class
     
  10. minoneer

    Benefactor

    That sounds like a valid approach. You somehow have to find all chunks wich are pregenerated - how you do it is up to you :)
     
  11. Loading all the mobs into memory at the same time would not be a good idea :) however, all currently loaded mobs should be fine
     
  12. i did the check for the chunks loading them one every tick, all togheter would make the server crash

    i erased serveral thousands of mobs, mobs spawning was disabled, for testing i run the erase mob command once again and it found some more mobs (much less than the first time) in the same area which weren't erased...

    so this method is not 100% working, not sure why, maybe when called, the garbage collector unloads the chunks before i delete the mobs on the chunk