Simplify the sleep options

This commit is contained in:
William Blake Galbreath
2019-11-30 03:45:51 -06:00
parent 770a249587
commit 3505afaa43
2 changed files with 47 additions and 33 deletions

View File

@@ -427,18 +427,12 @@ limit-villager-iron-golem-spawns
sleep
~~~~~
* only-at-night
* only-with-condition
- **default:** false
- **description:** Make players only sleep at night. Night times specified below. Rest of the sleep options are ignored if this is false
* night-start
- **default:** 12541
- **description:** The starting time of night (in ticks) when a player can first sleep
* night-end
- **default:** 23458
- **description:** The ending time of night (in ticks) when a player cannot sleep
* invert-condition
- **default:** false
- **description:** Invert the result of the time condition. Time condition is `!(time < start && time > end)`. Inverting it removes the `!`
- **description:** Make players only sleep when the following time condition is true
* condition
- **default:** "time >= 12541 && time <= 23458"
- **description:** The time condition for player to be able to sleep
idle-timeout
~~~~~~~~~~~~

View File

@@ -1,27 +1,51 @@
From 7774dfe2612bbb0fb48b15bed689a11afbb11b89 Mon Sep 17 00:00:00 2001
From b3254cd0c8a19f1773a50e43315a75fdb36a4ec0 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Sat, 30 Nov 2019 03:30:17 -0600
Subject: [PATCH] Add sleep options
---
.../java/net/minecraft/server/EntityHuman.java | 15 +++++++++++++--
.../java/net/pl3x/purpur/PurpurWorldConfig.java | 11 +++++++++++
2 files changed, 24 insertions(+), 2 deletions(-)
.../net/minecraft/server/EntityHuman.java | 25 ++++++++++++++++---
.../net/pl3x/purpur/PurpurWorldConfig.java | 7 ++++++
2 files changed, 29 insertions(+), 3 deletions(-)
diff --git a/src/main/java/net/minecraft/server/EntityHuman.java b/src/main/java/net/minecraft/server/EntityHuman.java
index 3610611299..2bb4ae2265 100644
index 3610611299..26e1b8e2dc 100644
--- a/src/main/java/net/minecraft/server/EntityHuman.java
+++ b/src/main/java/net/minecraft/server/EntityHuman.java
@@ -143,6 +143,17 @@ public abstract class EntityHuman extends EntityLiving {
@@ -14,6 +14,11 @@ import java.util.OptionalInt;
import java.util.UUID;
import java.util.function.Predicate;
import javax.annotation.Nullable;
+// Purpur start
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+// Purpur end
// CraftBukkit start
import org.bukkit.craftbukkit.entity.CraftHumanEntity;
@@ -25,7 +30,6 @@ import org.bukkit.event.entity.EntityCombustByEntityEvent;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerVelocityEvent;
-import org.bukkit.util.Vector;
// CraftBukkit end
public abstract class EntityHuman extends EntityLiving {
@@ -143,6 +147,21 @@ public abstract class EntityHuman extends EntityLiving {
this.datawatcher.register(EntityHuman.bw, new NBTTagCompound());
}
+ // Purpur start
+ private ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
+
+ private boolean unableToSleepRightNow() {
+ if (world.purpurConfig.sleepOnlyAtNight) {
+ long time = world.getDayTime();
+ boolean wakeUp = time < world.purpurConfig.sleepStart && time > world.purpurConfig.sleepEnd;
+ return world.purpurConfig.sleepInvertCondition == wakeUp;
+ if (world.purpurConfig.sleepOnlyWithCondition) {
+ try {
+ scriptEngine.eval("time = " + world.getDayTime());
+ return !(Boolean) scriptEngine.eval(world.purpurConfig.sleepCondition);
+ } catch (ScriptException ignore) {
+ }
+ }
+ return world.isDayTime();
+ }
@@ -30,7 +54,7 @@ index 3610611299..2bb4ae2265 100644
@Override
public void tick() {
this.noclip = this.isSpectator();
@@ -160,7 +171,7 @@ public abstract class EntityHuman extends EntityLiving {
@@ -160,7 +179,7 @@ public abstract class EntityHuman extends EntityLiving {
this.sleepTicks = 100;
}
@@ -39,7 +63,7 @@ index 3610611299..2bb4ae2265 100644
this.wakeup(false, true, true);
}
} else if (this.sleepTicks > 0) {
@@ -1268,7 +1279,7 @@ public abstract class EntityHuman extends EntityLiving {
@@ -1268,7 +1287,7 @@ public abstract class EntityHuman extends EntityLiving {
return Either.left(EntityHuman.EnumBedResult.NOT_POSSIBLE_HERE);
}
@@ -49,22 +73,18 @@ index 3610611299..2bb4ae2265 100644
}
diff --git a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
index a1237c5b9d..8b62b0588a 100644
index a1237c5b9d..f61ed338ce 100644
--- a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
@@ -161,6 +161,17 @@ public class PurpurWorldConfig {
@@ -161,6 +161,13 @@ public class PurpurWorldConfig {
limitVillagerIronGolemSpawns = getInt("limit-villager-iron-golem-spawns", limitVillagerIronGolemSpawns);
}
+ public boolean sleepOnlyAtNight = false;
+ public long sleepStart = 12541L;
+ public long sleepEnd = 23458L;
+ public boolean sleepInvertCondition = false;
+ public boolean sleepOnlyWithCondition = false;
+ public String sleepCondition = "time >= 12541 && time <= 23458";
+ private void sleepSettings() {
+ sleepOnlyAtNight = getBoolean("sleep.only-at-night", sleepOnlyAtNight);
+ sleepStart = getInt("sleep.night-start", (int) sleepStart);
+ sleepEnd = getInt("sleep.night-end", (int) sleepEnd);
+ sleepInvertCondition = getBoolean("sleep.invert-condition", sleepInvertCondition);
+ sleepOnlyWithCondition = getBoolean("sleep.only-with-condition", sleepOnlyWithCondition);
+ sleepCondition = getString("sleep.condition", sleepCondition);
+ }
+
public boolean idleTimeoutKick = true;