Solved Getting a chest inventory from a Block

Discussion in 'Plugin Development' started by Shadow_tingBRO, Dec 7, 2019.

Thread Status:
Not open for further replies.
  1. I have been trying to do this for a while, it is pretty self-explanatory. I have a location in a world which is a chest block which I 100% know is a chest and have checked. I then used this code to get its inventory:

    Code:
    Chest chest1 = (Chest) world.getBlockAt(factionchestLocation).getState();
    
    However I get a ClassCastException, org.bukkit.craftbukkit.<morepackages>.CraftBlockState cannot be cast to org.bukkit.block.Chest

    It doesnt make any sense I have searched forums and nobody has an answer. What am I doing wrong?

    EDIT: I fixed it, I had to create variable for the states and block and then cast:

    Code:
            Block c1=w.getIfLoaded().getBlockAt(factionchest.toLocation(w));
            Block c2 = w.getIfLoaded().getBlockAt(new WorldlessLocation(factionchest.getX(),
                    factionchest.getY()+2,factionchest.getZ()).toLocation(w));
            BlockState c1state = c1.getState();
            BlockState c2state = c2.getState();
            Chest chest1 = (Chest)c1state;
            Chest chest2 = (Chest)c2state;
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Dec 7, 2019
Thread Status:
Not open for further replies.

Share This Page