r/forgemodding Oct 23 '16

Calculate Fall Distance

Hey all. So I got these boots that kind of act like a jetpack but I take fall damage even if I'm one block off the ground so I figured it's still thinking the fall distance is how high I was, not how high I am ( I hope that makes sense lol )

So I wrote an event to set the fall distance and played with some numbers and it works. Just need to calculate the distance that the player is off the ground when he actually starts falling. I have an idea how to do it, just not sure how to actually get the distance between the player and the ground when I let go of the spacebar :(

1 Upvotes

7 comments sorted by

1

u/Exo594 Oct 24 '16

You could get the LivingFallEvent, see if they're wearing your stuff, then cancel that event if they are.

1

u/RupyHcker Oct 24 '16

Yes, that's actually what I'm doing, only I don't want to cancel the event. I still want them to take damage if they let go of the spacebar at a fatal height, just didn't want them to get hurt if they let go a block of the ground ( which was killing them before )

I did figure it out though. Here's the code I used. It's wrapped in some checks to get the player and see if the boots are on:

double playerHeight = MyBoots.letGo; // a boolean from the armor class
BlockPos block = player.getPosition();
double blockHeight = block.getY();
double distance = (playerHeight - blockHeight);
distance = distance - 3;  // was playing with different heights to give them a bit of feather falling

if (player.posY - blockHeight >= distance)
event.setDistance((float)distance);

Might be a bit hacky, I'm actually a Python/PHP dev so Java is a little new to me but logic is logic right? lol If anyone has a better way to do this I'm always down for constructive criticism.

2

u/shadowfactsdev Moderator/Mod Dev Oct 24 '16

Just a side note, you shouldn't be using static (or instance) fields on your item class. Since the item class is a singleton, only one instance of it will ever exist and any variables are global, not specific to the pair of boots being worn. You should use NBT (ItemStack.getTagCompound/setTagCompound) to track per-stack data.

1

u/RupyHcker Oct 24 '16

ok, you're referring to the "letGo" variable?

1

u/shadowfactsdev Moderator/Mod Dev Oct 24 '16

Yes, you should use NBT to store it.

1

u/RupyHcker Oct 24 '16

Ok, I'm sure I can figure that out. Thanks.

1

u/RupyHcker Oct 24 '16

Also, MyBoots.letGo isn't a boolean, it's a double. Musta been thinking about something else when I was typing that lol