From cf4a2d134e3f4c839df872297485ac6de9685ea8 Mon Sep 17 00:00:00 2001 From: mworzala Date: Sat, 27 Jan 2024 12:09:41 -0500 Subject: [PATCH] feat: expose isFaceFull for testing if a block shape has a full face --- .../net/minestom/server/collision/Shape.java | 8 +++++ .../minestom/server/collision/ShapeImpl.java | 5 +++ .../minestom/server/collision/TestShape.java | 32 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/test/java/net/minestom/server/collision/TestShape.java diff --git a/src/main/java/net/minestom/server/collision/Shape.java b/src/main/java/net/minestom/server/collision/Shape.java index 3b2db3d17..232b637d4 100644 --- a/src/main/java/net/minestom/server/collision/Shape.java +++ b/src/main/java/net/minestom/server/collision/Shape.java @@ -9,6 +9,14 @@ import org.jetbrains.annotations.NotNull; public interface Shape { boolean isOccluded(@NotNull Shape shape, @NotNull BlockFace face); + /** + * Returns true if the given block face is completely covered by this shape, false otherwise. + * @param face The face to test + */ + default boolean isFaceFull(@NotNull BlockFace face) { + return false; + } + /** * Checks if two bounding boxes intersect. * diff --git a/src/main/java/net/minestom/server/collision/ShapeImpl.java b/src/main/java/net/minestom/server/collision/ShapeImpl.java index c01aa2932..801dd7f74 100644 --- a/src/main/java/net/minestom/server/collision/ShapeImpl.java +++ b/src/main/java/net/minestom/server/collision/ShapeImpl.java @@ -150,6 +150,11 @@ public final class ShapeImpl implements Shape { return isFaceCovered(allRectangles) == 2; } + @Override + public boolean isFaceFull(@NotNull BlockFace face) { + return (((blockOcclusion >> face.ordinal()) & 1) == 1); + } + @Override public boolean intersectBox(@NotNull Point position, @NotNull BoundingBox boundingBox) { for (BoundingBox blockSection : collisionBoundingBoxes) { diff --git a/src/test/java/net/minestom/server/collision/TestShape.java b/src/test/java/net/minestom/server/collision/TestShape.java new file mode 100644 index 000000000..080d0d0e7 --- /dev/null +++ b/src/test/java/net/minestom/server/collision/TestShape.java @@ -0,0 +1,32 @@ +package net.minestom.server.collision; + +import net.minestom.server.instance.block.Block; +import net.minestom.server.instance.block.BlockFace; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestShape { + + private static @NotNull Stream isFullFaceCases() { + return Stream.of( + Arguments.of(Block.STONE, BlockFace.BOTTOM, true), + Arguments.of(Block.ENCHANTING_TABLE, BlockFace.BOTTOM, true), + Arguments.of(Block.ENCHANTING_TABLE, BlockFace.TOP, false), + Arguments.of(Block.ENCHANTING_TABLE, BlockFace.NORTH, false), + Arguments.of(Block.ACACIA_FENCE, BlockFace.TOP, false), + Arguments.of(Block.IRON_BARS, BlockFace.TOP, false) + ); + } + + @ParameterizedTest + @MethodSource("isFullFaceCases") + void isFullFace(@NotNull Block block, @NotNull BlockFace face, boolean isFullFace) { + assertEquals(block.registry().collisionShape().isFaceFull(face), isFullFace); + } +}