Learning - Get the block type below a player

Discussion in 'Plugin Development' started by Spemble, Aug 2, 2014.

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

    Spemble

    I am trying to make an if statement that checks if the block below a player is... idk, a Lapis Lazuli Block... I am learning, and find that people on here know and can help faster than searching google (something I'm not good at.)
    Also, instead of giving me working code, could you only hint me in the right directions so I can learn? Thanks!

    I'm thinking something like
    if(getType( ... )
    I don't know how to say what block I am talking about, all of the methods seem to be functions assuming that it already knows the block, so I was thinking [ new Block(player.getLocation()) ] but the block that the player is walking on is already there.... ???

    I'm probably making no sense, but neither is the code for me so please don't hate.

    Thanks,

    Spemble
     
  2. Offline

    xTigerRebornx

  3. Offline

    Spemble

    xTigerRebornx
    So how does it know the block coords? Do I get them from the event or the player?

    Don't I need to have the block initiated somehow first so it knows which block to go off of?
     
  4. Offline

    xTigerRebornx

    Spemble Depends on how you want to do it and when you want to do the check. Player#getLocation() returns the location, one below that is the block below them.
     
  5. Offline

    Spemble

    xTigerRebornx

    So something like this?

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent event) {
    3. Player player = event.getPlayer();
    4. Block block = world.getBlockAt(player.getLocation());
    5. if(getType(block.getRelative(BlockFace.DOWN, 1)) == Material.LAPIS_BLOCK){
    6. Code here
    7. }
    8. }
     
  6. Offline

    fireblast709

    Spemble looks good. Although, what does getType(Block) do?
     
  7. Offline

    Spemble

    Hmmm... I also don't know how to create the world... variable? *sigh*

    fireblast709
    It gets the type of the block.
    Returns: block type
    I'm guessing as a Material...?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  8. Offline

    Giraffeknee

    Spemble
    Just do player.getLocation().getBlock()
     
  9. Offline

    fireblast709

    Spemble Assuming you return block.getType(), then your code should be correct
     
  10. Offline

    Spemble

    Where can I find a function like that myself in the future? I can't find it in
    http://jd.bukkit.org/beta/apidocs/index.html

    fireblast709
    Also, how do I get the world? world.getBlockAt() can't work if it doesn't know what "world" is. The player was made with
    Player player = event.getPlayer();
    but I'm not sure how to do that with "world"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  11. Offline

    fireblast709

  12. Offline

    Spemble

    Oh, *FACEDESK*
    Looking at the player interface, not block.

    fireblast709
    Giraffeknee

    For:
    Block block = world.getBlockAt(player.getLocation());
    it has an error for under "world" and it says "world cannot be resolved"

    Then at:
    if(block.getType(player.getLocation().getBlock()) == Material.LAPIS_BLOCK)
    it has an error for under "getType" and it says "The method getType() in the type Block is not applicable for the arguments (Block)"

    *sigh*

    I think I might need to take a break from this XD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  13. Offline

    Giraffeknee

    Spemble
    Assuming block = player.getLocation().getBlock(),
    if (block.getType().equals(Material.LAPIS_BLOCK))
     
  14. Offline

    fireblast709

    Spemble
    This can be used instead of world.getBlockAt(Location). Also, getType() (and getters in general) don't have any parameters. Just call it without any parameters
     
Thread Status:
Not open for further replies.

Share This Page