压缩这段代码?

时间:2013-07-29 01:36:35

标签: java bukkit

是否有可能以某种方式压缩这两个循环?我不得不加倍它们,因为第二个循环处理第一个循环忽略的块

        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++;
                }
        }

1 个答案:

答案 0 :(得分:1)

而不是第二个循环,你应该创建一个非实体块的map / arrayList

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

然后再次完成整个循环,只需循环地图/列表并创建它们

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

这不会真正压缩代码,但可以节省你第二次做大量循环的时间。