Class EntitySleepEvents

java.lang.Object
net.fabricmc.fabric.api.entity.event.v1.EntitySleepEvents

public final class EntitySleepEvents extends Object
Events about the sleep of living entities.

These events can be categorized into three groups:

  1. Simple listeners: START_SLEEPING and STOP_SLEEPING
  2. Predicates: ALLOW_BED, ALLOW_SLEEP_TIME, ALLOW_RESETTING_TIME, ALLOW_NEARBY_MONSTERS, ALLOW_SETTING_SPAWN and ALLOW_SLEEPING

    Note: Only the ALLOW_BED event applies to non-player entities.

  3. Modifiers: MODIFY_SLEEPING_DIRECTION, SET_BED_OCCUPATION_STATE and MODIFY_WAKE_UP_POSITION

Sleep events are useful for making custom bed blocks that do not extend BedBlock. Custom beds generally only need a custom ALLOW_BED checker and a MODIFY_SLEEPING_DIRECTION callback, but the other events might be useful as well.

  • Field Details

    • ALLOW_SLEEPING

      public static final Event<EntitySleepEvents.AllowSleeping> ALLOW_SLEEPING
      An event that checks whether a player can start to sleep in a bed-like block. This event only applies to sleeping using PlayerEntity.trySleep(BlockPos).

      Note: Please use the more detailed events ALLOW_SLEEP_TIME and ALLOW_NEARBY_MONSTERS if they match your use case! This helps with mod compatibility.

      If this event returns a PlayerEntity.SleepFailureReason, it is used as the return value of PlayerEntity.trySleep(BlockPos) and sleeping fails. A null return value means that the player will start sleeping.

      When this event is called, all vanilla sleeping checks have already succeeded, i.e. this event is used in addition to vanilla checks. The more detailed events ALLOW_SLEEP_TIME and ALLOW_NEARBY_MONSTERS are also checked before this event.

    • START_SLEEPING

      public static final Event<EntitySleepEvents.StartSleeping> START_SLEEPING
      An event that is called when an entity starts to sleep.
    • STOP_SLEEPING

      public static final Event<EntitySleepEvents.StopSleeping> STOP_SLEEPING
      An event that is called when an entity stops sleeping and wakes up.
    • ALLOW_BED

      public static final Event<EntitySleepEvents.AllowBed> ALLOW_BED
      An event that is called to check whether a block is valid for sleeping.

      Used for checking whether the block at the current sleeping position is a valid bed block. If false, the player wakes up.

      This event is only checked during sleeping, so an entity can start sleeping on any block, but will immediately wake up if this check fails.

      See Also:
      • LivingEntity.isSleepingInBed()
    • ALLOW_SLEEP_TIME

      public static final Event<EntitySleepEvents.AllowSleepTime> ALLOW_SLEEP_TIME
      An event that checks whether the current time of day is valid for sleeping.

      Note that if sleeping during day time is allowed, the game will still reset the time to 0 if the usual conditions are met, unless forbidden with ALLOW_RESETTING_TIME.

    • ALLOW_NEARBY_MONSTERS

      public static final Event<EntitySleepEvents.AllowNearbyMonsters> ALLOW_NEARBY_MONSTERS
      An event that checks whether players can sleep when monsters are nearby.

      This event can also be used to force a failing result, meaning it can do custom monster checks.

    • ALLOW_RESETTING_TIME

      public static final Event<EntitySleepEvents.AllowResettingTime> ALLOW_RESETTING_TIME
      An event that checks whether a sleeping player counts into skipping the current day and resetting the time to 0.

      When this event is called, all vanilla time resetting checks have already succeeded, i.e. this event is used in addition to vanilla checks.

    • MODIFY_SLEEPING_DIRECTION

      public static final Event<EntitySleepEvents.ModifySleepingDirection> MODIFY_SLEEPING_DIRECTION
      An event that can be used to provide the entity's sleep direction if missing.

      This is useful for custom bed blocks that need to determine the sleeping direction themselves. If the block is not a BedBlock, you need to provide the sleeping direction manually with this event.

    • ALLOW_SETTING_SPAWN

      public static final Event<EntitySleepEvents.AllowSettingSpawn> ALLOW_SETTING_SPAWN
      An event that checks whether a player's spawn can be set when sleeping.

      Vanilla always allows this operation.

    • SET_BED_OCCUPATION_STATE

      public static final Event<EntitySleepEvents.SetBedOccupationState> SET_BED_OCCUPATION_STATE
      An event that sets the occupation state of a bed.

      Note that this is not needed for blocks using BedBlock, which are handled automatically.

    • MODIFY_WAKE_UP_POSITION

      public static final Event<EntitySleepEvents.ModifyWakeUpPosition> MODIFY_WAKE_UP_POSITION
      An event that can be used to provide the entity's wake-up position if missing.

      This is useful for custom bed blocks that need to determine the wake-up position themselves. If the block is not a BedBlock, you need to provide the wake-up position manually with this event.