@Override
 public void onBlockDispense(BlockDispenseEvent event) {
   for (Field field : plugin.fields) {
     if (field.enableDispense && field.inField(event.getBlock().getLocation(), event.getItem())) {
       Vector v =
           new Vector(0, .2, -1)
               .multiply(
                   field
                       .dispensePower); // event.getVelocity().normalize().multiply(field.dispensePower);
       event.setVelocity(v);
       return;
     }
   }
 }
  /*
   * Called when a block is damaged.
   */
  @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
  public void onBlockDispense(BlockDispenseEvent event) {
    ConfigurationManager cfg = plugin.getGlobalStateManager();
    WorldConfiguration wcfg = cfg.get(event.getBlock().getWorld());

    if (wcfg.blockPotions.size() > 0) {
      ItemStack item = event.getItem();
      if (item.getType() == Material.POTION && !BukkitUtil.isWaterPotion(item)) {
        Potion potion = Potion.fromDamage(BukkitUtil.getPotionEffectBits(item));
        for (PotionEffect effect : potion.getEffects()) {
          if (potion.isSplash() && wcfg.blockPotions.contains(effect.getType())) {
            event.setCancelled(true);
            return;
          }
        }
      }
    }
  }
  public ItemStack b(ISourceBlock isourceblock, ItemStack itemstack) {
    EnumFacing enumfacing = BlockDispenser.b(isourceblock.h());
    double d0 = isourceblock.getX() + (double) enumfacing.c();
    double d1 = (double) ((float) isourceblock.getBlockY() + 0.2F);
    double d2 = isourceblock.getZ() + (double) enumfacing.e();

    // CraftBukkit start
    World world = isourceblock.k();
    ItemStack itemstack1 = itemstack.a(1);
    org.bukkit.block.Block block =
        world
            .getWorld()
            .getBlockAt(
                isourceblock.getBlockX(), isourceblock.getBlockY(), isourceblock.getBlockZ());
    CraftItemStack craftItem = CraftItemStack.asCraftMirror(itemstack1);

    BlockDispenseEvent event =
        new BlockDispenseEvent(block, craftItem.clone(), new org.bukkit.util.Vector(d0, d1, d2));
    if (!BlockDispenser.eventFired) {
      world.getServer().getPluginManager().callEvent(event);
    }

    if (event.isCancelled()) {
      itemstack.count++;
      return itemstack;
    }

    if (!event.getItem().equals(craftItem)) {
      itemstack.count++;
      // Chain to handler for new item
      ItemStack eventStack = CraftItemStack.asNMSCopy(event.getItem());
      IDispenseBehavior idispensebehavior =
          (IDispenseBehavior) BlockDispenser.a.a(eventStack.getItem());
      if (idispensebehavior != IDispenseBehavior.a && idispensebehavior != this) {
        idispensebehavior.a(isourceblock, eventStack);
        return itemstack;
      }
    }

    itemstack1 = CraftItemStack.asNMSCopy(event.getItem());
    EntityFireworks entityfireworks =
        new EntityFireworks(
            isourceblock.k(),
            event.getVelocity().getX(),
            event.getVelocity().getY(),
            event.getVelocity().getZ(),
            itemstack1);

    isourceblock.k().addEntity(entityfireworks);
    // itemstack.a(1); // Handled during event processing
    // CraftBukkit end

    return itemstack;
  }
  // CraftBukkit - priv to public
  public void dispense(World world, int i, int j, int k, Random random) {
    int l = world.getData(i, j, k);
    byte b0 = 0;
    byte b1 = 0;

    if (l == 3) {
      b1 = 1;
    } else if (l == 2) {
      b1 = -1;
    } else if (l == 5) {
      b0 = 1;
    } else {
      b0 = -1;
    }

    TileEntityDispenser tileentitydispenser = (TileEntityDispenser) world.getTileEntity(i, j, k);

    if (tileentitydispenser != null) {
      // CraftBukkit start
      int dispenseSlot = tileentitydispenser.findDispenseSlot();
      ItemStack itemstack = null;
      if (dispenseSlot > -1) {
        itemstack = tileentitydispenser.getContents()[dispenseSlot];

        // Copy item stack, because we want it to have 1 item
        itemstack =
            new ItemStack(itemstack.id, 1, itemstack.getData(), itemstack.getEnchantments());
      }
      // CraftBukkit end

      double d0 = (double) i + (double) b0 * 0.6D + 0.5D;
      double d1 = (double) j + 0.5D;
      double d2 = (double) k + (double) b1 * 0.6D + 0.5D;

      if (itemstack == null) {
        world.triggerEffect(1001, i, j, k, 0);
      } else {
        // CraftBukkit start
        double d3 = random.nextDouble() * 0.1D + 0.2D;
        double motX = (double) b0 * d3;
        double motY = 0.20000000298023224D;
        double motZ = (double) b1 * d3;
        motX += random.nextGaussian() * 0.007499999832361937D * 6.0D;
        motY += random.nextGaussian() * 0.007499999832361937D * 6.0D;
        motZ += random.nextGaussian() * 0.007499999832361937D * 6.0D;

        org.bukkit.block.Block block = world.getWorld().getBlockAt(i, j, k);
        org.bukkit.inventory.ItemStack bukkitItem = new CraftItemStack(itemstack).clone();

        BlockDispenseEvent event =
            new BlockDispenseEvent(block, bukkitItem, new Vector(motX, motY, motZ));
        world.getServer().getPluginManager().callEvent(event);

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

        if (event.getItem().equals(bukkitItem)) {
          // Actually remove the item
          tileentitydispenser.splitStack(dispenseSlot, 1);
        }

        motX = event.getVelocity().getX();
        motY = event.getVelocity().getY();
        motZ = event.getVelocity().getZ();

        itemstack = CraftItemStack.createNMSItemStack(event.getItem());
        // CraftBukkit end

        if (itemstack.id == Item.ARROW.id) {
          EntityArrow entityarrow = new EntityArrow(world, d0, d1, d2);

          entityarrow.shoot((double) b0, 0.10000000149011612D, (double) b1, 1.1F, 6.0F);
          entityarrow.fromPlayer = true;
          world.addEntity(entityarrow);
          world.triggerEffect(1002, i, j, k, 0);
        } else if (itemstack.id == Item.EGG.id) {
          EntityEgg entityegg = new EntityEgg(world, d0, d1, d2);

          entityegg.a((double) b0, 0.10000000149011612D, (double) b1, 1.1F, 6.0F);
          world.addEntity(entityegg);
          world.triggerEffect(1002, i, j, k, 0);
        } else if (itemstack.id == Item.SNOW_BALL.id) {
          EntitySnowball entitysnowball = new EntitySnowball(world, d0, d1, d2);

          entitysnowball.a((double) b0, 0.10000000149011612D, (double) b1, 1.1F, 6.0F);
          world.addEntity(entitysnowball);
          world.triggerEffect(1002, i, j, k, 0);
        } else if (itemstack.id == Item.POTION.id && ItemPotion.c(itemstack.getData())) {
          EntityPotion entitypotion = new EntityPotion(world, d0, d1, d2, itemstack.getData());

          entitypotion.a((double) b0, 0.10000000149011612D, (double) b1, 1.375F, 3.0F);
          world.addEntity(entitypotion);
          world.triggerEffect(1002, i, j, k, 0);
        } else if (itemstack.id == Item.EXP_BOTTLE.id) {
          EntityThrownExpBottle entitythrownexpbottle =
              new EntityThrownExpBottle(world, d0, d1, d2);

          entitythrownexpbottle.a((double) b0, 0.10000000149011612D, (double) b1, 1.375F, 3.0F);
          world.addEntity(entitythrownexpbottle);
          world.triggerEffect(1002, i, j, k, 0);
        } else if (itemstack.id == Item.MONSTER_EGG.id) {
          ItemMonsterEgg.a(
              world,
              itemstack.getData(),
              d0 + (double) b0 * 0.3D,
              d1 - 0.3D,
              d2 + (double) b1 * 0.3D);
          world.triggerEffect(1002, i, j, k, 0);
        } else if (itemstack.id == Item.FIREBALL.id) {
          EntitySmallFireball entitysmallfireball =
              new EntitySmallFireball(
                  world,
                  d0 + (double) b0 * 0.3D,
                  d1,
                  d2 + (double) b1 * 0.3D,
                  (double) b0 + random.nextGaussian() * 0.05D,
                  random.nextGaussian() * 0.05D,
                  (double) b1 + random.nextGaussian() * 0.05D);

          world.addEntity(entitysmallfireball);
          world.triggerEffect(1009, i, j, k, 0);
        } else {
          EntityItem entityitem = new EntityItem(world, d0, d1 - 0.3D, d2, itemstack);
          // CraftBukkit start
          // double d3 = random.nextDouble() * 0.1D + 0.2D; // Moved up
          entityitem.motX = motX;
          entityitem.motY = motY;
          entityitem.motZ = motZ;
          // CraftBukkit end
          world.addEntity(entityitem);
          world.triggerEffect(1000, i, j, k, 0);
        }

        world.triggerEffect(2000, i, j, k, b0 + 1 + (b1 + 1) * 3);
      }
    }
  }
  public ItemStack b(ISourceBlock isourceblock, ItemStack itemstack) {
    ItemBucket itembucket = (ItemBucket) itemstack.getItem();
    BlockPosition blockposition =
        isourceblock.getBlockPosition().shift(BlockDispenser.b(isourceblock.f()));

    // CraftBukkit start
    World world = isourceblock.i();
    int x = blockposition.getX();
    int y = blockposition.getY();
    int z = blockposition.getZ();
    if (world.isEmpty(blockposition)
        || !world.getType(blockposition).getBlock().getMaterial().isBuildable()) {
      org.bukkit.block.Block block =
          world
              .getWorld()
              .getBlockAt(
                  isourceblock.getBlockPosition().getX(),
                  isourceblock.getBlockPosition().getY(),
                  isourceblock.getBlockPosition().getZ());
      CraftItemStack craftItem = CraftItemStack.asCraftMirror(itemstack);

      BlockDispenseEvent event =
          new BlockDispenseEvent(block, craftItem.clone(), new org.bukkit.util.Vector(x, y, z));
      if (!BlockDispenser.eventFired) {
        world.getServer().getPluginManager().callEvent(event);
      }

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

      if (!event.getItem().equals(craftItem)) {
        // Chain to handler for new item
        ItemStack eventStack = CraftItemStack.asNMSCopy(event.getItem());
        IDispenseBehavior idispensebehavior =
            (IDispenseBehavior) BlockDispenser.M.get(eventStack.getItem());
        if (idispensebehavior != IDispenseBehavior.a && idispensebehavior != this) {
          idispensebehavior.a(isourceblock, eventStack);
          return itemstack;
        }
      }

      itembucket = (ItemBucket) CraftItemStack.asNMSCopy(event.getItem()).getItem();
    }
    // CraftBukkit end

    if (itembucket.a(isourceblock.i(), blockposition)) {
      // CraftBukkit start - Handle stacked buckets
      Item item = Items.BUCKET;
      if (--itemstack.count == 0) {
        itemstack.setItem(Items.BUCKET);
        itemstack.count = 1;
      } else if (((TileEntityDispenser) isourceblock.getTileEntity()).addItem(new ItemStack(item))
          < 0) {
        this.b.a(isourceblock, new ItemStack(item));
      }
      // CraftBukkit end
      return itemstack;
    } else {
      return this.b.a(isourceblock, itemstack);
    }
  }
  public ItemStack b(ISourceBlock isourceblock, ItemStack itemstack) {
    World world = isourceblock.i();
    IPosition iposition = BlockDispenser.a(isourceblock);
    EnumDirection enumdirection = BlockDispenser.b(isourceblock.f());
    IProjectile iprojectile = this.a(world, iposition);

    // iprojectile.shoot((double) enumdirection.getAdjacentX(), (double) ((float)
    // enumdirection.getAdjacentY() + 0.1F), (double) enumdirection.getAdjacentZ(), this.b(),
    // this.a());
    // CraftBukkit start
    ItemStack itemstack1 = itemstack.a(1);
    org.bukkit.block.Block block =
        world
            .getWorld()
            .getBlockAt(
                isourceblock.getBlockPosition().getX(),
                isourceblock.getBlockPosition().getY(),
                isourceblock.getBlockPosition().getZ());
    CraftItemStack craftItem = CraftItemStack.asCraftMirror(itemstack1);

    BlockDispenseEvent event =
        new BlockDispenseEvent(
            block,
            craftItem.clone(),
            new org.bukkit.util.Vector(
                (double) enumdirection.getAdjacentX(),
                (double) ((float) enumdirection.getAdjacentY() + 0.1F),
                (double) enumdirection.getAdjacentZ()));
    if (!BlockDispenser.eventFired) {
      world.getServer().getPluginManager().callEvent(event);
    }

    if (event.isCancelled()) {
      itemstack.count++;
      return itemstack;
    }

    if (!event.getItem().equals(craftItem)) {
      itemstack.count++;
      // Chain to handler for new item
      ItemStack eventStack = CraftItemStack.asNMSCopy(event.getItem());
      IDispenseBehavior idispensebehavior =
          (IDispenseBehavior) BlockDispenser.N.get(eventStack.getItem());
      if (idispensebehavior != IDispenseBehavior.a && idispensebehavior != this) {
        idispensebehavior.a(isourceblock, eventStack);
        return itemstack;
      }
    }

    iprojectile.shoot(
        event.getVelocity().getX(),
        event.getVelocity().getY(),
        event.getVelocity().getZ(),
        this.b(),
        this.a());
    ((Entity) iprojectile).projectileSource =
        new org.bukkit.craftbukkit.projectiles.CraftBlockProjectileSource(
            (TileEntityDispenser) isourceblock.getTileEntity());
    // CraftBukkit end
    world.addEntity((Entity) iprojectile);
    // itemstack.a(1); // CraftBukkit - Handled during event processing
    return itemstack;
  }
  @EventHandler(priority = EventPriority.HIGHEST)
  public void onBlockDispense(BlockDispenseEvent event) {
    Block block = event.getBlock();
    if (block.getType() == Material.DISPENSER) {

      Cannon cannon = plugin.getCannon(block, false);
      if (cannon == null) {
        return;
      }

      Material material = event.getItem().getType();

      Grenade grenade = plugin.getArmageddonConfig().getGrenade(material);

      if (grenade.getType() == Type.DUD || !grenade.isCannonUse()) {
        return;
      }

      Dispenser dispenser = (Dispenser) block.getType().getNewData(block.getData());

      double vx = cannon.getVelocity() * Math.cos((double) (Math.PI * cannon.getAngle() / 180));
      double y = cannon.getVelocity() * Math.sin((double) (Math.PI * cannon.getAngle() / 180));

      double x = 0;
      double z = 0;

      int yaw = 0;

      switch (dispenser.getFacing()) {
        case NORTH:
          x = -vx;
          yaw = 90;
          break;

        case SOUTH:
          x = vx;
          yaw = 270;
          break;

        case WEST:
          z = vx;
          yaw = 0;
          break;

        case EAST:
          z = -vx;
          yaw = 180;
          break;
      }

      Vector initialVelocity = new Vector(x, y, z);
      Location location = block.getRelative(dispenser.getFacing()).getLocation().add(0.5, 0.5, 0.5);
      World world = block.getWorld();

      Entity entity = null;

      x = location.getX();
      y = location.getY();
      z = location.getZ();

      Location locClone = location.clone();

      switch (grenade.getType()) {
        case PIG:
          entity = world.spawn(location, Pig.class);
          break;

        case COW:
          entity = world.spawn(location, Cow.class);
          break;

        case SHEEP:
          entity = world.spawn(location, Sheep.class);
          break;

        case TNT:
          entity = world.spawn(location, TNTPrimed.class);
          ((TNTPrimed) entity).setFuseTicks(cannon.getFuse());
          break;

        case EXPLOSIVE:
        case NUCLEAR:
        case WATER_BALLOON:
        case SPIDER_WEB:
        case SNARE:
        case STUN:
          entity = world.spawn(location, Snowball.class);
          break;

        case MOLOTOV:
          locClone.setPitch(0);
          locClone.setYaw(yaw);
          entity = world.spawn(locClone, Fireball.class);
          LivingEntity owner = plugin.getServer().getPlayer(cannon.getOwner());
          if (owner == null) {
            // get the closest living entity
            double distance = Double.MAX_VALUE;
            for (LivingEntity e : world.getLivingEntities()) {
              if (e.getLocation().distance(locClone) < distance) {
                distance = e.getLocation().distance(locClone);
                owner = e;
              }
            }
          }
          ((Fireball) entity).setShooter(owner);
          break;

        default:
          return;
      }

      if (entity != null) {
        if (!(entity instanceof Fireball)) {
          entity.setVelocity(initialVelocity);
        }

        if (!(entity instanceof LivingEntity)) {
          plugin.registerGrenade(entity, grenade);
        }

        plugin.adjustInventoryAndUsage(
            ((org.bukkit.block.Dispenser) block.getState()).getInventory(),
            cannon,
            material,
            grenade.getUses());

        event.setCancelled(true);
        world.createExplosion(location, 0);
      }
    }
  }