Get chest inventory

Discussion in 'Plugin Development' started by Fin999888777, Oct 16, 2012.

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

    Fin999888777

    Hey... yes I know google is my friend and I used it very much but I can't solve this problem since 4 days, it'll be some stupid mistake, but I am preety new in programming bukkit plugins so I hope I can find somebody who will help a nooby :D I want to get the inventory of a chest, by checking if a player rightclicks a block and wehn it's a chest the player should get a message with the items in the chest...

    I will give u the problematic part of my code:

    if(event.getAction()==Action.RIGHT_CLICK_BLOCK) {
    Block b = event.getClickedBlock();
    Player player = event.getPlayer();
    if(event.getClickedBlock().getTypeId()==54) {
    Chest chest = ((Chest) b);
    ItemStack[] z;
    z=((Chest) b).getBlockInventory().getContents();
    for (int i=0; i<z.length; i++) {
    player.sendMessage("Ein Item:"+z);
    }
    }

    But bukkit has got a problem with the transforming of a block into a chest enum...
    I get the following error in my server console if a player opens a chest:

    ClassCastException: org.bukkit.block.CraftBlock cannot be cast to org.bukkit.block.chest
    (in the line marked red)

    I understand what the error message is telling me, but I think it should be able to cast the block to the chest enum...

    I don't know if I have a stupid mistake or if it simply not possible.. If it isn't possible could u give me an oteher suggestion?

    Fin


    I hate it always whhen I think it's k now I will write something in the bukkit forum I found the solution:
    For everyone having the same problem: Chest chest = (Chest) b.getState();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page