Solved CraftBlockState cannot be cast to Chest

Discussion in 'Plugin Development' started by StellarFX, Apr 30, 2019.

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

    StellarFX

    Hello,

    I'm trying to check if the block behind the sign is a chest, and if so, get his content.
    I did my code and everything, but I have an error :

    org.bukkit.craftbukkit.v1_13_R2.block.CraftBlockState cannot be cast to org.bukkit.block.Chest

    Here's my code :

    Code:
    org.bukkit.material.Sign sd = (org.bukkit.material.Sign) sign.getData();
                                        Block attachedBlock = b.getRelative(sd.getAttachedFace());
                                        BlockState state = attachedBlock.getLocation().getBlock().getState();
                                        player.sendMessage("oi");
                                        Chest chest = (Chest)attachedBlock.getLocation().getBlock().getState();
    
                                        int i = 0;
                                        player.sendMessage("Oui..");
                                        if (attachedBlock.getLocation().getBlock().getType() == Material.CHEST) {
                                            player.sendMessage("Ouiii");
                                            for (ItemStack is : chest.getInventory().getContents()) {
                                                player.sendMessage("OuiIi");
                                                if(is.getType() == Material.matchMaterial(bloc)) {
                                                    player.sendMessage("Wi");
                                                    if(is.getAmount() == 1) {
                                                        player.sendMessage("Ui");
                                                        economy.withdrawPlayer(player.getName(), Integer.parseInt(Splitted[1]));
                                                        player.getInventory().addItem(new ItemStack(Material.matchMaterial(bloc)));
                                                        player.sendMessage(prefix + "You bought a §6" + sign.getLine(2).toString() + " §7!");
                                                        }
    Please help !
    StellarFX
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    StellarFX

    @timtower I import
    Code:
    org.bukkit.block.BlockState
     
  4. Offline

    KarimAKL

    @StellarFX You need to import 'org.bukkit.block.BlockState', not 'org.bukkit.craftbukkit.v1_13_R2.block.CraftBlockState'
     
  5. Offline

    timtower Administrator Administrator Moderator

    And what Chest did you import?
     
  6. Offline

    StellarFX

    I import
    Code:
    org.bukkit.block.Chest
     
  7. Offline

    timtower Administrator Administrator Moderator

    And if you import the other chest?
     
  8. Offline

    StellarFX

    I tried to import the "org.bukkit.material.Chest", it makes my whole code doesn't work.
     
  9. Offline

    timtower Administrator Administrator Moderator

    @StellarFX Try it without the getState() then.
     
  10. Offline

    StellarFX

    Then I have this error :
    Code:
    org.bukkit.craftbukkit.v1_13_R2.block.CraftBlock cannot be cast to org.bukkit.block.Chest
    I'm importing
    Code:
    org.bukkit.block.Block
     
  11. Offline

    timtower Administrator Administrator Moderator

    @StellarFX Why are you casting before you are sure that it is a chest?
    Might have been the issue the entire time.
     
  12. Offline

    StellarFX

    It was. I just fixed it before you sent this message. Thanks for your help !

    Fixed. I had to check before my chest object if the attachedblock is a chest.
    Code :
    Code:
    if (attachedBlock.getLocation().getBlock().getType() == Material.CHEST) {
                                            Chest chest = (Chest)attachedBlock.getLocation().getBlock().getState();
     
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page