Skip to content

Commit

Permalink
fix: make IdMask thread-safe (#2361)
Browse files Browse the repository at this point in the history
  • Loading branch information
dordsor21 authored Jul 20, 2023
1 parent cdd546e commit f65c474
Showing 1 changed file with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
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);
}

@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
Expand All @@ -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
Expand Down

0 comments on commit f65c474

Please sign in to comment.