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;
  }
Example #2
0
 @Override
 public void run() {
   try {
     while (this.plugin.buffer.size() > 0) {
       final Item item = this.plugin.buffer.pollFirstEntry().getValue();
       if (item.isValid()) {
         final Location cauldronLoc = item.getLocation().getBlock().getLocation().clone();
         if (this.plugin.magicBenches.contains(cauldronLoc)) { // So now we act !
           final ItemStack itemStack = item.getItemStack();
           if (itemStack.getEnchantments().size() != 0) {
             final Result res =
                 this.plugin.randomizer.randomize(itemStack); // The itemStack/item is now modified
             byte data = 0; // Wool color
             float ratio = 1f; // Volume & pitch of thunder sound
             switch (res) {
               case CLEAN:
                 data = 15; // Black
                 ratio = 0.2f;
                 break;
               case LOSS:
                 data = 14; // Red
                 ratio = 0.4f;
                 break;
               case NONE:
                 data = 0; // White
                 ratio = 0.6f;
                 break;
               case BOOST:
                 data = 4; // Yellow
                 ratio = 0.8f;
                 break;
               case OVERBOOST:
                 data = 5; // Green
                 ratio = 1f;
                 break;
             }
             this.plugin.magicBenches.remove(cauldronLoc);
             cauldronLoc
                 .getWorld()
                 .playSound(cauldronLoc, Sound.AMBIENCE_THUNDER, ratio, ratio * 2f);
             cauldronLoc.subtract(0, 1, 0); // Is now Egg location
             cauldronLoc.getWorld().playEffect(item.getLocation(), Effect.ENDER_SIGNAL, 0);
             if (cauldronLoc.getBlock().getType() != Material.DRAGON_EGG) {
               // Prevent possible cheat : if the player remove the egg before this happen, we
               // remove every enchants
               // As the interact event is cancelled in ME_Listener, this should never happen.
               for (final Enchantment e : itemStack.getEnchantments().keySet()) {
                 itemStack.removeEnchantment(e);
               }
               cauldronLoc.getWorld().playSound(cauldronLoc, Sound.ENDERDRAGON_DEATH, 1.0f, 1.5f);
             }
             cauldronLoc.getBlock().setType(Material.AIR); // Delete the Egg
             this.plugin.fakeWool(
                 cauldronLoc,
                 data,
                 this.plugin.getNearbyPlayers(item, 10, 5, 10)); // Wool color effect
           }
         }
       }
       if (item.isValid()) {
         item.setPickupDelay(0);
       }
     }
   } catch (final Exception e) {
     e.printStackTrace();
   }
 }