Java Code Examples for org.bukkit.event.block.BlockBreakEvent#isDropItems()
The following examples show how to use
org.bukkit.event.block.BlockBreakEvent#isDropItems() .
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 Slimefun4 with GNU General Public License v3.0 | 7 votes |
private void dropItems(BlockBreakEvent e, List<ItemStack> drops) { if (!drops.isEmpty()) { e.getBlock().setType(Material.AIR); if (e.isDropItems()) { for (ItemStack drop : drops) { if (drop != null && drop.getType() != Material.AIR) { e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), drop); } } } } }
Example 2
Source File: DataHandler.java From MineTinker with GNU General Public License v3.0 | 4 votes |
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; }