Solved How do I set a 3x3 area under the player's location.

Discussion in 'Plugin Development' started by Denziz, Mar 30, 2014.

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

    Denziz

    Hello there.. Well I want to know what the topic says. How do I set a area of 3x3

    I have been testing stuff and I can't figure it out!

    Code:java
    1. if(commandLabel.equalsIgnoreCase("test")){
    2. if(player.isOp()){
    3. setblockobs(42, player.getLocation(), player);
    4. }
    5. }


    Code:java
    1. public void setblockobs(int blockid, Location location, Player player){
    2. Location locz = player.getLocation();
    3. locz.setY(locz.getY() -1);
    4. Block blockchange = locz.getBlock();
    5. blockchange.setType(Material.IRON_BLOCK);
    6. }


    Help is appreciated :D

    fireblast709
    Do you know how to do? :p

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

    Scizzr

    Code:
    Location loc; //you need to specify this
    int radius = 1; //this is 1 block out from the center, so a total of 1+(1*2); 2 would be 5 blocks, 3 would be 7 blocks, and so on
    setFloor(loc, radius, Material.IRON_BLOCK);
    public void setFloor(Location center, int radius, Material material) {
        for (int xMod = -radius; xMod <= radius; xMod++) {
            for (int zMod = -radius; zMod <= radius; zMod++) {
                Block theBlock = center.getBlock().getRelativeTo(xMod, 0, zMod);
                theBlock.setType(material);
            }
        }
    }
    
    Untested but should work.
    Modify it to fit your needs.
     
  3. Offline

    Denziz

    How do I do that?

    Also, sorry but now it will create a 3x3 cube? I want it to be only X and Z, so that it will be created on the ground (Flat). I might just be wrong lol..
     
  4. Offline

    Scizzr

    Fixed the first part.

    Regarding specifying the location, you need to tell it where the center of the platform should be, aka the middle of the 3x3.
    Code:
    [ ][ ][ ]
    [ ][L][ ]
    [ ][ ][ ]
    
    If I'm standing at L, that's the center.
     
  5. Offline

    Denziz

    The platform of the center should be where the player stands, then set the 3x3 area under him.
     
  6. Offline

    Scizzr

    Exactly. So this then, to get the block they're on top of:
    Code:
    Location loc = player.getLocation().clone().add(0.0, -1.0, 0.0);
    int radius = 1;
    setFloor(loc, radius, Material.IRON_BLOCK);
     
    public void setFloor(Location center, int radius, Material material) {
        for (int xMod = -radius; xMod <= radius; xMod++) {
            for (int zMod = -radius; zMod <= radius; zMod++) {
                Block theBlock = center.getBlock().getRelativeTo(xMod, 0, zMod);
                theBlock.setType(material);
            }
        }
    }
    
     
  7. Offline

    Denziz

    Now it is creating a 7x7 area. But it is not creating it under where I stand. And when I am creating it, It doesn't create it in the center, like....

    X = block
    Y = player

    X X X X X X X
    X X X X X X X
    X X Y X X X X
    X X X X X X X
    X X X X X X X
    X X X X X X X
    X X X X X X X
     
  8. Offline

    callum2904


    You are using 3 as your radius, use 1 so that it creates the circle 1 block around the player
     
  9. Offline

    Scizzr

    I don't know then. My could does this:
    Code:
    for (int xMod = -radius; xMod <= radius; xMod++) {
        (starts at -radius or -1, goes to radius, or 1... that's -1, 0, 1)
        for (int zMod = -radius; zMod <= radius; zMod++) {
            (starts at -radius or -1, goes to radius, or 1... that's -1, 0, 1)
            Block theBlock = center.getBlock().getRelativeTo(xMod, 0, zMod);
            (gets the block relative to the original, with xMod from -1 to 1, and zMod from -1 to 1)
            theBlock.setType(material);
            (sets that block to the provided material)
        }
    }
    
    A test of the code, and the printed message:
    Code:
    int radius = 1;
    for (int xMod = -radius; xMod <= radius; xMod++) {
        for (int zMod = -radius; zMod <= radius; zMod++) {
            System.out.println(String.format("%d, 0, %d", xMod, zMod));
        }
    }
     
    //result
    -1, 0, -1
    -1, 0, 0
    -1, 0, 1
    0, 0, -1
    0, 0, 0
    0, 0, 1
    1, 0, -1
    1, 0, 0
    1, 0, 1
    
    That problem you're having, is it maybe something you've done?

    It's not really a circle, just some number from the middle. :)

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

    Denziz

    Code:java
    1. if(commandLabel.equalsIgnoreCase("test")){
    2. if(player.isOp()){
    3. Location loc = player.getLocation().clone().add(-1.0, 0.0, -1.0);
    4. setFloor(loc, 3, Material.IRON_BLOCK);
    5. }
    6. }


    Code:java
    1. int radius = 1; //this is 1 block out from the center, so a total of 1+(1*2); 2 would be 5 blocks, 3 would be 7 blocks, and so on
    2.  
    3. public void setFloor(Location center, int radius, Material material) {
    4. for (int xMod = -radius; xMod <= radius; xMod++) {
    5. for (int zMod = -radius; zMod <= radius; zMod++) {
    6. Block theBlock = center.getBlock().getRelative(xMod, 0, zMod);
    7. theBlock.setType(material);
    8. }
    9. }
    10. }


    So I changed it into "getRelative"

    Migh be that causing the problem?

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

    Scizzr

    Yeah, I fixed my code earlier. You need to fix the first bit.
    add(-1.0, 0.0, -1.0);
    needs to be
    add(0.0, -1.0, 0.0);

    Also, you're setting the radius to 3, which would yield a 7x7 area. the side-length is = 1+(radius*2)
    setFloor(loc, 3, Material.IRON_BLOCK);
    needs to be
    setFloor(loc, 1, Material.IRON_BLOCK);
     
    Denziz likes this.
  12. Offline

    Denziz

    Scizzr
    Cheers m8, It works. Check out your conversations :D
    Take care.
     
Thread Status:
Not open for further replies.

Share This Page