/**
  * Remove a island from SkyBlock.
  *
  * @param l given location
  */
 public void removeIsland(Location l) {
   if (l != null) {
     int px = l.getBlockX();
     int py = l.getBlockY();
     int pz = l.getBlockZ();
     for (int x = -15; x <= 15; x++) {
       for (int y = -15; y <= 15; y++) {
         for (int z = -15; z <= 15; z++) {
           Block b = new Location(l.getWorld(), px + x, py + y, pz + z).getBlock();
           if (b.getType() != Material.AIR) {
             if (b.getType() == Material.CHEST) {
               Chest c = (Chest) b.getState();
               ItemStack[] items = new ItemStack[c.getInventory().getContents().length];
               c.getInventory().setContents(items);
             } else if (b.getType() == Material.FURNACE) {
               Furnace f = (Furnace) b.getState();
               ItemStack[] items = new ItemStack[f.getInventory().getContents().length];
               f.getInventory().setContents(items);
             } else if (b.getType() == Material.DISPENSER) {
               Dispenser d = (Dispenser) b.getState();
               ItemStack[] items = new ItemStack[d.getInventory().getContents().length];
               d.getInventory().setContents(items);
             }
             b.setType(Material.AIR);
           }
         }
       }
     }
   }
 }
  @EventHandler(priority = EventPriority.LOWEST)
  public void onInventoryOpenEvent(InventoryOpenEvent event) {
    if (event.isCancelled()
        || event.getInventory() == null
        || event.getInventory().getHolder() == null) return;

    final Player player = (Player) event.getPlayer();
    final InventoryHolder h = event.getInventory().getHolder();

    if (h instanceof Chest
        || h instanceof DoubleChest
        || h instanceof Furnace
        || h instanceof Dispenser) {
      if (player.getWorld() != plugin.getIslandWorld()) return;
      if (player.isOp()) return;
      if (player.hasPermission("islandworld.bypass.island")) return;

      Location loc = player.getLocation();
      if (event.getInventory().getHolder() instanceof Chest) {
        final Chest chest = (Chest) event.getInventory().getHolder();
        loc = chest.getLocation();
      } else if (event.getInventory().getHolder() instanceof Furnace) {
        final Furnace fur = (Furnace) event.getInventory().getHolder();
        loc = fur.getLocation();
      } else if (event.getInventory().getHolder() instanceof DoubleChest) {
        final DoubleChest chest = (DoubleChest) event.getInventory().getHolder();
        loc = chest.getLocation();
      } else if (event.getInventory().getHolder() instanceof Dispenser) {
        final Dispenser disp = (Dispenser) event.getInventory().getHolder();
        loc = disp.getLocation();
      }

      if (!(plugin.canBuildOnLocation(player, loc))) event.setCancelled(true);
    }

    plugin.debug(
        "Event :"
            + event.getEventName()
            + ", class: "
            + h.getClass()
            + ", cancelled:"
            + event.isCancelled());
  }
  /** Turns the factory on */
  public void powerOn() {
    // put active to true
    active = true;

    // Set attached lever
    if (FactoryModPlugin.LEVER_OUTPUT_ENABLED) {
      setActivationLever(true);
    }

    // lots of code to make the furnace light up, without loosing contents.
    Furnace furnace = (Furnace) factoryPowerSourceLocation.getBlock().getState();
    byte data = furnace.getData().getData();
    ItemStack[] oldContents = furnace.getInventory().getContents();
    furnace.getInventory().clear();
    factoryPowerSourceLocation.getBlock().setType(Material.BURNING_FURNACE);
    furnace = (Furnace) factoryPowerSourceLocation.getBlock().getState();
    furnace.setRawData(data);
    furnace.update();
    furnace.getInventory().setContents(oldContents);
    // reset the production timer
    currentProductionTimer = 0;
  }
  /** Turns the factory off. */
  public void powerOff() {
    if (active) {
      if (FactoryModPlugin.LEVER_OUTPUT_ENABLED) {
        setActivationLever(false);
      }

      // lots of code to make the furnace turn off, without loosing contents.
      Furnace furnace = (Furnace) factoryPowerSourceLocation.getBlock().getState();
      byte data = furnace.getData().getData();
      ItemStack[] oldContents = furnace.getInventory().getContents();
      furnace.getInventory().clear();
      factoryPowerSourceLocation.getBlock().setType(Material.FURNACE);
      furnace = (Furnace) factoryPowerSourceLocation.getBlock().getState();
      furnace.setRawData(data);
      furnace.update();
      furnace.getInventory().setContents(oldContents);

      // put active to false
      active = false;
      // reset the production timer
      currentProductionTimer = 0;
    }
  }
 @EventHandler(priority = EventPriority.NORMAL)
 public void onEntityExplode(EntityExplodeEvent event) {
   if (event.isCancelled() || event.getEntity() == null) return;
   Boolean cancel = false;
   EntityType entity = event.getEntityType();
   FlagPermissions perms = Residence.getPermsByLoc(event.getEntity().getLocation());
   if (entity == EntityType.CREEPER) {
     if (!perms.has("creeper", perms.has("explode", true))) {
       cancel = true;
     }
   }
   if (entity == EntityType.PRIMED_TNT) {
     if (!perms.has("tnt", perms.has("explode", true))) {
       cancel = true;
     }
   }
   if (entity == EntityType.FIREBALL) {
     if (!perms.has("fireball", perms.has("explode", true))) {
       event.setCancelled(true);
       event.getEntity().remove();
     }
   }
   if (cancel) {
     event.setCancelled(true);
     event.getEntity().remove();
   } else {
     for (Block block : event.blockList()) {
       FlagPermissions blockperms = Residence.getPermsByLoc(block.getLocation());
       if ((!blockperms.has("fireball", perms.has("explode", true))
               && entity == EntityType.FIREBALL)
           || (!blockperms.has("tnt", perms.has("explode", true))
               && entity == EntityType.PRIMED_TNT)
           || (!blockperms.has("creeper", perms.has("explode", true))
               && entity == EntityType.CREEPER)) {
         if (block != null) {
           ItemStack[] inventory = null;
           BlockState save = block.getState();
           if (block.getType() == Material.CHEST) {
             Chest chest = (Chest) save;
             inventory = chest.getInventory().getContents();
             chest.getInventory().clear();
           }
           if (block.getType() == Material.FURNACE
               || block.getType() == Material.BURNING_FURNACE) {
             Furnace furnace = (Furnace) save;
             inventory = furnace.getInventory().getContents();
             furnace.getInventory().clear();
           }
           if (block.getType() == Material.BREWING_STAND) {
             BrewingStand brew = (BrewingStand) save;
             inventory = brew.getInventory().getContents();
             brew.getInventory().clear();
           }
           if (block.getType() == Material.DISPENSER) {
             Dispenser dispenser = (Dispenser) save;
             inventory = dispenser.getInventory().getContents();
             dispenser.getInventory().clear();
           }
           if (block.getType() == Material.JUKEBOX) {
             Jukebox jukebox = (Jukebox) save;
             if (jukebox.isPlaying()) {
               inventory = new ItemStack[1];
               inventory[0] = new ItemStack(jukebox.getPlaying());
               jukebox.setPlaying(null);
             }
           }
           ExplodeRestore.put(save, inventory);
           block.setType(Material.AIR);
         }
       }
     }
     Residence.getServ()
         .getScheduler()
         .scheduleSyncDelayedTask(
             Residence.getServ().getPluginManager().getPlugin("Residence"),
             new Runnable() {
               public void run() {
                 for (BlockState block : ExplodeRestore.keySet().toArray(new BlockState[0])) {
                   ItemStack[] inventory = ExplodeRestore.get(block);
                   block.update(true);
                   if (inventory != null) {
                     if (block.getType() == Material.CHEST)
                       ((Chest) block.getLocation().getBlock().getState())
                           .getInventory()
                           .setContents(inventory);
                     if (block.getType() == Material.FURNACE
                         || block.getType() == Material.BURNING_FURNACE)
                       ((Furnace) block.getLocation().getBlock().getState())
                           .getInventory()
                           .setContents(inventory);
                     if (block.getType() == Material.BREWING_STAND)
                       ((BrewingStand) block.getLocation().getBlock().getState())
                           .getInventory()
                           .setContents(inventory);
                     if (block.getType() == Material.DISPENSER)
                       ((Dispenser) block.getLocation().getBlock().getState())
                           .getInventory()
                           .setContents(inventory);
                     if (block.getType() == Material.JUKEBOX)
                       ((Jukebox) block.getLocation().getBlock().getState())
                           .setPlaying(inventory[0].getType());
                   }
                 }
                 ExplodeRestore.clear();
               }
             },
             1L);
   }
 }
  protected boolean collect() {

    Block b = SignUtil.getBackBlock(BukkitUtil.toSign(getSign()).getBlock());

    int x = b.getX();
    int y = b.getY() + 1;
    int z = b.getZ();
    Block bl = BukkitUtil.toSign(getSign()).getBlock().getWorld().getBlockAt(x, y, z);
    for (Entity en : BukkitUtil.toSign(getSign()).getChunk().getEntities()) {
      if (!(en instanceof Item)) {
        continue;
      }
      Item item = (Item) en;
      if (!ItemUtil.isStackValid(item.getItemStack()) || item.isDead() || !item.isValid()) {
        continue;
      }
      int ix = item.getLocation().getBlockX();
      int iy = item.getLocation().getBlockY();
      int iz = item.getLocation().getBlockZ();
      if (ix == getSign().getX() && iy == getSign().getY() && iz == getSign().getZ()) {

        // Create two test stacks to check against
        ItemStack[] testStacks = new ItemStack[] {null, null};

        // Create test stack #1
        try {
          if (getSign().getLine(2).contains(":")) {
            int id = Integer.parseInt(getSign().getLine(2).split(":")[0]);
            int data = Integer.parseInt(getSign().getLine(2).split(":")[1]);
            testStacks[0] = new ItemStack(id, 0, (short) data);
          } else {
            int id = Integer.parseInt(getSign().getLine(2));
            testStacks[1] = new ItemStack(id, 1, (short) 0, (byte) 0);
          }
        } catch (Exception ignored) {
        }

        // Create test stack #2
        try {
          if (getSign().getLine(3).contains(":")) {
            int id = Integer.parseInt(getSign().getLine(3).split(":")[0]);
            int data = Integer.parseInt(getSign().getLine(3).split(":")[1]);
            testStacks[1] = new ItemStack(id, 0, (short) data);
          } else {
            int id = Integer.parseInt(getSign().getLine(2));
            testStacks[1] = new ItemStack(id, 1, (short) 0, (byte) 0);
          }
        } catch (Exception ignored) {
        }

        // Check to see if it matches either test stack, if not stop
        if (testStacks[0] != null)
          if (ItemUtil.areItemsIdentical(testStacks[0], item.getItemStack())) {
            continue;
          }
        if (testStacks[1] != null)
          if (!ItemUtil.areItemsIdentical(testStacks[1], item.getItemStack())) {
            continue;
          }

        // Add the items to a container, and destroy them.
        if (bl.getTypeId() == BlockID.CHEST)
          if (((Chest) bl.getState()).getInventory().firstEmpty() != -1) {

            ((Chest) bl.getState()).getInventory().addItem(item.getItemStack());
            item.remove();
            return true;
          }

        if (bl.getTypeId() == BlockID.DISPENSER)
          if (((Dispenser) bl.getState()).getInventory().firstEmpty() != -1) {

            ((Dispenser) bl.getState()).getInventory().addItem(item.getItemStack());
            item.remove();
            return true;
          }

        if (bl.getTypeId() == BlockID.BREWING_STAND) {

          if (!ItemUtil.isAPotionIngredient(item.getItemStack())) return false;
          if (((BrewingStand) bl.getState()).getInventory().getIngredient() == null
              || ItemUtil.areItemsIdentical(
                  ((BrewingStand) bl.getState()).getInventory().getIngredient(),
                  item.getItemStack())) {

            if (((BrewingStand) bl.getState()).getInventory().getIngredient() == null) {
              ((BrewingStand) bl.getState()).getInventory().setIngredient(item.getItemStack());
            } else {
              ItemUtil.addToStack(
                  ((BrewingStand) bl.getState()).getInventory().getIngredient(),
                  item.getItemStack());
            }
            item.remove();
            return true;
          }
        }

        if (bl.getTypeId() == BlockID.FURNACE || bl.getTypeId() == BlockID.BURNING_FURNACE) {

          Furnace fur = (Furnace) bl.getState();

          if (ItemUtil.isFurnacable(item.getItemStack())
              && (fur.getInventory().getSmelting() == null
                  || ItemUtil.areItemsIdentical(
                      item.getItemStack(), fur.getInventory().getSmelting()))) {
            if (fur.getInventory().getSmelting() == null) {
              fur.getInventory().setSmelting(item.getItemStack());
            } else {
              ItemUtil.addToStack(
                  ((Furnace) bl.getState()).getInventory().getSmelting(), item.getItemStack());
            }
            item.remove();
            return true;
          }

          if (ItemUtil.isAFuel(item.getItemStack())
              && (fur.getInventory().getFuel() == null
                  || ItemUtil.areItemsIdentical(
                      item.getItemStack(), fur.getInventory().getFuel()))) {
            if (fur.getInventory().getFuel() == null) {
              fur.getInventory().setFuel(item.getItemStack());
            } else {
              ItemUtil.addToStack(
                  ((Furnace) bl.getState()).getInventory().getFuel(), item.getItemStack());
            }
            item.remove();
            return true;
          }
        }
      }
    }
    return false;
  }