Alternatives to PlayerMoveEvent

Discussion in 'Spigot Plugin Development' started by Ukrainian_Reaper, Aug 3, 2015.

  1. Hey guys! It seems that every time I come across the need to utilize the PlayerMoveEvent I get cautionary. The event itself sounds very intensive on a server if the method body contains a-lot of code(loops, new instantiated objects, etc). It sounds kind of redundant to use the event when most of the time I only care about when the players location changes and not their orientation and direction they're looking. I feel like using it would put unnecessary weight onto servers.

    Does anyone know of any alternative methods for detecting player location changes other than just schedulers constantly checking if the players old location isn't the same as their new one or the above mentioned EventListener?
     
  2. You can easily check if the player moved instead of looked around in a PlayerMoveEvent.
     
    • Agree Agree x 1
  3. But then we would still have to call the PlayerMoveEvent. It would be called literally thousands of times per second if a player if running or PvPing for example.
     
  4. Code (Text):
    if(event.getTo().distance(event.getFrom()) == 0) return;
     
    • Agree Agree x 2
  5. Thats not what I was asking, sorry if it wasn't clear. I've used the event before and know how to read an API. The event unlike other events is called far more often, thousands of times per second if a player is running, as opposed to for example, InventoryInteract, which would be called on the rare occasion of an inventory being manipulated. It just seems redundant.
     
  6. Only execute your code if the player moves to a new block instead of executing it every time a player moves.
     
    • Agree Agree x 1
  7. Why would they make an event if it's super inefficient? PlayerMoveEvent is fine, what you do inside of it, causes the lag, etc.
     
    • Agree Agree x 3
  8. Use distanceSquared here, it's more performant than using the distance method.
     
    • Agree Agree x 3
    • Winner Winner x 1
  9. Just check pro block and it's fine.
     
  10. If you're really concerned about the lagg caused from the event, you could make a new task which runs every half a second or so, and execute your code inside of there, you can loop through each player and do your code. I believe it would be less laggy that way.
     
    • Like Like x 2
  11. This is what I was considering after reading some of the replies. Thanks!
     
  12. That's totally right. I suggest you doing this in your every plugin to avoid player move event usage, that way you could also make a config option for the move detection speed: i.e. set to 5 ticks (0.25 second) by default, and if the plugin seems to cause too much lag, the server owners will be able to set it to a higher tick value.
     
  13. I've had to edit some existing plugins recently, one of them was utilizing the move event manner for some kind of mysql logging and using a similar check posted above. Was causing masses of tps and other issues, but this was a combined issue with the mass of mysql data being checked and inserted.

    I'd stick away from it, regardless of what others are saying; the alternatives are created dependent on your needs.
     
  14. Has it made MySql connections on player move events in the main server thread?
    That's the worst thing, that can be even made.
    You should always use an async thread for these mysql operations, because they can cause very big lags, i.e. 5-10ms lag per request.
     
  15. Yup, one of the most awkward things I've de compiled in my life. Requesting every movement, every join, every leave; oh yeah and every damage, even worse he was using asyncchat for commands.
     
  16. Which one is this plugin, please send me a link or the plugin, I would like to see, how many crazy staff does its author made :)
     
  17. It's actually hilarious b/c he made a post on this thread and i didn't even realize till now...

    Jesus Christ.