don't persist temporary nicknames

This commit is contained in:
Draycia committed 2021-09-01 13:51:36 -07:00
1 parent 3b1987b5a2
commit f866157d97
5 files changed
+61 -3

No files matched your search

@@ -27,6 +27,13 @@ public interface CarbonPlayer extends Audience, Identified {
*/
String username();
/**
* Checks if the player has a display name set through {@link CarbonPlayer#displayName(Component)}
*
* @return if the player has a display name set through {@link CarbonPlayer#displayName(Component)}
*/
boolean hasCustomDisplayName();
/**
* Gets the player's display name, shown in places like chat and tab menu.
*
@@ -44,6 +51,17 @@ public interface CarbonPlayer extends Audience, Identified {
*/
void displayName(final @Nullable Component displayName);
/**
* Temporarily sets the player's display name.<br>
* Setting null is equivalent to setting the display name to the username.
* If the player logs off or is otherwise disconnected from the server,
* their temporary display name will be reset.
*
* @param displayName the new display name
* @since 2.0.0
*/
void temporaryDisplayName(final @Nullable Component displayName);
/**
* The player's UUID, often used for identification purposes.
*
@@ -38,13 +38,13 @@ public class BukkitPlayerJoinListener implements Listener {
final CarbonPlayer player = result.player();
Bukkit.getScheduler().scheduleSyncDelayedTask((Plugin) this.carbonChat, () -> {
if (player.displayName() != null) {
if (player.hasCustomDisplayName()) {
player.displayName(player.displayName());
} else {
final @Nullable Component nickname = this.createDefaultNickname(player);
if (nickname != null) {
player.displayName(nickname);
player.temporaryDisplayName(nickname);
}
}
});
@@ -52,6 +52,7 @@ public class BukkitPlayerJoinListener implements Listener {
}
private @Nullable Component createDefaultNickname(final CarbonPlayer player) {
// TODO: command to re-calculate and apply default nicknames
if (BukkitCapabilities.vaultEnabled()) {
final Chat vaultChat = Objects.requireNonNull(BukkitCapabilities.chat());
final Player bukkitPlayer = Bukkit.getPlayer(player.uuid());
@@ -57,11 +57,31 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon implements Forw
}
}
@Override
public void temporaryDisplayName(@Nullable Component displayName) {
this.carbonPlayer.temporaryDisplayName(displayName);
final @Nullable Player player = this.player();
if (player != null) {
// Update player's name in chat
player.displayName(displayName);
// Update player's name in the tab player list
player.playerListName(displayName);
}
}
@Override
public String username() {
return this.carbonPlayer.username();
}
@Override
public boolean hasCustomDisplayName() {
return this.carbonPlayer.hasCustomDisplayName();
}
@Override
public @Nullable Component displayName() {
// if (this.carbonPlayer.displayName() != null) {
@@ -43,7 +43,10 @@ public class CarbonPlayerSerializerGson implements JsonSerializer<CarbonPlayerCo
) {
final JsonObject object = new JsonObject();
object.add("displayName", context.serialize(player.displayName()));
if (player.hasCustomDisplayName()) {
object.add("displayName", context.serialize(player.displayName()));
}
object.add("selectedChannel", context.serialize(player.selectedChannel()));
object.add("username", context.serialize(player.username()));
object.add("uuid", context.serialize(player.uuid()));
@@ -20,6 +20,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
protected boolean deafened = false;
protected @Nullable Component displayName;
protected transient @Nullable Component temporaryDisplayName; // Don't persist temp names
protected boolean muted = false;
protected @Nullable ChatChannel selectedChannel;
protected boolean spying = false;
@@ -58,6 +59,10 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override
public @Nullable Component displayName() {
if (this.temporaryDisplayName != null) {
return this.temporaryDisplayName;
}
return this.displayName;
}
@@ -66,6 +71,12 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.displayName = displayName;
}
@Override
public void temporaryDisplayName(final @Nullable Component temporaryDisplayName) {
// TODO: support durations (expires after X time)
this.temporaryDisplayName = temporaryDisplayName;
}
@Override
public boolean hasPermission(final String permission) {
return false;
@@ -136,6 +147,11 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
return this.username;
}
@Override
public boolean hasCustomDisplayName() {
return this.displayName != null;
}
@Override
public UUID uuid() {
return this.uuid;