skbidi
Some checks failed
/ deploy (push) Has been cancelled

This commit is contained in:
Minecon724 2024-11-30 11:30:15 +01:00
parent 61e7865c19
commit fa5dc94d61
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
15 changed files with 241 additions and 28 deletions

View file

@ -36,3 +36,11 @@ Updates ALL* your plugins \
### Hardcore <sup><sup><sub>P</sub></sup></sup> ### Hardcore <sup><sup><sub>P</sub></sup></sup>
Hardcore hearts by chance Hardcore hearts by chance
### Sleep
Sleeping doesn't skip night, but speeds it up. The more players, the faster it goes.
### Instant sleep
One can instantly skip, but only a part of the night. \
There's 5 players on the server. A night is 10 minutes long. \
Each player can instantly skip 2 minutes of the night at any time, even if others aren't sleeping

View file

@ -40,7 +40,10 @@ public record TweaksConfig(
boolean updaterEnabled, boolean updaterEnabled,
boolean hardcoreEnabled, boolean hardcoreEnabled,
float hardcoreChance float hardcoreChance,
boolean sleepEnabled,
boolean sleepInstant
) { ) {
public static final int CONFIG_VERSION = 1; public static final int CONFIG_VERSION = 1;
private static TweaksConfig config; private static TweaksConfig config;
@ -95,6 +98,9 @@ public record TweaksConfig(
boolean hardcoreEnabled = config.getBoolean("hardcore.enabled"); boolean hardcoreEnabled = config.getBoolean("hardcore.enabled");
float hardcoreChance = (float) config.getDouble("hardcore.chance"); float hardcoreChance = (float) config.getDouble("hardcore.chance");
boolean sleepEnabled = config.getBoolean("sleep.enabled");
boolean sleepInstant = config.getBoolean("sleep.instant");
TweaksConfig.config = new TweaksConfig( TweaksConfig.config = new TweaksConfig(
isProtocolLib, isProtocolLib,
hideWorldBorder, hideWorldBorder,
@ -105,7 +111,8 @@ public record TweaksConfig(
compassEnabled, compassWidth, compassPrecision, compassEnabled, compassWidth, compassPrecision,
pomodoroEnabled, pomodoroForce, pomodoroEnabled, pomodoroForce,
updaterEnabled, updaterEnabled,
hardcoreEnabled, hardcoreChance hardcoreEnabled, hardcoreChance,
sleepEnabled, sleepInstant
); );
return TweaksConfig.config; return TweaksConfig.config;

View file

@ -16,6 +16,7 @@ import eu.m724.tweaks.ping.PingChecker;
import eu.m724.tweaks.ping.PingCommands; import eu.m724.tweaks.ping.PingCommands;
import eu.m724.tweaks.pomodoro.PomodoroCommands; import eu.m724.tweaks.pomodoro.PomodoroCommands;
import eu.m724.tweaks.pomodoro.PomodoroManager; import eu.m724.tweaks.pomodoro.PomodoroManager;
import eu.m724.tweaks.sleep.SleepManager;
import eu.m724.tweaks.updater.UpdaterManager; import eu.m724.tweaks.updater.UpdaterManager;
import eu.m724.tweaks.worldborder.WorldBorderManager; import eu.m724.tweaks.worldborder.WorldBorderManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -89,6 +90,9 @@ public class TweaksPlugin extends JavaPlugin {
new HardcoreManager().init(this); new HardcoreManager().init(this);
} }
if (config.sleepEnabled()) {
new SleepManager().init(this);
}
getLogger().info("Took %.3f milliseconds".formatted((System.nanoTime() - start) / 1000000.0)); getLogger().info("Took %.3f milliseconds".formatted((System.nanoTime() - start) / 1000000.0));
} }

View file

