diff --git a/src/main/java/com/github/steveplays28/realisticsleep/api/RealisticSleepApi.java b/src/main/java/com/github/steveplays28/realisticsleep/api/RealisticSleepApi.java new file mode 100644 index 0000000..75231c0 --- /dev/null +++ b/src/main/java/com/github/steveplays28/realisticsleep/api/RealisticSleepApi.java @@ -0,0 +1,29 @@ +package com.github.steveplays28.realisticsleep.api; + +import net.minecraft.world.World; +import org.jetbrains.annotations.NotNull; + +import static com.github.steveplays28.realisticsleep.util.SleepMathUtil.DAY_LENGTH; +import static com.github.steveplays28.realisticsleep.util.SleepMathUtil.calculateTicksUntilAwake; + +/** + * Realistic Sleep's API. This class contains methods and ways to grab information from Realistic Sleep that may be useful to other mods. + */ +public class RealisticSleepApi { + /** + * @param world The world (aka dimension) + * @return The sleep progress of the world as a percentage. + * @since v1.8.4 + */ + public static float getSleepProgress(@NotNull World world) { + return (DAY_LENGTH - calculateTicksUntilAwake(getTimeOfDay(world))) / DAY_LENGTH / 100f; + } + + /** + * @param world The world (aka dimension) + * @return The time of day of the world in ticks, with the day count filtered out using a modulo operator. This means that when Minecraft returns the time of day of a world, the day count doesn't have any effect on the resulting time. Equal to (int) world.getLevelProperties().getTimeOfDay() % DAY_LENGTH. + */ + public static int getTimeOfDay(@NotNull World world) { + return (int) world.getLevelProperties().getTimeOfDay() % DAY_LENGTH; + } +} diff --git a/src/main/java/com/github/steveplays28/realisticsleep/mixin/ServerWorldMixin.java b/src/main/java/com/github/steveplays28/realisticsleep/mixin/ServerWorldMixin.java index 69d8aee..cd810d4 100644 --- a/src/main/java/com/github/steveplays28/realisticsleep/mixin/ServerWorldMixin.java +++ b/src/main/java/com/github/steveplays28/realisticsleep/mixin/ServerWorldMixin.java @@ -1,5 +1,6 @@ package com.github.steveplays28.realisticsleep.mixin; +import com.github.steveplays28.realisticsleep.api.RealisticSleepApi; import com.github.steveplays28.realisticsleep.util.SleepMathUtil; import net.minecraft.network.packet.s2c.play.WorldTimeUpdateS2CPacket; import net.minecraft.server.MinecraftServer; @@ -86,9 +87,8 @@ public void tickInject(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { int sleepingPlayerCount = sleepManager.getSleeping(); int playerCount = getPlayers().size(); double sleepingRatio = (double) sleepingPlayerCount / playerCount; - timeStepPerTick = SleepMathUtil.calculateTimeStepPerTick( - sleepingRatio, config.sleepSpeedMultiplier, timeStepPerTick); - int timeOfDay = (int) worldProperties.getTimeOfDay() % DAY_LENGTH; + timeStepPerTick = SleepMathUtil.calculateTimeStepPerTick(sleepingRatio, config.sleepSpeedMultiplier, timeStepPerTick); + int timeOfDay = RealisticSleepApi.getTimeOfDay(this); // TODO: Don't assume the TPS is 20 int secondsUntilAwake = Math.abs(SleepMathUtil.calculateSecondsUntilAwake(timeOfDay, timeStepPerTick, 20)); @@ -105,7 +105,7 @@ public void tickInject(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { // Fetch config values and do calculations timeStepPerTickRounded = (int) Math.round(timeStepPerTick); - int ticksUntilAwake = Math.abs(SleepMathUtil.calculateTicksUntilAwake(timeOfDay)); + int ticksUntilAwake = SleepMathUtil.calculateTicksUntilAwake(timeOfDay); double sleepingPercentage = sleepingRatio * 100; var isNight = SleepMathUtil.isNightTime(timeOfDay); var nightDayOrThunderstormText = Text.translatable( diff --git a/src/main/java/com/github/steveplays28/realisticsleep/util/SleepMathUtil.java b/src/main/java/com/github/steveplays28/realisticsleep/util/SleepMathUtil.java index c082a9c..e46d06a 100644 --- a/src/main/java/com/github/steveplays28/realisticsleep/util/SleepMathUtil.java +++ b/src/main/java/com/github/steveplays28/realisticsleep/util/SleepMathUtil.java @@ -20,7 +20,7 @@ public static int calculateTicksToTimeOfDay(int timeOfDay, int targetTimeOfDay) } public static int calculateTicksUntilAwake(int currentTimeOfDay) { - return calculateTicksToTimeOfDay(currentTimeOfDay, isNightTime(currentTimeOfDay) ? DAWN_WAKE_UP_TIME : DUSK_WAKE_UP_TIME); + return Math.abs(calculateTicksToTimeOfDay(currentTimeOfDay, isNightTime(currentTimeOfDay) ? DAWN_WAKE_UP_TIME : DUSK_WAKE_UP_TIME)); } public static int calculateSecondsUntilAwake(int currentTimeOfDay, double timeStepPerTick, double tps) {