[SOLVED]Getting block that the button is on

Discussion in 'Plugin Development' started by mollekake, Aug 20, 2012.

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


    How can i get the block that the button is on?
    I want the button to be cancelled when the player has no XP, which i think i know how to do.
    But some buttons are free, so need to find the block that the button is on.

    Unless you have a simpler solution?
  2. Offline


    Button button = ((Button) buttonBlock.getState());
    Block block = buttonBlock.getRelative(button.getAttachedFace());
    Btw, what do you mean with "some buttons are free"?
  3. Offline


    thanks! by that i mean that if the player has XP, the player will loose 1 level for pushing a button, but some buttons are free to push, require no xp.
  4. Offline


    Button implements Attachable, so if you have a block, get its BlockState, cast it to Button, and use getAttachedFace().

    This, obviously, is without any checks before casting, you can figure that out.
                Block block = //from your event;
                Button button = (Button) block.getState();
                Block attachedBlock = block.getRelative(button.getAttachedFace());
    Apparently, I'm slow, lol
  5. Offline


    OK, so i've gotten further, but why won't this work?
    Block block = event.getClickedBlock();
                Button button = (Button) block.getState();
                Block attachedBlock = block.getRelative(button.getAttachedFace());
                if(event.getAction() == Action.PHYSICAL){
                    if(attachedBlock == attachedBlock.getType(Material.IRON_BLOCK)){
  6. Offline


    I've never used the PHYSICAL enum, have you tried the left click block one?
  7. Offline


    no havent tried, will try, but i get an error on tje last if line:
    the method gettype() in the type Block is not applicable for the arguemnts(Material)

    i don't get how i can do what i want :S
  8. Offline


    if(attachedBlock == attachedBlock.getType(Material.IRON_BLOCK))
    is complete wrong...:
    if(attachedBlock.getType() == Material.IRON_BLOCK))
  9. Offline


    guess i was thinking all wrong. thanks! seems to work now, will test more.

    getting an error:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.block.CraftBlockState cannot be cast to org.bukkit.block.Sign
    so i tried this:

    Block block = event.getClickedBlock();
            //Button button = (Button) block.getState();
            if(block instanceof Button){
                System.out.print("A button");
                System.out.print("Not a button");
    no matter what i try, nothing is registred as a button.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 28, 2016
  10. Offline


    Because Block can never be instanceof Button.
    You need to check whether event.getClickedBlock().getState() is instanceof Button.
  11. Offline


    You need to get your stuff in order:
    - first check if action is LEFT/RIGHT_CLICK_BLOCK (physical is for pressure plates)
    - store the clicked block's BlockState to a variable
    - check if blockstate is instanceof Button
    - store the cast blockstate to Button in a variable
    - get attached face and get that block the button is attached to
    - depending on the block, give take level from player
    - or decide for yourself if the button is free or not, I have no clue how you want to set that
  12. Offline


    Don't get why it doesn't say it's a button:
    if(event.getAction() == Action.LEFT_CLICK_BLOCK){
            if(event.getClickedBlock().getState() instanceof Button){
                System.out.print("is a button");
                System.out.print("not a button");
    it says it's not a button every time. is this not correct?
  13. Offline


    Actually Button is a Material, not a BlockState, I think you can use block.getData() instanceof Button.
  14. Offline


    That's what i figured out too.
    but i found a plugin that deals with buttons, and borrowed some code. this is what i got, it's working but i haven't gone through it and remove uneccesary code.

    //Button payment stuff
        private Block getButtonBlock(Block btn){
            case 1:
                return btn.getWorld().getBlockAt(btn.getX()-1, btn.getY(), btn.getZ());
            case 2:
                return btn.getWorld().getBlockAt(btn.getX()+1, btn.getY(), btn.getZ());
            case 3:
                return btn.getWorld().getBlockAt(btn.getX(), btn.getY(), btn.getZ()-1);
            case 4:
                return btn.getWorld().getBlockAt(btn.getX(), btn.getY(), btn.getZ()+1);
                return null;
        //spawn eggs, infinite cake, button payments/verification
        public void onPlayerInteract(PlayerInteractEvent event){
        if(event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_BLOCK){
            if(event.getClickedBlock() != null && event.getClickedBlock().getType() == Material.STONE_BUTTON){
                Block btned = null;
                    if((btned = getButtonBlock(event.getClickedBlock())) != null && btned.getType() == Material.IRON_BLOCK){
                        //System.out.print("DEBUG: Button on Iron Block!");
    it does not however, use the attachedface event, so i'm gonna look more into it.
  15. Offline


    The attached face is just an interface for easyness, but that code works just as fine because it gets the data value, which attached face also uses.
Thread Status:
Not open for further replies.

Share This Page