Skip to content

Commit

Permalink
chore: add a way to init a mob spawned from prefab before AttemptSpaw…
Browse files Browse the repository at this point in the history
…n is fired
  • Loading branch information
0ffz committed May 13, 2024
1 parent 68579d3 commit 824d882
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ import org.bukkit.Location
import org.bukkit.persistence.PersistentDataContainer


fun Location.spawnFromPrefab(prefab: PrefabKey): Result<BukkitEntity> {
fun Location.spawnFromPrefab(prefab: PrefabKey, initEntityPreEvent: GearyEntity.() -> Unit = {}): Result<BukkitEntity> {
val entity = prefabs.manager[prefab] ?: return Result.failure(IllegalArgumentException("No prefab found"))
return spawnFromPrefab(entity)
return spawnFromPrefab(entity, initEntityPreEvent = initEntityPreEvent)
}


fun Location.spawnFromPrefab(
prefab: GearyEntity,
existingPDC: PersistentDataContainer? = null,
initEntityPreEvent: GearyEntity.() -> Unit = {}
): Result<BukkitEntity> {
return runCatching {
val entity = entity {
if (existingPDC != null) loadComponentsFrom(existingPDC)
extend(prefab)
initEntityPreEvent()
emit(AttemptSpawn(this@spawnFromPrefab))
}
val bukkit = entity.get<BukkitEntity>() ?: error("Entity was not created when spawning from prefab")
Expand Down

0 comments on commit 824d882

Please sign in to comment.