Solved Will this work? (Creating a 3d rectangle with two points)

Discussion in 'Plugin Development' started by meguy26, May 27, 2015.

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

    meguy26

    I have some code that I cannot test right now, can someone tell me if it will work? It is supposed to create a rectangle like worldedit between two points:
    Code:
        public WorldEditor drawRectangle(Location corner, Location corner2, Material mat){
            this.checkLoc(corner, corner2);
            int maxX;
            int maxZ;
            int maxY;
           
            int minX;
            int minZ;
            int minY;
           
            if(corner.getBlockX() > corner2.getBlockX()){
                maxX = corner.getBlockX();
                minX = corner2.getBlockX();
            } else {
                maxX = corner2.getBlockX();
                minX = corner.getBlockX();
            }
           
            if(corner.getBlockZ() > corner2.getBlockZ()){
                maxZ = corner.getBlockZ();
                minZ = corner2.getBlockZ();
            } else {
                maxZ = corner2.getBlockZ();
                minZ = corner.getBlockZ();
            }
           
            if(corner.getBlockY() > corner2.getBlockY()){
                maxY = corner.getBlockY();
                minY = corner2.getBlockY();
            } else {
                maxY = corner2.getBlockY();
                minY = corner.getBlockY();
            }
           
            for(int x = minX; x < maxX; x++){
                for(int z = minZ; z < maxZ; z++){
                    for(int y = minY; y < maxY; y++){
                        new Location(this.world, x, y, x).getBlock().setType(mat);
                    }
                }
            }
           
            return this;
        }
     
  2. Firstly, I think it's meant to be '<=' not '<' for the nested for loops. Reason for this is, if the two location's block Y is equal to each other, then this won't set the block in that Y location only as 'MinY == MaxY' not 'MinY < MaxY'.

    Secondly, why are you making a new Location every time when you can just do: this.world.getBlockAt(x, y, z).setType(mat);

    Thirdly, why do those 'if' statements when you can just do:
    Code:
    maxX = Math.max(corner.getBlockX(), corner2.getBlockX());
    maxY = Math.max(corner.getBlockY(), corner2.getBlockY());
    maxZ = Math.max(corner.getBlockZ(), corner2.getBlockZ());
    minX = Math.min(corner.getBlockX(), corner2.getBlockZ());
    minY = Math.min(corner.getBlockY(), corner2.getBlockY());
    minZ = Math.min(corner.getBlockZ(), corner2.getBlockZ());
    
     
    Last edited: May 27, 2015
  3. Offline

    meguy26

    @KingFaris11
    1. Yes you are right, it should be.
    2. Because I forgot about getBlockAt...
    3. because I did not know that was a thing....

    Thank You!


    EDIT:
    Again thank you a lot, the code looks a lot cleaner now:
    Code:
        public WorldEditor drawRectangle(Location corner, Location corner2, Material mat){
            this.checkLoc(corner, corner2);
            int maxX = Math.max(corner.getBlockX(), corner2.getBlockX());
            int maxZ = Math.max(corner.getBlockZ(), corner2.getBlockZ());
            int maxY = Math.max(corner.getBlockY(), corner2.getBlockY());
           
            int minX = Math.min(corner.getBlockX(), corner2.getBlockX());
            int minZ = Math.min(corner.getBlockZ(), corner2.getBlockZ());
            int minY = Math.min(corner.getBlockY(), corner2.getBlockY());
           
            for(int x = minX; x <= maxX; x++){
                for(int z = minZ; z <= maxZ; z++){
                    for(int y = minY; y <= maxY; y++){
                        this.world.getBlockAt(x, y, z).setType(mat);
                    }
                }
            }
           
            return this;
        }
     
    Last edited: May 27, 2015
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page