Check if item used to break block is correct

Discussion in 'Plugin Development' started by georgetalks2, Jan 5, 2023.

Thread Status:
Not open for further replies.
  1. Offline


    howdy! i'm currently trying to make a plugin that instantly transfers an item to a players inventory after the block is broken,

    i've managed to get the item to not drop and for it to still break, but now i'm trying to get the item to transfer to the players inventory. i tried just giving them the drop, but that means whatever item they break it with, they still get the block. here's my code so far:

        public void onBlockBroken(BlockBreakEvent event){
            Collection<ItemStack> Drops = event.getBlock().getDrops();
            Player PlayerBroke = event.getPlayer();
            if (PlayerBroke != null){
                // was broken by player
                ItemStack HeldItem = PlayerBroke.getItemInHand(); // item in hand
                // i wanna check if the item is correct but no idea how lol //
    thanks for helping! :D
  2. Offline


    If you want to check if the item is, say, a diamond pick you can do
    if (HeldItem.getType() == Material.DIAMOND_PICKAXE) {
    Though your vars should be camelCase not PascalCase if you want to adhere to conventions.

    Also is this 1.9 or newer? If so, you should be using PlayerBroke.getInventory().getItemInMainHand() instead of getItemInHand as that's deprecated.

    Also also, you really don't have to clear the drops if you are cancelling the event - if cancelled, it isn't going to drop anyway.
  3. Offline


    @Strahan thank you for the suggestions, but i don't want to manually check against every type of item. is there a way i could check what would actually drop?
  4. Offline


    1.18.2 i did not test it
    block.getDrops(player.getActiveItem()).toArray(new ItemStack[0])
    #getDrops(ITEMSTACK) will return a collection of what would drop if broken by this itemstack.
Thread Status:
Not open for further replies.

Share This Page