From 907ad8528e91d8a1ce72683c17a2ca778167a740 Mon Sep 17 00:00:00 2001
From: Jordan <dordsor21@gmail.com>
Date: Fri, 17 Jun 2022 22:41:25 +0100
Subject: [PATCH] Don't do the unbelievable bad use of a MutableBlockVector2 in
 the creation of a set (#1825)

* Don't do the unbelievable bad use of a MutableBlockVector2 in the creation of a set
 - Fixes IntellectualSites/PlotSquared#3683

* Clean up remnants of the use of MutableBlockVector2
---
 .../bukkit/adapter/Regenerator.java                 |  1 -
 .../core/util/collection/MemBlockSet.java           |  6 +++---
 .../com/sk89q/worldedit/regions/CuboidRegion.java   | 13 ++++++-------
 3 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/Regenerator.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/Regenerator.java
index 84a2f5753..363f8d646 100644
--- a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/Regenerator.java
+++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/Regenerator.java
@@ -440,7 +440,6 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
         );
         Region adjustedRegion = new CuboidRegion(newMin, newMax);
         return adjustedRegion.getChunks().stream()
-                .map(c -> BlockVector2.at(c.getX(), c.getZ()))
                 .sorted(Comparator
                         .comparingInt(BlockVector2::getZ)
                         .thenComparingInt(BlockVector2::getX)) //needed for RegionLimitedWorldAccess
diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/collection/MemBlockSet.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/collection/MemBlockSet.java
index 178b0052e..33fc963c3 100644
--- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/collection/MemBlockSet.java
+++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/collection/MemBlockSet.java
@@ -93,7 +93,6 @@ public final class MemBlockSet extends BlockSet {
             @Override
             public Iterator<BlockVector2> iterator() {
                 return new Iterator<>() {
-                    private final MutableBlockVector2 mutable = new MutableBlockVector2();
                     private boolean hasNext;
                     private int X;
                     private int Z;
@@ -131,9 +130,10 @@ public final class MemBlockSet extends BlockSet {
 
                     @Override
                     public BlockVector2 next() {
-                        mutable.setComponents(setX + getBlockOffsetX(), setZ + getBlockOffsetZ());
+                        // Maintain correct order of method call/variable use
+                        BlockVector2 result = BlockVector2.at(setX + getBlockOffsetX(), setZ + getBlockOffsetZ());
                         init();
-                        return mutable;
+                        return result;
                     }
 
                     @Override
diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java b/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java
index d7d285212..7f793d36b 100644
--- a/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java
+++ b/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java
@@ -377,12 +377,11 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion {
         final int size = (maxX - minX + 1) * (maxZ - minZ + 1);
 
         //FAWE start
-        return new AbstractSet<BlockVector2>() {
+        return new AbstractSet<>() {
             @Nonnull
             @Override
             public Iterator<BlockVector2> iterator() {
-                return new Iterator<BlockVector2>() {
-                    final MutableBlockVector2 mutable = new MutableBlockVector2(0, 0);
+                return new Iterator<>() {
 
                     final int bx = minX;
                     final int bz = minZ;
@@ -410,8 +409,8 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion {
 
                     @Override
                     public BlockVector2 next() {
-                        mutable.mutX(x);
-                        mutable.mutZ(z);
+                        int curX = x;
+                        int curZ = z;
                         if (++x > rtx) {
                             if (++z > rtz) {
                                 if (x > tx) {
@@ -427,7 +426,7 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion {
                                         }
                                         x = tx;
                                         hasNext = false;
-                                        return mutable;
+                                        return BlockVector2.at(curX, curZ);
                                     }
                                 } else {
                                     z = rbz;
@@ -442,7 +441,7 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion {
                                 x = rbx;
                             }
                         }
-                        return mutable;
+                        return BlockVector2.at(curX, curZ);
                     }
                 };
             }