Use Component in NamedAndIdentified

This commit is contained in:
Kieran Wallbanks 2021-04-12 15:08:27 +01:00
parent 8d192fcb9c
commit a16c8d3273
3 changed files with 30 additions and 13 deletions

View File

@ -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<Entity> 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) {

View File

@ -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<String>, Identified {
public interface NamedAndIdentified extends Named<Component>, Identified {
/**
* Creates a {@link NamedAndIdentified} instance with an empty name and a random UUID.
@ -15,7 +18,7 @@ public interface NamedAndIdentified extends Named<String>, 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<String>, 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<String>, 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<String>, 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);
}
}

View File

@ -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;
}