Re-implement richer mask and transform parsing (#1223)

Co-authored-by: dordsor21 <dordsor21@gmail.com>
Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com>
This commit is contained in:
dordsor21
2021-08-16 10:03:06 +01:00
committed by GitHub
parent 50137b31c4
commit d4d98708f9
109 changed files with 4068 additions and 2414 deletions

View File

@ -37,6 +37,7 @@ public abstract class FaweRegionExtent extends ResettableExtent implements IBatc
this.limit = limit;
}
@Override
public abstract boolean contains(int x, int y, int z);
public abstract boolean contains(int x, int z);

View File

@ -81,74 +81,74 @@ public class NullExtent extends FaweRegionExtent implements IBatchProcessor {
@Override
public List<Entity> getEntities() {
return Collections.emptyList();
throw reason;
}
@Nullable
@Override
public Entity createEntity(Location arg0, BaseEntity arg1) {
return null;
throw reason;
}
@Override
public BlockState getBlock(BlockVector3 position) {
return BlockTypes.AIR.getDefaultState();
throw reason;
}
@Override
public BlockState getBlock(int x, int y, int z) {
return BlockTypes.AIR.getDefaultState();
throw reason;
}
@Override
public BaseBlock getFullBlock(BlockVector3 position) {
return getBlock(position).toBaseBlock();
throw reason;
}
@Override
public BaseBlock getFullBlock(int x, int y, int z) {
return getBlock(x, y, z).toBaseBlock();
throw reason;
}
@Override
public BiomeType getBiome(BlockVector3 position) {
return BiomeTypes.THE_VOID;
throw reason;
}
@Override
public BiomeType getBiomeType(int x, int y, int z) {
return BiomeTypes.THE_VOID;
throw reason;
}
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block) throws WorldEditException {
return false;
throw reason;
}
@Override
public <T extends BlockStateHolder<T>> boolean setBlock(int x, int y, int z, T block)
throws WorldEditException {
return false;
throw reason;
}
@Override
public ResettableExtent setExtent(Extent extent) {
return this;
throw reason;
}
@Override
public boolean setTile(int x, int y, int z, CompoundTag tile) throws WorldEditException {
return false;
throw reason;
}
@Override
public boolean setBiome(BlockVector3 position, BiomeType biome) {
return false;
throw reason;
}
@Override
public boolean setBiome(int x, int y, int z, BiomeType biome) {
return false;
throw reason;
}
@Override
@ -371,7 +371,7 @@ public class NullExtent extends FaweRegionExtent implements IBatchProcessor {
@Override
public ProcessorScope getScope() {
return ProcessorScope.ADDING_BLOCKS;
throw reason;
}
}

View File

@ -1,49 +0,0 @@
package com.fastasyncworldedit.core.extent;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockStateHolder;
public class OffsetExtent extends ResettableExtent {
private final int dx;
private final int dy;
private final int dz;
public OffsetExtent(Extent parent, int dx, int dy, int dz) {
super(parent);
this.dx = dx;
this.dy = dy;
this.dz = dz;
}
@Override
public boolean setBiome(BlockVector3 position, BiomeType biome) {
return getExtent()
.setBiome(position.getBlockX() + dx, position.getBlockY() + dy, position.getBlockZ() + dz,
biome
);
}
@Override
public boolean setBiome(int x, int y, int z, BiomeType biome) {
return getExtent().setBiome(x + dx, y + dy, z + dz, biome);
}
@Override
public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 location, T block)
throws WorldEditException {
return getExtent().setBlock(location.getBlockX() + dx, location.getBlockY() + dy,
location.getBlockZ() + dz, block
);
}
@Override
public <T extends BlockStateHolder<T>> boolean setBlock(int x, int y, int z, T block)
throws WorldEditException {
return getExtent().setBlock(x + dx, y + dy, z + dz, block);
}
}

View File

@ -2,6 +2,7 @@ package com.fastasyncworldedit.core.extent;
import com.fastasyncworldedit.core.util.ExtentTraverser;
import com.fastasyncworldedit.core.util.ReflectionUtils;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
@ -38,7 +39,7 @@ public class ResettableExtent extends AbstractDelegateExtent implements Serializ
&& next instanceof ResettableExtent) {
((ResettableExtent) next).setExtent(extent);
} else {
new ExtentTraverser(this).setNext(new AbstractDelegateExtent(extent));
new ExtentTraverser<Extent>(this).setNext(extent);
}
return this;
}

