Solved Getting a chest inventory from a Block

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

  1. Offline


    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:

    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:

            Block c1=w.getIfLoaded().getBlockAt(factionchest.toLocation(w));
            Block c2 = w.getIfLoaded().getBlockAt(new WorldlessLocation(factionchest.getX(),
            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

Share This Page