Fix #1009: Make canPlaceOn and canDestroy use Material instead of Block (#1010)

This commit is contained in:
Cody 2022-05-01 03:44:33 -05:00 committed by GitHub
parent 8a876a4a59
commit ef53559349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 1 deletions

View File

@ -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

View File

@ -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<Arguments> 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));
}
}