1010import com .mojang .logging .LogUtils ;
1111import eu .pb4 .polymer .core .api .entity .PolymerEntityUtils ;
1212import net .fabricmc .fabric .api .command .v2 .CommandRegistrationCallback ;
13+ import net .fabricmc .fabric .api .entity .event .v1 .EntitySleepEvents ;
1314import net .fabricmc .fabric .api .event .player .UseBlockCallback ;
1415import net .minecraft .SharedConstants ;
1516import net .minecraft .block .BedBlock ;
@@ -223,6 +224,21 @@ private static boolean isAir(BlockState state, BlockPos pos, Entity entity) {
223224 return state .isAir () || state .getCollisionShape (entity .getWorld (), pos , ShapeContext .of (entity )).isEmpty ();
224225 }
225226
227+ private static boolean maySleep (final Entity entity , final BlockPos pos ) {
228+ if (entity instanceof ServerPlayerEntity player ) {
229+ final var result = EntitySleepEvents .ALLOW_SLEEP_TIME .invoker ().allowSleepTime (player , pos , false );
230+ if (result .isAccepted ()) {
231+ return true ;
232+ }
233+ if (result == ActionResult .PASS ) {
234+ return !player .getWorld ().isDay ();
235+ }
236+ return false ;
237+ }
238+
239+ return !entity .getWorld ().isDay ();
240+ }
241+
226242 public static ActionResult sit (@ NotNull final World world , @ NotNull final BlockState state ,
227243 @ NotNull final BlockPos pos , @ NotNull final Entity entity , final double topHeight , final boolean command ) {
228244 final double minY = pos .getY () + topHeight ;
@@ -249,7 +265,7 @@ public static ActionResult sit(@NotNull final World world, @NotNull final BlockS
249265 return sit (world , entity , x , y , z , minY );
250266 }
251267
252- if (state .getBlock () instanceof BedBlock && world . isDay ( )) {
268+ if (state .getBlock () instanceof BedBlock && ! maySleep ( entity , pos )) {
253269 if (!command && entity instanceof ServerPlayerEntity player ) {
254270 // Let the bed explode as it should normally.
255271 if (!BedBlock .isBedWorking (world )) {
@@ -269,7 +285,7 @@ public static ActionResult sit(@NotNull final World world, @NotNull final BlockS
269285 }
270286
271287 // Set the spawn point for the player as one would expect.
272- if (head != null ) {
288+ if (head != null && EntitySleepEvents . ALLOW_SETTING_SPAWN . invoker (). allowSettingSpawn ( player , head ) ) {
273289 player .setSpawnPoint (world .getRegistryKey (), head , player .getYaw (), false , true );
274290 }
275291 }
0 commit comments