Searching if block is near block.

Discussion in 'Plugin Development' started by Dallas, Mar 17, 2011.

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


    Hey in my plugin,
    There is a goldblock.
    When the user rightclicks the block it needs to search one block around it for a diamondblock and then return the location of this block.
    Is this possible?
    If so how.
    Much thanks
  2. Offline


    There is a method on the world object called getBlockAt(x, y, z) that you can use to get nearby blocks. You can also use the getRelative() methods off the block object itself. A suggestion would be to do a nested for loop to get these, e.g.:

    Block block = event.getBlock();
    for(int z = -1; z <= 1; z++) {
      for(int x = -1; x <= 1; x++) {
        for(int y = -1; y <= 1; y++) {
          if(block.getRelative(x, y, z).getType().equals(Material.DIAMOND_ORE)) {
            System.out.println("I found a diamond block nearby...");
    Sorry if this code does not compile, doing it off the top of my head just to give you can idea of how to search.
  3. Offline

    Edward Hand

    elterqiro has the right idea (but you might want to change DIAMOND_ORE to DIAMOND_BLOCK if that's what you meant).

    His code checks all blocks in a cube around, so it will also pick up blocks touching only by a corner/diagonally.
    This might be what you want it to do (you didn't specify). If you want it to be touching the block, face-to-face then change the if statement to:
    if(x*x+y*y+z*z==1 && block.getRelative(x, y, z).getType().equals(Material.DIAMOND_BLOCK)) {
  4. Offline


    Ok, im not really understanding, he wants it so when somone right clicks the gold block (PlayerInteractEvent event)
    it would do the code, but how do register the block if its in the playerinteractevent?
  5. Offline


    Get the target block and check if it's a gold block:

     public void interact(PlayerInteractEvent e){
            if(e.getPlayer().getTargetBlock(null, distance).getType().equals(Material.GOLD_BLOCK)){
                //your script
Thread Status:
Not open for further replies.

Share This Page