mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-17 05:31:23 +01:00
Add NamedAndIdentified interface
This commit is contained in:
parent
916650b4c8
commit
8d192fcb9c
@ -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;
|
||||
* <p>
|
||||
* 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<ShowEntity> {
|
||||
public class Entity implements Viewable, Tickable, EventHandler, DataContainer, PermissionHandler, HoverEventSource<ShowEntity>, Identified {
|
||||
|
||||
private static final Map<Integer, Entity> ENTITY_BY_ID = new ConcurrentHashMap<>();
|
||||
private static final Map<UUID, Entity> ENTITY_BY_UUID = new ConcurrentHashMap<>();
|
||||
|
@ -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;
|
||||
* <p>
|
||||
* 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<ShowEntity>, Identified {
|
||||
public class Player extends LivingEntity implements CommandSender, Localizable, HoverEventSource<ShowEntity>, 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;
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
19
src/main/java/net/minestom/server/utils/identity/Named.java
Normal file
19
src/main/java/net/minestom/server/utils/identity/Named.java
Normal file
@ -0,0 +1,19 @@
|
||||
package net.minestom.server.utils.identity;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* An object with a name.
|
||||
*
|
||||
* @param <T> the type of the name
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Named<T> {
|
||||
|
||||
/**
|
||||
* Gets the name of this object.
|
||||
*
|
||||
* @return the name
|
||||
*/
|
||||
@NotNull T getName();
|
||||
}
|
@ -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<String>, 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user