From 22c29fc6b22cb45bc388af5368121efb5cc89d2b Mon Sep 17 00:00:00 2001
From: Zach Brown <zach.brown@destroystokyo.com>
Date: Wed, 2 Mar 2016 00:21:24 -0600
Subject: [PATCH] Fix redstone lag issues


diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
index 7c0e61f..facb98c 100644
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
@@ -155,4 +155,15 @@ public class PaperWorldConfig {
         netherVoidTopDamage = getBoolean( "nether-ceiling-void-damage", false );
         log("Top of the nether void damage: " + netherVoidTopDamage);
     }
+
+    public int tickNextTickCap;
+    public boolean tickNextTickListCapIgnoresRedstone;
+    private void tickNextTickCap() {
+        tickNextTickCap = getInt("tick-next-tick-list-cap", 1000); // Higher values will be friendlier to vanilla style mechanics (to a point) but may hurt performance
+        tickNextTickListCapIgnoresRedstone = getBoolean("tick-next-tick-list-cap-ignores-redstone", false); // Redstone TickNextTicks will always bypass the preceding cap
+        log("WorldServer TickNextTick cap set at " + tickNextTickCap);
+        log("WorldServer TickNextTickList cap always processes redstone: " + tickNextTickListCapIgnoresRedstone);
+
+    }
+
 }
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
index 4f9136d..8f37b97 100644
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
@@ -690,6 +690,8 @@ public class WorldServer extends World implements IAsyncTaskHandler {
             if (false) { // CraftBukkit
                 throw new IllegalStateException("TickNextTick list out of synch");
             } else {
+                // Paper start - No, stop doing this, it affects things like redstone
+                /*
                 if (i > 1000) {
                     // CraftBukkit start - If the server has too much to process over time, try to alleviate that
                     if (i > 20 * 1000) {
@@ -699,6 +701,11 @@ public class WorldServer extends World implements IAsyncTaskHandler {
                     }
                     // CraftBukkit end
                 }
+                */
+                if (i > paperConfig.tickNextTickCap) {
+                    i = paperConfig.tickNextTickCap;
+                }
+                // Paper end
 
                 this.methodProfiler.a("cleaning");
 
@@ -716,6 +723,24 @@ public class WorldServer extends World implements IAsyncTaskHandler {
                     this.U.add(nextticklistentry);
                 }
 
+                // Paper start - Allow redstone ticks to bypass the tickNextTickListCap
+                if (paperConfig.tickNextTickListCapIgnoresRedstone) {
+                    Iterator<NextTickListEntry> iterator = this.nextTickList.iterator();
+                    while (iterator.hasNext()) {
+                        NextTickListEntry next = iterator.next();
+                        if (!flag && next.b > this.worldData.getTime()) {
+                            break;
+                        }
+
+                        IBlockData data = next.a().getBlockData();
+                        if (next.a().isPowerSource(data) || next.a() instanceof IInventory) {
+                            iterator.remove();
+                            this.U.add(next);
+                        }
+                    }
+                }
+                // Paper end
+
                 this.methodProfiler.b();
                 this.methodProfiler.a("ticking");
                 Iterator iterator = this.U.iterator();
-- 
2.7.3