From 549f89701318257e1d70c91aeebfc4738d9fd657 Mon Sep 17 00:00:00 2001 From: Tofaa <82680183+Tofaa2@users.noreply.github.com> Date: Fri, 23 Feb 2024 13:54:47 +0400 Subject: [PATCH] add createPlayer --- .../java/me/tofaa/entitylib/EntityLibAPI.java | 2 ++ .../entitylib/common/AbstractEntityLibAPI.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java b/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java index df43445..1acf174 100644 --- a/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java +++ b/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java @@ -36,6 +36,8 @@ public interface EntityLibAPI { @NotNull WrapperPlayer spawnPlayer(UserProfile profile, Location location); + @NotNull WrapperPlayer createPlayer(UserProfile profile); + @NotNull T spawnEntity(@NotNull Class wrapperClass, @NotNull EntityType entityType, @NotNull Location location); @NotNull WrapperEntity spawnEntity(@NotNull EntityType entityType, @NotNull Location location); diff --git a/common/src/main/java/me/tofaa/entitylib/common/AbstractEntityLibAPI.java b/common/src/main/java/me/tofaa/entitylib/common/AbstractEntityLibAPI.java index 7521b87..824489c 100644 --- a/common/src/main/java/me/tofaa/entitylib/common/AbstractEntityLibAPI.java +++ b/common/src/main/java/me/tofaa/entitylib/common/AbstractEntityLibAPI.java @@ -94,6 +94,23 @@ else if (meta instanceof ThrownExpBottleMeta) { return player; } + @Override + public @NotNull WrapperPlayer createPlayer(UserProfile profile) { + if (getEntity(profile.getUUID()) != null) { + throw new IllegalArgumentException("Entity with UUID " + profile.getUUID() + " already exists in this world."); + } + + int id = EntityLib.getPlatform().getEntityIdProvider().provide(profile.getUUID(), EntityTypes.PLAYER); + while (entitiesById.containsKey + (id)) { + id = EntityLib.getPlatform().getEntityIdProvider().provide(profile.getUUID(), EntityTypes.PLAYER); + } + WrapperPlayer player = new WrapperPlayer(profile, id); + entities.put(player.getUuid(), player); + entitiesById.put(player.getEntityId(), player); + return player; + } + @Override public @NotNull T1 spawnEntity(@NotNull T1 entity, @NotNull Location location) { entity.spawn(location);