Condense or optimize method

Discussion in 'Plugin Development' started by Milkywayz, May 18, 2013.

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

    Milkywayz

    The first three elements of the b array are the x,y,z of block 1.
    The last three elements of the b array are the x,y,z of block 2.

    This method gets the blocks between the two blocks. Using all three dimensions as to get all blocks such as how worldeditting works.

    This code works, but is a little bulky and I know it can be condensed. I do NOT want to make variables for the '(b[0] > b[3] ? b[3] : b[0]);' as that condenses nothing.

    The method is static because it's a utility method statically imported by another class.

    Code:
    public static void gen(SkullWall w, int[] b) {
            int a = 0;
            w.slots = new ArrayList<Slot>();
            for (int x = (b[0] > b[3] ? b[3] : b[0]); x <= (b[0] < b[3] ? b[3] : b[0]); x++) {
                for (int y = (b[1] > b[4] ? b[4] : b[1]); y <= (b[1] < b[4] ? b[4] : b[1]); y++) {
                    for (int z = (b[2] > b[5] ? b[5] : b[2]); z <= (b[2] < b[5] ? b[5] : b[2]); z++) {
                        w.slots.add(new Slot(w, w.getWorld().getBlockAt(x, y, z), a++));
                    }
                }
            }
        }
     
  2. Offline

    MrTwiggy

    Stuff like

    (b[0] > b[3] ? b[3] : b[0])
    can be written as
    Math.min(b[0], b[3])
     
    microgeek likes this.
  3. Offline

    Milkywayz

    MrTwiggy
    Thanks, definitely helped condense the code down.
     
Thread Status:
Not open for further replies.

Share This Page