From dd3d8ea8bd94d2c28039c6ab59f9efb3fefde466 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Thu, 19 Dec 2024 19:09:28 +0100 Subject: [PATCH] Split up block accessor --- .../soulfiremc/jmh/PathfindingBenchmark.java | 10 +-- .../com/soulfiremc/test/PathfindingTest.java | 67 ++++++++++--------- ...sor.java => TestBlockAccessorBuilder.java} | 18 +++-- 3 files changed, 52 insertions(+), 43 deletions(-) rename server/src/test/java/com/soulfiremc/test/utils/{TestBlockAccessor.java => TestBlockAccessorBuilder.java} (73%) diff --git a/server/src/jmh/java/com/soulfiremc/jmh/PathfindingBenchmark.java b/server/src/jmh/java/com/soulfiremc/jmh/PathfindingBenchmark.java index ed26c700a..18d3e20ef 100644 --- a/server/src/jmh/java/com/soulfiremc/jmh/PathfindingBenchmark.java +++ b/server/src/jmh/java/com/soulfiremc/jmh/PathfindingBenchmark.java @@ -28,7 +28,7 @@ import com.soulfiremc.server.protocol.bot.state.TagsState; import com.soulfiremc.server.util.SFHelpers; import com.soulfiremc.server.util.structs.GsonInstance; -import com.soulfiremc.test.utils.TestBlockAccessor; +import com.soulfiremc.test.utils.TestBlockAccessorBuilder; import com.soulfiremc.test.utils.TestPathConstraint; import lombok.extern.slf4j.Slf4j; import net.kyori.adventure.key.Key; @@ -69,7 +69,7 @@ public void setup() { log.info("Parsing world data..."); var maxY = 0; - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); for (var x = 0; x < data.length; x++) { var xArray = data[x]; for (var y = 0; y < xArray.length; y++) { @@ -83,10 +83,12 @@ public void setup() { log.info("Calculating world data..."); + var builtAccessor = accessor.build(); + // Find the first safe block at 0 0 var safeY = 0; for (var y = maxY; y >= 0; y--) { - if (accessor.getBlockState(0, y, 0).blockType() != BlockType.AIR) { + if (builtAccessor.getBlockState(0, y, 0).blockType() != BlockType.AIR) { safeY = y + 1; break; } @@ -97,7 +99,7 @@ public void setup() { log.info("Initial state: {}", initialState.blockPosition().formatXYZ()); routeFinder = new RouteFinder(new MinecraftGraph(new TagsState(), - accessor, + builtAccessor, inventory, TestPathConstraint.INSTANCE), new PosGoal(100, 80, 100)); diff --git a/server/src/test/java/com/soulfiremc/test/PathfindingTest.java b/server/src/test/java/com/soulfiremc/test/PathfindingTest.java index 0af259cd3..16de10b3e 100644 --- a/server/src/test/java/com/soulfiremc/test/PathfindingTest.java +++ b/server/src/test/java/com/soulfiremc/test/PathfindingTest.java @@ -28,7 +28,7 @@ import com.soulfiremc.server.pathfinding.graph.ProjectedInventory; import com.soulfiremc.server.protocol.bot.container.SFItemStack; import com.soulfiremc.server.util.DefaultTagsState; -import com.soulfiremc.test.utils.TestBlockAccessor; +import com.soulfiremc.test.utils.TestBlockAccessorBuilder; import com.soulfiremc.test.utils.TestPathConstraint; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -42,14 +42,15 @@ public class PathfindingTest { @Test public void testPathfindingStraight() { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(1, 0, 0, BlockType.STONE); accessor.setBlockAt(2, 0, 0, BlockType.STONE); var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, inventory, + accessor.build(), + inventory, TestPathConstraint.INSTANCE), new PosGoal(2, 1, 0)); var initialState = NodeState.forInfo(new SFVec3i(0, 1, 0), inventory); @@ -61,7 +62,7 @@ public void testPathfindingStraight() { @Test public void testPathfindingImpossible() { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(1, 0, 0, BlockType.STONE); accessor.setBlockAt(2, 0, 0, BlockType.STONE); @@ -70,7 +71,7 @@ public void testPathfindingImpossible() { var routeFinder = new RouteFinder( new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), // This is impossible to reach @@ -84,14 +85,14 @@ public void testPathfindingImpossible() { @Test public void testPathfindingDiagonal() { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(1, 0, 1, BlockType.STONE); accessor.setBlockAt(2, 0, 2, BlockType.STONE); var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(2, 1, 2)); @@ -104,7 +105,7 @@ public void testPathfindingDiagonal() { @Test public void testPathfindingDiagonalImpossible() { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(1, 0, 1, BlockType.STONE); accessor.setBlockAt(2, 0, 2, BlockType.STONE); @@ -117,7 +118,7 @@ public void testPathfindingDiagonalImpossible() { var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(2, 1, 2)); @@ -130,13 +131,13 @@ public void testPathfindingDiagonalImpossible() { @ParameterizedTest @ValueSource(ints = {1, 2, 3}) public void testPathfindingJump(int height) { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(1, height, 0, BlockType.STONE); var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(1, height + 1, 0)); @@ -154,13 +155,13 @@ public void testPathfindingJump(int height) { @ParameterizedTest @ValueSource(ints = {1, 2, 3}) public void testPathfindingJumpDiagonal(int height) { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(1, height, 1, BlockType.STONE); var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(1, height + 1, 1)); @@ -178,13 +179,13 @@ public void testPathfindingJumpDiagonal(int height) { @ParameterizedTest @ValueSource(ints = {1, 2, 3, 4, 5}) public void testPathfindingFall(int height) { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(1, -height, 0, BlockType.STONE); var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(1, -height + 1, 0)); @@ -202,13 +203,13 @@ public void testPathfindingFall(int height) { @ParameterizedTest @ValueSource(ints = {1, 2, 3, 4, 5}) public void testPathfindingFallDiagonal(int height) { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(1, -height, 1, BlockType.STONE); var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(1, -height + 1, 1)); @@ -226,13 +227,13 @@ public void testPathfindingFallDiagonal(int height) { @ParameterizedTest @ValueSource(ints = {1, 2, 3, 4, 5}) public void testPathfindingGapJump(int gapLength) { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(gapLength + 1, 0, 0, BlockType.STONE); var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(gapLength + 1, 1, 0)); @@ -250,12 +251,12 @@ public void testPathfindingGapJump(int gapLength) { @Test public void testPathfindingUp() { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.STONE)), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(0, 2, 0)); @@ -268,12 +269,12 @@ public void testPathfindingUp() { @ParameterizedTest @ValueSource(ints = {15, 20, 25}) public void testPathfindingUpStacking(int amount) { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeWithAmount(ItemType.STONE, amount)), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(0, 21, 0)); @@ -290,13 +291,13 @@ public void testPathfindingUpStacking(int amount) { @Test public void testPathfindingDown() { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(0, -1, 0, BlockType.STONE); var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.DIAMOND_PICKAXE)), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(0, 0, 0)); @@ -308,7 +309,7 @@ public void testPathfindingDown() { @Test public void testPathfindingThroughWallToMoveUp() { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(1, 0, 0, BlockType.STONE); accessor.setBlockAt(1, 1, 0, BlockType.STONE); @@ -317,7 +318,7 @@ public void testPathfindingThroughWallToMoveUp() { var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.DIAMOND_PICKAXE)), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(2, 1, 0)); @@ -330,7 +331,7 @@ public void testPathfindingThroughWallToMoveUp() { @ParameterizedTest @ValueSource(booleans = {true, false}) public void testPathfindingMoveUpSideUnsafe(boolean unsafe) { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(0, 3, 0, BlockType.STONE); if (unsafe) { @@ -339,7 +340,7 @@ public void testPathfindingMoveUpSideUnsafe(boolean unsafe) { var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.DIAMOND_PICKAXE)), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(0, 2, 0)); @@ -357,7 +358,7 @@ public void testPathfindingMoveUpSideUnsafe(boolean unsafe) { @ParameterizedTest @ValueSource(booleans = {true, false}) public void testPathfindingDigSideUnsafe(boolean unsafe) { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(0, -1, 0, BlockType.STONE); if (unsafe) { @@ -366,7 +367,7 @@ public void testPathfindingDigSideUnsafe(boolean unsafe) { var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.DIAMOND_PICKAXE)), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(0, 0, 0)); @@ -384,7 +385,7 @@ public void testPathfindingDigSideUnsafe(boolean unsafe) { @ParameterizedTest @ValueSource(ints = {1, 2, 3, 4}) public void testPathfindingDigBelowUnsafe(int level) { - var accessor = new TestBlockAccessor(); + var accessor = new TestBlockAccessorBuilder(); accessor.setBlockAt(0, 0, 0, BlockType.STONE); accessor.setBlockAt(0, -1, 0, BlockType.LAVA); accessor.setBlockAt(0, -2, 0, BlockType.LAVA); @@ -395,7 +396,7 @@ public void testPathfindingDigBelowUnsafe(int level) { var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.DIAMOND_PICKAXE)), TestPathConstraint.INSTANCE); var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE, - accessor, + accessor.build(), inventory, TestPathConstraint.INSTANCE), new PosGoal(0, 0, 0)); diff --git a/server/src/test/java/com/soulfiremc/test/utils/TestBlockAccessor.java b/server/src/test/java/com/soulfiremc/test/utils/TestBlockAccessorBuilder.java similarity index 73% rename from server/src/test/java/com/soulfiremc/test/utils/TestBlockAccessor.java rename to server/src/test/java/com/soulfiremc/test/utils/TestBlockAccessorBuilder.java index a9671e6a7..ed8b20e15 100644 --- a/server/src/test/java/com/soulfiremc/test/utils/TestBlockAccessor.java +++ b/server/src/test/java/com/soulfiremc/test/utils/TestBlockAccessorBuilder.java @@ -24,15 +24,15 @@ import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; -public class TestBlockAccessor implements BlockAccessor { +public class TestBlockAccessorBuilder { private final Long2ObjectMap blocks = new Long2ObjectOpenHashMap<>(); private final BlockState defaultBlock; - public TestBlockAccessor() { + public TestBlockAccessorBuilder() { this(BlockState.forDefaultBlockType(BlockType.AIR)); } - public TestBlockAccessor(BlockState defaultBlock) { + public TestBlockAccessorBuilder(BlockState defaultBlock) { this.defaultBlock = defaultBlock; } @@ -40,8 +40,14 @@ public void setBlockAt(int x, int y, int z, BlockType block) { blocks.put(VectorHelper.asLong(x, y, z), BlockState.forDefaultBlockType(block)); } - @Override - public BlockState getBlockState(int x, int y, int z) { - return blocks.getOrDefault(VectorHelper.asLong(x, y, z), defaultBlock); + public BlockAccessor build() { + return new TestBlockAccessor(blocks, defaultBlock); + } + + private record TestBlockAccessor(Long2ObjectMap blocks, BlockState defaultBlock) implements BlockAccessor { + @Override + public BlockState getBlockState(int x, int y, int z) { + return blocks.getOrDefault(VectorHelper.asLong(x, y, z), defaultBlock); + } } }