From 8d192fcb9c1afc80361f91efad73986aac194704 Mon Sep 17 00:00:00 2001 From: Kieran Wallbanks Date: Mon, 12 Apr 2021 10:49:04 +0100 Subject: [PATCH] Add NamedAndIdentified interface --- .../net/minestom/server/entity/Entity.java | 3 +- .../net/minestom/server/entity/Player.java | 20 +++++-- .../server/utils/identity/Identified.java | 19 +++++++ .../minestom/server/utils/identity/Named.java | 19 +++++++ .../utils/identity/NamedAndIdentified.java | 51 ++++++++++++++++++ .../identity/NamedAndIdentifiedImpl.java | 54 +++++++++++++++++++ 6 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 src/main/java/net/minestom/server/utils/identity/Identified.java create mode 100644 src/main/java/net/minestom/server/utils/identity/Named.java create mode 100644 src/main/java/net/minestom/server/utils/identity/NamedAndIdentified.java create mode 100644 src/main/java/net/minestom/server/utils/identity/NamedAndIdentifiedImpl.java diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index cbc3f25b8..85466b201 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -39,6 +39,7 @@ import net.minestom.server.utils.callback.OptionalCallback; import net.minestom.server.utils.chunk.ChunkCallback; import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.entity.EntityUtils; +import net.minestom.server.utils.identity.Identified; import net.minestom.server.utils.player.PlayerUtils; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.TimeUnit; @@ -61,7 +62,7 @@ import java.util.function.UnaryOperator; *

* To create your own entity you probably want to extends {@link LivingEntity} or {@link EntityCreature} instead. */ -public class Entity implements Viewable, Tickable, EventHandler, DataContainer, PermissionHandler, HoverEventSource { +public class Entity implements Viewable, Tickable, EventHandler, DataContainer, PermissionHandler, HoverEventSource, Identified { private static final Map ENTITY_BY_ID = new ConcurrentHashMap<>(); private static final Map ENTITY_BY_UUID = new ConcurrentHashMap<>(); diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 98adfe5fd..2fac59fde 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -66,6 +66,7 @@ import net.minestom.server.utils.*; import net.minestom.server.utils.chunk.ChunkCallback; import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.entity.EntityUtils; +import net.minestom.server.utils.identity.NamedAndIdentified; import net.minestom.server.utils.instance.InstanceUtils; import net.minestom.server.utils.inventory.PlayerInventoryUtils; import net.minestom.server.utils.player.PlayerUtils; @@ -90,7 +91,7 @@ import java.util.function.UnaryOperator; *

* You can easily create your own implementation of this and use it with {@link ConnectionManager#setPlayerProvider(PlayerProvider)}. */ -public class Player extends LivingEntity implements CommandSender, Localizable, HoverEventSource, Identified { +public class Player extends LivingEntity implements CommandSender, Localizable, HoverEventSource, Identified, NamedAndIdentified { private long lastKeepAlive; private boolean answerKeepAlive; @@ -1282,12 +1283,21 @@ public class Player extends LivingEntity implements CommandSender, Localizable, } /** - * Gets the player username. + * Gets the player's username. * - * @return the player username + * @return the player's username */ - @NotNull - public String getUsername() { + @Override + public @NotNull String getName() { + return username; + } + + /** + * Gets the player's username. + * + * @return the player's username + */ + public @NotNull String getUsername() { return username; } diff --git a/src/main/java/net/minestom/server/utils/identity/Identified.java b/src/main/java/net/minestom/server/utils/identity/Identified.java new file mode 100644 index 000000000..ec4f40781 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/identity/Identified.java @@ -0,0 +1,19 @@ +package net.minestom.server.utils.identity; + +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +/** + * An object with a unique identifier. + */ +@FunctionalInterface +public interface Identified { + + /** + * Gets the unique identifier for this object. + * + * @return the uuid + */ + @NotNull UUID getUuid(); +} diff --git a/src/main/java/net/minestom/server/utils/identity/Named.java b/src/main/java/net/minestom/server/utils/identity/Named.java new file mode 100644 index 000000000..64dedb3ef --- /dev/null +++ b/src/main/java/net/minestom/server/utils/identity/Named.java @@ -0,0 +1,19 @@ +package net.minestom.server.utils.identity; + +import org.jetbrains.annotations.NotNull; + +/** + * An object with a name. + * + * @param the type of the name + */ +@FunctionalInterface +public interface Named { + + /** + * Gets the name of this object. + * + * @return the name + */ + @NotNull T getName(); +} diff --git a/src/main/java/net/minestom/server/utils/identity/NamedAndIdentified.java b/src/main/java/net/minestom/server/utils/identity/NamedAndIdentified.java new file mode 100644 index 000000000..3f25b8a9d --- /dev/null +++ b/src/main/java/net/minestom/server/utils/identity/NamedAndIdentified.java @@ -0,0 +1,51 @@ +package net.minestom.server.utils.identity; + +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +/** + * An object with a string name and a {@link UUID} identity. + */ +public interface NamedAndIdentified extends Named, Identified { + + /** + * Creates a {@link NamedAndIdentified} instance with an empty name and a random UUID. + * + * @return the named and identified instance + */ + static @NotNull NamedAndIdentified empty() { + return of("", UUID.randomUUID()); + } + + /** + * Creates a {@link NamedAndIdentified} instance with a given name and a random UUID. + * + * @param name the name + * @return the named and identified instance + */ + static @NotNull NamedAndIdentified named(@NotNull String name) { + return of(name, UUID.randomUUID()); + } + + /** + * Creates a {@link NamedAndIdentified} instance with an empty name and a given UUID. + * + * @param uuid the uuid + * @return the named and identified instance + */ + static @NotNull NamedAndIdentified identified(@NotNull UUID uuid) { + return of("", uuid); + } + + /** + * Creates a {@link NamedAndIdentified} instance with a given name and UUID. + * + * @param name the name + * @param uuid the uuid + * @return the named and identified instance + */ + static @NotNull NamedAndIdentified of(@NotNull String 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 new file mode 100644 index 000000000..f36ecf403 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/identity/NamedAndIdentifiedImpl.java @@ -0,0 +1,54 @@ +package net.minestom.server.utils.identity; + +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; +import java.util.UUID; + +/** + * Simple implementation of {@link NamedAndIdentified}. + */ +class NamedAndIdentifiedImpl implements NamedAndIdentified { + private final String name; + private final UUID uuid; + + /** + * Creates a new named and identified implementation. + * + * @param name the name + * @param uuid the uuid + * @see NamedAndIdentified#of(String, UUID) + */ + NamedAndIdentifiedImpl(@NotNull String 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() { + return this.name; + } + + @Override + public @NotNull UUID getUuid() { + return this.uuid; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + NamedAndIdentifiedImpl that = (NamedAndIdentifiedImpl) o; + return this.uuid.equals(that.uuid); + } + + @Override + public int hashCode() { + return Objects.hash(this.uuid); + } + + @Override + public String toString() { + return String.format("NamedAndIdentifiedImpl{name='%s', uuid=%s}", this.name, this.uuid); + } +}