1.15.2 Explosion Event cancel blocks drops

Discussion in 'Spigot Plugin Development' started by Joshuak52, Feb 12, 2020.

Thread Status:
Not open for further replies.
  1. Hey, got a line of code and I am trying to clear the blocks that drops on the ground

    Code (Text):
    @EventHandler
        public void onExplode(final EntityExplodeEvent event) {
            if (!(event.getEntity() instanceof Player)) {
                return;
            }
            final Player player = (Player) event.getEntity();
            for (final Block blk : event.blockList()) {
                if (this.isInAllowedRegion(blk.getLocation()) && WGUtil.canBuild(player, blk.getLocation())) {
                    ItemStack itemInMainHand = player.getInventory().getItemInMainHand();
                    player.getInventory().addItem((ItemStack[])blk.getDrops(itemInMainHand).toArray(new ItemStack[0]));
                    blk.getDrops().clear();
                }
            }
        }
     
  2. Solved this issue with code below,

    Code (Text):
        @EventHandler
        public void onExplode(final EntityExplodeEvent event) {
            if (!(event.getEntity() instanceof Player)) {
                return;
            }
            final Player player = (Player) event.getEntity();
            for (final Block blk : event.blockList()) {
                if (this.isInAllowedRegion(blk.getLocation()) && WGUtil.canBuild(player, blk.getLocation())) {
                    ItemStack itemInMainHand = player.getInventory().getItemInMainHand();
                    player.getInventory().addItem((ItemStack[])blk.getDrops(itemInMainHand).toArray(new ItemStack[0]));
                    blk.setType(Material.AIR);
                }
            }
        }
     
Thread Status:
Not open for further replies.