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 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,

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
* 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,7 +31,6 @@ public class SleepListener implements Listener {
if (event.getBedEnterResult() == PlayerBedEnterEvent.BedEnterResult.OK) {
SleepState.playersSleeping++;
if (instant) {
World world = event.getPlayer().getWorld();
long day = world.getFullTime() / 24000;
@ -38,6 +38,9 @@ public class SleepListener implements Listener {
lastDay = day;
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);
}
}
}

View file

@ -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