Class MobSpawnEvent

java.lang.Object
net.neoforged.bus.api.Event
net.neoforged.neoforge.event.entity.EntityEvent
net.neoforged.neoforge.event.entity.living.MobSpawnEvent
Direct Known Subclasses:
FinalizeSpawnEvent, MobDespawnEvent, MobSpawnEvent.PositionCheck

public abstract class MobSpawnEvent extends EntityEvent
This class holds all events relating to the entire flow of mob spawns.
Currently, the events have the following flow for any given mob spawn:

Before the spawn is attempted MobSpawnEvent.SpawnPlacementCheck is fired, to determine if the spawn may occur based on mob-specific rules.
After the entity is created MobSpawnEvent.PositionCheck is fired, to determine if the selected position is legal for the entity.
If both checks succeeded,

invalid reference
FinalizeSpawn
is fired, which performs initialization on the newly-spawned entity.
Finally, if the spawn was not cancelled via
invalid reference
FinalizeSpawn#setSpawnCancelled
, then EntityJoinLevelEvent is fired as the entity enters the world.

invalid reference
AllowDespawn
is not related to the mob spawn event flow, as it fires when a despawn is attempted.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    This event is fired when a mob checks for a valid spawn position, after SpawnPlacements.checkSpawnRules(net.minecraft.world.entity.EntityType<T>, net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.entity.EntitySpawnReason, net.minecraft.core.BlockPos, net.minecraft.util.RandomSource) has been evaluated.
    Conditions validated here include the following: Obstruction - mobs inside blocks or fluids. Pathfinding - if the spawn block is valid for pathfinding. Sea Level - Ocelots check if the position is above sea level. Spawn Block - Ocelots check if the below block is grass or leaves.
    static class 
    This event is fired when Spawn Placements (aka Spawn Rules) are checked, before a mob attempts to spawn.
    Spawn Placement checks include light levels, slime chunks, grass blocks for animals, and others in the same vein.
    The purpose of this event is to permit runtime changes to any or all spawn placement logic without having to wrap the placement for each entity.

    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 final net.minecraft.world.level.ServerLevelAccessor
     
    private final double
     
    private final double
     
    private final double
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    MobSpawnEvent(net.minecraft.world.entity.Mob mob, net.minecraft.world.level.ServerLevelAccessor level, double x, double y, double z)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    net.minecraft.world.entity.Mob
     
    net.minecraft.world.level.ServerLevelAccessor
     
    double
     
    double
     
    double
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • level

      private final net.minecraft.world.level.ServerLevelAccessor level
    • x

      private final double x
    • y

      private final double y
    • z

      private final double z
  • Constructor Details

    • MobSpawnEvent

      @Internal protected MobSpawnEvent(net.minecraft.world.entity.Mob mob, net.minecraft.world.level.ServerLevelAccessor level, double x, double y, double z)
  • Method Details

    • getEntity

      public net.minecraft.world.entity.Mob getEntity()
      Overrides:
      getEntity in class EntityEvent
    • getLevel

      public net.minecraft.world.level.ServerLevelAccessor getLevel()
      Returns:
      The level relating to the mob spawn action
    • getX

      public double getX()
      Returns:
      The x-coordinate relating to the mob spawn action
    • getY

      public double getY()
      Returns:
      The y-coordinate relating to the mob spawn action
    • getZ

      public double getZ()
      Returns:
      The z-coordinate relating to the mob spawn action