Search in sources :

Example 1 with MobRemovalEvent

use of com.palmergames.bukkit.towny.event.MobRemovalEvent in project Towny by ElgarL.

the class MobRemovalTimerTask method run.

@Override
public void run() {
    // Build a list of mobs to be removed
    List<LivingEntity> livingEntitiesToRemove = new ArrayList<LivingEntity>();
    for (World world : server.getWorlds()) {
        TownyWorld townyWorld;
        // Filter worlds not registered
        try {
            townyWorld = TownyUniverse.getDataSource().getWorld(world.getName());
        } catch (NotRegisteredException e) {
            // World was not registered by Towny, so we skip all mobs in it.
            continue;
        } catch (NullPointerException ex) {
            // Spigot has unloaded this world.
            continue;
        }
        // Filter worlds not using towny.
        if (!townyWorld.isUsingTowny())
            continue;
        // Filter worlds that will always pass all checks in a world, regardless of possible conditions.
        if (townyWorld.isForceTownMobs() && townyWorld.hasWorldMobs())
            continue;
        //
        for (LivingEntity livingEntity : world.getLivingEntities()) {
            Location livingEntityLoc = livingEntity.getLocation();
            if (!livingEntityLoc.getChunk().isLoaded())
                continue;
            Coord coord = Coord.parseCoord(livingEntityLoc);
            try {
                TownBlock townBlock = townyWorld.getTownBlock(coord);
                // Check if mobs are always allowed inside towns in this world.
                if (townyWorld.isForceTownMobs())
                    continue;
                // Check if plot allows mobs.
                if (townBlock.getPermissions().mobs)
                    continue;
                // Check if the plot is registered to a town.
                Town town = townBlock.getTown();
                // Check if the town this plot is registered to allows mobs.
                if (town.hasMobs())
                    continue;
                // Check that Towny is removing this type of entity inside towns.
                if (!isRemovingTownEntity(livingEntity))
                    continue;
            } catch (NotRegisteredException x) {
                // Check if we're allowing mobs in unregistered plots in this world.
                if (townyWorld.hasWorldMobs())
                    continue;
                // Check that Towny is removing this type of entity in unregistered plots.
                if (!isRemovingWorldEntity(livingEntity))
                    continue;
            }
            // Check if entity is a Citizens NPC
            if (plugin.isCitizens2()) {
                if (CitizensAPI.getNPCRegistry().isNPC(livingEntity))
                    continue;
            }
            livingEntitiesToRemove.add(livingEntity);
        }
    }
    MobRemovalEvent mobRemovalEvent;
    for (LivingEntity livingEntity : livingEntitiesToRemove) {
        mobRemovalEvent = new MobRemovalEvent(livingEntity);
        plugin.getServer().getPluginManager().callEvent(mobRemovalEvent);
        if (!mobRemovalEvent.isCancelled()) {
            TownyMessaging.sendDebugMsg("MobRemoval Removed: " + livingEntity.toString());
            livingEntity.remove();
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) MobRemovalEvent(com.palmergames.bukkit.towny.event.MobRemovalEvent) ArrayList(java.util.ArrayList) World(org.bukkit.World) Location(org.bukkit.Location)

Aggregations

MobRemovalEvent (com.palmergames.bukkit.towny.event.MobRemovalEvent)1 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)1 ArrayList (java.util.ArrayList)1 Location (org.bukkit.Location)1 World (org.bukkit.World)1 LivingEntity (org.bukkit.entity.LivingEntity)1