diff --git a/src/main/java/baritone/chunk/CachedWorld.java b/src/main/java/baritone/chunk/CachedWorld.java index 3abcb8b3..20d58a38 100644 --- a/src/main/java/baritone/chunk/CachedWorld.java +++ b/src/main/java/baritone/chunk/CachedWorld.java @@ -85,13 +85,32 @@ public final class CachedWorld implements IBlockTypeAccess { return region.getBlock(x & 511, y, z & 511); } - public final LinkedList getLocationsOf(String block) { + public final LinkedList getLocationsOf(String block, int minimum, int maxRegionDistanceSq) { LinkedList res = new LinkedList<>(); - this.cachedRegions.values().forEach(region -> { - if (region != null) - for (BlockPos pos : region.getLocationsOf(block)) - res.add(pos); - }); + int playerRegionX = playerFeet().getX() >> 9; + int playerRegionZ = playerFeet().getZ() >> 9; + + int searchRadius = 0; + while (searchRadius <= maxRegionDistanceSq) { + for (int xoff = -searchRadius; xoff <= searchRadius; xoff++) { + for (int zoff = -searchRadius; zoff <= searchRadius; zoff++) { + double distance = xoff * xoff + zoff * zoff; + if (distance != searchRadius) { + continue; + } + int regionX = xoff + playerRegionX; + int regionZ = zoff + playerRegionZ; + CachedRegion region = getOrCreateRegion(regionX, regionZ); + if (region != null) + for (BlockPos pos : region.getLocationsOf(block)) + res.add(pos); + } + } + if (res.size() >= minimum) { + return res; + } + searchRadius++; + } return res; } @@ -139,7 +158,7 @@ public final class CachedWorld implements IBlockTypeAccess { * @param regionZ The region Z coordinate * @return The region located at the specified coordinates */ - private CachedRegion getOrCreateRegion(int regionX, int regionZ) { + private synchronized CachedRegion getOrCreateRegion(int regionX, int regionZ) { return cachedRegions.computeIfAbsent(getRegionID(regionX, regionZ), id -> { CachedRegion newRegion = new CachedRegion(regionX, regionZ); newRegion.load(this.directory); diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 78f418ea..bf4a412f 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -120,7 +120,7 @@ public class ExampleBaritoneControl extends Behavior { } if (msg.toLowerCase().startsWith("find")) { String blockType = msg.toLowerCase().substring(4).trim(); - LinkedList locs = WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(blockType); + LinkedList locs = WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(blockType, 1, 4); displayChatMessageRaw("Have " + locs.size() + " locations"); for (BlockPos pos : locs) { Block actually = BlockStateInterface.get(pos).getBlock(); @@ -133,7 +133,7 @@ public class ExampleBaritoneControl extends Behavior { } if (msg.toLowerCase().startsWith("mine")) { String blockType = msg.toLowerCase().substring(4).trim(); - List locs = new ArrayList<>(WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(blockType)); + List locs = new ArrayList<>(WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(1, 1)); if (locs.isEmpty()) { displayChatMessageRaw("No locations known"); event.cancel(); @@ -148,9 +148,9 @@ public class ExampleBaritoneControl extends Behavior { .filter(pos -> !ChunkPacker.blockToString(BlockStateInterface.get(pos).getBlock()).equalsIgnoreCase(blockType)) .collect(Collectors.toList())); - if (locs.size() > 10) { - displayChatMessageRaw("Pathing to any of closest 10"); - locs = locs.subList(0, 10); + if (locs.size() > 30) { + displayChatMessageRaw("Pathing to any of closest 30"); + locs = locs.subList(0, 30); } PathingBehavior.INSTANCE.setGoal(new GoalComposite(locs.stream().map(GoalTwoBlocks::new).toArray(Goal[]::new))); PathingBehavior.INSTANCE.path();