View File

@ -8,6 +8,8 @@ import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
@ -136,7 +138,14 @@ public class PNGWriter implements ClipboardWriter {
poly3X[3] = (int) cpx;
poly3Y[3] = (int) (cpy + dpxi[1]);
Color colorTop = new Color(tu.getColor(block.getBlockType()));
BlockType type = block.getBlockType();
int color;
if (type == BlockTypes.GRASS_BLOCK) {
color = tu.getColor(clipboard.getBiome(mutable));
} else {
color = tu.getColor(type);
}
Color colorTop = new Color(color);
g2.setColor(colorTop);
if (fill) {

View File

@ -8,12 +8,18 @@ public class Linear3DTransform extends SelectTransform {
private final ResettableExtent[] extentsArray;
/**
* New instance
*
* @param extents list of extents to choose from
*/
public Linear3DTransform(ResettableExtent[] extents) {
this.extentsArray = extents;
}
@Override
public ResettableExtent setExtent(Extent extent) {
super.setExtent(extent);
for (ResettableExtent cur : extentsArray) {
cur.setExtent(extent);
}

View File

@ -9,12 +9,18 @@ public class LinearTransform extends SelectTransform {
private final ResettableExtent[] extentsArray;
private int index;
/**
* New instance
*
* @param extents list of extents to choose from
*/
public LinearTransform(ResettableExtent[] extents) {
this.extentsArray = extents;
}
@Override
public ResettableExtent setExtent(Extent extent) {
super.setExtent(extent);
for (ResettableExtent cur : extentsArray) {
cur.setExtent(extent);
}
@ -26,7 +32,7 @@ public class LinearTransform extends SelectTransform {
if (index == extentsArray.length) {
index = 0;
}
return extentsArray[index];
return extentsArray[index++];
}
@Override

View File

@ -17,6 +17,11 @@ public class MultiTransform extends RandomTransform {
private ResettableExtent[] extents;
/**
* New instance
*
* @param extents list of extents to set blocks to
*/
public MultiTransform(Collection<ResettableExtent> extents) {
for (ResettableExtent extent : extents) {
add(extent, 1);
@ -65,6 +70,16 @@ public class MultiTransform extends RandomTransform {
return result;
}
@Override
public boolean setBiome(int x, int y, int z, BiomeType biome) {
// don't use streams for each block place, it'd be incredibly slow
boolean result = false;
for (AbstractDelegateExtent extent : extents) {
result |= extent.setBiome(x, y, z, biome);
}
return result;
}
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity entity) {

View File

@ -0,0 +1,77 @@
package com.fastasyncworldedit.core.extent.transform;
import com.fastasyncworldedit.core.extent.ResettableExtent;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockStateHolder;
public class OffsetTransform extends ResettableExtent {
private final int dx;
private final int dy;
private final int dz;
/**
* New instance
*
* @param parent extent to set to
* @param dx offset x
* @param dy offset y
* @param dz offset z
*/
public OffsetTransform(Extent parent, int dx, int dy, int dz) {
super(parent);
this.dx = dx;
this.dy = dy;
this.dz = dz;
}
@Override
public boolean setBiome(BlockVector3 location, BiomeType biome) {
int x = location.getX() + dx;
int y = location.getX() + dy;
int z = location.getX() + dz;
if (!getExtent().contains(x, y, z)) {
return false;
}
return getExtent().setBiome(x, y, z, biome);
}
@Override
public boolean setBiome(int x, int y, int z, BiomeType biome) {
x += dx;
y += dy;
z += dz;
if (!getExtent().contains(x, y, z)) {
return false;
}
return getExtent().setBiome(x, y, z, biome);
}
@Override
public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 location, T block)
throws WorldEditException {
int x = location.getX() + dx;
int y = location.getX() + dy;
int z = location.getX() + dz;
if (!getExtent().contains(x, y, z)) {
return false;
}
return getExtent().setBlock(x, y, z, block);
}
@Override
public <T extends BlockStateHolder<T>> boolean setBlock(int x, int y, int z, T block)
throws WorldEditException {
x += dx;
y += dy;
z += dz;
if (!getExtent().contains(x, y, z)) {
return false;
}
return getExtent().setBlock(x, y, z, block);
}
}

View File

@ -1,7 +1,6 @@
package com.fastasyncworldedit.core.extent.transform;
import com.fastasyncworldedit.core.extent.ResettableExtent;
import com.fastasyncworldedit.core.math.MutableBlockVector3;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.pattern.Pattern;
@ -12,6 +11,12 @@ public class PatternTransform extends ResettableExtent {
private final Pattern pattern;
/**
* New instance
*
* @param parent extent to set to
* @param pattern pattern to apply
*/
public PatternTransform(Extent parent, Pattern pattern) {
super(parent);
this.pattern = pattern;

View File

@ -1,5 +1,6 @@
package com.fastasyncworldedit.core.extent;
package com.fastasyncworldedit.core.extent.transform;
import com.fastasyncworldedit.core.extent.ResettableExtent;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
@ -15,6 +16,14 @@ public class RandomOffsetTransform extends ResettableExtent {
private final int dz;
private transient SplittableRandom random;
/**
* New instance
*
* @param parent extent to set to
* @param dx range of x values to choose from (0 -> x)
* @param dy range of y values to choose from (0 -> y)
* @param dz range of z values to choose from (0 -> z)
*/
public RandomOffsetTransform(Extent parent, int dx, int dy, int dz) {
super(parent);
this.dx = dx + 1;
@ -24,10 +33,24 @@ public class RandomOffsetTransform extends ResettableExtent {
}
@Override
public boolean setBiome(BlockVector3 pos, BiomeType biome) {
int x = pos.getBlockX() + random.nextInt(1 + (dx << 1)) - dx;
int y = pos.getBlockY() + random.nextInt(1 + (dy << 1)) - dy;
int z = pos.getBlockZ() + random.nextInt(1 + (dz << 1)) - dz;
public boolean setBiome(BlockVector3 position, BiomeType biome) {
int x = position.getBlockX() + random.nextInt(1 + (dx << 1)) - dx;
int y = position.getBlockY() + random.nextInt(1 + (dy << 1)) - dy;
int z = position.getBlockZ() + random.nextInt(1 + (dz << 1)) - dz;
if (!getExtent().contains(x, y, z)) {
return false;
}
return getExtent().setBiome(x, y, z, biome);
}
@Override
public boolean setBiome(int x, int y, int z, BiomeType biome) {
x = x + random.nextInt(1 + (dx << 1)) - dx;
y = y + random.nextInt(1 + (dy << 1)) - dy;
z = z + random.nextInt(1 + (dz << 1)) - dz;
if (!getExtent().contains(x, y, z)) {
return false;
}
return getExtent().setBiome(x, y, z, biome);
}
@ -37,6 +60,9 @@ public class RandomOffsetTransform extends ResettableExtent {
int x = pos.getBlockX() + random.nextInt(1 + (dx << 1)) - dx;
int y = pos.getBlockY() + random.nextInt(1 + (dy << 1)) - dy;
int z = pos.getBlockZ() + random.nextInt(1 + (dz << 1)) - dz;
if (!getExtent().contains(x, y, z)) {
return false;
}
return getExtent().setBlock(x, y, z, block);
}
@ -46,6 +72,9 @@ public class RandomOffsetTransform extends ResettableExtent {
x = x + random.nextInt(1 + (dx << 1)) - dx;
y = y + random.nextInt(1 + (dy << 1)) - dy;
z = z + random.nextInt(1 + (dz << 1)) - dz;
if (!getExtent().contains(x, y, z)) {
return false;
}
return getExtent().setBlock(x, y, z, block);
}

View File

@ -29,6 +29,11 @@ public class RandomTransform extends SelectTransform {
this(new TrueRandom());
}
/**
* New instance
*
* @param random {@link SimpleRandom} used to choose between transforms, given weights
*/
public RandomTransform(SimpleRandom random) {
this.random = random;
}
@ -49,6 +54,7 @@ public class RandomTransform extends SelectTransform {
collection = RandomCollection.of(weights, random);
extents = new LinkedHashSet<>(weights.keySet());
}
super.setExtent(extent);
for (ResettableExtent current : extents) {
current.setExtent(extent);
}

View File

@ -2,6 +2,7 @@ package com.fastasyncworldedit.core.extent.transform;
import com.fastasyncworldedit.core.extent.ResettableExtent;
import com.fastasyncworldedit.core.math.MutableBlockVector3;
import com.fastasyncworldedit.core.math.MutableVector3;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
@ -18,61 +19,76 @@ public class ScaleTransform extends ResettableExtent {
private final double dx;
private final double dy;
private final double dz;
private transient MutableBlockVector3 mutable = new MutableBlockVector3();
private transient MutableVector3 mutable = new MutableVector3();
private transient int minY;
private transient int maxy;
private transient BlockVector3 min;
/**
* New instance
*
* @param parent extent to set to
* @param dx x axis scaling
* @param dy y axis scaling
* @param dz z axis scaling
*/
public ScaleTransform(Extent parent, double dx, double dy, double dz) {
super(parent);
this.dx = dx;
this.dy = dy;
this.dz = dz;
this.maxy = parent.getMaximumPoint().getBlockY();
this.minY = parent.getMinY();
this.maxy = parent.getMaxY();
}
@Override
public ResettableExtent setExtent(Extent extent) {
min = null;
maxy = extent.getMaximumPoint().getBlockY();
mutable = new MutableBlockVector3();
mutable = new MutableVector3();
this.minY = extent.getMinY();
this.maxy = extent.getMaxY();
return super.setExtent(extent);
}
private void getPos(BlockVector3 pos) {
private MutableVector3 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 new MutableVector3(mutable);
}
private void getPos(int x, int y, int z) {
private MutableVector3 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 new MutableVector3(mutable);
}
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block)
throws WorldEditException {
boolean result = false;
getPos(location);
double sx = mutable.getX();
double sy = mutable.getY();
double sz = mutable.getZ();
MutableVector3 vector3 = getPos(location);
MutableBlockVector3 pos = new MutableBlockVector3();
double sx = vector3.getX();
double sy = vector3.getY();
double sz = vector3.getZ();
double ex = sx + dx;
double ey = Math.min(maxy, sy + dy);
double ey = Math.max(minY, Math.min(maxy, sy + dy));
double ez = sz + 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);
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)) {
if (!getExtent().contains(pos)) {
continue;
}
result |= super.setBlock(pos, block);
}
}
}
@ -82,17 +98,21 @@ public class ScaleTransform extends ResettableExtent {
@Override
public boolean setBiome(BlockVector3 position, BiomeType biome) {
boolean result = false;
getPos(position);
double sx = mutable.getX();
double sy = mutable.getY();
double sz = mutable.getZ();
MutableVector3 vector3 = getPos(position);
MutableBlockVector3 pos = new MutableBlockVector3();
double sx = vector3.getX();
double sy = vector3.getY();
double sz = vector3.getZ();
double ex = sx + dx;
double ey = Math.min(maxy, sy + dy);
double ey = Math.max(minY, Math.min(maxy, sy + dy));
double ez = sz + 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.setBiome(mutable, biome);
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)) {
if (!getExtent().contains(pos)) {
continue;
}
result |= super.setBiome(pos, biome);
}
}
}
@ -103,17 +123,45 @@ 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;
getPos(x1, y1, z1);
double sx = mutable.getX();
double sy = mutable.getY();
double sz = mutable.getZ();
double ex = mutable.getX() + dx;
MutableVector3 vector3 = getPos(x1, y1, z1);
MutableBlockVector3 pos = new MutableBlockVector3();
double sx = vector3.getX();
double sy = vector3.getY();
double sz = vector3.getZ();
double ex = vector3.getX() + dx;
double ey = Math.min(maxy, sy + dy);
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);
double ez = vector3.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)) {
if (!getExtent().contains(pos)) {
continue;
}
result |= super.setBlock(pos, block);
}
}
}
return result;
}
@Override
public boolean setBiome(int x1, int y1, int z1, BiomeType biome) {
boolean result = false;
MutableVector3 vector3 = getPos(x1, y1, z1);
MutableBlockVector3 pos = new MutableBlockVector3();
double sx = vector3.getX();
double sy = vector3.getY();
double sz = vector3.getZ();
double ex = sx + dx;
double ey = Math.max(minY, 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)) {
if (!getExtent().contains(pos)) {
continue;
}
result |= super.setBiome(pos, biome);
}
}
}
@ -123,11 +171,13 @@ 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(),
mutable.toVector3(),
getPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()),
location.getYaw(), location.getPitch()
);
if (!getExtent().contains(newLoc.toBlockPoint())) {
return null;
}
return super.createEntity(newLoc, entity);
}

View File

@ -57,4 +57,9 @@ public abstract class SelectTransform extends ResettableExtent {
return getExtent(position).setBiome(position, biome);
}
@Override
public boolean setBiome(int x, int y, int z, BiomeType biome) {
return getExtent(x, y, z).setBiome(x, y, z, biome);
}
}