I wanted to ask if anyone knows how to check the number of online players when someone leaves the server. When joining I know how to count them, but when leavening, I don't know how to take them off.
When joining, it's like this for me: The player [PLAYER NAME] has entered the server [1/20]
event.setJoinMessage ("The player" + event.getPlayer (). GetName () + "has entered the server." + "[" + Bukkit.getOnlinePlayers (). Size () + "/" + Bukkit.getMaxPlayers () + "]");
But if there are two people on the server and one of them leaves, the number of players in brackets remains on [2/20] even though only one person is on the server.
event.setQuitMessage ("The player" + event.getPlayer (). GetName () + "has left the server." + "[" + Bukkit.getOnlinePlayers (). Size () + "/" + Bukkit.getMaxPlayers () + "]");
Does anyone know an answer how I deduct the players when leavening? I already say thank you for the answer. And I hope I was able to explain my problem well.
The player leaving the player will only be removed from the list of online players (Bukkit.getOnlinePlayers ()) after the PlayerQuitEvent has ended.
In order to output the new number at the PlayerQuitEvent, you simply have to manually subtract the leaving player.
Bukkit.getOnlinePlayers (). Size () -1
So then:
event.setQuitMessage ("The player" + event.getPlayer (). GetName () + "has left the server." + "[" + Bukkit.getOnlinePlayers (). Size () -1 + "/" + Bukkit.getMaxPlayers () + "]");
Thanks.
You're welcome.
I tried it, but then I got the error: The operator - is undefined for the argument type (s) string, int and if I put a comma to the -1 then comes: The method setQuitMessage (String) in the type PlayerQuitEvent is not applicable for the arguments (String, String) do you know what I can do about it?
Put a bracket around the Bukkit.getOnlinePlayers (). Size () -1.
Then Java should understand that you want to subtract 1 from the number of players - and then insert the result into the string.
… + (Bukkit.getOnlinePlayers (). Size () -1) +…
So:
event.setQuitMessage ("The player" + event.getPlayer (). GetName () + "has left the server." + "[" + (Bukkit.getOnlinePlayers (). Size () -1) + "/" + Bukkit .getMaxPlayers () + "]");
Does this work?
Yes it works, thank you very much.