From 77215fb8492c4fc22f2a029443271d817f76538c Mon Sep 17 00:00:00 2001 From: Tofaa <82680183+Tofaa2@users.noreply.github.com> Date: Sun, 18 Feb 2024 00:10:48 +0400 Subject: [PATCH] add createEntity --- .../java/me/tofaa/entitylib/EntityLibAPI.java | 5 +++ .../common/AbstractEntityLibAPI.java | 36 +++++++++++++++++++ test-plugin/build.gradle | 2 +- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java b/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java index 2ce726a..95dc7e5 100644 --- a/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java +++ b/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java @@ -30,6 +30,11 @@ public interface EntityLibAPI { void onEnable(); + @NotNull T createEntity(UUID uuid, int entityId, EntityType type); + + @NotNull T createEntity(EntityType type); + + @NotNull WrapperPlayer spawnPlayer(UserProfile profile, Location location); @NotNull T spawnEntity(@NotNull Class wrapperClass, @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 d6ab72d..7521b87 100644 --- a/common/src/main/java/me/tofaa/entitylib/common/AbstractEntityLibAPI.java +++ b/common/src/main/java/me/tofaa/entitylib/common/AbstractEntityLibAPI.java @@ -40,6 +40,42 @@ public abstract class AbstractEntityLibAPI implements EntityLibAPI { this.tickContainers = settings.shouldTickTickables() ? new HashSet<>() : Collections.emptyList(); } + @Override + public @NotNull T1 createEntity(UUID uuid, int entityId, EntityType type) { + if (entities.containsKey(uuid)) { + throw new IllegalArgumentException("Entity with UUID " + uuid + " already exists in this world."); + } + if (entitiesById.containsKey(entityId)) { + throw new IllegalArgumentException("Entity with ID " + entityId + " already exists in this world."); + } + EntityMeta meta = EntityMeta.createMeta(entityId, type); + WrapperEntity e; + if (meta instanceof LivingEntityMeta) { + e = new WrapperLivingEntity(entityId, uuid, type, meta); + } + else if (meta instanceof ThrownExpBottleMeta) { + e = new WrapperExperienceOrbEntity(entityId, uuid, type, meta); + } + else { + e = new WrapperEntity(entityId, uuid, type, meta); + } + entities.put(uuid, e); + entitiesById.put(entityId, e); + return (T1) e; + } + + @Override + public @NotNull T1 createEntity(EntityType type) { + UUID uuid = EntityLib.getPlatform().getEntityUuidProvider().provide(type); + while (entities.containsKey(uuid)) { + uuid = EntityLib.getPlatform().getEntityUuidProvider().provide(type); + } + int entityId = EntityLib.getPlatform().getEntityIdProvider().provide(uuid, type); + while (entitiesById.containsKey(entityId)) { + entityId = EntityLib.getPlatform().getEntityIdProvider().provide(uuid, type); + } + return createEntity(uuid, entityId, type); + } @Override public @NotNull WrapperPlayer spawnPlayer(UserProfile profile, Location location) { diff --git a/test-plugin/build.gradle b/test-plugin/build.gradle index 5d4977e..a31b1d2 100644 --- a/test-plugin/build.gradle +++ b/test-plugin/build.gradle @@ -27,7 +27,7 @@ repositories { dependencies { compileOnly('org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT') compileOnly('com.github.retrooper.packetevents:spigot:2.0.2') - implementation("com.github.Tofaa2.EntityLib:api:2.0.0-PRE") + implementation("com.github.Tofaa2.EntityLib:spigot:2.0.0-SNAPSHOT") } tasks {