Add sleep heal option

This commit is contained in:
Minecon724 2025-01-02 20:21:12 +01:00
parent 4bf6ec5ae7
commit afed2a8f78
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
3 changed files with 21 additions and 13 deletions

View file

@ -49,6 +49,7 @@ public record TweaksConfig(
boolean sleepEnabled, boolean sleepEnabled,
boolean sleepInstant, boolean sleepInstant,
double sleepHeal,
boolean authEnabled, boolean authEnabled,
boolean authForce, boolean authForce,
@ -125,6 +126,7 @@ public record TweaksConfig(
boolean sleepEnabled = config.getBoolean("sleep.enabled"); boolean sleepEnabled = config.getBoolean("sleep.enabled");
boolean sleepInstant = config.getBoolean("sleep.instant"); boolean sleepInstant = config.getBoolean("sleep.instant");
double sleepHeal = config.getDouble("sleep.heal");
boolean authEnabled = config.getBoolean("auth.enabled"); boolean authEnabled = config.getBoolean("auth.enabled");
boolean authForce = config.getBoolean("auth.force"); boolean authForce = config.getBoolean("auth.force");
@ -154,7 +156,7 @@ public record TweaksConfig(
pomodoroEnabled, pomodoroForce, pomodoroEnabled, pomodoroForce,
updaterEnabled, updaterEnabled,
hardcoreEnabled, hardcoreChance, hardcoreEnabled, hardcoreChance,
sleepEnabled, sleepInstant, sleepEnabled, sleepInstant, sleepHeal,
authEnabled, authForce, authHostname, authEnabled, authForce, authHostname,
redstoneEnabled, redstoneListen, redstoneEnabled, redstoneListen,
knockbackModifiers, knockbackModifiers,

View file

@ -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 * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text. * in the project root for the full license text.
*/ */
@ -21,6 +21,7 @@ import java.util.Set;
public class SleepListener implements Listener { public class SleepListener implements Listener {
private final boolean instant = TweaksConfig.getConfig().sleepInstant(); 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 final Set<Player> skippedCurrentNight = new HashSet<>();
private long lastDay = 0; private long lastDay = 0;
@ -30,7 +31,6 @@ public class SleepListener implements Listener {
if (event.getBedEnterResult() == PlayerBedEnterEvent.BedEnterResult.OK) { if (event.getBedEnterResult() == PlayerBedEnterEvent.BedEnterResult.OK) {
SleepState.playersSleeping++; SleepState.playersSleeping++;
if (instant) {
World world = event.getPlayer().getWorld(); World world = event.getPlayer().getWorld();
long day = world.getFullTime() / 24000; long day = world.getFullTime() / 24000;
@ -38,6 +38,9 @@ public class SleepListener implements Listener {
lastDay = day; 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)); 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)); world.setTime(Math.min(world.getTime() + (long) (10917 * onePlayerRatio), 23459));
skippedCurrentNight.add(event.getPlayer()); skippedCurrentNight.add(event.getPlayer());
@ -53,8 +56,9 @@ public class SleepListener implements Listener {
} }
@EventHandler @EventHandler
public void onPlayerBedLeave(TimeSkipEvent event) { public void onTimeSkip(TimeSkipEvent event) {
if (event.getSkipReason() == TimeSkipEvent.SkipReason.NIGHT_SKIP) if (event.getSkipReason() == TimeSkipEvent.SkipReason.NIGHT_SKIP) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
}

View file

@ -83,6 +83,9 @@ hardcore:
# Makes sleeping # Makes sleeping
# And adds a nice animation # 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: sleep:
enabled: true enabled: true
# This gives every player a "share" of the night # 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 # 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 # Leaving the bed and reentering it does nothing
instant: false instant: false
# Percentage: playersSleepingPercentage gamerule # How many hearts to heal after sleeping
# If instant: how much % of players to skip the night heal: 2.0
# If not: how much % make skipping full speed
# "Hostname" authentication # "Hostname" authentication
# This makes a player need to join a unique hostname like "asd123.example.com" where "asd123" is the key # This makes a player need to join a unique hostname like "asd123.example.com" where "asd123" is the key