0

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

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

1 回答 1

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
}

这不会真正压缩代码,但可以节省您第二次执行大规模循环的时间。

于 2013-07-29T02:24:40.287 回答