From afed2a8f780a73b2962c84c377d46cfe97212e7b Mon Sep 17 00:00:00 2001
From: Minecon724 <git@m724.eu>
Date: Thu, 2 Jan 2025 20:21:12 +0100
Subject: [PATCH] Add sleep heal option

---
 .../java/eu/m724/tweaks/TweaksConfig.java     |  4 +++-
 .../eu/m724/tweaks/sleep/SleepListener.java   | 22 +++++++++++--------
 src/main/resources/config.yml                 |  8 ++++---
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/src/main/java/eu/m724/tweaks/TweaksConfig.java b/src/main/java/eu/m724/tweaks/TweaksConfig.java
index 011e74e..4a34ec7 100644
--- a/src/main/java/eu/m724/tweaks/TweaksConfig.java
+++ b/src/main/java/eu/m724/tweaks/TweaksConfig.java
@@ -49,6 +49,7 @@ public record TweaksConfig(
 
         boolean sleepEnabled,
         boolean sleepInstant,
+        double sleepHeal,
 
         boolean authEnabled,
         boolean authForce,
@@ -125,6 +126,7 @@ public record TweaksConfig(
 
         boolean sleepEnabled = config.getBoolean("sleep.enabled");
         boolean sleepInstant = config.getBoolean("sleep.instant");
+        double sleepHeal = config.getDouble("sleep.heal");
 
         boolean authEnabled = config.getBoolean("auth.enabled");
         boolean authForce = config.getBoolean("auth.force");
@@ -154,7 +156,7 @@ public record TweaksConfig(
                 pomodoroEnabled, pomodoroForce,
                 updaterEnabled,
                 hardcoreEnabled, hardcoreChance,
-                sleepEnabled, sleepInstant,
+                sleepEnabled, sleepInstant, sleepHeal,
                 authEnabled, authForce, authHostname,
                 redstoneEnabled, redstoneListen,
                 knockbackModifiers,
diff --git a/src/main/java/eu/m724/tweaks/sleep/SleepListener.java b/src/main/java/eu/m724/tweaks/sleep/SleepListener.java
index 6e0c23c..ad3736e 100644
--- a/src/main/java/eu/m724/tweaks/sleep/SleepListener.java
+++ b/src/main/java/eu/m724/tweaks/sleep/SleepListener.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2024  Minecon724
+ * Copyright (C) 2025  Minecon724
  * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
  * in the project root for the full license text.
  */
@@ -21,6 +21,7 @@ import java.util.Set;
 
 public class SleepListener implements Listener {
     private final boolean instant = TweaksConfig.getConfig().sleepInstant();
+    private final double heal = TweaksConfig.getConfig().sleepHeal() * 2; // hearts to half hearts
 
     private final Set<Player> skippedCurrentNight = new HashSet<>();
     private long lastDay = 0;
@@ -30,14 +31,16 @@ public class SleepListener implements Listener {
         if (event.getBedEnterResult() == PlayerBedEnterEvent.BedEnterResult.OK) {
             SleepState.playersSleeping++;
 
-            if (instant) {
-                World world = event.getPlayer().getWorld();
+            World world = event.getPlayer().getWorld();
 
-                long day = world.getFullTime() / 24000;
-                if (day != lastDay) skippedCurrentNight.clear();
-                lastDay = day;
+            long day = world.getFullTime() / 24000;
+            if (day != lastDay) skippedCurrentNight.clear();
+            lastDay = day;
 
-                if (!skippedCurrentNight.contains(event.getPlayer())) {
+            if (!skippedCurrentNight.contains(event.getPlayer())) {
+                event.getPlayer().setHealth(event.getPlayer().getHealth() + heal);
+
+                if (instant) {
                     double onePlayerRatio = 1 / (event.getPlayer().getServer().getOnlinePlayers().size() * (world.getGameRuleValue(GameRule.PLAYERS_SLEEPING_PERCENTAGE) / 100.0));
                     world.setTime(Math.min(world.getTime() + (long) (10917 * onePlayerRatio), 23459));
                     skippedCurrentNight.add(event.getPlayer());
@@ -53,8 +56,9 @@ public class SleepListener implements Listener {
     }
 
     @EventHandler
-    public void onPlayerBedLeave(TimeSkipEvent event) {
-        if (event.getSkipReason() == TimeSkipEvent.SkipReason.NIGHT_SKIP)
+    public void onTimeSkip(TimeSkipEvent event) {
+        if (event.getSkipReason() == TimeSkipEvent.SkipReason.NIGHT_SKIP) {
             event.setCancelled(true);
+        }
     }
 }
diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml
index 4524099..4aa2426 100644
--- a/src/main/resources/config.yml
+++ b/src/main/resources/config.yml
@@ -83,6 +83,9 @@ hardcore:
 
 # Makes sleeping
 # And adds a nice animation
+# Percentage: playersSleepingPercentage gamerule
+# If instant: how much % of players to skip the night
+# If not: how much % make skipping full speed
 sleep:
   enabled: true
   # This gives every player a "share" of the night
@@ -90,9 +93,8 @@ sleep:
   # For example, if 5 players online and night is 5 minutes, one can go to sleep and skip 1 minute of the night
   # Leaving the bed and reentering it does nothing
   instant: false
-  # Percentage: playersSleepingPercentage gamerule
-  # If instant: how much % of players to skip the night
-  # If not: how much % make skipping full speed
+  # How many hearts to heal after sleeping
+  heal: 2.0
 
 # "Hostname" authentication
 # This makes a player need to join a unique hostname like "asd123.example.com" where "asd123" is the key