diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index db3fea8fb..a93054ad6 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -79,7 +79,7 @@ public class BlockPlacementListener { canPlaceBlock = false; // Spectators can't place blocks } else if (player.getGameMode() == GameMode.ADVENTURE) { //Check if the block can be placed on the block - canPlaceBlock = usedItem.meta().getCanPlaceOn().contains(interactedBlock); + canPlaceBlock = usedItem.meta().getCanPlaceOn().contains(Block.fromNamespaceId(interactedBlock.namespace())); } // Get the newly placed block position diff --git a/src/test/java/net/minestom/server/entity/player/PlayerBlockPlacementIntegrationTest.java b/src/test/java/net/minestom/server/entity/player/PlayerBlockPlacementIntegrationTest.java new file mode 100644 index 000000000..b63b4b8a9 --- /dev/null +++ b/src/test/java/net/minestom/server/entity/player/PlayerBlockPlacementIntegrationTest.java @@ -0,0 +1,57 @@ +package net.minestom.server.entity.player; + +import net.minestom.server.api.Env; +import net.minestom.server.api.EnvTest; +import net.minestom.server.coordinate.Pos; +import net.minestom.server.entity.GameMode; +import net.minestom.server.entity.Player; +import net.minestom.server.instance.block.Block; +import net.minestom.server.instance.block.BlockFace; +import net.minestom.server.item.ItemStack; +import net.minestom.server.item.Material; +import net.minestom.server.network.packet.client.play.ClientPlayerBlockPlacementPacket; +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; + +@EnvTest +public class PlayerBlockPlacementIntegrationTest { + + @ParameterizedTest + @MethodSource("placeBlockFromAdventureModeParams") + public void placeBlockFromAdventureMode(Block baseBlock, Block canPlaceOn, Env env) { + var instance = env.createFlatInstance(); + var connection = env.createConnection(); + var player = connection.connect(instance, new Pos(0, 42, 0)).join(); + + instance.setBlock(2, 41, 0, baseBlock); + + player.setGameMode(GameMode.ADVENTURE); + player.setItemInMainHand(ItemStack.builder(Material.WHITE_WOOL).meta(m -> m.canPlaceOn(canPlaceOn)).build()); + + var packet = new ClientPlayerBlockPlacementPacket( + Player.Hand.MAIN, new Pos(2, 41, 0), BlockFace.WEST, + 1f, 1f, 1f, + false + ); + player.addPacketToQueue(packet); + player.interpretPacketQueue(); + + var placedBlock = instance.getBlock(1, 41, 0); + + assertEquals("minecraft:white_wool", placedBlock.name()); + } + + private static Stream placeBlockFromAdventureModeParams() { + return Stream.of( + Arguments.of(Block.ACACIA_STAIRS.withProperty("facing", "south"), Block.ACACIA_STAIRS), + Arguments.of(Block.ACACIA_STAIRS, Block.ACACIA_STAIRS.withProperty("facing", "south")), + Arguments.of(Block.ACACIA_STAIRS.withProperty("facing", "south"), Block.ACACIA_STAIRS.withProperty("facing", "south")), + Arguments.of(Block.AMETHYST_BLOCK, Block.AMETHYST_BLOCK)); + } + +}