Some cleanup of mutables

This commit is contained in:
dordsor21 2021-08-11 14:00:12 +01:00
parent 8928556c1d
commit 6f5430a940
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
14 changed files with 70 additions and 69 deletions

View File

@ -1,6 +1,5 @@
package com.fastasyncworldedit.core.extent;
import com.fastasyncworldedit.core.math.MutableBlockVector3;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent;
@ -15,7 +14,6 @@ public class BlockTranslateExtent extends AbstractDelegateExtent {
private final int dx;
private final int dy;
private final int dz;
private final MutableBlockVector3 mutable = new MutableBlockVector3();
public BlockTranslateExtent(Extent extent, int dx, int dy, int dz) {
super(extent);
@ -26,15 +24,12 @@ public class BlockTranslateExtent extends AbstractDelegateExtent {
@Override
public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 location, T block) throws WorldEditException {
mutable.mutX(location.getX() + dx);
mutable.mutY(location.getY() + dy);
mutable.mutZ(location.getZ() + dz);
return getExtent().setBlock(mutable, block);
return getExtent().setBlock(location.getX(), location.getY(), location.getZ(), block);
}
@Override
public <T extends BlockStateHolder<T>> boolean setBlock(int x, int y, int z, T block) throws WorldEditException {
return this.setBlock(BlockVector3.at(x, y, z), block);
return getExtent().setBlock(x, y, z, block);
}
@Override

View File

@ -42,7 +42,7 @@ public class SourceMaskExtent extends TemporalExtent {
@Override
public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 location, T block) throws WorldEditException {
set(location.getBlockX(), location.getBlockY(), location.getBlockZ(), block);
return mask.test(location) && super.setBlock(location, block);
return mask.test(location) && super.setBlock(location.getX(), location.getY(), location.getZ(), block);
}
@Override

View File

@ -167,7 +167,6 @@ public class MinecraftStructure implements ClipboardReader, ClipboardWriter {
}
Map<String, Object> structure = FaweCache.IMP.asMap("version", 1, "author", owner);
// ignored: version / owner
MutableBlockVector3 mutable = new MutableBlockVector3(0, 0, 0);
Int2ObjectArrayMap<Integer> indexes = new Int2ObjectArrayMap<>();
// Size
structure.put("size", Arrays.asList(width, height, length));

View File

@ -68,6 +68,7 @@ public class ScalableHeightMap implements HeightMap {
int maxY = clipboard.getMaximumPoint().getBlockY();
int clipHeight = maxY - minY + 1;
HashSet<IntPair> visited = new HashSet<>();
MutableBlockVector3 bv = new MutableBlockVector3();
for (BlockVector3 pos : clipboard.getRegion()) {
IntPair pair = new IntPair(pos.getBlockX(), pos.getBlockZ());
if (visited.contains(pair)) {
@ -77,7 +78,7 @@ public class ScalableHeightMap implements HeightMap {
int xx = pos.getBlockX();
int zz = pos.getBlockZ();
int highestY = minY;
MutableBlockVector3 bv = new MutableBlockVector3(pos);
bv.setComponents(pos);
for (int y = minY; y <= maxY; y++) {
bv.mutY(y);
BlockState block = clipboard.getBlock(bv);

View File

@ -807,6 +807,7 @@ public class NMSRelighter implements Relighter {
if (current != 0 && current < currentLight) {
iChunk.setBlockLight(x, y, z, 0);
if (current > 1) {
mutableBlockPos.setComponents(x, y, z);
if (!visited.containsKey(mutableBlockPos)) {
MutableBlockVector3 index = new MutableBlockVector3(x, y, z);
visited.put(index, present);

View File

@ -39,24 +39,22 @@ public class ScaleTransform extends ResettableExtent {
return super.setExtent(extent);
}
private BlockVector3 getPos(BlockVector3 pos) {
private void getPos(BlockVector3 pos) {
if (min == null) {
min = pos;
}
mutable.mutX(min.getX() + (pos.getX() - min.getX()) * dx);
mutable.mutY(min.getY() + (pos.getY() - min.getY()) * dy);
mutable.mutZ(min.getZ() + (pos.getZ() - min.getZ()) * dz);
return mutable;
}
private BlockVector3 getPos(int x, int y, int z) {
private void getPos(int x, int y, int z) {
if (min == null) {
min = BlockVector3.at(x, y, z);
}
mutable.mutX(min.getX() + (x - min.getX()) * dx);
mutable.mutY(min.getY() + (y - min.getY()) * dy);
mutable.mutZ(min.getZ() + (z - min.getZ()) * dz);
return mutable;
}
@ -64,17 +62,17 @@ public class ScaleTransform extends ResettableExtent {
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block)
throws WorldEditException {
boolean result = false;
MutableBlockVector3 pos = new MutableBlockVector3(getPos(location));
double sx = pos.getX();
double sy = pos.getY();
double sz = pos.getZ();
getPos(location);
double sx = mutable.getX();
double sy = mutable.getY();
double sz = mutable.getZ();
double ex = sx + dx;
double ey = Math.min(maxy, sy + dy);
double ez = sz + dz;
for (pos.mutY(sy); pos.getY() < ey; pos.mutY(pos.getY() + 1)) {
for (pos.mutZ(sz); pos.getZ() < ez; pos.mutZ(pos.getZ() + 1)) {
for (pos.mutX(sx); pos.getX() < ex; pos.mutX(pos.getX() + 1)) {
result |= super.setBlock(pos, block);
for (mutable.mutY(sy); mutable.getY() < ey; mutable.mutY(mutable.getY() + 1)) {
for (mutable.mutZ(sz); mutable.getZ() < ez; mutable.mutZ(mutable.getZ() + 1)) {
for (mutable.mutX(sx); mutable.getX() < ex; mutable.mutX(mutable.getX() + 1)) {
result |= super.setBlock(mutable, block);
}
}
}
@ -84,17 +82,17 @@ public class ScaleTransform extends ResettableExtent {
@Override
public boolean setBiome(BlockVector3 position, BiomeType biome) {
boolean result = false;
MutableBlockVector3 pos = new MutableBlockVector3(getPos(position));
double sx = pos.getX();
double sy = pos.getY();
double sz = pos.getZ();
getPos(position);
double sx = mutable.getX();
double sy = mutable.getY();
double sz = mutable.getZ();
double ex = sx + dx;
double ey = Math.min(maxy, sy + dy);
double ez = sz + dz;
for (pos.mutY(sy); pos.getY() < ey; pos.mutY(pos.getY() + 1)) {
for (pos.mutZ(sz); pos.getZ() < ez; pos.mutZ(pos.getZ() + 1)) {
for (pos.mutX(sx); pos.getX() < ex; pos.mutX(pos.getX() + 1)) {
result |= super.setBiome(pos, biome);
for (mutable.mutY(sy); mutable.getY() < ey; mutable.mutY(mutable.getY() + 1)) {
for (mutable.mutZ(sz); mutable.getZ() < ez; mutable.mutZ(mutable.getZ() + 1)) {
for (mutable.mutX(sx); mutable.getX() < ex; mutable.mutX(mutable.getX() + 1)) {
result |= super.setBiome(mutable, biome);
}
}
}
@ -105,17 +103,17 @@ public class ScaleTransform extends ResettableExtent {
public <B extends BlockStateHolder<B>> boolean setBlock(int x1, int y1, int z1, B block)
throws WorldEditException {
boolean result = false;
MutableBlockVector3 pos = new MutableBlockVector3(getPos(x1, y1, z1));
double sx = pos.getX();
double sy = pos.getY();
double sz = pos.getZ();
double ex = pos.getX() + dx;
getPos(x1, y1, z1);
double sx = mutable.getX();
double sy = mutable.getY();
double sz = mutable.getZ();
double ex = mutable.getX() + dx;
double ey = Math.min(maxy, sy + dy);
double ez = pos.getZ() + dz;
for (pos.mutY(sy); pos.getY() < ey; pos.mutY(pos.getY() + 1)) {
for (pos.mutZ(sz); pos.getZ() < ez; pos.mutZ(pos.getZ() + 1)) {
for (pos.mutX(sx); pos.getX() < ex; pos.mutX(pos.getX() + 1)) {
result |= super.setBlock(pos, block);
double ez = mutable.getZ() + dz;
for (mutable.mutY(sy); mutable.getY() < ey; mutable.mutY(mutable.getY() + 1)) {
for (mutable.mutZ(sz); mutable.getZ() < ez; mutable.mutZ(mutable.getZ() + 1)) {
for (mutable.mutX(sx); mutable.getX() < ex; mutable.mutX(mutable.getX() + 1)) {
result |= super.setBlock(mutable, block);
}
}
}
@ -125,8 +123,9 @@ public class ScaleTransform extends ResettableExtent {
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity entity) {
getPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
Location newLoc = new Location(location.getExtent(),
getPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()).toVector3(),
mutable.toVector3(),
location.getYaw(), location.getPitch()
);
return super.createEntity(newLoc, entity);

View File

@ -1,6 +1,5 @@
package com.fastasyncworldedit.core.function.mask;
import com.fastasyncworldedit.core.math.MutableBlockVector3;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.SolidBlockMask;
@ -21,8 +20,6 @@ public class AngleMask extends SolidBlockMask implements ResettableMask {
protected final int maxY;
protected final int distance;
protected transient MutableBlockVector3 mutable = new MutableBlockVector3();
public AngleMask(Extent extent, double min, double max, boolean overlay, int distance) {
super(extent);
this.mask = new CachedMask(new SolidBlockMask(extent));
@ -36,7 +33,6 @@ public class AngleMask extends SolidBlockMask implements ResettableMask {
@Override
public void reset() {
mutable = new MutableBlockVector3();
cacheBotX = Integer.MIN_VALUE;
cacheBotZ = Integer.MIN_VALUE;
lastX = Integer.MIN_VALUE;

View File

@ -1,6 +1,5 @@
package com.fastasyncworldedit.core.function.pattern;
import com.fastasyncworldedit.core.math.MutableBlockVector3;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
@ -20,10 +19,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
public class RandomFullClipboardPattern extends AbstractPattern {
private final Extent extent;
private final MutableBlockVector3 mutable = new MutableBlockVector3();
private final List<ClipboardHolder> clipboards;
private final boolean randomRotate;
private final boolean randomFlip;
private final Vector3 flipVector = Vector3.at(1, 0, 0).multiply(-2).add(1, 1, 1);
public RandomFullClipboardPattern(Extent extent, List<ClipboardHolder> clipboards, boolean randomRotate, boolean randomFlip) {
checkNotNull(clipboards);
@ -42,7 +41,7 @@ public class RandomFullClipboardPattern extends AbstractPattern {
holder.setTransform(new AffineTransform().rotateY(ThreadLocalRandom.current().nextInt(4) * 90));
}
if (randomFlip) {
transform = transform.scale(Vector3.at(1, 0, 0).multiply(-2).add(1, 1, 1));
transform = transform.scale(flipVector);
}
if (!transform.isIdentity()) {
holder.setTransform(transform);

View File

@ -36,9 +36,9 @@ public class SurfaceRandomOffsetPattern extends AbstractPattern {
private BlockVector3 travel(BlockVector3 pos) {
cur.setComponents(pos);
MutableBlockVector3 next;
for (int move = 0; move < moves; move++) {
int index = 0;
MutableBlockVector3 next;
for (int i = 0; i < allowed.length; i++) {
next = buffer[i];
BlockVector3 dir = BreadthFirstSearch.DIAGONAL_DIRECTIONS[i];

View File

@ -26,7 +26,7 @@ public class BlockVector3ChunkMap<T> implements IAdaptedMap<BlockVector3, T, Sho
int x = MathMan.untripleBlockCoordX(key);
int y = MathMan.untripleBlockCoordY(key);
int z = MathMan.untripleBlockCoordZ(key);
return MutableBlockVector3.get(x, y, z);
return BlockVector3.at(x, y, z);
}
@Override

View File

@ -309,7 +309,6 @@ public class LocalBlockVectorSet implements Set<BlockVector3> {
public void forEach(BlockVectorSetVisitor visitor) {
int size = size();
int index = -1;
BlockVector3 mVec = MutableBlockVector3.get(0, 0, 0);
for (int i = 0; i < size; i++) {
index = set.nextSetBit(index + 1);
int b1 = (index & 0xFF);

View File

@ -2079,7 +2079,6 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
int px = mutableBlockVector3.getBlockX();
int py = mutableBlockVector3.getBlockY();
int pz = mutableBlockVector3.getBlockZ();
MutableBlockVector3 mutable = new MutableBlockVector3();
final int ceilRadiusX = (int) Math.ceil(radiusX);
final int ceilRadiusZ = (int) Math.ceil(radiusZ);
@ -2122,10 +2121,10 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
}
for (int y = 0; y < height; ++y) {
this.setBlock(mutable.setComponents(px + x, py + y, pz + z), block);
this.setBlock(mutable.setComponents(px - x, py + y, pz + z), block);
this.setBlock(mutable.setComponents(px + x, py + y, pz - z), block);
this.setBlock(mutable.setComponents(px - x, py + y, pz - z), block);
this.setBlock(mutableBlockVector3.setComponents(px + x, py + y, pz + z), block);
this.setBlock(mutableBlockVector3.setComponents(px - x, py + y, pz + z), block);
this.setBlock(mutableBlockVector3.setComponents(px + x, py + y, pz - z), block);
this.setBlock(mutableBlockVector3.setComponents(px - x, py + y, pz - z), block);
}
}
}
@ -2158,10 +2157,10 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
for (int y = 0; y < height; ++y) {
//FAWE start - mutable
this.setBlock(mutable.setComponents(px + x, py + y, pz + z), block);
this.setBlock(mutable.setComponents(px - x, py + y, pz + z), block);
this.setBlock(mutable.setComponents(px + x, py + y, pz - z), block);
this.setBlock(mutable.setComponents(px - x, py + y, pz - z), block);
this.setBlock(mutableBlockVector3.setComponents(px + x, py + y, pz + z), block);
this.setBlock(mutableBlockVector3.setComponents(px - x, py + y, pz + z), block);
this.setBlock(mutableBlockVector3.setComponents(px + x, py + y, pz - z), block);
this.setBlock(mutableBlockVector3.setComponents(px - x, py + y, pz - z), block);
//FAWE end
}
}

View File

@ -213,10 +213,11 @@ public interface Extent extends InputExtent, OutputExtent {
}
default int getHighestTerrainBlock(final int x, final int z, int minY, int maxY, Mask filter) {
maxY = Math.min(maxY, Math.max(0, maxY));
minY = Math.max(0, minY);
maxY = Math.min(maxY, getMaxY());
minY = Math.max(getMinY(), minY);
BlockVector3 pos = MutableBlockVector3.at(x, minY, z);
for (int y = maxY; y >= minY; --y) {
if (filter.test(MutableBlockVector3.get(x, y, z))) {
if (filter.test(pos.mutY(y))) {
return y;
}
}
@ -286,28 +287,29 @@ public interface Extent extends InputExtent, OutputExtent {
int clearanceAbove = maxY - y;
int clearanceBelow = y - minY;
int clearance = Math.min(clearanceAbove, clearanceBelow);
boolean state = !mask.test(MutableBlockVector3.get(x, y, z));
BlockVector3 pos = MutableBlockVector3.get(x, y, z);
boolean state = !mask.test(pos);
int offset = state ? 0 : 1;
for (int d = 0; d <= clearance; d++) {
int y1 = y + d;
if (mask.test(MutableBlockVector3.get(x, y1, z)) != state) {
if (mask.test(pos.mutY(y1)) != state) {
return y1 - offset;
}
int y2 = y - d;
if (mask.test(MutableBlockVector3.get(x, y2, z)) != state) {
if (mask.test(pos.mutY(y2)) != state) {
return y2 + offset;
}
}
if (clearanceAbove != clearanceBelow) {
if (clearanceAbove < clearanceBelow) {
for (int layer = y - clearance - 1; layer >= minY; layer--) {
if (mask.test(MutableBlockVector3.get(x, layer, z)) != state) {
if (mask.test(pos.mutY(layer)) != state) {
return layer + offset;
}
}
} else {
for (int layer = y + clearance + 1; layer <= maxY; layer++) {
if (mask.test(MutableBlockVector3.get(x, layer, z)) != state) {
if (mask.test(pos.mutY(layer)) != state) {
return layer - offset;
}
}

View File

@ -34,6 +34,9 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
public class NoiseFilter extends AbstractMask {
//FAWE start - mutable
private MutableVector3 mutable;
//FAWE end
private NoiseGenerator noiseGenerator;
private double density;
@ -65,6 +68,9 @@ public class NoiseFilter extends AbstractMask {
public void setNoiseGenerator(NoiseGenerator noiseGenerator) {
checkNotNull(noiseGenerator);
this.noiseGenerator = noiseGenerator;
//FAWE start - mutable
this.mutable = new MutableVector3();
//FAWE end
}
/**
@ -83,11 +89,16 @@ public class NoiseFilter extends AbstractMask {
checkArgument(density >= 0, "density must be >= 0");
checkArgument(density <= 1, "density must be <= 1");
this.density = density;
//FAWE start - mutable
this.mutable = new MutableVector3();
//FAWE end
}
@Override
public boolean test(BlockVector3 vector) {
return noiseGenerator.noise(MutableVector3.get(vector.getX(), vector.getY(), vector.getZ())) <= density;
//FAWE start - mutable
return noiseGenerator.noise(mutable.setComponents(vector.getX(), vector.getZ(), vector.getZ())) <= density;
//FAWE end
}
@Nullable