From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Sat, 10 Oct 2020 14:29:55 -0500
Subject: [PATCH] Configurable daylight cycle


diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetTimePacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetTimePacket.java
index 689ad22925b2561f7c8db961743eb1f821dbb25f..fa3c960992cc240161817e54659d83fed259f2fe 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetTimePacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetTimePacket.java
@@ -5,7 +5,7 @@ import net.minecraft.network.protocol.Packet;
 
 public class ClientboundSetTimePacket implements Packet<ClientGamePacketListener> {
     private final long gameTime;
-    private final long dayTime;
+    private long dayTime; public void setDayTime(long dayTime) { this.dayTime = dayTime; } // Purpur
 
     public ClientboundSetTimePacket(long time, long timeOfDay, boolean doDaylightCycle) {
         this.gameTime = time % 192000; // Paper - fix guardian beam
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index f8801c73d4e96949143a5917761b35c7563a6725..33ee5844fed6cca2aecf8b84b03c396b445ba0c2 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -196,6 +196,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
     final Int2ObjectMap<EnderDragonPart> dragonParts;
     private final StructureFeatureManager structureFeatureManager;
     private final boolean tickTime;
+    private double fakeTime; // Purpur
     // Tuinity start - execute chunk tasks mid tick
     public long lastMidTickExecuteFailure;
     // Tuinity end - execute chunk tasks mid tick
@@ -573,6 +574,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
         this.getCraftServer().addWorld(this.getWorld()); // CraftBukkit
 
         this.asyncChunkTaskManager = new com.destroystokyo.paper.io.chunk.ChunkTaskManager(this); // Paper
+        this.fakeTime = this.serverLevelData.getDayTime(); // Purpur
     }
 
     public void setWeatherParameters(int clearDuration, int rainDuration, boolean raining, boolean thundering) {
@@ -838,6 +840,18 @@ public class ServerLevel extends Level implements WorldGenLevel {
             this.liquidTicks.nextTick(); // Paper
             this.serverLevelData.getScheduledEvents().tick(this.server, i);
             if (this.levelData.getGameRules().getBoolean(GameRules.RULE_DAYLIGHT)) {
+                // Purpur start
+                double incrementTimeBy = 12000.0D / (double) (isDay() ? this.purpurConfig.daytimeTicks : this.purpurConfig.nighttimeTicks);
+                if (incrementTimeBy != 1.0D) {
+                    this.fakeTime += incrementTimeBy;
+                    this.setDayTime(this.fakeTime);
+                    net.minecraft.network.protocol.game.ClientboundSetTimePacket packet = new net.minecraft.network.protocol.game.ClientboundSetTimePacket(getGameTime(), getDayTime(), true);
+                    for (ServerPlayer player : this.players) {
+                        packet.setDayTime(player.getPlayerTime());
+                        player.connection.send(packet);
+                    }
+                } else
+                // Purpur end
                 this.setDayTime(this.levelData.getDayTime() + 1L);
             }
 
@@ -846,6 +860,12 @@ public class ServerLevel extends Level implements WorldGenLevel {
 
     public void setDayTime(long timeOfDay) {
         this.serverLevelData.setDayTime(timeOfDay);
+        // Purpur start
+        this.fakeTime = timeOfDay;
+    }
+    public void setDayTime(double i) {
+        this.serverLevelData.setDayTime((long) i);
+        // Purpur end
     }
 
     public void tickCustomSpawners(boolean spawnMonsters, boolean spawnAnimals) {
diff --git a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
index 90ace8ccf344a0f7b496e7d9cb5d49d6789d166b..f21a02e3763d42ebe8130802269a99be54b24163 100644
--- a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
@@ -104,6 +104,13 @@ public class PurpurWorldConfig {
         armorstandStepHeight = (float) getDouble("gameplay-mechanics.armorstand.step-height", armorstandStepHeight);
     }
 
+    public int daytimeTicks = 12000;
+    public int nighttimeTicks = 12000;
+    private void daytimeCycleSettings() {
+        daytimeTicks = getInt("gameplay-mechanics.daylight-cycle-ticks.daytime", daytimeTicks);
+        nighttimeTicks = getInt("gameplay-mechanics.daylight-cycle-ticks.nighttime", nighttimeTicks);
+    }
+
     public int elytraDamagePerSecond = 1;
     public double elytraDamageMultiplyBySpeed = 0;
     public boolean elytraIgnoreUnbreaking = false;
