I created an item that i can use to check if the player can see the center of a specific block like this:
public static boolean lineOfSight(int maxRange, Entity player, BlockPos pos)
{
Vec3 vec3 = player.getEyePosition();
Vec3 vec31 = pos.getCenter();
if (vec31.distanceTo(vec3) > maxRange) { return false; }
else
{
BlockHitResult result = player.level().clip(new ClipContext(vec3, vec31, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, player));
return result.getBlockPos().getCenter().equals(pos.getCenter());
}
}
This works as far as i need it.
Now i wanted to create a block that can do the same. In this case: If the block can see the center of a specific block. I tried it in a similar way:
public static boolean lineOfSight(int maxRange, Level pLevel, Vec3 posFrom, BlockPos posTo)
{
Vec3 vec31 = posTo.getCenter();
if (vec31.distanceTo(posFrom) > maxRange) { return false; }
else
{
BlockHitResult result = pLevel.clip(new ClipContext(posFrom, vec31, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, null));
return result.getBlockPos().getCenter().equals(posTo.getCenter());
}
}
But obviously this does not work, because this method always returns false. The explanation is simple: The block only can see itself, because i raycast/clip from inside of my block.
Is there a simple way to use clip and skip this initial block where the raycast is coming from?