0

これらの 2 つのループをどうにかして圧縮することは可能ですか? 2 番目のループは最初のループで無視されたブロックを処理するため、それらを 2 倍にする必要がありました。

        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

2 番目のループを実行する代わりに、非ソリッド ブロックのマップ/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
}

これは実際にはコードを圧縮しませんが、大規模なループを 2 回行う必要がなくなります。

于 2013-07-29T02:24:40.287 に答える