Skip to content

Commit d328582

Browse files
committed
feat: added mod support via events
Cerulean can now allow you to sleep during the day.
1 parent 5d53615 commit d328582

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Project Specifics
2-
projectVersion=0.8.1
2+
projectVersion=0.9.1
33
modrinthId=G9eJHDO2
44

55
# Minecraft

src/main/java/gay/ampflower/polysit/Main.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.mojang.logging.LogUtils;
1111
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
1212
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
13+
import net.fabricmc.fabric.api.entity.event.v1.EntitySleepEvents;
1314
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
1415
import net.minecraft.SharedConstants;
1516
import 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
}

src/main/resources/fabric.mod.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"minecraft": "${minecraft_required}",
2424
"polymer-core": "*",
2525
"fabric-command-api-v2": "*",
26+
"fabric-entity-events-v1": "*",
2627
"fabric-events-interaction-v0": "*",
2728
"fabric-key-binding-api-v1": "*",
2829
"fabric-lifecycle-events-v1": "*"

0 commit comments

Comments
 (0)