EssentialsX

EssentialsX

2M Downloads

Custom quit message shows outdated online player count

marcel2215 opened this issue ยท 1 comments

commented

Type of bug

Other unexpected behaviour

/ess dump all output

n/a

Error log (if applicable)

No response

Bug description

When a player quits, the {ONLINE} placeholder in custom-quit-message is replaced with the number of players before leaving, so it's always +1 more than it should be. This is probably due to PlayerQuit event being fired before the player is disconnected.

To fix this issue, we need to simply subtract 1 from ess.getOnlinePlayers().size() in the onPlayerQuit event handler.

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerQuit(final PlayerQuitEvent event) {
final User user = ess.getUser(event.getPlayer());
final Integer pendingId = pendingMotdTasks.remove(user.getUUID());
if (pendingId != null) {
ess.getScheduler().cancelTask(pendingId);
}
if (hideJoinQuitMessages() || (ess.getSettings().allowSilentJoinQuit() && user.isAuthorized("essentials.silentquit"))) {
event.setQuitMessage(null);
} else if (ess.getSettings().isCustomQuitMessage() && event.getQuitMessage() != null) {
final Player player = event.getPlayer();
final String msg = ess.getSettings().getCustomQuitMessage()
.replace("{PLAYER}", player.getDisplayName())
.replace("{USERNAME}", player.getName())
.replace("{ONLINE}", NumberFormat.getInstance().format(ess.getOnlinePlayers().size()))
.replace("{UPTIME}", DateUtil.formatDateDiff(ManagementFactory.getRuntimeMXBean().getStartTime()))
.replace("{PREFIX}", FormatUtil.replaceFormat(ess.getPermissionsHandler().getPrefix(player)))
.replace("{SUFFIX}", FormatUtil.replaceFormat(ess.getPermissionsHandler().getSuffix(player)));
event.setQuitMessage(msg.isEmpty() ? null : msg);
}
user.startTransaction();
if (ess.getSettings().removeGodOnDisconnect() && user.isGodModeEnabled()) {
user.setGodModeEnabled(false);
}
if (user.isVanished()) {
user.setLeavingHidden(true);
user.setVanished(false);
}
user.setLogoutLocation();
if (user.isRecipeSee()) {
user.getBase().getOpenInventory().getTopInventory().clear();
}
final ArrayList<HumanEntity> viewers = new ArrayList<>(user.getBase().getInventory().getViewers());
for (final HumanEntity viewer : viewers) {
if (viewer instanceof Player) {
final User uviewer = ess.getUser((Player) viewer);
if (uviewer.isInvSee()) {
uviewer.getBase().closeInventory();
}
}
}
user.updateActivity(false, AfkStatusChangeEvent.Cause.QUIT);
if (!user.isHidden()) {
user.setLastLogout(System.currentTimeMillis());
}
user.stopTransaction();
user.dispose();
}

.replace("{ONLINE}", NumberFormat.getInstance().format(ess.getOnlinePlayers().size()))

Steps to reproduce

  1. Put {ONLINE} placeholder in your custom-quit-message. For example:
custom-quit-message: "&c&l- &4{USERNAME} &cleft the game &8(&7{ONLINE} online&8)"
  1. Join and then leave the server.
  2. Notice that the number is 1 instead of 0 despite no one being online.

Expected behaviour

The {ONLINE} placeholder should be replaced with the updated number of players after someone leaves.

Actual behaviour

The {ONLINE} placeholder is replaced with the outdated number of players before someone left.

commented
custom-join-message: "&a&l+ &2{USERNAME} &ajoined the game &8(&7{ONLINE} online&8)"
custom-quit-message: "&c&l- &4{USERNAME} &cleft the game &8(&7{ONLINE} online&8)"

image