From bc5241274c83c4541e48490cea324b45170002d4 Mon Sep 17 00:00:00 2001
From: Jesse Boyd <jessepaleg@gmail.com>
Date: Thu, 15 Mar 2018 18:44:36 +1100
Subject: [PATCH] Fix CFI smooth creating incorrect `//undo`

---
 .../jnbt/anvil/HeightMapMCAGenerator.java     | 26 ++++++++++++++++---
 .../object/collection/DifferentialArray.java  | 10 ++++---
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/core/src/main/java/com/boydti/fawe/jnbt/anvil/HeightMapMCAGenerator.java b/core/src/main/java/com/boydti/fawe/jnbt/anvil/HeightMapMCAGenerator.java
index ff9206eb..15aad52e 100644
--- a/core/src/main/java/com/boydti/fawe/jnbt/anvil/HeightMapMCAGenerator.java
+++ b/core/src/main/java/com/boydti/fawe/jnbt/anvil/HeightMapMCAGenerator.java
@@ -476,7 +476,27 @@ public class HeightMapMCAGenerator extends MCAWriter implements SimpleWorld, Faw
             }
         } else {
             this.heights.setByte(index, (byte) (blockHeight));
+        }
+    }
 
+    private final void setLayerHeightRaw(int index, int height) {
+        int blockHeight = (height) >> 3;
+        int layerHeight = (height) & 0x7;
+        setLayerHeightRaw(index, blockHeight, layerHeight);
+    }
+
+    private final void setLayerHeightRaw(int index, int blockHeight, int layerHeight) {
+        int floorId = floor.get()[index] >> 4;
+        if (floorId == 78 || floorId == 80) {
+            if (layerHeight != 0) {
+                this.heights.getByteArray()[index] = (byte) (blockHeight + 1);
+                this.floor.getCharArray()[index] = (char) (1248 + layerHeight);
+            } else {
+                this.heights.getByteArray()[index] = (byte) (blockHeight);
+                this.floor.getCharArray()[index] = (char) (1280);
+            }
+        } else {
+            this.heights.getByteArray()[index] = (byte) (blockHeight);
         }
     }
 
@@ -512,7 +532,7 @@ public class HeightMapMCAGenerator extends MCAWriter implements SimpleWorld, Faw
                             int height = img.getRGB(x, z) & 0xFF;
                             if (height == 255 || height > 0 && !white && PseudoRandom.random.nextInt(256) <= height) {
                                 int newHeight = table.average(x, z, index);
-                                setLayerHeight(index, newHeight);
+                                setLayerHeightRaw(index, newHeight);
                             }
                         }
                     }
@@ -525,7 +545,7 @@ public class HeightMapMCAGenerator extends MCAWriter implements SimpleWorld, Faw
                             mutable.mutY(y);
                             if (mask.test(mutable)) {
                                 int newHeight = table.average(x, z, index);
-                                setLayerHeight(index, newHeight);
+                                setLayerHeightRaw(index, newHeight);
                             }
                         }
                     }
@@ -533,7 +553,7 @@ public class HeightMapMCAGenerator extends MCAWriter implements SimpleWorld, Faw
                     for (int z = 0; z < getLength(); z++) {
                         for (int x = 0; x < getWidth(); x++, index++) {
                             int newHeight = table.average(x, z, index);
-                            setLayerHeight(index, newHeight);
+                            setLayerHeightRaw(index, newHeight);
                         }
                     }
                 }
diff --git a/core/src/main/java/com/boydti/fawe/object/collection/DifferentialArray.java b/core/src/main/java/com/boydti/fawe/object/collection/DifferentialArray.java
index 890417d7..e8064195 100644
--- a/core/src/main/java/com/boydti/fawe/object/collection/DifferentialArray.java
+++ b/core/src/main/java/com/boydti/fawe/object/collection/DifferentialArray.java
@@ -60,8 +60,8 @@ public final class DifferentialArray<T> implements DifferentialCollection<T> {
                 changes = (T) (changesChars = new char[length]);
             }
         }
-        boolean changed = false;
         T tmp;
+        boolean changed = this.changed;
         if (changed) {
             tmp = (T) MainUtil.copyNd(data);
         } else {
@@ -83,9 +83,11 @@ public final class DifferentialArray<T> implements DifferentialCollection<T> {
                 if (tmpChar != dataChar) {
                     this.changed = true;
                     tmpChars[i] -= dataChar;
+                } else {
+                    tmpChars[i] = 0;
                 }
             }
-            if (tmp != changes) {
+            if (changed) {
                 for (int i = 0; i < tmpChars.length; i++) {
                     changesChars[i] += tmpChars[i];
                 }
@@ -98,9 +100,11 @@ public final class DifferentialArray<T> implements DifferentialCollection<T> {
                 if (tmpByte != dataByte) {
                     this.changed = true;
                     tmpBytes[i] -= dataByte;
+                } else {
+                    tmpBytes[i] = 0;
                 }
             }
-            if (tmp != changes) {
+            if (changed) {
                 for (int i = 0; i < tmpBytes.length; i++) {
                     changesBytes[i] += tmpBytes[i];
                 }