Compare commits

...
Author SHA1 Message Date
tmetcalfe89 10dc51d1ee Merge branch 'main' of https://gitlab.com/cable-mc/cobblemon into timinc/pre-and-post-spawn-event 2025-02-10 16:13:34 -08:00
tmetcalfe89 22c7c44ec0 clarifying spawned event with a pre and post
At the moment, the Cobblemon POKEMON_ENTITY_SPAWN event triggers before the entity is finalized. Since the introduction of fishing bait effects, this has become a problem for anyone looking to subscribe to the spawning of the finalized entity. I propose a quick split between a PRE and a POST version of the event. The PRE would be for anyone looking to modify the entity just before it's created, and the POST would be for anyone looking to analyze the final product.
This would introduce a breaking change due to renaming ENTITY_SPAWN and POKEMON_ENTITY_SPAWN to a more accurate name.
2025-02-10 16:12:56 -08:00
2 changed files with 15 additions and 3 deletions
@@ -138,12 +138,23 @@ object CobblemonEvents {
@JvmField
val POKEMON_ENTITY_SAVE_TO_WORLD = CancelableObservable<PokemonEntitySaveToWorldEvent>()
@JvmField
val ENTITY_SPAWN = CancelableObservable<SpawnEvent<*>>()
val ENTITY_SPAWN_PRE = CancelableObservable<SpawnEvent<*>>()
@JvmField
val ENTITY_SPAWN_POST = EventObservable<SpawnEvent<*>>()
@JvmField
val SHINY_CHANCE_CALCULATION = EventObservable<ShinyChanceCalculationEvent>()
@JvmField
val POKEMON_ENTITY_SPAWN = ENTITY_SPAWN
val POKEMON_ENTITY_SPAWN_PRE = ENTITY_SPAWN_PRE
.pipe(
filter { it.entity is PokemonEntity },
map {
@Suppress("UNCHECKED_CAST")
it as SpawnEvent<PokemonEntity>
}
)
@JvmField
val POKEMON_ENTITY_SPAWN = ENTITY_SPAWN_POST
.pipe(
filter { it.entity is PokemonEntity },
map {
@@ -31,10 +31,11 @@ abstract class SingleEntitySpawnAction<T : Entity>(
val e = createEntity() ?: return null
e.setPos(ctx.position.toVec3d().add(0.5, 1.0, 0.5))
var shouldSpawn = false
CobblemonEvents.ENTITY_SPAWN.postThen(SpawnEvent(e, ctx), ifSucceeded = {
CobblemonEvents.ENTITY_SPAWN_PRE.postThen(SpawnEvent(e, ctx), ifSucceeded = {
this.entity.emit(e)
ctx.world.addFreshEntity(e)
shouldSpawn = true
CobblemonEvents.ENTITY_SPAWN_POST.post(SpawnEvent(e, ctx))
})
return if (shouldSpawn) {