One time I was scripting something in Lua for a Figura model (basically a custom player model you can use with a Minecraft mod) I was making for a friend. His character was really big and heavy, so I wanted to give him something where if he fell far enough to take fall damage, he would make a loud THUD sound.
There was just one problem: Figura doesn't natively have a way to detect whether or not you're taking fall damage, so I would have to track it based on the fall itself.
At first, I tried to do this by measuring how much time since the player started descending. This kinda sucked ass because the player could descend at any speed and as long as they reached the ground after enough time to set off the sound, it would go off, even if they didn't actually fall for long enough to take damage.
Then, I tried to base it on the velocity on impact, setting it to the exact impact velocity of falling exactly four blocks. This worked just fine… as long as you were falling exactly four blocks. Any other fall distance and it wouldn't recognize your velocity as being high enough to actually trigger the sound. Even jumping off the fourth block would fuck up the velocity.
Lastly, I realized that the velocity could be used to find the fall distance via an integral. To set up the integral, I put the velocity (measured in blocks per tick) into a variable that would steadily increment every tick, which gave the exact fall distance every time. If this variable was more than 4 on impact, it would trigger a sound. Finally, it worked every time, no matter how far you fell or how fast you were going on impact.
But there was a new problem now: it would even trigger in situations where you wouldn't normally have taken fall damage, such as being underwater or falling onto a slime block. I had to set special edge cases for these types of things, and at this point my friend was like "You should probably just give up tbh, it's not that big a deal"
Eventually, I got it to the point where most of the edge cases were out. But then my friend sent me one last bug, which we both agreed was easily the funniest one of them all.
If you paused the game after hitting the ground… This would happen. (VOLUME WARNING!)
#minecraft #figura #modded-minecraft #programming #pain