gpt4 book ai didi

java - 压缩这段代码?

转载 作者:行者123 更新时间:2023-12-01 14:18:17 25 4
gpt4 key购买 nike

是否可以以某种方式压缩这两个循环?我必须将它们加倍,因为第二个循环处理被第一个循环忽略的 block

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

最佳答案

然后执行第二个循环,您应该只创建非固体 block 的 map /数组列表

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

然后,不要再次执行整个循环,只需循环 map /列表并创建它们

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

这不会真正压缩代码,但可以让您不必再次执行大量循环。

关于java - 压缩这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17914813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com