implements https://github.com/CanaryModTeam/CanaryLib/commit/1b18de6eb5e0d3a65a17e0a85410e12239784cd4