# Items on the Ground

Discussion in 'Plugin Development' started by travja, Dec 14, 2012.

Not open for further replies.
1. Offline

### travja

Hey, wondering if you guys could help me out, I need to determine when the given item/entity hits the ground. I could cast it to a projectile or spawn in an egg and track that but I just want the item. So, I need to know when it hits the ground so I can do other things with it. I could probably check how close it is to the ground but that would require a lot of schedulers and work, if there is any easy way to see when it hits the ground that would be great. Thanks guys!

#1
2. Offline

How accurate do you need this to be?
EDIT: by accurate i mean how fast does it need to be detected?

#2
3. Offline

### travja

About the time that the item hits the ground, so basically, could I use the velocity of the item to calculate it or would I use something else. Basically, it's going to be a time bomb.

#3
4. Offline

Yes I would suggest running a scheduler constantly checking if the item's velocity is 0. If it is, then that means its on the ground

#4
5. Offline

### travja

OK, what if the item is sliding, would I check for a decrease in velocity? If it decreased, then it's on the ground?

#5
6. Offline

Hmm might be easier to get its location, then subtract 1 from the y, then get the block at that location and if it isn't air, then its on the ground?

#6
7. Offline

### travja

Maybe, but would it actually decrease in velocity when sliding? So I can use the subtract y, velocity, and Maybe cast to projectile... ClassCastException?

#7
8. Offline

### 4am

I'd launch a repeating sync task which iterates over a list of tracked entities and check if (Entity.getVelocity().getY() ==0) (if you looked for zero movement overall, then you'd have to wait if it slid across ice, for example), maybe every 5 ticks or so; but test it with shorter intervals (I'd say 2 is the lowest you can safely go). I'm not sure how expensive Entity.getVelocity().getY() in terms of CPU, but it's just a couple function calls, derefernces and fetching a double for a comparison, should be pretty quick.

When you find one that has stopped moving downwards, remove from the list and do whatever else it is you want to do. If it is moving down, continue to the next loop iteration immediately.

If you are finding that this triggers at the peak of an arc (zero-crossing as the item loses upward momentum due to gravity and begins to fall), add a check to see if it is above a solid block. Only do this check if the velocity check passes first (nest your if blocks, don't use an And condition). This might be a good safeguard but is probably a rare occurrence, and shouldn't add much overhead to have your code that much tighter.

Your event code can then add an item to the list whenever appropriate, and it will take care of itself via the task. Since the task is sync, no worries about threading issues (it's all in the main thread). As long as your code isn't causing slowdown by trying to do too much, this shouldn't make lag an issue unless you end up tracking lots and lots of entities. Setting off an explosion is probably not intense enough in and of itself, although of course many explosions at once might be.

I'd recommend stress testing it as hard as you can before moving it to production - you might have to introduce flood protection; break out of the loop after x iterations to prevent lag. The entities toward the end of the list which end up skipped will remain on the list until the entities being monitored hit the ground and are removed. This could delay detonation/fuse/whatever, but it will stop short of grinding the server down with it. Hopefully you can set the flood max relatively high, and it will be of little concern.

#8
9. Offline

I'd just like to compliment you on one of the best written replies/explanations I have ever seen

#9
chasechocolate and 4am like this.
10. Offline

### 4am

And I'd like to thank you all for rekindling an idea I had way back to make mushroom grenades! TO ECLIPSE

#10
11. Offline

TO NETBEANS

#11
12. Offline

### Dreeass

To lower the amount schedules you need to perform is by calculating the derivative of the way it is falling and calculate the time when it hits the ground.

#12