Skip to content

Instantly share code, notes, and snippets.

@John-Paul-R
Created January 9, 2022 19:18
Show Gist options
  • Save John-Paul-R/b63e5d74b7e37041107404190cdc7097 to your computer and use it in GitHub Desktop.
Save John-Paul-R/b63e5d74b7e37041107404190cdc7097 to your computer and use it in GitHub Desktop.
Get the "top" block at a given position using world.raycast.
public class TopCommand implements Command<ServerCommandSource> {
@Override
public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
ServerPlayerEntity player = source.getPlayer();
World world = source.getWorld();
Vec3d playerPos = player.getPos();
int new_y;
double new_x = playerPos.x;
double new_z = playerPos.z;
BlockHitResult blockHitResult = world.raycast(new RaycastContext(
new Vec3d(new_x, world.getHeight(), new_z),
new Vec3d(new_x, 1, new_z),
RaycastContext.ShapeType.COLLIDER,
RaycastContext.FluidHandling.SOURCE_ONLY,
player
));
new_y = blockHitResult.getBlockPos().getY() + 1;
return new_y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment