[SOLVED] Checking the block under a player

Discussion in 'Plugin Development' started by number1_Master, Feb 17, 2012.

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

    number1_Master

    How would you check the block under a player. In an if statement, would you start like this:
    Code:java
    1. if(player.getLocation().getBlock().getRelative(BlockFace.DOWN) == )
    2. {
    3.  
    4. }

    ... and that is where I am stuck. I can't figure out what to put there. I thought you would just have to get the type or material or... ya. Something similar to those. I tried a few things but... How would you do it?
     
  2. Offline

    barneebrown

    Seeing as you haven't gotten an answer yet.... It may not be the best way, but I use:

    Code:
    @EventHandler(priority = EventPriority.NORMAL)
    public void onPlayerMoveEvent(PlayerMoveEvent event)
        {
    Location loc = player.getPLayer().getLocation;
    loc.setY(loc.getY() -2);
     
    int block = loc.getWorld().getBlockTypeIdAt(loc)
    if (block == 22)
    {
    //code goes here
    }
    }
    
    block == 22 refers to Lapis Lazuli
     
  3. Offline

    number1_Master

    I know block 22 is Lapis :p
    ahh, I was interpreting this differently. So this will get the Y, then check what the block is, then if, ya...
    wats with -2 of getY?
     
  4. Offline

    Njol

    Code:java
    1. if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getMaterial() == Material.LAPIS_BLOCK) {
    2.  
    3. }
     
    nunber1_Master likes this.
  5. Offline

    barneebrown

    Njol's code is more efficient =D
    ignore my post

     
  6. The -2 means that it will go down 2 blocks of the player, because I believe that players location is his head so it has to go down 2 to get the block he/she is standing.

    Please tell me if you find this confusing.

    Keir
     
  7. Offline

    ZNickq

     
  8. Offline

    number1_Master

    I don't find that confusing. I always thought the location was the feet. :/

    I see. Ok, ya. Some of the errors I got were because of a block not being a material type thing
    thanks, this is what i was looking for!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  9. That is what I think it means anyway.
     
    nunber1_Master likes this.
  10. Offline

    number1_Master

    The method getMaterial() is undefined for the type Block. The option is to cast an method to the receiver.
     
  11. Offline

    Njol

  12. Offline

    number1_Master

Thread Status:
Not open for further replies.

Share This Page