Skip to content

Commit 24b7285

Browse files
hudsonandresJRoy
andauthored
fix: reset nickname when player changes Minecraft account name (#6470)
Closes #6469 --------- Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
1 parent 5c45ee2 commit 24b7285

4 files changed

Lines changed: 26 additions & 4 deletions

File tree

Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,19 @@ private void joinFlow(final User user, final long currentTime, final String mess
420420

421421
final String lastAccountName = user.getLastAccountName(); // For comparison
422422
user.setLastAccountName(user.getBase().getName());
423+
424+
final boolean newUsername = lastAccountName != null && !lastAccountName.equals(user.getBase().getName());
425+
426+
// If the Minecraft account name changed, reset the nickname so the old one doesn't persist
427+
if (ess.getSettings().isResetNickOnNameChange() && newUsername && user.getNickname() != null) {
428+
user.setNickname(null);
429+
}
430+
423431
user.setLastLogin(currentTime);
424432
user.setDisplayNick();
425433
updateCompass(user);
426434
user.setLeavingHidden(false);
427435

428-
// Check for new username. If they don't want the message, let's just say it's false.
429-
final boolean newUsername = ess.getSettings().isCustomNewUsernameMessage() && lastAccountName != null && !lastAccountName.equals(user.getBase().getName());
430-
431436
if (!ess.getVanishedPlayersNew().isEmpty() && !user.isAuthorized("essentials.vanish.see")) {
432437
for (final String p : ess.getVanishedPlayersNew()) {
433438
final Player toVanish = ess.getServer().getPlayerExact(p);
@@ -453,7 +458,7 @@ private void joinFlow(final User user, final long currentTime, final String mess
453458
} else if (message == null || hideJoinQuitMessages()) {
454459
effectiveMessage = null;
455460
} else if (ess.getSettings().isCustomJoinMessage()) {
456-
final String msg = (newUsername ? ess.getSettings().getCustomNewUsernameMessage() : ess.getSettings().getCustomJoinMessage())
461+
final String msg = (newUsername && ess.getSettings().isCustomNewUsernameMessage() ? ess.getSettings().getCustomNewUsernameMessage() : ess.getSettings().getCustomJoinMessage())
457462
.replace("{PLAYER}", user.getDisplayName()).replace("{USERNAME}", user.getName())
458463
.replace("{UNIQUE}", NumberFormat.getInstance().format(ess.getUsers().getUserCount()))
459464
.replace("{ONLINE}", NumberFormat.getInstance().format(ess.getOnlinePlayers().size()))

Essentials/src/main/java/com/earth2me/essentials/ISettings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public interface ISettings extends IConf {
9494

9595
String getNicknamePrefix();
9696

97+
boolean isResetNickOnNameChange();
98+
9799
String getOperatorColor() throws Exception;
98100

99101
boolean getPerWarpPermission();

Essentials/src/main/java/com/earth2me/essentials/Settings.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public class Settings implements net.ess3.api.ISettings {
147147
private boolean logCommandBlockCommands;
148148
private boolean logConsoleCommands;
149149
private Set<Predicate<String>> nickBlacklist;
150+
private boolean resetNickOnNameChange;
150151
private double maxProjectileSpeed;
151152
private boolean removeEffectsOnHeal;
152153
private Map<String, String> worldAliases;
@@ -508,11 +509,20 @@ private String _getNicknamePrefix() {
508509
return config.getString("nickname-prefix", "~");
509510
}
510511

512+
private boolean _resetNickOnNameChange() {
513+
return config.getBoolean("reset-nick-on-name-change", false);
514+
}
515+
511516
@Override
512517
public String getNicknamePrefix() {
513518
return nicknamePrefix;
514519
}
515520

521+
@Override
522+
public boolean isResetNickOnNameChange() {
523+
return resetNickOnNameChange;
524+
}
525+
516526
@Override
517527
public double getTeleportCooldown() {
518528
return config.getDouble("teleport-cooldown", 0);
@@ -886,6 +896,7 @@ public void reloadConfig() {
886896
}
887897

888898
nicknamePrefix = _getNicknamePrefix();
899+
resetNickOnNameChange = _resetNickOnNameChange();
889900
operatorColor = _getOperatorColor();
890901
changePlayerListName = _changePlayerListName();
891902
configDebug = _isDebug();

Essentials/src/main/resources/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ nick-blacklist:
5151
# For example, if "&6Notch" has 7 characters (2 are part of a color code), a length of 5 is used when this option is set to true.
5252
ignore-colors-in-max-nick-length: false
5353

54+
# When this option is enabled, any nickname set by a player will be reset when their Minecraft account name changes.
55+
# This prevents old nicknames (including RGB-formatted ones) from persisting after a name change.
56+
reset-nick-on-name-change: false
57+
5458
# When this option is enabled, display names for hidden players will not be shown. This prevents players from being
5559
# able to see that they are online while vanished.
5660
hide-displayname-in-vanish: true

0 commit comments

Comments
 (0)