Add a filter block

Could it be possible to add a filter that will detect the first block
the BlockRay hit. The filter should detect if it hit the torch, snow
layer, slab, etc or if it hit the air of the block(Example: Around the
torch).

        Player player = event.getCause().first(Player.class).get();
        BlockRay optHit = BlockRay.from(player).filter(BlockRay.allFilter()).build();
        BlockState state = null;

        while(optHit.hasNext() && state == null){
            BlockRayHit ray = optHit.next();
            Vector3d lookPos = ray.getBlockPosition().toDouble();
            BlockState blockState = player.getWorld().getBlock(lookPos.toInt());

            state = !blockState.getType().equals(BlockTypes.AIR)?blockState:null;
        }

        System.out.println(state.getType());

https:https://forums-cdn.spongepowered.org/uploads/default/optimized/2X/2/2b995d83348c1d3736ad693f07b4abe4644fcda5_1_690x412.jpg

You can use the filter(Predicate)-Method to filter out block you don’t want. In your case you could make a list of blocks to ignore. For Example:

        Player player = event.getCause().first(Player.class).get();
        BlockRay optHit = BlockRay.from(player).filter(filterBlocks()).build();
        BlockRayHit hit = optHit.next();
        BlockState state = hit.getExtent().getBlock(hit.getBlockPosition());

private static final ImmutableList<BlockType> blocks = ImmutableList.<BlockType>builder()
            .add(BlockTypes.AIR)
            .add(BlockTypes.SNOW_LAYER)
            //...
            .build();
    
public static <E extends Extent> Predicate<BlockRayHit<E>> filterBlocks() {
        return input -> blocks.contains(input.getExtent().getBlockType(input.getBlockPosition()));
}
1 Like

How do you enable the display of the “minecraft:snow_layer” text? Sorry, irrelevant, but useful.

player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, TextStyles.BOLD, "minecraft:snow_layer"));
1 Like