Checking for a block near a player

Discussion in 'Plugin Development' started by teozfrank, Jan 13, 2014.

Thread Status:
Not open for further replies.
  1. hi guys how would i check to see if a player is in a 3 x 3 x 3 block radius from obsidian? i have tryed 3 for loops for x y and z but it only ended up crashing the server xD.

    Code:java
    1. for (int x1 = loc1x; x > loc2x; x++) {
    2. for (int y1 = loc1y; y > loc2y; y++) {
    3. for (int z1 = loc1z; z > loc2z; z++) {
    4. Block block = world.getBlockAt(x, y, z);
    5. if (block.getType() == Material.OBSIDIAN){
    6. return true;
    7. }
    8. }
    9. }
    10. }


    am i going in the right direction?

    thanks :D
     
  2. Offline

    calebbfmv

    I see, when are you checking this event?
     
  3. when a command is executed it checks this..
     
  4. Offline

    JRL1004

    teozfrank Your variables are x1, y1, and z1, but you are incrementing x, y, z.
     
  5. yeah i noticed that.. tried to fix it but nothing it keeps crashing the server, is there a better way to go about this?
     
  6. Offline

    Gopaintman

    Does it just crash it and you don't get a stack trace? If there is one post it. Most probably you're running out of RAM because you're saving a block each time. Try just doing
    Code:java
    1. if(world.getBlockAt(x,y,z).getType().equals(Material.Obsidian){
    2. return true;
    3. }
     
  7. Offline

    JazzaG

    `getBlockAt(int, int, int)` gets the block using absolute points. You need to get the block relative to the player using `getRelative(int, int, int)`

    Code:
    int radius = 3;
    for(int i = -radius; i <= radius; i++) {
        for(int j = -radius; j <= radius; j++) {
            for(int k = -radius; k <= radius; k++) {
                if(player.getBlock().getRelative(i, j, k).getType() == Material.OBSIDIAN)
                    return true;
            }
        }
    }
     
    calebbfmv likes this.
Thread Status:
Not open for further replies.

Share This Page