[Tut] Active mob remover

Discussion in 'Plugin Development' started by Milkywayz, Apr 24, 2012.

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

    Milkywayz

    So basically part of my plugin blocks the spawn of mobs, that part works, theres a feature where if a mob targets you and its blocked it gets removed, same with if you hit the mob and its blocked. This is because if you don't restart the server and you block mobs through config, they don't just despawn, they need to be removed somehow, the code below is my first attempt at removing mobs. When a chunk is loaded, it removes the blocked mobs, somewhat useful :p. Also this can be used on chunk unload too. Hopefully this was never posted before so i don't look like a dumb***, i couldn't find anything like it so its probably not out there, this could be used for other arrays too.
    PHP:
        /*
        * Active mob remover by milkywayz
        */
        
    @EventHandler(priority EventPriority.LOWEST)
        public 
    void onChunkLoad(ChunkLoadEvent e) {
            
    /*
            * Check if the chunk load is in one of the worlds enabled in config
            */
            
    List<Stringworldz plugin.getConfig().getStringList(
                    
    "World.Worldname");
            for (
    String worldname worldz) {
                if (
    e.getWorld().getName().equals(worldname)) {
                    
    //Iterate through the entities in the chunk
                    
    for(Entity en e.getChunk().getEntities()) {
                        final 
    String mob en.getType().toString().toLowerCase();
                        
    /*
                        * In config theres a boolean called (disabled: mobs: blaze:, for example
                        * It's the exact same as the entity type just in lowercase, thats what the
                        * above variable is for, turning it into nice formating
                        */
                        
    if(plugin.getConfig().getBoolean("disabled.mobs." mob)) {
                            
    //The try and catch isnt nessacary but i though i might use it
                            //Because working with chunks can sometimes end badly
                            
    try {
                            
    en.remove();
                            
    //Remove the blocked mobs (might remove all mobs in that chunk, but a chunk is 16x16
                            //And the chances of other mobs being in there is usually low enough for this to be
                            //Worth the risk
                            
    } catch(NullPointerException ex) {
                                
    /*
                                * Dont know where a null could happen, but if theres one, catch it and log it
                                */
                                
    Spawnegg.log.log(Level.WARNINGex.getMessage());
                            }
                        }
                    }
                }
                }
        }
     
Thread Status:
Not open for further replies.

Share This Page