Fix Linear pattern index incrementing above array length (#2626)

* Fix index incrementing above array length in async

* Add fork method
This commit is contained in:
Zeranny 2024-03-24 14:49:58 +00:00 committed by GitHub
parent 5e234a7003
commit c10f58ac95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,6 @@
package com.fastasyncworldedit.core.function.pattern; package com.fastasyncworldedit.core.function.pattern;
import com.fastasyncworldedit.core.queue.Filter;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.pattern.AbstractPattern; import com.sk89q.worldedit.function.pattern.AbstractPattern;
@ -7,6 +8,8 @@ import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BaseBlock;
import java.util.Arrays;
public class LinearBlockPattern extends AbstractPattern implements ResettablePattern { public class LinearBlockPattern extends AbstractPattern implements ResettablePattern {
private final Pattern[] patternsArray; private final Pattern[] patternsArray;
@ -15,7 +18,7 @@ public class LinearBlockPattern extends AbstractPattern implements ResettablePat
/** /**
* Create a new {@link Pattern} instance * Create a new {@link Pattern} instance
* *
* @param patterns array of patterns to linearly choose from based on x/z coordinates * @param patterns array of patterns to linearly choose from
*/ */
public LinearBlockPattern(Pattern[] patterns) { public LinearBlockPattern(Pattern[] patterns) {
this.patternsArray = patterns; this.patternsArray = patterns;
@ -23,18 +26,20 @@ public class LinearBlockPattern extends AbstractPattern implements ResettablePat
@Override @Override
public BaseBlock applyBlock(BlockVector3 position) { public BaseBlock applyBlock(BlockVector3 position) {
if (index == patternsArray.length) { index = (index + 1) % patternsArray.length;
index = 0; return patternsArray[index].applyBlock(position);
}
return patternsArray[index++].applyBlock(position);
} }
@Override @Override
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException { public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
if (index == patternsArray.length) { index = (index + 1) % patternsArray.length;
index = 0; return patternsArray[index].apply(extent, get, set);
} }
return patternsArray[index++].apply(extent, get, set);
@Override
public Filter fork() {
final Pattern[] forked = Arrays.stream(this.patternsArray).map(Pattern::fork).toArray(Pattern[]::new);
return new LinearBlockPattern(forked);
} }
@Override @Override