From f65c4743bda5d9cea218c7f7edbf324042a9dcf0 Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 20 Jul 2023 16:56:11 +0100 Subject: [PATCH] fix: make IdMask thread-safe (#2361) --- .../core/function/mask/IdMask.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/mask/IdMask.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/mask/IdMask.java index e76c78a890..49c90183ae 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/mask/IdMask.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/mask/IdMask.java @@ -5,9 +5,11 @@ import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.math.BlockVector3; +import java.util.concurrent.atomic.AtomicInteger; + public class IdMask extends AbstractExtentMask implements ResettableMask { - private transient int id = -1; + private final AtomicInteger id = new AtomicInteger(-1); public IdMask(Extent extent) { super(extent); @@ -15,12 +17,9 @@ public IdMask(Extent extent) { @Override public boolean test(Extent extent, BlockVector3 vector) { - if (id != -1) { - return extent.getBlock(vector).getInternalBlockTypeId() == id; - } else { - id = extent.getBlock(vector).getInternalBlockTypeId(); - return true; - } + int blockID = extent.getBlock(vector).getInternalBlockTypeId(); + int testId = id.compareAndExchange(-1, blockID); + return blockID == testId || testId == -1; } @Override @@ -30,12 +29,12 @@ public boolean test(BlockVector3 vector) { @Override public void reset() { - this.id = -1; + this.id.set(-1); } @Override public Mask copy() { - return new IdMask(getExtent()); + return this; } @Override