Java Code Examples for org.bukkit.event.block.BlockBreakEvent#setExpToDrop()

The following examples show how to use org.bukkit.event.block.BlockBreakEvent#setExpToDrop() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockListener.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
private void handleBlockLoot(BlockBreakEvent event){
	Material material = event.getBlock().getType();
	if(blockLoots.containsKey(material)){
		BlockLootConfiguration lootConfig = blockLoots.get(material);
		Location loc = event.getBlock().getLocation().add(.5,.5,.5);
		event.getBlock().setType(Material.AIR);
		event.setExpToDrop(lootConfig.getAddXp());
		loc.getWorld().dropItem(loc, lootConfig.getLoot().clone());
		UhcItems.spawnExtraXp(loc,lootConfig.getAddXp());
	}
}
 
Example 2
Source File: NerfQuartzXPModule.java    From UHC with MIT License 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void on(BlockBreakEvent event) {
    if (!isEnabled() || event.getBlock().getType() != Material.QUARTZ_ORE) return;

    final int count;
    if (higher == lower) {
        count = higher;
    } else {
        count = RANDOM.nextInt(higher - lower + 1) + lower;
    }

    event.setExpToDrop(count);
}
 
Example 3
Source File: PickaxeOfContainment.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public BlockBreakHandler getItemHandler() {
    return new BlockBreakHandler() {

        @Override
        public boolean isPrivate() {
            return false;
        }

        @Override
        public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List<ItemStack> drops) {
            if (isItem(item)) {
                if (!Slimefun.hasUnlocked(e.getPlayer(), PickaxeOfContainment.this, true)) {
                    return true;
                }

                Block b = e.getBlock();

                if (b.getType() != Material.SPAWNER) {
                    return true;
                }

                ItemStack spawner = breakSpawner(b);
                b.getLocation().getWorld().dropItemNaturally(b.getLocation(), spawner);

                e.setExpToDrop(0);
                e.setDropItems(false);
                return true;
            }
            else {
                if (e.getBlock().getType() == Material.SPAWNER) e.setDropItems(false);
                return false;
            }
        }
    };
}
 
Example 4
Source File: DataHandler.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
public static boolean playerBreakBlock(@NotNull Player player, Block block, @NotNull ItemStack itemStack) {
    //Trigger BlockBreakEvent
    BlockBreakEvent breakEvent = new BlockBreakEvent(block, player);
    ItemMeta meta = itemStack.getItemMeta();
    if (meta != null && !meta.hasEnchant(Enchantment.SILK_TOUCH)) breakEvent.setExpToDrop(calculateExp(block.getType()));
    Bukkit.getPluginManager().callEvent(breakEvent);

    //Check if Event got cancelled and if not destroy the block and check if the player can successfully break the blocks (incl. drops)
    //Block#breakNaturally(ItemStack itemStack) can not be used as it drops Items itself (without Event and we don't want that)
    if (!breakEvent.isCancelled()) {
        //Get all drops to drop
        Collection<ItemStack> items = block.getDrops(itemStack);

        //Set Block to Material.AIR (effectively breaks the Block)
        block.setType(Material.AIR);
        //TODO: Play Sound?

        //Check if items need to be dropped
        if (breakEvent.isDropItems()) {
            List<Item> itemEntities = items.stream()
                    .map(entry -> player.getWorld().dropItemNaturally(block.getLocation(), entry)) //World#spawnEntity() does not work for Items
                    .collect(Collectors.toList());

            //Trigger BlockDropItemEvent (internally also used for Directing)
            BlockDropItemEvent event = new BlockDropItemEvent(block, block.getState(), player, new ArrayList<>(itemEntities));
            Bukkit.getPluginManager().callEvent(event);

            //check if Event got cancelled
            if (!event.isCancelled()) {
                //Remove all drops that should be dropped
                itemEntities.removeIf(element -> event.getItems().contains(element));
            }
            itemEntities.forEach(Item::remove);
        }

        //Check if Exp needs to be dropped
        if (breakEvent.getExpToDrop() > 0) {
            //Spawn Experience Orb
            ExperienceOrb orb = (ExperienceOrb) player.getWorld().spawnEntity(block.getLocation(), EntityType.EXPERIENCE_ORB);
            orb.setExperience(breakEvent.getExpToDrop());
        }

        return true;
    }

    return false;
}