Solved Help me with creating a wall!

Discussion in 'Plugin Development' started by kasszz, May 5, 2013.

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

    kasszz

    Dear bukketiers,

    I'm currenly creating a wall on a right click, but something weird happens.

    First, this is my code:
    Code:
    else if(player.getItemInHand().getItemMeta().getDisplayName().equals("Arrow Wand") && !cooldown.contains(player.getDisplayName() + " Arrow Wand")){
                                cooldown.add(player.getDisplayName() + " Arrow Wand");
                                for(int x = -2; x <= 2; x++){
                                    for(int y = 0; y <= 2; y++){
                                        Location targetBlockLoc = player.getTargetBlock(null, 60).getLocation().add(x, y, 0);
                                        Block targetBlock = targetBlockLoc.getBlock();
                                        System.out.print("TEST x = " + targetBlockLoc.getX() + ", Y = " + targetBlockLoc.getY());
                                        if(targetBlock.getType() == Material.AIR){
                                            targetBlock.setType(Material.LEAVES);
                                            System.out.print("x is " + x + " en y is " + y);
                                        }
                                    }
                                }
    I want a wall like this:

    x x x x x
    x x x x x
    x x x x x

    But it turns out like this:

    y y x x x
    x x y x x
    x x x x x
    x x y y y

    x= wall
    y= air

    (hmmm, it doesnt like my shape or something because this is not the shape that I'm making :S EDIT: it works, only took me 6 times xD)

    NO CLUE WHATS GOING ON HERE!
    HEELLLPPPP!!!!! thanks :)

    And I know that it isn't working correctly when you are looking to the Z-axis.

    So, I found out that when you click on the side of a block it works, the wall spawns on the block in the right way, but if I click on top of that block it messes the wall up again :( wtf :(

    Digi helppp xD

    Can no one help me with this code :( ?

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

    xize

    I made something like this:
    Code:
     @EventHandler
     public void walls(PlayerInteractEvent e) {
      if(e.getPlayer().getItemInHand().getType() == Material.ARROW && e.getAction() == Action.RIGHT_CLICK_AIR) {
       Location loc = e.getPlayer().getTargetBlock(null, 60).getLocation();
       int X = (int) loc.getX();
       int Y = (int) loc.getY();
       int Z = (int) loc.getZ();
       for(int x=0; x < 6; x++) {
        for(int y = 0; y<5;y++) {
         Block block = loc.getBlock().getWorld().getBlockAt(X+x, Y+y, Z);
         block.setType(Material.LEAVES);
        }
       }
      } else {
       return;
      }
     }
    
    maybe it's not the best way, because I don't use the block face to set the direction as wall.
     
  3. Offline

    kasszz


    I think that is my problem aswell, is there a good way to fix it, the block face thing I mean ?
    It does work a lot better, thanks :)
    Now the hard part with the z axis xD
     
  4. Offline

    xize

    I think the best way is checking on the direction of the BlockFace, then make for each if and else expression a other for each loop which loops in via the BlockFace direction.

    I whas trying to make this:p

    Code:
    @EventHandler
    public void walls(PlayerInteractEvent e) {
      if(e.getPlayer().getItemInHand().getType() == Material.ARROW && e.getAction() == Action.RIGHT_CLICK_AIR) {
      if(e.getPlayer().getLocation().getBlock().getFace(e.getPlayer().getLocation().getBlock()) == BlockFace.NORTH) {
     
      } else if(e.getPlayer().getLocation().getBlock().getFace(e.getPlayer().getLocation().getBlock()) == BlockFace.EAST) {
     
      } else if(e.getPlayer().getLocation().getBlock().getFace(e.getPlayer().getLocation().getBlock()) == BlockFace.SOUTH) {
     
      } else if(e.getPlayer().getLocation().getBlock().getFace(e.getPlayer().getLocation().getBlock()) == BlockFace.WEST) {
     
      }
      } else {
      return;
      }
    }
    
    but I'm not sure what happends when it's not equals to North, , East, South, West
     
  5. Offline

    kasszz

    Now that I'm testing it.... it allready works the right way xD and no error.
    the only thing that I have to do now is looking is the player need to use the Z or the X axis :p

    xize

    why does east still behave like north or south :S ?

    I tried playerDirection > -135 && playerDirection < -45
    and playerDirection < -135 && playerDirection > -45

    code:
    Code:
                            else if(player.getItemInHand().getItemMeta().getDisplayName().equals("Arrow Wand") && !cooldown.contains(player.getDisplayName() + " Arrow Wand")){
                                cooldown.add(player.getDisplayName() + " Arrow Wand");
                                Location loc = player.getTargetBlock(null, 60).getLocation();
                                Float playerDirection = player.getLocation().getYaw();
                                int X = (int) loc.getX();
                                int Y = (int) loc.getY() + 1;
                                int Z = (int) loc.getZ();
                                //-135 tot -45
                                // 135 tot  45
                                if(playerDirection <= 135 && playerDirection >= 45){
                                    for(int z = -2; z < 3; z++) {
                                        for(int y = 0; y < 3; y++) {
                                            Block block = loc.getBlock().getWorld().getBlockAt(X, Y+y, Z+z);
                                            if(block.getType() == Material.AIR){
                                                block.setType(Material.LEAVES);
                                            }
                                        }
                                    }
                                }
                                else if(playerDirection > -135 && playerDirection < -45){
                                    for(int z = -2; z < 3; z++) {
                                        for(int y = 0; y < 3; y++) {
                                            Block block = loc.getBlock().getWorld().getBlockAt(X, Y+y, Z+z);
                                            if(block.getType() == Material.AIR){
                                                block.setType(Material.LEAVES);
                                            }
                                        }
                                    }
                                }
                                else{
                                    for(int x = -2; x < 3; x++) {
                                        for(int y = 0; y < 3; y++) {
                                            Block block = loc.getBlock().getWorld().getBlockAt(X+x, Y+y, Z);
                                            if(block.getType() == Material.AIR){
                                                block.setType(Material.LEAVES);
                                            }
                                        }
                                    }
                                }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  6. Offline

    xize

    I think it is, because you have to work with negative intenger aswell.
    I managed to get it to work but just for a part.

    North works, south works aswell, but east or west works but the another don't

    this mine:

    Code:
    if(e.getPlayer().getItemInHand().getType() == Material.ARROW && e.getAction() == Action.RIGHT_CLICK_AIR) {
      if(e.getPlayer().getLocation().getYaw() > 80 && e.getPlayer().getLocation().getYaw() < 95 || e.getPlayer().getLocation().getYaw() < -80 && e.getPlayer().getLocation().getYaw() < -95) {
        Location loc = e.getPlayer().getTargetBlock(null, 60).getLocation(); 
        int X = (int) loc.getX();
        int Y = (int) loc.getY();
        int Z = (int) loc.getZ();
        for(int x=0; x < 6; x++) {
        for(int y = 0; y<5;y++) {
          Block block = loc.getBlock().getWorld().getBlockAt(X+x, Y+y, Z);
          block.setType(Material.LEAVES);
        }
        }
      } else {
        Location loc = e.getPlayer().getTargetBlock(null, 60).getLocation(); 
        int X = (int) loc.getX();
        int Y = (int) loc.getY();
        int Z = (int) loc.getZ();
        for(int z=0; z < 6; z++) {
        for(int y = 0; y<5;y++) {
          Block block = loc.getBlock().getWorld().getBlockAt(X, Y+y, Z+z);
          block.setType(Material.LEAVES);
        }
        }
      }
      } else {
      return;
      }
    }
    
    but still it's hard for me to find the way to fix that math is not my best:p
    3 positions works but one fails which give me no clue why:p
     
  7. Offline

    kasszz

    Hmmm, but if you work with negative int's you can just make the > a < or is it something with bukkit ?

    xize

    Hmmm Now I have this:
    Code:
    else if(player.getItemInHand().getItemMeta().getDisplayName().equals("Wall of Leaf Wand") && !cooldown.contains(player.getDisplayName() + " Wall of Leaf Wand")){
                                cooldown.add(player.getDisplayName() + " Wall of Leaf Wand");
                                Location loc = player.getTargetBlock(null, 60).getLocation();
                                Float playerDirection = player.getLocation().getYaw();
                                int X = (int) loc.getX();
                                int Y = (int) loc.getY() + 1;
                                int Z = (int) loc.getZ();
                                System.out.print("x is " + player.getLocation().getYaw());
                                if((playerDirection >= 90 && playerDirection <= 180) || (playerDirection >=270 && playerDirection <= 360)){
                                    for(int z = -2; z < 3; z++) {
                                        for(int y = 0; y < 3; y++) {
                                            Block block = loc.getBlock().getWorld().getBlockAt(X, Y+y, Z+z);
                                            if(block.getType() == Material.AIR){
                                                block.setType(Material.LEAVES);
                                            }
                                        }
                                    }
                                }
                                else{
                                    for(int x = -2; x < 3; x++) {
                                        for(int y = 0; y < 3; y++) {
                                            Block block = loc.getBlock().getWorld().getBlockAt(X+x, Y+y, Z);
                                            if(block.getType() == Material.AIR){
                                                block.setType(Material.LEAVES);
                                            }
                                        }
                                    }
                                }
    because getYaw is just 0 to 360

    but now my South and my West are switched :S

    help xD ?

    I found it out, I tought that
    South is 0 - 90
    West is 90-180
    North is 180-270
    East is 270-360

    but after a long hard think and a lot of debugging.

    South is 316 - 45
    West is 46 - 135
    North is 136 - 225
    East is 226 - 315

    but it works now :) thank xize :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page