add createEntity

This commit is contained in:
Tofaa 2024-02-18 00:10:48 +04:00
parent c7375def17
commit 77215fb849
3 changed files with 42 additions and 1 deletions

View file

@ -30,6 +30,11 @@ public interface EntityLibAPI<T> {
void onEnable(); void onEnable();
@NotNull <T extends WrapperEntity> T createEntity(UUID uuid, int entityId, EntityType type);
@NotNull <T extends WrapperEntity> T createEntity(EntityType type);
@NotNull WrapperPlayer spawnPlayer(UserProfile profile, Location location); @NotNull WrapperPlayer spawnPlayer(UserProfile profile, Location location);
@NotNull <T extends WrapperEntity> T spawnEntity(@NotNull Class<T> wrapperClass, @NotNull EntityType entityType, @NotNull Location location); @NotNull <T extends WrapperEntity> T spawnEntity(@NotNull Class<T> wrapperClass, @NotNull EntityType entityType, @NotNull Location location);

View file

@ -40,6 +40,42 @@ public abstract class AbstractEntityLibAPI<P, T> implements EntityLibAPI<T> {
this.tickContainers = settings.shouldTickTickables() ? new HashSet<>() : Collections.emptyList(); this.tickContainers = settings.shouldTickTickables() ? new HashSet<>() : Collections.emptyList();
} }
@Override
public <T1 extends WrapperEntity> @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 <T1 extends WrapperEntity> @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 @Override
public @NotNull WrapperPlayer spawnPlayer(UserProfile profile, Location location) { public @NotNull WrapperPlayer spawnPlayer(UserProfile profile, Location location) {

View file

@ -27,7 +27,7 @@ repositories {
dependencies { dependencies {
compileOnly('org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT') compileOnly('org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT')
compileOnly('com.github.retrooper.packetevents:spigot:2.0.2') 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 { tasks {