[Question] Block click action

Discussion in 'Spigot Plugin Development' started by Hangar555, Jun 5, 2016.

  1. How do I do that when a player right click a block he performance a command?

    Yes, I'm noob at the moment.... and I'm learning Java, so don't be rude ;)
     
  2. Upon interaction:
    if (get action of event == Action.RIGHT_CLICK_BLOCK) {
    // Perform command
    }

    Something like that? c:
     
  3. such as....
    Code (Java):

        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
           Action action = e.getAction();
           Player player = e.getPlayer();
           if (event.getAction.equals(Action.RIGHT_CLICK_BLOCK)) {
            player.performCommand("help");  
             
        }
                 }
             }
        }
    }
     
     
  4. Correct! ^-^
     
  5. Well... "getAction cannot be resolved or is not a field"
     
  6. Code (Java):
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                e.getPlayer().performCommand("help");
            }
        }
    See the differences?
     
  7. Yeah. THANKS! If I want to change the block (for example, only grass block) how do I do it?

    THANKS AGAIN
     
    • Friendly Friendly x 1
  8. if (e.getClickedBlock.getType() == Material.DIRT) { // ? }
     
  9. To set a block you simply get the block and change it's type. World#getBlockAt(x, y, z).setType(Material.DESIRED_MATERIAL)