diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 2fac59fde..46d3fc5f1 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -97,6 +97,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, private boolean answerKeepAlive; private String username; + private Component usernameComponent; protected final PlayerConnection playerConnection; // All the entities that this player can see protected final Set viewableEntities = ConcurrentHashMap.newKeySet(); @@ -1283,13 +1284,22 @@ public class Player extends LivingEntity implements CommandSender, Localizable, } /** - * Gets the player's username. + * Gets the player's name as a component. This will either return the display name + * (if set) or a component holding the username. * - * @return the player's username + * @return the name */ @Override - public @NotNull String getName() { - return username; + public @NotNull Component getName() { + if (this.displayName != null) { + return this.displayName; + } else { + if (this.usernameComponent == null) { + this.usernameComponent = Component.text(this.username); + } + + return this.usernameComponent; + } } /** @@ -1309,6 +1319,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, */ public void setUsernameField(@NotNull String username) { this.username = username; + this.usernameComponent = null; } private void sendChangeGameStatePacket(@NotNull ChangeGameStatePacket.Reason reason, float value) { diff --git a/src/main/java/net/minestom/server/utils/identity/NamedAndIdentified.java b/src/main/java/net/minestom/server/utils/identity/NamedAndIdentified.java index 3f25b8a9d..b786e0dd5 100644 --- a/src/main/java/net/minestom/server/utils/identity/NamedAndIdentified.java +++ b/src/main/java/net/minestom/server/utils/identity/NamedAndIdentified.java @@ -1,13 +1,16 @@ package net.minestom.server.utils.identity; +import net.kyori.adventure.text.Component; import org.jetbrains.annotations.NotNull; import java.util.UUID; +import static net.minestom.server.utils.identity.NamedAndIdentifiedImpl.EMPTY; + /** * An object with a string name and a {@link UUID} identity. */ -public interface NamedAndIdentified extends Named, Identified { +public interface NamedAndIdentified extends Named, Identified { /** * Creates a {@link NamedAndIdentified} instance with an empty name and a random UUID. @@ -15,7 +18,7 @@ public interface NamedAndIdentified extends Named, Identified { * @return the named and identified instance */ static @NotNull NamedAndIdentified empty() { - return of("", UUID.randomUUID()); + return of(EMPTY, UUID.randomUUID()); } /** @@ -24,7 +27,7 @@ public interface NamedAndIdentified extends Named, Identified { * @param name the name * @return the named and identified instance */ - static @NotNull NamedAndIdentified named(@NotNull String name) { + static @NotNull NamedAndIdentified named(@NotNull Component name) { return of(name, UUID.randomUUID()); } @@ -35,7 +38,7 @@ public interface NamedAndIdentified extends Named, Identified { * @return the named and identified instance */ static @NotNull NamedAndIdentified identified(@NotNull UUID uuid) { - return of("", uuid); + return of(EMPTY, uuid); } /** @@ -45,7 +48,7 @@ public interface NamedAndIdentified extends Named, Identified { * @param uuid the uuid * @return the named and identified instance */ - static @NotNull NamedAndIdentified of(@NotNull String name, @NotNull UUID uuid) { + static @NotNull NamedAndIdentified of(@NotNull Component name, @NotNull UUID uuid) { return new NamedAndIdentifiedImpl(name, uuid); } } diff --git a/src/main/java/net/minestom/server/utils/identity/NamedAndIdentifiedImpl.java b/src/main/java/net/minestom/server/utils/identity/NamedAndIdentifiedImpl.java index f36ecf403..a81457d40 100644 --- a/src/main/java/net/minestom/server/utils/identity/NamedAndIdentifiedImpl.java +++ b/src/main/java/net/minestom/server/utils/identity/NamedAndIdentifiedImpl.java @@ -1,5 +1,6 @@ package net.minestom.server.utils.identity; +import net.kyori.adventure.text.Component; import org.jetbrains.annotations.NotNull; import java.util.Objects; @@ -9,7 +10,9 @@ import java.util.UUID; * Simple implementation of {@link NamedAndIdentified}. */ class NamedAndIdentifiedImpl implements NamedAndIdentified { - private final String name; + static final Component EMPTY = Component.empty(); + + private final Component name; private final UUID uuid; /** @@ -17,15 +20,15 @@ class NamedAndIdentifiedImpl implements NamedAndIdentified { * * @param name the name * @param uuid the uuid - * @see NamedAndIdentified#of(String, UUID) + * @see NamedAndIdentified#of(Component, UUID) */ - NamedAndIdentifiedImpl(@NotNull String name, @NotNull UUID uuid) { + NamedAndIdentifiedImpl(@NotNull Component name, @NotNull UUID uuid) { this.name = Objects.requireNonNull(name, "name cannot be null"); this.uuid = Objects.requireNonNull(uuid, "uuid cannot be null"); } @Override - public @NotNull String getName() { + public @NotNull Component getName() { return this.name; }