Support sleep mechanics across multiple worlds
Refactored `TimeForwardRunnable` to handle multiple worlds instead of a single hardcoded world. The code now dynamically retrieves sleep percentage and processes time advancement for each world, improving flexibility and compatibility.
This commit is contained in:
parent
232e0bfa9a
commit
c096674a15
1 changed files with 19 additions and 18 deletions
|
@ -14,23 +14,23 @@ 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) / 100.0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (World world : server.getWorlds()) {
|
||||
var gameRuleValue = world.getGameRuleValue(GameRule.PLAYERS_SLEEPING_PERCENTAGE);
|
||||
if (gameRuleValue == null) gameRuleValue = 100;
|
||||
double percentage = gameRuleValue / 100.0;
|
||||
|
||||
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
|
||||
int onlinePlayers = (int) (world.getPlayers().size() / percentage);
|
||||
|
||||
double sleepPercentage = (double) playersSleeping / onlinePlayers;
|
||||
|
||||
|
@ -48,3 +48,4 @@ public class TimeForwardRunnable extends BukkitRunnable {
|
|||
world.setTime(world.getTime() + perSkip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue