1.12.2 Pausing an event listener

Discussion in 'Spigot Plugin Development' started by UnspecifiedUser, Aug 2, 2020 at 3:28 AM.

  1. Hello,

    I'm wondering if it's possible to pause an event listener for a given number of ticks.

    I want to use a listener on PlayerMoveEvent, but it would be called each single tick, and I think that all the checks I have to perform could slow the server, and there is no need to check on each tick.
  2. Hi,
    By checking if getFrom() and getTo() block is the same, you can reduce the amount of work you server has to do.

    Location from = event.getFrom();
    Location to = event.getTo();
    if(!to.getWorld().getBlockAt(to).equals(from.getWorld().getBlockAt(from))) // checks if you moved at least one block
    • Like Like x 1
  3. 3 possible solutions
    1. Make a custom runnable that runs whenever you want it to check what you want to check
    2. Use a runnable to register the listener and to un register it later, running on a loop
    3. Check if x, y and z from aren’t the same as to, if one of them is different it means that the player moved (so do not include yaw and pitch in your verification)
    • Like Like x 1
  4. xyz problem. what are you trying to do? sounds like theres a better way to handle it then the way youre thinking.
    also, none of the above helps make any sense, besides max's third "solution", though this still doesnt solve the question.
  5. Two things, an anti afk, and checking if players in a specific world have reached a checkpoint (for a dungeon).

    So checking if x, y, z aren't the same seems the better solution for anti afk. For the checkpoint, I think I would make a runnable that checks every 10 ticks maybe if the players (that are registered in a dungeon) are on a checkpoint. But I'm wondering if this kind of runnable is not consuming too much CPU, maybe I could use the anti afk listener for the checkpoints.

    Edit : for checkpoints, I will check if the players are near to the checkpoint block with a Location.distance(Location) so I think it can consume a lot of CPU
  6. the issue with using a regulated task to check if someone has completed a checkpoint is that its always possible for someone to accidentally miss the checkpoint due to server lag, latency, whatever. would having players interact with something not the best method? i understand its not as clean to force an interact but depending on your design you may run into sisues in the future.

    as for an anti afk, using the playermoveevent wont really help, as you need to know when the playermoveevent is NOT being called. i mean sure you could schedule a task on each tick for the playermoveevent that but that gets tedious and uses a lot of resources. youre better off using a task for this and seeing if their movement does not change over a period of time

    use Location#distanceSquared so that the Math#sqrt function isnt called, which is heavy. youd have to square the variable youre comparing against, but that takes no resources
    • Like Like x 1
  7. Good idea, thanks

    For the dungeons, I wanted to avoid forcing players to interact with something, but I can search a way that would not be annoying/not rp.

    Ok, I also have to listen to whether they killed mobs or not, so they are not kicked when they are just farming xp in a spawner