Solved CraftBlock cannot be cast to Skull

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

Thread Status:
Not open for further replies.
  1. 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 (Text):
    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. Cast the BlockState to the Skull instead.
    Code (Java):
    Skull skull = (Skull) block.getState();
     
  3. Skull is a BlockState. You can check this by looking going to the documentation and looking at where ist says "extends".
    Therefore, you'll need to get the state, not the block:
    Code (Text):
    Skull skull = (Skull) block.getState();
     
  4. Solved, thank you!
     
Thread Status:
Not open for further replies.