Generating a Cube with 2 points?!

Discussion in 'Plugin Development' started by killjoy64, May 25, 2012.

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

    killjoy64

    I've been trying for a while and I can't just seem to successfully generate a cube with 2 points...

    Code:
        private void define(Player player, String arena){
            World world = player.getWorld();
           
            int x_start = infection.poz1.get(player).getBlockX();
            int y_start = infection.poz1.get(player).getBlockY();
            int z_start = infection.poz1.get(player).getBlockZ();
       
            int x_end = infection.poz2.get(player).getBlockX();
            int y_end = infection.poz2.get(player).getBlockY();
            int z_end = infection.poz2.get(player).getBlockZ();
           
            int x_length;
            int y_length;
            int z_length;
           
            if(x_start < 0){
                if(x_end < 0){
                    if(x_end > x_start){
                        x_length = x_end - x_start;
                    } else {
                        x_length = x_start - x_end;
                    }
                } else {
                    x_length = x_end - x_start;
                }
            } else {
                if(x_start > x_end){
                    x_length = x_start - x_end;
                } else {
                    x_length = x_end - x_start;
                }
            }
            if(y_start < 0){
                if(y_end < 0){
                    if(y_end > y_start){
                        y_length = y_end - y_start;
                    } else {
                        y_length = y_start - y_end;
                    }
                } else {
                    y_length = y_end - y_start;
                }
            } else {
                if(y_start > y_end){
                    y_length = y_start - y_end;
                } else {
                    y_length = y_end - y_start;
                }
            }
            if(z_start < 0){
                if(z_end < 0){
                    if(z_end > z_start){
                        z_length = z_end - z_start;
                    } else {
                        z_length = z_start - z_end;
                    }
                } else {
                    z_length = z_end - z_start;
                }
            } else {
                if(z_start > z_end){
                    z_length = z_start - z_end;
                } else {
                    z_length = z_end - z_start;
                }
            }
           
            player.sendMessage("x length was " + x_length);
            player.sendMessage("y length was " + y_length);
            player.sendMessage("z length was " + z_length);
           
            for(int x = 0; x <= x_length; x++){
               
                Block blockX1 = world.getBlockAt(x_start, y_start, z_start);
                Block blockX2 = world.getBlockAt(x_start, y_start, z_end);
               
                blockX1.setTypeId(20);
                blockX2.setTypeId(20);
               
                if(x_start + x_length == x_end){
                    Block blockX3 = world.getBlockAt(x_start + x, y_start, z_start);
                    Block blockX4 = world.getBlockAt(x_start + x, y_start, z_end);
                    blockX3.setTypeId(20);
                    blockX4.setTypeId(20);
                   
                    infection.blocks.put(arena, blockX1);
                    infection.blocks.put(arena, blockX2);
                } else {
                    Block blockX3 = world.getBlockAt(x_start - x, y_start, z_start);
                    Block blockX4 = world.getBlockAt(x_start -x, y_start, z_end);
                    blockX3.setTypeId(20);
                    blockX4.setTypeId(20);
                   
                    infection.blocks.put(arena, blockX1);
                    infection.blocks.put(arena, blockX2);
                }
               
                for(int z = 0; z <= z_length; z++){
                    if(z_start + z_length == z_end){
                        Block blockZ1 = world.getBlockAt(x_start, y_start, z_start + z);
                        Block blockZ2 = world.getBlockAt(x_end, y_start, z_start + z);
                        blockZ1.setTypeId(20);
                        blockZ2.setTypeId(20);
                       
                        infection.blocks.put(arena, blockZ1);
                        infection.blocks.put(arena, blockZ2);
                    } else {
                        Block blockZ1 = world.getBlockAt(x_start, y_start, z_start - z);
                        Block blockZ2 = world.getBlockAt(x_end, y_start, z_start - z);
                        blockZ1.setTypeId(20);
                        blockZ2.setTypeId(20);
                       
                        infection.blocks.put(arena, blockZ1);
                        infection.blocks.put(arena, blockZ2);
                    }
                   
                    for(int y = 0; y <= y_length; y++){
                        if(y_start + y_length == y_end){
                            Block blockY1 = world.getBlockAt(x_start, y_start + y, z_start + z); 
                            Block blockY2 = world.getBlockAt(x_end, y_start + y, z_start + z);   
                            Block blockY3 = world.getBlockAt(x_start - x, y_start + y, z_end);    // Right Wall
                            Block blockY4 = world.getBlockAt(x_start - x, y_start, z_start + z);  // Bottom
                            Block blockY5 = world.getBlockAt(x_start - x, y_start + y, z_start);  // Left Wall
                            Block blockY6 = world.getBlockAt(x_start - x, y_end, z_start + z);    // Cube Roof
                            blockY1.setTypeId(20);
                            blockY2.setTypeId(20);
                            blockY3.setTypeId(20);
                            blockY4.setTypeId(20);
                            blockY5.setTypeId(20);
                            blockY6.setTypeId(20);
                           
                            infection.blocks.put(arena, blockY1);
                            infection.blocks.put(arena, blockY2);
                            infection.blocks.put(arena, blockY3);
                            infection.blocks.put(arena, blockY4);
                            infection.blocks.put(arena, blockY5);
                            infection.blocks.put(arena, blockY6);
                        } else {
                            Block blockY1 = world.getBlockAt(x_start, y_start - y, z_start - z);
                            Block blockY2 = world.getBlockAt(x_end, y_start - y, z_start - z);
                            Block blockY3 = world.getBlockAt(x_start + x, y_start - y, z_end);
                            Block blockY4 = world.getBlockAt(x_start + x, y_start, z_start - z);
                            Block blockY5 = world.getBlockAt(x_start + x, y_start - y, z_start);
                            Block blockY6 = world.getBlockAt(x_start + x, y_end, z_start - z);
                            blockY1.setTypeId(20);   
                            blockY2.setTypeId(20);
                            blockY3.setTypeId(20);
                            blockY4.setTypeId(20);
                            blockY5.setTypeId(20);
                            blockY6.setTypeId(20);
                           
                            infection.blocks.put(arena, blockY1);
                            infection.blocks.put(arena, blockY2);
                            infection.blocks.put(arena, blockY3);
                            infection.blocks.put(arena, blockY4);
                            infection.blocks.put(arena, blockY5);
                            infection.blocks.put(arena, blockY6);
                        }
                    }
                }
            }
        }
    I know it's a lot... it works 80% of the time, how can I get the other 20% back? :/
     
  2. Offline

    ZachBora

    What's infection and poz1 and 2?
     
  3. Offline

    killjoy64

    It's just a HashMap to put the blocks location into so I can use that later on when this gets done.

    Oh and poz1 is Position1, poz2 is Position2

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  4. Offline

    ZachBora

    I would have done the code differently and much simpler. In it's current state it seems overly complicated.

    What I would do is something like this
    Code:
    int minX;
    int maxX;
    int minZ;
    int maxZ;
    int minY;
    int maxY;
     
    if loc1.x < loc2.x
    {
    minX = loc1.x
    maxX = loc2.x
    }else{
    minX = loc2.x
    maxX = loc1.x
    }
    //same for Z and Y
     
    for(x = minX; x <= maxX; x++)
    {
      for(z = minZ; z <= maxZ; z++)
      {
        for(y = minY; y <= maxY; y++)
        {
        }
      }
    }
    }
     
  5. Offline

    desht

    Can be made even simpler with Math.min() and Math.max():
    PHP:
    int minX Math.min(loc1.xloc2.x);
    int maxX Math.max(loc1.xloc2.x);
    //... and for y & z
     
    nicholasntp and ZachBora like this.
  6. Offline

    killjoy64

    Wow.... I have been making this WAY more complicated than it looks! Thank you very much guys! :D
     
Thread Status:
Not open for further replies.

Share This Page