Skip to content

Commit

Permalink
Add playersSleepingPercentage GameRule (#2158)
Browse files Browse the repository at this point in the history
  • Loading branch information
lt-name authored Jan 14, 2024
1 parent 25cabab commit bec975c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/main/java/cn/nukkit/level/GameRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public enum GameRule {
SHOW_DEATH_MESSAGES("showDeathMessages"),
SPAWN_RADIUS("spawnRadius"),
TNT_EXPLODES("tntExplodes"),
SHOW_TAGS("showTags");
SHOW_TAGS("showTags"),
PLAYERS_SLEEPING_PERCENTAGE("playersSleepingPercentage");

private final String name;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/cn/nukkit/level/GameRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static GameRules getDefault() {
gameRules.gameRules.put(SPAWN_RADIUS, new Value<>(Type.INTEGER, 5));
gameRules.gameRules.put(TNT_EXPLODES, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(SHOW_TAGS, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(PLAYERS_SLEEPING_PERCENTAGE, new Value<>(Type.INTEGER, 100));

return gameRules;
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/cn/nukkit/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -974,15 +974,16 @@ public void checkSleep() {
return;
}

boolean resetTime = true;
int playerCount = 0;
int sleepingPlayerCount = 0;
for (Player p : this.getPlayers().values()) {
if (!p.isSleeping()) {
resetTime = false;
break;
playerCount++;
if (p.isSleeping()) {
sleepingPlayerCount++;
}
}

if (resetTime) {
if (playerCount > 0 && sleepingPlayerCount / playerCount * 100 >= this.gameRules.getInteger(GameRule.PLAYERS_SLEEPING_PERCENTAGE)) {
int time = this.getTime() % Level.TIME_FULL;

if (time >= Level.TIME_NIGHT && time < Level.TIME_SUNRISE) {
Expand Down

0 comments on commit bec975c

Please sign in to comment.