Get a chest block's inventory

Discussion in 'Plugin Development' started by Delano, Mar 2, 2020.

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

    Delano

    I'm trying to get a chest block's inventory during the BlockBreakEvent however I'm having trouble casting BlockState to Chest, so I can get the BlockInventory and clear it.

    I swear I've done this in the past and it's worked, and I've been googling for hours and pretty much every thread I've found on multiple websites have it done exactly how I'm doing it. I have no clue what I'm doing wrong and would appreciate some help.

    Code:
    @EventHandler
    public void onBreak(BlockBreakEvent event) {
        if(event.getBlock().getType() == Material.CHEST) {
            if(event.getBlock().getState() instanceof Chest) {
                Chest chest = (Chest) event.getBlock().getState();
                chest.getBlockInventory().clear();
                event.getPlayer().sendMessage("is chest");
            } else {
                event.getPlayer().sendMessage("not chest");
            }
        }
    }
    
    The "not chest" message goes off every time. Appreciate any help, thanks in advance.
     
  2. Offline

    wand555

    Maybe try and print out the state before you check so you know for sure with what state youre dealing with.
    In theory your code should work.
     
  3. Offline

    KarimAKL

    @Delano Have you accidentally imported the wrong "Chest"? i.e. org.bukkit.block.data.type.Chest instead of org.bukkit.block.Chest.
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page