2013-07-29 2 views
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++; 
       } 
     } 
+0

여기에서 if-else가 작동하지 않는 이유는 무엇입니까? –

+0

카운트 var가 사용되는 방식으로 인해, 태스크 액션의 실행을 상쇄하는 방법으로 사용되므로, 가장 좋은 방법은 확실하지 않습니다. – clienthax

+0

@HarshalPandya 분명히 버그가 있기 때문에 비 고체 블록이 제대로 부착되지 않아 배치해야하는 시스템. – ObieMD5

답변

1

오히려 무시 된 블록으로 두 번째 루프 거래는 다음 방금 두 번째 루프를 작성해야합니다 때문에 그들을 두 번했다 지도/비 고체 블록의 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 
} 
를 만들

이것은 실제로 코드를 압축하지는 않지만 대규모 루프를 두 번 수행하지 않아도됩니다.

관련 문제