1.16.5 PlayerQuitEvent does not occur after the player actually leaves

Discussion in 'Spigot Plugin Development' started by CaptainCapsize, Jun 8, 2021.

  1. I am trying to create a plugin that keeps track of the players logged in, and updates whenever someone joins or leaves. The leaving part will not work however, as, according to my console using some log outputs, the onQuit method using the PlayerQuitEvent gets executes right before the "player has left". Using Thread.sleep got me nowhere either as that just halts everything.
    How should I approach this?

    Code (Java):
        @EventHandler(priority = EventPriority.NORMAL)
        public void onQuit(PlayerQuitEvent event){
            getLogger().info("Player left, updating player list...");
            writePlayers(); //This uses Bukkit.getServer().getOnlinePlayers() to list all Players
  2. Just filter the leaving player out of your #getOnlinePlayers call
    • Agree Agree x 1
  3. You can use a bukkitrunnable to run the task like 5 ticks later (1/4 sec)
  4. Run an async task, it'll be executed after the player leaves most likely. Or you can delay it by 1+ ticks if it doesn't.
    • Optimistic Optimistic x 1
  5. Don't run an async task, you'll probably cause a concurrency issue with your own code doing that.

    Like others have said, you can run a (sync) task on the next tick, the server will have reflected the change by then.
    • Agree Agree x 1
  6. you don't need to run a task in this scenario like what @Scarsz said u can just filter out the player who left the server.

    smth like
    Code (Java):
    writePlayers(Player player) {
          List<UUID> uuid = Bukkit.getOnlinePlayers().stream().filter(p -> !p.equals(player)).map(Entity::getUniqueId).collect(Collectors.toList());
    Code (Java):
    // on quit
    #6 Lifeonblack, Jun 9, 2021
    Last edited: Jun 9, 2021
    • Useful Useful x 1
  7. You should bear in mind that a server processes a number of events when a player leaves the server.
    If you are delaying any task events, then any data may be 'null' because the player has left the server.

    Wrong -
    Quote 'using the PlayerQuitEvent gets executes right before the "player has left".'
    The process is triggered by the player disconnection.
    Any Client activity from that Player will be 'null'
    #7 Goldentoenail, Jun 9, 2021
    Last edited: Jun 9, 2021
  8. Thanks for the helpful advice everyone, I went with the filtering approach and it works flawlessly!
    • Like Like x 1