public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    Entity attacker = event.getDamager();
    Entity defender = event.getEntity();

    if (defender instanceof Player) {
      Player player = (Player) defender;

      GlobalConfiguration cfg = plugin.getGlobalConfiguration();
      WorldConfiguration wcfg = cfg.getWorldConfig(player.getWorld().getName());

      if (cfg.isInvinciblePlayer(player.getName())) {
        event.setCancelled(true);
        return;
      }

      if (attacker != null && attacker instanceof Player) {
        if (wcfg.useRegions) {
          Vector pt = toVector(defender.getLocation());
          RegionManager mgr =
              plugin.getGlobalRegionManager().getRegionManager(player.getWorld().getName());

          if (!mgr.getApplicableRegions(pt).isStateFlagAllowed(Flags.PVP)) {
            ((Player) attacker).sendMessage(ChatColor.DARK_RED + "You are in a no-PvP area.");
            event.setCancelled(true);
            return;
          }
        }
      }

      if (attacker != null && attacker instanceof Monster) {
        if (attacker instanceof Creeper && wcfg.blockCreeperExplosions) {
          event.setCancelled(true);
          return;
        }

        if (wcfg.useRegions) {
          Vector pt = toVector(defender.getLocation());
          RegionManager mgr =
              plugin.getGlobalRegionManager().getRegionManager(player.getWorld().getName());
          ApplicableRegionSet set = mgr.getApplicableRegions(pt);

          if (!set.isStateFlagAllowed(Flags.MOB_DAMAGE)) {
            event.setCancelled(true);
            return;
          }

          if (attacker instanceof Creeper) {
            if (!set.isStateFlagAllowed(Flags.CREEPER_EXPLOSION)) {
              event.setCancelled(true);
              return;
            }
          }
        }
      }
    }
  }
  @Override
  public void onEntityExplode(EntityExplodeEvent event) {

    if (event.isCancelled()) {
      return;
    }

    GlobalConfiguration cfg = plugin.getGlobalConfiguration();
    Location l = event.getLocation();
    WorldConfiguration wcfg = cfg.getWorldConfig(l.getWorld().getName());

    if (event.getEntity() instanceof LivingEntity) {

      if (wcfg.blockCreeperBlockDamage) {
        event.setCancelled(true);
        return;
      }

      if (wcfg.blockCreeperExplosions) {
        event.setCancelled(true);
        return;
      }

      if (wcfg.useRegions) {
        Vector pt = toVector(l);
        RegionManager mgr = plugin.getGlobalRegionManager().getRegionManager(wcfg.getWorldName());

        if (!mgr.getApplicableRegions(pt).isStateFlagAllowed(Flags.CREEPER_EXPLOSION)) {
          event.setCancelled(true);
          return;
        }
      }
    } else { // Shall assume that this is TNT
      if (wcfg.blockTNT) {
        event.setCancelled(true);
        return;
      }

      if (wcfg.useRegions) {
        Vector pt = toVector(l);
        RegionManager mgr = plugin.getGlobalRegionManager().getRegionManager(wcfg.getWorldName());

        if (!mgr.getApplicableRegions(pt).isStateFlagAllowed(Flags.TNT)) {
          event.setCancelled(true);
          return;
        }
      }
    }
  }
  public void onEntityDamageByBlock(EntityDamageByBlockEvent event) {

    Entity defender = event.getEntity();
    DamageCause type = event.getCause();

    if (defender instanceof Player) {
      Player player = (Player) defender;

      GlobalConfiguration cfg = plugin.getGlobalConfiguration();
      WorldConfiguration wcfg = cfg.getWorldConfig(player.getWorld().getName());

      if (cfg.isInvinciblePlayer(player.getName())) {
        event.setCancelled(true);
        return;
      }

      if (wcfg.disableLavaDamage && type == DamageCause.LAVA) {
        event.setCancelled(true);
        return;
      }

      if (wcfg.disableContactDamage && type == DamageCause.CONTACT) {
        event.setCancelled(true);
        return;
      }
    }
  }
  public void registerEvents() {

    PluginManager pm = plugin.getServer().getPluginManager();

    pm.registerEvent(Event.Type.ENTITY_DAMAGED, this, Priority.High, plugin);
    pm.registerEvent(Event.Type.ENTITY_EXPLODE, this, Priority.High, plugin);
    pm.registerEvent(Event.Type.CREATURE_SPAWN, this, Priority.High, plugin);
  }
  @Override
  public void onCreatureSpawn(CreatureSpawnEvent event) {
    if (event.isCancelled()) {
      return;
    }

    GlobalConfiguration cfg = plugin.getGlobalConfiguration();
    WorldConfiguration wcfg = cfg.getWorldConfig(event.getEntity().getWorld().getName());

    // CreatureType creaType = (CreatureType) CreatureType.valueOf(event.getMobType().toString());
    CreatureType creaType = event.getCreatureType();
    String creaName = "";
    Boolean cancelEvent = false;

    if (wcfg.blockCreatureSpawn.contains(creaType)) {
      cancelEvent = true;
    }

    if (wcfg.useRegions) {
      Vector pt = toVector(event.getEntity().getLocation());
      RegionManager mgr =
          plugin.getGlobalRegionManager().getRegionManager(event.getEntity().getWorld().getName());

      Boolean flagValue =
          mgr.getApplicableRegions(pt)
              .getStringFlag(Flags.DENY_SPAWN, true)
              .getValue("")
              .contains(creaType.getName());
      if (flagValue != null) {
        if (flagValue) {
          cancelEvent = true;
        } else {
          cancelEvent = false;
        }
      }
    }

    if (cancelEvent) {
      event.setCancelled(true);
      return;
    }
  }
  @Override
  public void onEntityDamage(EntityDamageEvent event) {

    if (event.isCancelled()) {
      return;
    }

    if (event instanceof EntityDamageByProjectileEvent) {
      this.onEntityDamageByProjectile((EntityDamageByProjectileEvent) event);
      return;
    } else if (event instanceof EntityDamageByEntityEvent) {
      this.onEntityDamageByEntity((EntityDamageByEntityEvent) event);
      return;
    } else if (event instanceof EntityDamageByBlockEvent) {
      this.onEntityDamageByBlock((EntityDamageByBlockEvent) event);
      return;
    }

    Entity defender = event.getEntity();
    DamageCause type = event.getCause();

    if (defender instanceof Player) {
      Player player = (Player) defender;

      GlobalConfiguration cfg = plugin.getGlobalConfiguration();
      WorldConfiguration wcfg = cfg.getWorldConfig(player.getWorld().getName());

      if (cfg.isInvinciblePlayer(player.getName())) {
        event.setCancelled(true);
        return;
      }

      if (wcfg.disableFallDamage && type == DamageCause.FALL) {
        event.setCancelled(true);
        return;
      }

      if (wcfg.disableFireDamage && (type == DamageCause.FIRE || type == DamageCause.FIRE_TICK)) {
        event.setCancelled(true);
        return;
      }

      if (wcfg.disableDrowningDamage && type == DamageCause.DROWNING) {
        event.setCancelled(true);
        return;
      }

      if (wcfg.teleportOnSuffocation && type == DamageCause.SUFFOCATION) {
        findFreePosition(player);
        event.setCancelled(true);
        return;
      }

      if (wcfg.disableSuffocationDamage && type == DamageCause.SUFFOCATION) {
        event.setCancelled(true);
        return;
      }

      if (type == DamageCause.DROWNING && cfg.isAmphibiousPlayer(player.getName())) {
        event.setCancelled(true);
        return;
      }
    }
  }