I am trying to hollow out a sphere, I already achieved this however it is incredibly slow (A few seconds for a sphere with a radius of 5, a few minutes for a sphere with a radius of 100)
This is my code:
BlockPos pos = player.getBlockPos();
int startX = pos.getX();
int startY = pos.getY();
int startZ = pos.getZ();
int squaredRadius = radius * radius;
int x1;
int y1;
int z1;
int flags = 2 | 8 | 16 | 32;
BlockPos.Mutable blockPos = new BlockPos.Mutable(0, 0, 0);
BlockState state = Blocks.AIR.getDefaultState();
for (int x = startX - radius; x < startX + radius; x++) {
for (int y = Math.max(0, startY - radius), maxY2 = Math.min(255, startY + radius); y < maxY2; y++) {
for (int z = startZ - radius; z < startZ + radius; z++) {
x1 = x - startX;
y1 = y - startY;
z1 = z - startZ;
if (x1 * x1 + y1 * y1 + z1 * z1 <= squaredRadius)
world.setBlockState(blockPos.set(x, y, z), state, flags);
}
}
}
How could I speed this up? (Or is it already as fast as it can be?)
EDIT: For a 100 radius it takes roughly 8 minutes on my machine