@ -33,7 +33,7 @@ public class PlayerPomodoro {
} }
public int getCycleDurationSeconds() { public int getCycleDurationSeconds() {
return isBreak ? 300 : 1500; return isBreak ? (pomodori < 3 ? 300 : 1200) : 1500;
} }
public long getRemainingSeconds(long now) { public long getRemainingSeconds(long now) {
@ -66,6 +66,7 @@ public class PlayerPomodoro {
public void next() { public void next() {
if (isBreak) { // from break to interval if (isBreak) { // from break to interval
this.pomodori++; this.pomodori++;
this.pomodori %= 4;
} }
this.intervalStart = System.nanoTime(); this.intervalStart = System.nanoTime();

View file

@ -6,42 +6,47 @@
package eu.m724.tweaks.pomodoro; package eu.m724.tweaks.pomodoro;
import eu.m724.tweaks.TweaksConfig;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.*;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
public class PomodoroListener implements Listener { public class PomodoroListener implements Listener {
@EventHandler private final boolean force = TweaksConfig.getConfig().pomodoroForce();
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
PlayerPomodoro timer = Pomodoros.get(player);
if (timer == null) return;
if (timer.isBreak()) { @EventHandler
if (timer.isCycleComplete()) { public void onPlayerLogin(PlayerLoginEvent event) {
timer.next(); Player player = event.getPlayer();
PlayerPomodoro pomodoro = Pomodoros.get(player);
if (pomodoro == null) return;
long remaining = pomodoro.getRemainingSeconds(System.nanoTime());
if (pomodoro.isBreak()) {
if (pomodoro.isCycleComplete()) {
pomodoro.next();
} else { } else {
if (force) {
event.getPlayer().kickPlayer( event.getPlayer().kickPlayer(
new ComponentBuilder() new ComponentBuilder()
.append(Pomodoros.formatTimer(timer, System.nanoTime())) .append(Pomodoros.formatTimer(pomodoro, remaining))
.build().toLegacyText() .build().toLegacyText()
); );
} }
} }
} }
}
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
PlayerPomodoro timer = Pomodoros.timers.get(player.getUniqueId()); PlayerPomodoro pomodoro = Pomodoros.get(player);
if (timer == null) return; if (pomodoro == null) return;
if (!timer.isBreak() && timer.isCycleComplete()) { if (!pomodoro.isBreak() && pomodoro.isCycleComplete()) {
timer.next(); pomodoro.next();
} }
} }

View file

@ -17,6 +17,6 @@ public class PomodoroManager {
public void init() { public void init() {
plugin.getServer().getPluginManager().registerEvents(new PomodoroListener(), plugin); plugin.getServer().getPluginManager().registerEvents(new PomodoroListener(), plugin);
new PomodoroRunnable().runTaskTimerAsynchronously(plugin, 0, 20L); new PomodoroRunnable(plugin).runTaskTimerAsynchronously(plugin, 0, 20L);
} }
} }

View file

@ -6,12 +6,22 @@
package eu.m724.tweaks.pomodoro; package eu.m724.tweaks.pomodoro;
import eu.m724.tweaks.Language;
import eu.m724.tweaks.TweaksConfig;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
public class PomodoroRunnable extends BukkitRunnable { public class PomodoroRunnable extends BukkitRunnable {
private final boolean force = TweaksConfig.getConfig().pomodoroForce();
private final Plugin plugin;
public PomodoroRunnable(Plugin plugin) {
this.plugin = plugin; // only used for kicking
}
@Override @Override
public void run() { public void run() {
long now = System.nanoTime(); long now = System.nanoTime();
@ -23,8 +33,14 @@ public class PomodoroRunnable extends BukkitRunnable {
// TODO make not always on // TODO make not always on
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, Pomodoros.formatTimer(pomodoro, remaining)); player.spigot().sendMessage(ChatMessageType.ACTION_BAR, Pomodoros.formatTimer(pomodoro, remaining));
if (remaining <= 0) if (remaining <= 0) {
player.playSound(player.getLocation(), Sound.BLOCK_ANVIL_FALL, 1.0f, 0.5f); player.playSound(player.getLocation(), Sound.BLOCK_ANVIL_FALL, 1.0f, 0.5f);
if (remaining < -60 && force) {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
player.kickPlayer(Language.getString("pomodoroEndKick"));
});
}
}
}); });
} }
} }

View file

