Skip to content

Commit c9109a4

Browse files
committed
make traverseBlocks use proper floor instead of force cast, use MutableBlockPos to prevent unnecessary allocation
1 parent e05c660 commit c9109a4

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/GeoScannerPeripheral.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939

4040
public class GeoScannerPeripheral extends BasePeripheral<IPeripheralOwner> {
4141

42-
/*
43-
Highly inspired by https://github.com/SquidDev-CC/plethora/ BlockScanner
44-
*/
42+
/**
43+
* Highly inspired by https://github.com/SquidDev-CC/plethora/ BlockScanner
44+
*/
4545

4646
public static final String PERIPHERAL_TYPE = "geo_scanner";
4747

@@ -64,7 +64,7 @@ public GeoScannerPeripheral(IPocketAccess pocket, IPocketUpgrade upgrade) {
6464

6565
private static List<Map<String, Object>> scan(List<Map<String, Object>> result, Level level, Vec3 center, int radius) {
6666
ScanUtils.relativeTraverseBlocks(level, center, radius, (state, pos) -> {
67-
HashMap<String, Object> data = new HashMap<>(6);
67+
HashMap<String, Object> data = new HashMap<>(6); // 5 normal fields + 1 "notOnShip" flag
6868
data.put("x", pos.x);
6969
data.put("y", pos.y);
7070
data.put("z", pos.z);

src/main/java/de/srendi/advancedperipherals/common/util/ScanUtils.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package de.srendi.advancedperipherals.common.util;
22

33
import net.minecraft.core.BlockPos;
4+
import net.minecraft.util.Mth;
45
import net.minecraft.world.level.Level;
56
import net.minecraft.world.level.block.state.BlockState;
67
import net.minecraft.world.phys.Vec3;
@@ -18,18 +19,23 @@ public static void traverseBlocks(Level world, Vec3 center, double radius, BiCon
1819

1920
public static void traverseBlocks(Level world, Vec3 center, double radius, BiConsumer<BlockState, Vec3> consumer, boolean relativePosition) {
2021
final double x = center.x, y = center.y, z = center.z;
21-
for (int oX = (int) (x - radius); oX <= (int) (x + radius); oX++) {
22-
for (int oY = (int) (y - radius); oY <= (int) (y + radius); oY++) {
23-
for (int oZ = (int) (z - radius); oZ <= (int) (z + radius); oZ++) {
24-
BlockPos subPos = new BlockPos(oX, oY, oZ);
25-
BlockState blockState = world.getBlockState(subPos);
26-
if (!blockState.isAir()) {
27-
if (relativePosition) {
28-
consumer.accept(blockState, new Vec3(oX + 0.5 - center.x, oY + 0.5 - center.y, oZ + 0.5 - center.z));
29-
} else {
30-
consumer.accept(blockState, new Vec3(oX, oY, oZ));
31-
}
22+
final int minX = Mth.floor(x - radius), maxX = Mth.floor(x + radius);
23+
final int minY = Mth.floor(y - radius), maxY = Mth.floor(y + radius);
24+
final int minZ = Mth.floor(z - radius), maxZ = Mth.floor(z + radius);
25+
final BlockPos.MutableBlockPos subPos = new BlockPos.MutableBlockPos();
26+
for (int oX = minX; oX <= maxX; oX++) {
27+
for (int oY = minY; oY <= maxY; oY++) {
28+
for (int oZ = minZ; oZ <= maxZ; oZ++) {
29+
BlockState blockState = world.getBlockState(subPos.set(oX, oY, oZ));
30+
if (blockState.isAir()) {
31+
continue;
3232
}
33+
consumer.accept(
34+
blockState,
35+
relativePosition
36+
? new Vec3(oX + 0.5 - x, oY + 0.5 - y, oZ + 0.5 - z)
37+
: new Vec3(oX, oY, oZ)
38+
);
3339
}
3440
}
3541
}

0 commit comments

Comments
 (0)