CraftBlock cannot be cast to Skull

Discussion in 'Plugin Development' started by Ammonium_, Jan 20, 2020.

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

    Ammonium_

    Hey all! I'm trying to get the placed skull's owner from a BlockPlaceEvent. In my code, after I verify that the BlockType is of Material.SKULL. This works nicely up to this point, it prints "This is a skull!" on chat whenever I place a skull. Then, I try to cast the Block instance to a Skull object, which in my mind should work nicely. However, it outputs "org.bukkit.craftbukkit.v1_12_R1.block.CraftBlock cannot be cast to org.bukkit.block.Skull" to the console. Am I doing something wrong, or is what I'm trying to do simply impossible? Thanks for helping.
    My code:
    Code:
    public void onBlockPlace(BlockPlaceEvent e) {
            Block block = e.getBlock();
            if (block.getType() == Material.SKULL){
                Bukkit.broadcastMessage("This is a skull!");
                Skull skull = (Skull) block;
                if (skull.getOwningPlayer().getName().equals("Ammonium_")) {
                    Bukkit.broadcastMessage("This is Ammonium's skull.");
                }
            }
        }
     
  2. Offline

    KarimAKL

    @Ammonium_ You need to cast block.getState() to Skull, not the block.
     
    Kars likes this.
Thread Status:
Not open for further replies.

Share This Page