@ -6,6 +6,7 @@
package eu.m724.tweaks.pomodoro; package eu.m724.tweaks.pomodoro;
import eu.m724.tweaks.TweaksConfig;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
@ -16,6 +17,8 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class Pomodoros { public class Pomodoros {
private static final boolean force = TweaksConfig.getConfig().pomodoroForce();
static final Map<UUID, PlayerPomodoro> timers = new HashMap<>(); static final Map<UUID, PlayerPomodoro> timers = new HashMap<>();
public static PlayerPomodoro get(Player player) { public static PlayerPomodoro get(Player player) {
@ -48,8 +51,14 @@ public class Pomodoros {
.append("%02d:%02d".formatted(remaining / 60, remaining % 60)) .append("%02d:%02d".formatted(remaining / 60, remaining % 60))
.color(ChatColor.GRAY); .color(ChatColor.GRAY);
} else { } else {
// TODO I think it's bad to put this here
if (force) {
remaining += 60;
} else {
remaining = 0;
}
builder builder
.append("%02d:%02d".formatted(-remaining / 60, -remaining % 60)) .append("%02d:%02d".formatted(remaining / 60, remaining % 60))
.color(remaining % 2 == 0 ? ChatColor.RED : ChatColor.YELLOW); .color(remaining % 2 == 0 ? ChatColor.RED : ChatColor.YELLOW);
} }
} }

View file

@ -0,0 +1,60 @@
/*
* Copyright (C) 2024 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks.sleep;
import eu.m724.tweaks.TweaksConfig;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import org.bukkit.event.world.TimeSkipEvent;
import java.util.HashSet;
import java.util.Set;
public class SleepListener implements Listener {
private final boolean instant = TweaksConfig.getConfig().sleepInstant();
private final Set<Player> skippedCurrentNight = new HashSet<>();
private long lastDay = 0;
@EventHandler
public void onPlayerBedEnter(PlayerBedEnterEvent event) {
if (event.getBedEnterResult() == PlayerBedEnterEvent.BedEnterResult.OK) {
SleepState.playersSleeping++;
if (instant) {
World world = event.getPlayer().getWorld();
long day = world.getFullTime() / 24000;
if (day != lastDay) skippedCurrentNight.clear();
lastDay = day;
if (!skippedCurrentNight.contains(event.getPlayer())) {
double onePlayerRatio = 1.0 / (event.getPlayer().getServer().getOnlinePlayers().size() * world.getGameRuleValue(GameRule.PLAYERS_SLEEPING_PERCENTAGE));
world.setTime((long) ((23459 - world.getTime()) * onePlayerRatio));
skippedCurrentNight.add(event.getPlayer());
}
}
}
}
@EventHandler
public void onPlayerBedLeave(PlayerBedLeaveEvent event) {
SleepState.playersSleeping--;
}
@EventHandler
public void onPlayerBedLeave(TimeSkipEvent event) {
if (event.getSkipReason() == TimeSkipEvent.SkipReason.NIGHT_SKIP)
event.setCancelled(true);
}
}

View file

@ -0,0 +1,18 @@
/*
* Copyright (C) 2024 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks.sleep;
import eu.m724.tweaks.TweaksConfig;
import org.bukkit.plugin.Plugin;
public class SleepManager {
public void init(Plugin plugin) {
plugin.getServer().getPluginManager().registerEvents(new SleepListener(), plugin);
if (!TweaksConfig.getConfig().sleepInstant())
new TimeForwardRunnable(plugin).runTaskTimer(plugin, 0, 1); // TODO maybe not
}
}

View file

@ -0,0 +1,11 @@
/*
* Copyright (C) 2024 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks.sleep;
public class SleepState {
static int playersSleeping;
}

View file

@ -0,0 +1,60 @@
/*
* Copyright (C) 2024 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks.sleep;
import org.bukkit.GameRule;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
public class TimeForwardRunnable extends BukkitRunnable {
private final Server server;
private final World world; // TODO multi worlds
private final double percentage;
public TimeForwardRunnable(Plugin plugin) {
this.server = plugin.getServer();
this.world = server.getWorld("world");
this.percentage = world.getGameRuleValue(GameRule.PLAYERS_SLEEPING_PERCENTAGE);
}
@Override
public void run() {
int playersSleeping = SleepState.playersSleeping;
//System.out.println(playersSleeping);
if (playersSleeping == 0) return;
int onlinePlayers = (int) (server.getOnlinePlayers().size() / percentage); // TODO optimize remove size every tick maybe
double sleepPercentage = (double) playersSleeping / onlinePlayers;
// we want sleep to take 200 ticks which is 10 seconds assuming all palyres onilien
long time = world.getTime();
long untilDay = 23459 - time;
if (untilDay == 0) return;
long perSkip = 200 + (100000 / -untilDay);
perSkip = Math.clamp(perSkip, 20, 200);
perSkip = (long) (perSkip * sleepPercentage);
/*long perSkip = (long) (Math.pow(untilDay, 2) / 10000);
perSkip = Math.clamp(perSkip, 5, 200);
perSkip = (long) (perSkip * sleepPercentage);*/
/*System.out.println("asdasd");
System.out.println(sleepPercentage);
System.out.println(untilDay);
System.out.println(perSkip);*/
world.setTime(world.getTime() + perSkip);
}
}

View file

@ -69,7 +69,7 @@ public class UpdateChecker extends BukkitRunnable {
private void alert() { private void alert() {
int n = availableUpdates.size(); int n = availableUpdates.size();
if (n == 0) return; if (n == 0) return;
logger.info(Language.getString("updateAvailableNotice")); logger.info(Language.getString("updateAvailableNotice").formatted(n));
availableUpdates.stream() availableUpdates.stream()
.map(u -> "- %s (%s -> %s)".formatted(u.resource().name(), u.running().label(), u.latest().label())) .map(u -> "- %s (%s -> %s)".formatted(u.resource().name(), u.running().label(), u.latest().label()))

View file

@ -63,6 +63,19 @@ hardcore:
# 0.0 - 1.0 decimal. This is if you want to make it like an Easter egg # 0.0 - 1.0 decimal. This is if you want to make it like an Easter egg
chance: 1.0 chance: 1.0
# Makes sleeping
# And adds a nice animation
sleep:
enabled: true
# This gives every player a "share" of the night
# They can instantly skip it by sleeping
# 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
# Finally, thank you for downloading Tweaks724, I hope you enjoy! # Finally, thank you for downloading Tweaks724, I hope you enjoy!
# Don't modify unless told to # Don't modify unless told to

View file

@ -5,3 +5,4 @@
# #
updateAvailableNotice = Available updates (%d): updateAvailableNotice = Available updates (%d):
pomodoroEndKick = Break time! Come back in 5 minutes.