Solved How do you get/set skull type on blocks?

Discussion in 'Plugin Development' started by Ne0nx3r0, Apr 11, 2013.

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

    Ne0nx3r0

    I spent the better part of last night trying different snippets and methods to get this to work... I'm so far only able to get and set the position of the skull using the block data.

    Can someone explain how you get and set the skull type of a block?
     
  2. Offline

    Rprrr

    Ne0nx3r0
    Code:
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e){
            Block block = e.getBlock();
            if (block.getState() instanceof Skull){
                Skull skull = (Skull) block.getState();
           
                SkullType skullType = skull.getSkullType();
            }
        }
     
  3. Offline

    Rprrr

    Digi
    Code:
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e){
     
            Block block = e.getBlock();
            BlockState blockState = e.getBlock().getState();
     
            if (blockState instanceof Skull){
     
                Skull skull = (Skull) blockState;
                SkullType skullType = skull.getSkullType();
            }
        }
    Better? :p
     
  4. Offline

    Ne0nx3r0

    Digi Rprrr That's what I was doing last night, and it seems to get the skull type okay, but then when I go to change it the change never seems to happen. It just changes the block to a white skull that is pointing the right direction.

    Doing this in a command.

    Code:java
    1.  
    2. Block b = player.getLocation().add(bk.xOffset, bk.yOffset, bk.zOffset).getBlock();
    3.  
    4. b.setTypeIdAndData(bk.blockType, bk.blockData, false);
    5.  
    6. if(bk.blockType == Material.SKULL.getId())
    7. {
    8. ((Skull) b.getState()).setSkullType(SkullType.WITHER);
    9. }
     
  5. Offline

    Ne0nx3r0

  6. This issue isn't solved for me :(

    I'm having trouble with getting a placed skull's type for a few days now (from a BlockPlaceEvent).
    I've tried the code above (by Rprrr), but it returns "SKELETON" every time, no matter which skull is actually placed in-game.

    I've looked around and tried a suggestion of calling .update() on the skull, but still nothing.

    Any help would be greatly appreciated - this is driving me insane!
     
  7. Prawny
    The BlockPlaceEvent has a getBlockReplacedState() method which you should use instead of block.getState().
     
  8. Digi
    Sorry, I worded that badly. I'm trying to get the type of skull that is currently being placed, i.e. the block that triggers the BlockPlaceEvent.
     
  9. Prawny yes, replaced state is the new state of the placed block which is replacing the old block state...

    You could've also used getInHandItem() and check data value but this is better.
     
  10. BlockPlaceEvent.getBlockReplacedState() returns the block's material that was previously there (which 95% of the time is air).
    I want the block being placed in the event (the item in the player's hand), not what was there before.
     
  11. Hmm, apparently I misunderstood the name for that xD

    But if that returns the previous state then event.getBlock() should return the new state... should, anyway.

    You can use just event.getItemInHand() then to check the data value which will tell you the skull type.
     
    Prawny likes this.
  12. Using getItemInHand() works, but it seems like an unnecessary work-around :/
    Thanks for the help!
     
  13. Prawny
    I belive it's not really possible to get the state of a block that doesn't exist yet, since the event is triggered before the block is updated.
     
Thread Status:
Not open for further replies.

Share This Page