Class FinalizeSpawnEvent

java.lang.Object
net.neoforged.bus.api.Event
All Implemented Interfaces:
net.neoforged.bus.api.ICancellableEvent

public class FinalizeSpawnEvent extends MobSpawnEvent implements net.neoforged.bus.api.ICancellableEvent
This event is fired before Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.EntitySpawnReason, net.minecraft.world.entity.SpawnGroupData) is called.
This allows mods to control mob initialization.
In vanilla code, this event is injected by a transformer and not via patch, so calls cannot be traced via call hierarchy (it is not source-visible).

Canceling this event will result in Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.EntitySpawnReason, net.minecraft.world.entity.SpawnGroupData) not being called, and the returned value always being null, instead of propagating the SpawnGroupData.
The entity will still be spawned. If you want to prevent the spawn, use setSpawnCancelled(boolean), which will cause Forge to prevent the spawn.

This event is fired on NeoForge.EVENT_BUS, and is only fired on the logical server.

See Also:
  • invalid reference
    EventHooks#onFinalizeSpawn
  • Nested Class Summary

    Nested classes/interfaces inherited from class net.neoforged.neoforge.event.entity.living.MobSpawnEvent

    MobSpawnEvent.PositionCheck, MobSpawnEvent.SpawnPlacementCheck

    Nested classes/interfaces inherited from class net.neoforged.neoforge.event.entity.EntityEvent

    EntityEvent.EnteringSection, EntityEvent.EntityConstructing, EntityEvent.Size
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private net.minecraft.world.DifficultyInstance
     
    private @Nullable net.minecraft.world.entity.SpawnGroupData
     
    private final @Nullable com.mojang.datafixers.util.Either<net.minecraft.world.level.block.entity.BlockEntity,net.minecraft.world.entity.Entity>
     
    private final net.minecraft.world.entity.EntitySpawnReason
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    FinalizeSpawnEvent(net.minecraft.world.entity.Mob entity, net.minecraft.world.level.ServerLevelAccessor level, double x, double y, double z, net.minecraft.world.DifficultyInstance difficulty, net.minecraft.world.entity.EntitySpawnReason spawnType, @Nullable net.minecraft.world.entity.SpawnGroupData spawnData, @Nullable com.mojang.datafixers.util.Either<net.minecraft.world.level.block.entity.BlockEntity,net.minecraft.world.entity.Entity> spawner)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    net.minecraft.world.DifficultyInstance
    Retrieves the DifficultyInstance for the chunk where the mob is about to be spawned.
    @Nullable net.minecraft.world.entity.SpawnGroupData
    Retrieves the SpawnGroupData for this entity.
    @Nullable com.mojang.datafixers.util.Either<net.minecraft.world.level.block.entity.BlockEntity,net.minecraft.world.entity.Entity>
    Retrieves the underlying BlockEntity or Entity that performed the spawn.
    net.minecraft.world.entity.EntitySpawnReason
    Retrieves the type of mob spawn that happened (the event that caused the spawn).
    boolean
    Returns the current spawn cancellation status, which can be changed via setSpawnCancelled(boolean).
    void
    setDifficulty(net.minecraft.world.DifficultyInstance inst)
    Sets the difficulty instance for this event, which will be propagated to Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.EntitySpawnReason, net.minecraft.world.entity.SpawnGroupData) unless cancelled.
    void
    setSpawnCancelled(boolean cancel)
    This method can be used to cancel the spawn of this mob.
    void
    setSpawnData(@Nullable net.minecraft.world.entity.SpawnGroupData data)
    Sets the spawn data for this entity.

    Methods inherited from class net.neoforged.neoforge.event.entity.living.MobSpawnEvent

    getEntity, getLevel, getX, getY, getZ

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface net.neoforged.bus.api.ICancellableEvent

    isCanceled, setCanceled
  • Field Details

    • spawnType

      private final net.minecraft.world.entity.EntitySpawnReason spawnType
    • spawner

      @Nullable private final @Nullable com.mojang.datafixers.util.Either<net.minecraft.world.level.block.entity.BlockEntity,net.minecraft.world.entity.Entity> spawner
    • difficulty

      private net.minecraft.world.DifficultyInstance difficulty
    • spawnData

      @Nullable private @Nullable net.minecraft.world.entity.SpawnGroupData spawnData
  • Constructor Details

    • FinalizeSpawnEvent

      @Internal public FinalizeSpawnEvent(net.minecraft.world.entity.Mob entity, net.minecraft.world.level.ServerLevelAccessor level, double x, double y, double z, net.minecraft.world.DifficultyInstance difficulty, net.minecraft.world.entity.EntitySpawnReason spawnType, @Nullable @Nullable net.minecraft.world.entity.SpawnGroupData spawnData, @Nullable @Nullable com.mojang.datafixers.util.Either<net.minecraft.world.level.block.entity.BlockEntity,net.minecraft.world.entity.Entity> spawner)
  • Method Details

    • getDifficulty

      public net.minecraft.world.DifficultyInstance getDifficulty()
      Retrieves the DifficultyInstance for the chunk where the mob is about to be spawned.
      Returns:
      The local difficulty instance
    • setDifficulty

      public void setDifficulty(net.minecraft.world.DifficultyInstance inst)
      Sets the difficulty instance for this event, which will be propagated to Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.EntitySpawnReason, net.minecraft.world.entity.SpawnGroupData) unless cancelled. The difficulty instance controls how likely certain random effects are to occur, or if certain mob abilities are enabled.
      Parameters:
      inst - The new difficulty instance.
    • getSpawnType

      public net.minecraft.world.entity.EntitySpawnReason getSpawnType()
      Retrieves the type of mob spawn that happened (the event that caused the spawn). The enum names are self-explanatory.
      Returns:
      The mob spawn type.
      See Also:
      • EntitySpawnReason
    • getSpawnData

      @Nullable public @Nullable net.minecraft.world.entity.SpawnGroupData getSpawnData()
      Retrieves the SpawnGroupData for this entity. When spawning mobs in a loop, this group data is used for the entire group and impacts future spawns. This is how entities like horses ensure that the whole group spawns as a single variant. How this is used varies on a per-entity basis.
      Returns:
      The spawn group data.
    • setSpawnData

      public void setSpawnData(@Nullable @Nullable net.minecraft.world.entity.SpawnGroupData data)
      Sets the spawn data for this entity. If this event is cancelled, this value is not used, since Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.EntitySpawnReason, net.minecraft.world.entity.SpawnGroupData) will not be called.
      Parameters:
      data - The new spawn data
      See Also:
    • getSpawner

      @Nullable public @Nullable com.mojang.datafixers.util.Either<net.minecraft.world.level.block.entity.BlockEntity,net.minecraft.world.entity.Entity> getSpawner()
      Retrieves the underlying BlockEntity or Entity that performed the spawn. This may be a SpawnerBlockEntity, TrialSpawnerBlockEntity, MinecartSpawner, or similar modded object.

      This is usually null unless the spawn type is a

      invalid reference
      spawner type
      , and may still be null even then.
      Returns:
      The spawner responsible for triggering the spawn, or null if none is available.
    • setSpawnCancelled

      public void setSpawnCancelled(boolean cancel)
      This method can be used to cancel the spawn of this mob.

      This method must be used if you want to block the spawn, as canceling the event only blocks the call to Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.EntitySpawnReason, net.minecraft.world.entity.SpawnGroupData).

      Note that if the spawn is cancelled, but the event is not, then Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.EntitySpawnReason, net.minecraft.world.entity.SpawnGroupData) will still be called, but the entity will not be spawned. Usually that has no side effects, but callers should be aware.

      Parameters:
      cancel - If the spawn should be cancelled (or not).
    • isSpawnCancelled

      public boolean isSpawnCancelled()
      Returns the current spawn cancellation status, which can be changed via setSpawnCancelled(boolean).
      Returns:
      If this mob's spawn is cancelled or not.