/** Called when a block is broken. */
  @Override
  public void onBlockBreak(BlockBreakEvent event) {
    if (event.isCancelled()) {
      return;
    }

    Player player = event.getPlayer();
    WorldConfiguration wcfg = getWorldConfig(player);

    if (!wcfg.itemDurability) {
      ItemStack held = player.getItemInHand();
      if (held.getTypeId() > 0) {
        held.setDurability((short) -1);
        player.setItemInHand(held);
      }
    }

    if (!plugin.getGlobalRegionManager().canBuild(player, event.getBlock())) {
      player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
      event.setCancelled(true);
      return;
    }

    if (wcfg.getBlacklist() != null) {
      if (!wcfg.getBlacklist()
          .check(
              new BlockBreakBlacklistEvent(
                  plugin.wrapPlayer(player),
                  toVector(event.getBlock()),
                  event.getBlock().getTypeId()),
              false,
              false)) {
        event.setCancelled(true);
        return;
      }

      if (!wcfg.getBlacklist()
          .check(
              new DestroyWithBlacklistEvent(
                  plugin.wrapPlayer(player),
                  toVector(event.getBlock()),
                  player.getItemInHand().getTypeId()),
              false,
              false)) {
        event.setCancelled(true);
        return;
      }
    }

    if (wcfg.isChestProtected(event.getBlock(), player)) {
      player.sendMessage(ChatColor.DARK_RED + "The chest is protected.");
      event.setCancelled(true);
      return;
    }
  }
  @Override
  public void onSnowForm(SnowFormEvent event) {
    if (event.isCancelled()) {
      return;
    }

    if (!plugin
        .getGlobalRegionManager()
        .allows(DefaultFlag.SNOW_FALL, event.getBlock().getLocation())) {
      event.setCancelled(true);
    }
  }
  /**
   * Called when a player places a block
   *
   * @param event Relevant event details
   */
  @Override
  public void onBlockPlace(BlockPlaceEvent event) {

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

    Block blockPlaced = event.getBlock();
    Player player = event.getPlayer();
    World world = blockPlaced.getWorld();

    ConfigurationManager cfg = plugin.getGlobalConfiguration();
    WorldConfiguration wcfg = cfg.get(world);

    if (wcfg.useRegions) {
      if (!plugin.getGlobalRegionManager().canBuild(player, blockPlaced.getLocation())) {
        player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
        event.setCancelled(true);
        return;
      }
    }

    if (wcfg.getBlacklist() != null) {
      if (!wcfg.getBlacklist()
          .check(
              new BlockPlaceBlacklistEvent(
                  plugin.wrapPlayer(player), toVector(blockPlaced), blockPlaced.getTypeId()),
              false,
              false)) {
        event.setCancelled(true);
        return;
      }
    }

    if (wcfg.simulateSponge && blockPlaced.getTypeId() == 19) {
      if (wcfg.redstoneSponges && blockPlaced.isBlockIndirectlyPowered()) {
        return;
      }

      int ox = blockPlaced.getX();
      int oy = blockPlaced.getY();
      int oz = blockPlaced.getZ();

      clearSpongeWater(world, ox, oy, oz);
    }
  }
  /** Called when a block is damaged. */
  @Override
  public void onBlockDamage(BlockDamageEvent event) {
    if (event.isCancelled()) {
      return;
    }

    Player player = event.getPlayer();
    Block blockDamaged = event.getBlock();

    // Cake are damaged and not broken when they are eaten, so we must
    // handle them a bit separately
    if (blockDamaged.getType() == Material.CAKE_BLOCK) {
      if (!plugin.getGlobalRegionManager().canBuild(player, blockDamaged)) {
        player.sendMessage(ChatColor.DARK_RED + "You're not invited to this tea party!");
        event.setCancelled(true);
        return;
      }
    }
  }
  /**
   * Called when a block is destroyed from burning
   *
   * @param event Relevant event details
   */
  @Override
  public void onBlockBurn(BlockBurnEvent event) {

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

    ConfigurationManager cfg = plugin.getGlobalConfiguration();
    WorldConfiguration wcfg = cfg.get(event.getBlock().getWorld());

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

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

    if (wcfg.disableFireSpreadBlocks.size() > 0) {
      Block block = event.getBlock();

      if (wcfg.disableFireSpreadBlocks.contains(block.getTypeId())) {
        event.setCancelled(true);
        return;
      }
    }

    if (wcfg.useRegions) {
      Block block = event.getBlock();
      Vector pt = toVector(block);
      RegionManager mgr = plugin.getGlobalRegionManager().get(block.getWorld());
      ApplicableRegionSet set = mgr.getApplicableRegions(pt);

      if (!set.allows(DefaultFlag.FIRE_SPREAD)) {
        event.setCancelled(true);
        return;
      }
    }
  }
  /** Called when a block gets ignited. */
  @Override
  public void onBlockIgnite(BlockIgniteEvent event) {
    if (event.isCancelled()) {
      return;
    }

    IgniteCause cause = event.getCause();
    Block block = event.getBlock();
    World world = block.getWorld();

    ConfigurationManager cfg = plugin.getGlobalConfiguration();
    WorldConfiguration wcfg = cfg.get(world);

    boolean isFireSpread = cause == IgniteCause.SPREAD;

    if (wcfg.preventLightningFire && cause == IgniteCause.LIGHTNING) {
      event.setCancelled(true);
      return;
    }

    if (wcfg.preventLavaFire && cause == IgniteCause.LAVA) {
      event.setCancelled(true);
      return;
    }

    if (wcfg.disableFireSpread && isFireSpread) {
      event.setCancelled(true);
      return;
    }
    /*
    if (wcfg.blockLighter && cause == IgniteCause.FLINT_AND_STEEL) {
        event.setCancelled(true);
        return;
    }
    */
    if (wcfg.fireSpreadDisableToggle && isFireSpread) {
      event.setCancelled(true);
      return;
    }

    if (wcfg.disableFireSpreadBlocks.size() > 0 && isFireSpread) {
      int x = block.getX();
      int y = block.getY();
      int z = block.getZ();

      if (wcfg.disableFireSpreadBlocks.contains(world.getBlockTypeIdAt(x, y - 1, z))
          || wcfg.disableFireSpreadBlocks.contains(world.getBlockTypeIdAt(x + 1, y, z))
          || wcfg.disableFireSpreadBlocks.contains(world.getBlockTypeIdAt(x - 1, y, z))
          || wcfg.disableFireSpreadBlocks.contains(world.getBlockTypeIdAt(x, y, z - 1))
          || wcfg.disableFireSpreadBlocks.contains(world.getBlockTypeIdAt(x, y, z + 1))) {
        event.setCancelled(true);
        return;
      }
    }

    if (wcfg.useRegions) {
      Vector pt = toVector(block);
      Player player = event.getPlayer();
      RegionManager mgr = plugin.getGlobalRegionManager().get(world);
      ApplicableRegionSet set = mgr.getApplicableRegions(pt);

      if (player != null && !plugin.getGlobalRegionManager().hasBypass(player, world)) {
        LocalPlayer localPlayer = plugin.wrapPlayer(player);

        if (cause == IgniteCause.FLINT_AND_STEEL && !set.canBuild(localPlayer)) {
          event.setCancelled(true);
          return;
        }

        if (cause == IgniteCause.FLINT_AND_STEEL && !set.allows(DefaultFlag.LIGHTER)) {
          event.setCancelled(true);
          return;
        }
      }

      if (wcfg.highFreqFlags && isFireSpread && !set.allows(DefaultFlag.FIRE_SPREAD)) {
        event.setCancelled(true);
        return;
      }

      if (wcfg.highFreqFlags && cause == IgniteCause.LAVA && !set.allows(DefaultFlag.LAVA_FIRE)) {
        event.setCancelled(true);
        return;
      }
    }
  }
  /** Called when fluids flow. */
  @Override
  public void onBlockFromTo(BlockFromToEvent event) {
    if (event.isCancelled()) {
      return;
    }

    World world = event.getBlock().getWorld();
    Block blockFrom = event.getBlock();
    Block blockTo = event.getToBlock();

    boolean isWater = blockFrom.getTypeId() == 8 || blockFrom.getTypeId() == 9;
    boolean isLava = blockFrom.getTypeId() == 10 || blockFrom.getTypeId() == 11;

    ConfigurationManager cfg = plugin.getGlobalConfiguration();
    WorldConfiguration wcfg = cfg.get(event.getBlock().getWorld());

    if (wcfg.simulateSponge && isWater) {
      int ox = blockTo.getX();
      int oy = blockTo.getY();
      int oz = blockTo.getZ();

      for (int cx = -wcfg.spongeRadius; cx <= wcfg.spongeRadius; cx++) {
        for (int cy = -wcfg.spongeRadius; cy <= wcfg.spongeRadius; cy++) {
          for (int cz = -wcfg.spongeRadius; cz <= wcfg.spongeRadius; cz++) {
            Block sponge = world.getBlockAt(ox + cx, oy + cy, oz + cz);
            if (sponge.getTypeId() == 19
                && (!wcfg.redstoneSponges || !sponge.isBlockIndirectlyPowered())) {
              event.setCancelled(true);
              return;
            }
          }
        }
      }
    }

    /*if (plugin.classicWater && isWater) {
    int blockBelow = blockFrom.getRelative(0, -1, 0).getTypeId();
    if (blockBelow != 0 && blockBelow != 8 && blockBelow != 9) {
    blockFrom.setTypeId(9);
    if (blockTo.getTypeId() == 0) {
    blockTo.setTypeId(9);
    }
    return;
    }
    }*/

    // Check the fluid block (from) whether it is air.
    // If so and the target block is protected, cancel the event
    if (wcfg.preventWaterDamage.size() > 0) {
      int targetId = blockTo.getTypeId();

      if ((blockFrom.getTypeId() == 0 || isWater) && wcfg.preventWaterDamage.contains(targetId)) {
        event.setCancelled(true);
        return;
      }
    }

    if (wcfg.allowedLavaSpreadOver.size() > 0 && isLava) {
      int targetId = blockTo.getRelative(0, -1, 0).getTypeId();

      if (!wcfg.allowedLavaSpreadOver.contains(targetId)) {
        event.setCancelled(true);
        return;
      }
    }

    if (wcfg.highFreqFlags
        && isWater
        && !plugin
            .getGlobalRegionManager()
            .allows(DefaultFlag.WATER_FLOW, blockFrom.getLocation())) {
      event.setCancelled(true);
      return;
    }

    if (wcfg.highFreqFlags
        && isLava
        && !plugin
            .getGlobalRegionManager()
            .allows(DefaultFlag.LAVA_FLOW, blockFrom.getLocation())) {
      event.setCancelled(true);
      return;
    }
  }