Detecting if a player clicks water (Similar to filling a bucket)

Discussion in 'Plugin Development' started by dark navi, Jul 18, 2012.

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

    dark navi

    Hey guys, I am trying to detect if a player is clicking on water (the surface or underwater). This is how I am currently doing it:​
    Code:
    @EventHandler (priority = EventPriority.MONITOR)
    public void onPlayerInteract(PlayerInteractEvent event)
    {
    Player player = event.getPlayer();
    //Detect if a player is within a few blocks of water and has clicked
                Block waterBlock = null;
                for(Block b : player.getLineOfSight(null, 2))
                {
                    if(b.getType().equals(Material.WATER) || b.getType().equals(Material.STATIONARY_WATER))
                        waterBlock = b;
                }
    }
    This seems to only work (on the playerIntereactEvent) if a place actually is selecting a block. Is there any way I can detect if a player clicks (Left and/or Right clicks) on the surface of water with nothing in their hand?
     
  2. is there anny more code we cant see?
     
  3. Offline

    dark navi

    Updated. It's within the PlayerInteractEvent.
     
  4. I think the player can see as up to 4 blocks insitead of 2 as you plced inside the code
     
  5. Few small tips:
    - You don't need equals() for enums, just use .getType() == Material.WATER
    - You might want to use "break;" when you find your block because if you don't it will keep searching for water blocks and keep overwriting that until it points to some water block at the bottom of the lake/ocean/whatever.
     
  6. Offline

    bartboy8

    for(Block b : player.getClickedBlock()){
    if(b == Material.WATER && event.getAction() == Action.RIGHT_CLICK_BLOCK){
    //do ur stuff
    }
    }
     
  7. bartboy8
    Why are you checking action inside the loop ?
    getClickedBlock() doesn't return an array... so you can't loop it.
    Also, he shouldn't check if action is clicking block because he's tracing the direction of player aim, since you can't actually select and click a water block.
     
Thread Status:
Not open for further replies.

Share This Page