Any way to detect if a player touch a block (not with hand - with body)

Discussion in 'Plugin Development' started by FenixAzul, Oct 6, 2011.

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

    FenixAzul

    M i know there a way to check if a player touched a block with hand but its there a way to get if a player have touch a block with his body?
     
  2. Offline

    Belf

    You can use onPlayerMove with player Location
     
  3. Offline

    FenixAzul

    i dont get it, how i can get if hes touching a block?
     
  4. Offline

    Torrent

    Yeah, this.
    Just make sure you get the block that's underneath the player, though.
     
  5. Offline

    FenixAzul

    nonono i dont mean tto see if hes touch the floor im trying to see if hes touching blocks like walls
     
  6. Offline

    Belf

    You can use something like that (badly coded and not tested, but that's the idea)

    Code:
    @Override
            public void onPlayerMove(PlayerMoveEvent event) {
    
                if (event.isCancelled()) {
                    return;
                }
    
                if (event.getFrom().getBlockX() == event.getTo().getBlockX()
                        && event.getFrom().getBlockY() == event.getTo().getBlockY()
                        && event.getFrom().getBlockZ() == event.getTo().getBlockZ()) {
                    return;
                }
    
                Player player = event.getPlayer();
                Location playerLoc = player.getLocation();
                World world = playerLoc.getWorld();
    
                for(int x = (int)(playerLoc.getX()-1); x<=(int)(playerLoc.getX()+1); x++) {
                    for(int y=(int)(playerLoc.getY()-1); y<=(int)(playerLoc.getY()+1); y++) {
                        for(int z=(int)(playerLoc.getZ()-1); z<=(int)(playerLoc.getZ()+1); z++) {
                            
                            if(world.getBlockAt(x, y, z).getTypeId() != 0) {
                                player.sendMessage("BlockID " + world.getBlockAt(x, y, z).getTypeId() + " at " + x + " ; " + y + " ; " + z);
                            }
                        }
                    }
                }
    }

    With this, you get the 3x3 blocks nearby the player.
     
  7. Offline

    FenixAzul

    ok i trouh there was another way
     
  8. Offline

    Belf

    If there is, i don't know how to do it :)
     
  9. Offline

    FenixAzul

Thread Status:
Not open for further replies.

Share This Page