Solved Fill chest

Discussion in 'Spigot Plugin Development' started by Poinball, Jun 11, 2017.

  1. Hey guys !!

    I get an error with my code, but I really dont understand why.
    I want to add a Gold_Helmet with a specific name in a chest in the world

    Can you please take a look at it :)

    Code (Text):
                ItemStack gold_helmet = new ItemStack(Material.GOLD_HELMET, 1);
                ItemMeta im = gold_helmet.getItemMeta();
                im.setDisplayName("Red King's Crown");
                gold_helmet.setItemMeta(im);

                World w = Bukkit.getWorld("world");
                Block b = w.getBlockAt(61, 69, 159);
                Chest c = (Chest)b;
                c.getBlockInventory().addItem(gold_helmet);
    [​IMG]

    thanks !
     
  2. Try casting it to the state with getState() instead.
     
    • Agree Agree x 1
  3. You imported the wrong 'Chest'. Import the block
     
  4. No he didn't?
    [​IMG]
     
    • Agree Agree x 1
  5. Check if Block#getState instanceof Chest, then cast to Chest. Also, change your convention. Shouldn't use underscores for field declarations generally, only tend to do that with static final fields I believe, should be camelCase in this case however. In other words, use 'goldHelmet' instead of 'gold_helmet'.
     
  6. I dont understand , Where do I cast with getState() ?

    Nah I tried but it doesnt work
     
  7. It will work...
    Code (Text):
    Chest chest = (Chest) block.getState();
     
    • Like Like x 1
    • Winner Winner x 1
  8. Code (Text):
    if (block.getState() instanceof Chest) {

    Chest chest = (Chest) block.getState();
    Inventory chestInventory = chest.getInventory()

    chestInventory.addItem(gold_helmet);

    }
     
    • Winner Winner x 1
  9. Yess !! It Work :D Thanks a lot
     
    • Like Like x 3