Solved Cast block to ladder

Discussion in 'Plugin Development' started by tyggerr, Feb 7, 2015.

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

    tyggerr

    Hi !
    I've been developping a plugin where I want to deny the ability for players to place ladders on some blocks (like dirt/grass). At the moment, I try to find where the ladder is attached and check if the player can place it there. However, I can't find a way to cast the block to Ladder, nor Attachable.

    Even after a lot of tests, I can't figure out how to cast a block to a Ladder.

    Here's my actual code :

    Code:
    @EventHandler
        public void playerTryToPlaceBlock(BlockPlaceEvent _event)
        {
            Material blockPlaced = _event.getBlock().getType();
    
            if (blockPlaced == Material.LADDER)
            {
                Block ladder = _event.getBlock();
                Ladder ladderPlaced = (Ladder) ladder.getState();
                Material ladderSupportMaterial = ladder.getRelative(ladderPlaced.getAttachedFace()).getType();
               
                if(ladderSupportMaterial == Material.DIRT || ladderSupportMaterial == Material.GRASS ||  ladderSupportMaterial == Material.BARRIER)
                {
                    _event.getPlayer().sendMessage(StringColor.Red + "This block cannot support a ladder !");
                    _event.setCancelled(true);
                }
            }
    I tried to cast to "Attachable", tried without the "getState()" even tried to cast to SimpleAttachableMaterialData.
    I have no more idea left, any help would be welcome. ^^
     
  2. Offline

    ColonelHedgehog

    Try:

    Code:
    ((Ladder) ladder_block.getData()).getAttachedFace()
     
  3. Offline

    Konato_K

  4. Offline

    tyggerr

    @ColonelHedgehog, thanks ! I tested with "getData" and found how to cast it :

    Code:
    Block blockPlaced = _event.getBlock();
    Ladder ladderPlaced = (Ladder) blockPlaced.getState().getData();
    
    Material ladderSupportMaterial = blockPlaced.getRelative(ladderPlaced.getAttachedFace()).getType();
    Problem solved ! :D

    @Konato_K, I'll try using your method, it seems better and faster. Thanks ! :D
     
Thread Status:
Not open for further replies.

Share This Page