Search in sources :

Example 1 with CollisionContext

use of net.minecraft.world.phys.shapes.CollisionContext in project blueprint by team-abnormals.

the class BlockUtil method canPlace.

/**
 * Checks if a player can place a {@link BlockState} at a given {@link BlockPos} in a given {@link Level}.
 *
 * @param level  A {@link Level} to use for checking the state at the given pos.
 * @param player A {@link Player} to use for collision context, or null for no context.
 * @param pos    A {@link BlockPos} to check at.
 * @param state  A {@link BlockState} to check.
 * @return If a player can place a {@link BlockState} at a given {@link BlockPos} in a given {@link Level}.
 */
public static boolean canPlace(Level level, @Nullable Player player, BlockPos pos, BlockState state) {
    CollisionContext selectionContext = player == null ? CollisionContext.empty() : CollisionContext.of(player);
    VoxelShape voxelshape = state.getCollisionShape(level, pos, selectionContext);
    VoxelShape offsetShape = level.getBlockState(pos).getCollisionShape(level, pos);
    return (offsetShape.isEmpty() || level.getBlockState(pos).getMaterial().isReplaceable()) && state.canSurvive(level, pos) && level.isUnobstructed(null, voxelshape.move(pos.getX(), pos.getY(), pos.getZ()));
}
Also used : CollisionContext(net.minecraft.world.phys.shapes.CollisionContext) VoxelShape(net.minecraft.world.phys.shapes.VoxelShape)

Example 2 with CollisionContext

use of net.minecraft.world.phys.shapes.CollisionContext in project TerraFirmaCraft by TerraFirmaCraft.

the class BlockItemPlacement method canPlace.

protected boolean canPlace(BlockPlaceContext context, BlockState stateToPlace) {
    Player player = context.getPlayer();
    CollisionContext selectionContext = player == null ? CollisionContext.empty() : CollisionContext.of(player);
    return (stateToPlace.canSurvive(context.getLevel(), context.getClickedPos())) && context.getLevel().isUnobstructed(stateToPlace, context.getClickedPos(), selectionContext);
}
Also used : CollisionContext(net.minecraft.world.phys.shapes.CollisionContext) Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer)

Example 3 with CollisionContext

use of net.minecraft.world.phys.shapes.CollisionContext in project Create by Creators-of-Create.

the class BeltTunnelItem method canPlace.

@Override
protected boolean canPlace(BlockPlaceContext ctx, BlockState state) {
    Player playerentity = ctx.getPlayer();
    CollisionContext iselectioncontext = playerentity == null ? CollisionContext.empty() : CollisionContext.of(playerentity);
    Level world = ctx.getLevel();
    BlockPos pos = ctx.getClickedPos();
    return (!this.mustSurvive() || AllBlocks.ANDESITE_TUNNEL.get().isValidPositionForPlacement(state, world, pos)) && world.isUnobstructed(state, pos, iselectioncontext);
}
Also used : CollisionContext(net.minecraft.world.phys.shapes.CollisionContext) Player(net.minecraft.world.entity.player.Player) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos)

Aggregations

CollisionContext (net.minecraft.world.phys.shapes.CollisionContext)3 Player (net.minecraft.world.entity.player.Player)2 BlockPos (net.minecraft.core.BlockPos)1 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 Level (net.minecraft.world.level.Level)1 VoxelShape (net.minecraft.world.phys.shapes.VoxelShape)1