0

Is it possible to compact these two loops somehow?, ive had to double them because the second loop deals with the blocks that were ignored by the first loop

        int count = 1;
        for(int y = 0; y < cuboidClipboard.getHeight(); y++)
        for(int x = 0; x < cuboidClipboard.getWidth(); x++)
        for(int z = 0; z < cuboidClipboard.getLength(); z++)
        {
            BaseBlock baseBlock = cuboidClipboard.getPoint(new Vector(x, y, z));
            Vector relativeVector = new Vector(x,y,z).add(orign);

            Block buildBlock = world.getBlockAt(relativeVector.getBlockX(), relativeVector.getBlockY(), relativeVector.getBlockZ());

            if(Material.getMaterial(baseBlock.getId()).isSolid())
            if(buildBlock.getTypeId() != baseBlock.getId())
                {
                    new PopBlockTask(buildBlock, world, baseBlock).runTaskLater(this, 20+(count*2));
                    count++;
                }
        }

        //we need to place non solid blocks last because they don't attach properly when theres no blocks around them
        for(int y = 0; y < cuboidClipboard.getHeight(); y++)
        for(int x = 0; x < cuboidClipboard.getWidth(); x++)
        for(int z = 0; z < cuboidClipboard.getLength(); z++)
        {
            BaseBlock baseBlock = cuboidClipboard.getPoint(new Vector(x, y, z));
            Vector relativeVector = new Vector(x,y,z).add(orign);

            Block buildBlock = world.getBlockAt(relativeVector.getBlockX(), relativeVector.getBlockY(), relativeVector.getBlockZ());

            if(!Material.getMaterial(baseBlock.getId()).isSolid())  
            if(buildBlock.getTypeId() != baseBlock.getId())
                {
                    new PopBlockTask(buildBlock, world, baseBlock).runTaskLater(this, 20+(count*2));
                    count++;
                }
        }
clienthax
  • 1,211
  • 2
  • 8
  • 12
  • why wouldn't an if-else work here? – Harshal Pandya Jul 29 '13 at 01:40
  • Because of the way the count var is being used, its used as a way to offset the execution of the task action, im just not sure on whats the best way to go about it – clienthax Jul 29 '13 at 01:43
  • @HarshalPandya because there is apparently a bug in the system that requires the non solid blocks to be placed after because they do not attach properly. – ObieMD5 Jul 29 '13 at 01:43
  • Could you reset the loop counter when the if evaluates to true so it gets run again on the problematic blocks? – Magikhead Jul 29 '13 at 02:13

1 Answers1

1

Rather then do the second loop you should just create a map/arrayList of the non-solid blocks

if(!Material.getMaterial(baseBlock.getId()).isSolid())  
    // Do the code that's there
else
    // Add to map/list the information you need (x, y, z, count?)
    // If you don't have some way to store the info, you could 
    // just create a small Object to do so or use a Map

Then rather that do the entire loop again, just loop the map/list and create them

for(Object o: theList)
{
    // Do the relevant code
}

This won't really compact the code, but saves you having to do the massive loop a second time.

Java Devil
  • 10,629
  • 7
  • 33
  • 48