From d85ad6e116c4dfe6fd35d9dc40848e55ab352380 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Fri, 17 Aug 2018 01:54:13 +1000 Subject: [PATCH] Implement CuboidClipboard --- .../fawe/object/schematic/Schematic.java | 8 +- .../com/sk89q/worldedit/CuboidClipboard.java | 1135 +++++++---------- .../com/sk89q/worldedit/extent/Extent.java | 68 +- .../extent/clipboard/BlockArrayClipboard.java | 4 + 4 files changed, 535 insertions(+), 680 deletions(-) diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/schematic/Schematic.java b/worldedit-core/src/main/java/com/boydti/fawe/object/schematic/Schematic.java index 3199ee960..1cb37536a 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/schematic/Schematic.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/schematic/Schematic.java @@ -37,6 +37,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.function.Consumer; import javax.annotation.Nullable; @@ -107,6 +108,10 @@ public class Schematic { } } + public EditSession paste(World world, Vector to, boolean allowUndo, boolean pasteAir, @Nullable Transform transform) { + return paste(world, to, allowUndo, pasteAir, true, transform); + } + /** * Paste this schematic in a world * @@ -117,7 +122,7 @@ public class Schematic { * @param transform * @return */ - public EditSession paste(World world, Vector to, boolean allowUndo, boolean pasteAir, @Nullable Transform transform) { + public EditSession paste(World world, Vector to, boolean allowUndo, boolean pasteAir, boolean copyEntities, @Nullable Transform transform) { checkNotNull(world); checkNotNull(to); Region region = clipboard.getRegion(); @@ -145,6 +150,7 @@ public class Schematic { if (transform != null && !transform.isIdentity()) { copy.setTransform(transform); } + copy.setCopyEntities(copyEntities); if (sourceMask != null) { new MaskTraverser(sourceMask).reset(extent); copy.setSourceMask(sourceMask); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/CuboidClipboard.java b/worldedit-core/src/main/java/com/sk89q/worldedit/CuboidClipboard.java index 8b39a3712..8cf3d7d6d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/CuboidClipboard.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/CuboidClipboard.java @@ -19,11 +19,37 @@ package com.sk89q.worldedit; +import com.boydti.fawe.object.IntegerTrio; +import com.boydti.fawe.object.schematic.Schematic; +import com.boydti.fawe.util.MainUtil; +import com.sk89q.jnbt.CompoundTag; +import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.command.ClipboardCommands; import com.sk89q.worldedit.command.SchematicCommands; import com.sk89q.worldedit.extent.Extent; +import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard; +import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat; import com.sk89q.worldedit.function.operation.ForwardExtentCopy; +import com.sk89q.worldedit.math.transform.AffineTransform; +import com.sk89q.worldedit.math.transform.Transform; +import com.sk89q.worldedit.regions.CuboidRegion; +import com.sk89q.worldedit.regions.Region; +import com.sk89q.worldedit.schematic.SchematicFormat; +import com.sk89q.worldedit.util.Countable; +import com.sk89q.worldedit.util.Direction; +import com.sk89q.worldedit.world.DataException; +import com.sk89q.worldedit.world.block.BlockState; +import com.sk89q.worldedit.world.block.BlockStateHolder; +import com.sk89q.worldedit.world.block.BlockTypes; +import com.sk89q.worldedit.world.registry.LegacyMapper; + +import java.io.File; +import java.io.IOException; +import java.util.*; +import java.util.function.Consumer; + +import static com.google.common.base.Preconditions.checkNotNull; /** * The clipboard remembers the state of a cuboid region. @@ -43,680 +69,437 @@ import com.sk89q.worldedit.function.operation.ForwardExtentCopy; */ @Deprecated public class CuboidClipboard { -// -// /** -// * An enum of possible flip directions. -// */ -// public enum FlipDirection { -// NORTH_SOUTH, -// WEST_EAST, -// UP_DOWN -// } -// -// public int[][] states; -// public HashMap nbtMap; -// public List entities = new ArrayList<>(); -// -// public Vector size; -// private int dx; -// private int dxz; -// private Vector offset; -// private Vector origin; -// -// /** -// * Constructs the clipboard. -// * -// * @param size the dimensions of the clipboard (should be at least 1 on every dimension) -// */ -// public CuboidClipboard(Vector size) { -// checkNotNull(size); -// MainUtil.warnDeprecated(BlockArrayClipboard.class, ClipboardFormat.class); -// origin = new Vector(); -// offset = new Vector(); -// this.size = size; -// this.dx = size.getBlockX(); -// this.dxz = dx * size.getBlockZ(); -// states = new int[dx * size.getBlockZ() * ((size.getBlockY() + 15) >> 4)][]; -// nbtMap = new HashMap<>(); -// } -// -// /** -// * Constructs the clipboard. -// * -// * @param size the dimensions of the clipboard (should be at least 1 on every dimension) -// * @param origin the origin point where the copy was made, which must be the -// * {@link CuboidRegion#getMinimumPoint()} relative to the copy -// */ -// public CuboidClipboard(Vector size, Vector origin) { -// checkNotNull(size); -// checkNotNull(origin); -// MainUtil.warnDeprecated(BlockArrayClipboard.class, ClipboardFormat.class); -// this.origin = origin; -// this.offset = new Vector(); -// this.size = size; -// this.dx = size.getBlockX(); -// this.dxz = dx * size.getBlockZ(); -// states = new int[dx * size.getBlockZ() * ((size.getBlockY() + 15) >> 4)][]; -// nbtMap = new HashMap<>(); -// } -// -// /** -// * Constructs the clipboard. -// * -// * @param size the dimensions of the clipboard (should be at least 1 on every dimension) -// * @param origin the origin point where the copy was made, which must be the -// * {@link CuboidRegion#getMinimumPoint()} relative to the copy -// * @param offset the offset from the minimum point of the copy where the user was -// */ -// public CuboidClipboard(Vector size, Vector origin, Vector offset) { -// checkNotNull(size); -// checkNotNull(origin); -// checkNotNull(offset); -// MainUtil.warnDeprecated(BlockArrayClipboard.class, ClipboardFormat.class); -// this.origin = origin; -// this.offset = offset; -// this.size = size; -// this.dx = size.getBlockX(); -// this.dxz = dx * size.getBlockZ(); -// states = new int[dx * size.getBlockZ() * ((size.getBlockY() + 15) >> 4)][]; -// nbtMap = new HashMap<>(); -// } -// -// public BaseBlock getBlock(Vector position) { -// return getBlock(position.getBlockX(), position.getBlockY(), position.getBlockZ()); -// } -// -// public BaseBlock getBlock(int x, int y, int z) { -// int i = x + z * dx + (y >> 4) * dxz; -// int[] idArray = states[i]; -// BlockStateHolder blockState; -// if (idArray == null) { -// blockState = BlockTypes.AIR.getDefaultState(); -// } else { -// blockState = BlockState.get(idArray[y & 0xF]); -// } -// CompoundTag nbt = nbtMap.get(new IntegerTrio(x, y, z)); -// return new BaseBlock(blockState, nbt); -// } -// -// public BaseBlock getLazyBlock(Vector position) { -// return getBlock(position); -// } -// -// public void setBlock(Vector location, BaseBlock block) { -// setBlock(location.getBlockX(), location.getBlockY(), location.getBlockZ(), block); -// } -// -// public boolean setBlock(int x, int y, int z, BaseBlock block) { -// if (block == null || block.getBlockType().getMaterial().isAir()) { -// int i = x + z * dx + (y >> 4) * dxz; -// int y2 = y & 0xF; -// int[] idArray = states[i]; -// if (idArray == null) { -// return true; -// } -// idArray[y2] = (byte) 0; -// nbtMap.remove(new IntegerTrio(x, y, z)); -// return true; -// } -// if (block.hasNbtData()) { -// nbtMap.put(new IntegerTrio(x, y, z), block.getNbtData()); -// } -// int i = x + z * dx + (y >> 4) * dxz; -// int y2 = y & 0xF; -// int[] idArray = states[i]; -// if (idArray == null) { -// idArray = new int[16]; -// states[i] = idArray; -// } -// idArray[y2] = block.getInternalId(); -// return true; -// } -// -// /** -// * Get the width (X-direction) of the clipboard. -// * -// * @return width -// */ -// public int getWidth() { -// return size.getBlockX(); -// } -// -// /** -// * Get the length (Z-direction) of the clipboard. -// * -// * @return length -// */ -// public int getLength() { -// return size.getBlockZ(); -// } -// -// /** -// * Get the height (Y-direction) of the clipboard. -// * -// * @return height -// */ -// public int getHeight() { -// return size.getBlockY(); -// } -// -// /** -// * Rotate the clipboard in 2D. It can only rotate by angles divisible by 90. -// * -// * @param angle in degrees -// */ -// @SuppressWarnings("deprecation") -// public void rotate2D(int angle) { -// angle = angle % 360; -// if (angle % 90 != 0) { // Can only rotate 90 degrees at the moment -// return; -// } -// if (angle == 0) { -// return; -// } -// if (angle > 180) { -// angle -= 360; -// } -// final boolean reverse = angle < 0; -// final int numRotations = Math.abs((int) Math.floor(angle / 90.0)); -// -// final int width = getWidth(); -// final int length = getLength(); -// final int height = getHeight(); -// final Vector sizeRotated = size.transform2D(angle, 0, 0, 0, 0).round(); -// final int shiftX = sizeRotated.getX() < 0 ? -sizeRotated.getBlockX() - 1 : 0; -// final int shiftZ = sizeRotated.getZ() < 0 ? -sizeRotated.getBlockZ() - 1 : 0; -// -// CuboidClipboard cloned = new CuboidClipboard(sizeRotated.positive().round()); -// -// BaseBlock air = new BaseBlock(BlockTypes.AIR.getDefaultState()); -// -// for (int x = 0; x < width; ++x) { -// for (int z = 0; z < length; ++z) { -// final Vector2D v = new Vector2D(x, z).transform2D(angle, 0, 0, shiftX, shiftZ).round(); -// final int newX = v.getBlockX(); -// final int newZ = v.getBlockZ(); -// for (int y = 0; y < height; ++y) { -// BaseBlock block = getBlock(x, y, z); -// if (block.getBlockType().getMaterial().isAir()) { -// continue; -// } -// if (reverse) { -// for (int i = 0; i < numRotations; ++i) { -// block.rotate90Reverse(); -// } -// } else { -// for (int i = 0; i < numRotations; ++i) { -// block.rotate90(); -// } -// } -// cloned.setBlock(newX, y, newZ, block); -// } -// } -// } -// this.states = cloned.states; -// this.nbtMap = cloned.nbtMap; -// size = new Vector(Math.abs(sizeRotated.getBlockX()), -// Math.abs(sizeRotated.getBlockY()), -// Math.abs(sizeRotated.getBlockZ())); -// offset = offset.transform2D(angle, 0, 0, 0, 0) -// .subtract(shiftX, 0, shiftZ).round(); -// dx = cloned.dx; -// dxz = cloned.dxz; -// } -// -// /** -// * Flip the clipboard. -// * -// * @param dir direction to flip -// */ -// public void flip(FlipDirection dir) { -// flip(dir, false); -// } -// -// /** -// * Flip the clipboard. -// * -// * @param dir direction to flip -// * @param aroundPlayer flip the offset around the player -// */ -// @SuppressWarnings("deprecation") -// public void flip(FlipDirection dir, boolean aroundPlayer) { -// checkNotNull(dir); -// -// final int width = getWidth(); -// final int length = getLength(); -// final int height = getHeight(); -// -// switch (dir) { -// case WEST_EAST: -// final int wid = (int) Math.ceil(width / 2.0f); -// for (int xs = 0; xs < wid; ++xs) { -// for (int z = 0; z < length; ++z) { -// for (int y = 0; y < height; ++y) { -// BaseBlock block1 = getBlock(xs, y, z); -// if (block1 != null) { -// block1.flip(dir); -// } -// // Skip the center plane -// if (xs == width - xs - 1) { -// continue; -// } -// BaseBlock block2 = getBlock(width - xs - 1, y, z); -// if (block2 != null) { -// block2.flip(dir); -// } -// setBlock(xs, y, z, block2); -// setBlock(width - xs - 1, y, z, block1); -// } -// } -// } -// -// if (aroundPlayer) { -// offset.mutX(1 - offset.getX() - width); -// } -// -// break; -// -// case NORTH_SOUTH: -// final int len = (int) Math.ceil(length / 2.0f); -// for (int zs = 0; zs < len; ++zs) { -// for (int x = 0; x < width; ++x) { -// for (int y = 0; y < height; ++y) { -// BaseBlock block1 = getBlock(x, y, zs); -// if (block1 != null) { -// block1.flip(dir); -// } -// -// // Skip the center plane -// if (zs == length - zs - 1) { -// continue; -// } -// -// BaseBlock block2 = getBlock(x, y, length - zs - 1); -// if (block2 != null) { -// block2.flip(dir); -// } -// -// setBlock(x, y, zs, block2); -// setBlock(x, y, length - zs - 1, block1); -// -// -// } -// } -// } -// -// if (aroundPlayer) { -// offset.mutZ(1 - offset.getZ() - length); -// } -// -// break; -// -// case UP_DOWN: -// final int hei = (int) Math.ceil(height / 2.0f); -// for (int ys = 0; ys < hei; ++ys) { -// for (int x = 0; x < width; ++x) { -// for (int z = 0; z < length; ++z) { -// BaseBlock block1 = getBlock(x, ys, z); -// if (block1 != null) { -// block1.flip(dir); -// } -// -// // Skip the center plane -// if (ys == height - ys - 1) { -// continue; -// } -// -// BaseBlock block2 = getBlock(x, height - ys - 1, z); -// if (block2 != null) { -// block2.flip(dir); -// } -// -// setBlock(x, ys, z, block2); -// setBlock(x, height - ys - 1, z, block1); -// } -// } -// } -// -// if (aroundPlayer) { -// offset.mutY(1 - offset.getY() - height); -// } -// -// break; -// } -// } -// -// /** -// * Copies blocks to the clipboard. -// * -// * @param editSession the EditSession from which to take the blocks -// */ -// public void copy(EditSession editSession) { -// for (int x = 0; x < size.getBlockX(); ++x) { -// for (int y = 0; y < size.getBlockY(); ++y) { -// for (int z = 0; z < size.getBlockZ(); ++z) { -// setBlock(x, y, z, editSession.getBlock(new Vector(x, y, z).add(getOrigin()))); -// } -// } -// } -// } -// -// /** -// * Copies blocks to the clipboard. -// * -// * @param editSession The EditSession from which to take the blocks -// * @param region A region that further constrains which blocks to take. -// */ -// public void copy(EditSession editSession, Region region) { -// for (int x = 0; x < size.getBlockX(); ++x) { -// for (int y = 0; y < size.getBlockY(); ++y) { -// for (int z = 0; z < size.getBlockZ(); ++z) { -// final Vector pt = new Vector(x, y, z).add(getOrigin()); -// if (region.contains(pt)) { -// setBlock(x, y, z, editSession.getBlock(pt)); -// } else { -// setBlock(x, y, z, null); -// } -// } -// } -// } -// } -// -// /** -// * Paste the clipboard at the given location using the given {@code EditSession}. -// *

-// *

This method blocks the server/game until the entire clipboard is -// * pasted. In the future, {@link ForwardExtentCopy} will be recommended, -// * which, if combined with the proposed operation scheduler framework, -// * will not freeze the game/server.

-// * -// * @param editSession the EditSession to which blocks are to be copied to -// * @param newOrigin the new origin point (must correspond to the minimum point of the cuboid) -// * @param noAir true to not copy air blocks in the source -// * @throws MaxChangedBlocksException thrown if too many blocks were changed -// */ -// public void paste(EditSession editSession, Vector newOrigin, boolean noAir) throws MaxChangedBlocksException { -// paste(editSession, newOrigin, noAir, false); -// } -// -// /** -// * Paste the clipboard at the given location using the given {@code EditSession}. -// *

-// *

This method blocks the server/game until the entire clipboard is -// * pasted. In the future, {@link ForwardExtentCopy} will be recommended, -// * which, if combined with the proposed operation scheduler framework, -// * will not freeze the game/server.

-// * -// * @param editSession the EditSession to which blocks are to be copied to -// * @param newOrigin the new origin point (must correspond to the minimum point of the cuboid) -// * @param noAir true to not copy air blocks in the source -// * @param entities true to copy entities -// * @throws MaxChangedBlocksException thrown if too many blocks were changed -// */ -// public void paste(EditSession editSession, Vector newOrigin, boolean noAir, boolean entities) throws MaxChangedBlocksException { -// place(editSession, newOrigin.add(offset), noAir); -// if (entities) { -// pasteEntities(newOrigin.add(offset)); -// } -// } -// -// /** -// * Paste the clipboard at the given location using the given {@code EditSession}. -// *

-// *

This method blocks the server/game until the entire clipboard is -// * pasted. In the future, {@link ForwardExtentCopy} will be recommended, -// * which, if combined with the proposed operation scheduler framework, -// * will not freeze the game/server.

-// * -// * @param editSession the EditSession to which blocks are to be copied to -// * @param newOrigin the new origin point (must correspond to the minimum point of the cuboid) -// * @param noAir true to not copy air blocks in the source -// * @throws MaxChangedBlocksException thrown if too many blocks were changed -// */ -// public void place(EditSession editSession, Vector newOrigin, boolean noAir) throws MaxChangedBlocksException { -// int ox = newOrigin.getBlockX(); -// int oy = newOrigin.getBlockY(); -// int oz = newOrigin.getBlockZ(); -// for (int y = 0; y < size.getBlockY(); ++y) { -// for (int z = 0; z < size.getBlockZ(); ++z) { -// for (int x = 0; x < size.getBlockX(); ++x) { -// final BaseBlock block = getBlock(x, y, z); -// if (block == null) { -// continue; -// } -// if (noAir && block.getBlockType().getMaterial().isAir()) { -// continue; -// } -// editSession.setBlock(x + ox, y + oy, z + oz, block); -// } -// } -// } -// editSession.flushQueue(); -// } -// -// /** -// * Paste the stored entities to the given position. -// * -// * @param newOrigin the new origin -// * @return a list of entities that were pasted -// */ -// public LocalEntity[] pasteEntities(Vector newOrigin) { -// LocalEntity[] entities = new LocalEntity[this.entities.size()]; -// for (int i = 0; i < this.entities.size(); ++i) { -// CopiedEntity copied = this.entities.get(i); -// if (copied.entity.spawn(copied.entity.getPosition().setPosition(copied.relativePosition.add(newOrigin)))) { -// entities[i] = copied.entity; -// } -// } -// return entities; -// } -// -// /** -// * Store an entity. -// * -// * @param entity the entity -// */ -// public void storeEntity(LocalEntity entity) { -// this.entities.add(new CopiedEntity(entity)); -// } -// -// /** -// * Get the block at the given position. -// *

-// *

If the position is out of bounds, air will be returned.

-// * -// * @param position the point, relative to the origin of the copy (0, 0, 0) and not to the actual copy origin -// * @return air, if this block was outside the (non-cuboid) selection while copying -// * @throws ArrayIndexOutOfBoundsException if the position is outside the bounds of the CuboidClipboard -// * @deprecated use {@link #getBlock(Vector)} instead -// */ -// @Deprecated -// public BaseBlock getPoint(Vector position) throws ArrayIndexOutOfBoundsException { -// final BaseBlock block = getBlock(position); -// if (block == null) { -// return new BaseBlock(BlockTypesAIR); -// } -// -// return block; -// } -// -// /** -// * Get the dimensions of the clipboard. -// * -// * @return the dimensions, where (1, 1, 1) is 1 wide, 1 across, 1 deep -// */ -// public Vector getSize() { -// return size; -// } -// -// /** -// * Saves the clipboard data to a .schematic-format file. -// * -// * @param path the path to the file to save -// * @throws IOException thrown on I/O error -// * @throws DataException thrown on error writing the data for other reasons -// * @deprecated use {@link SchematicFormat#MCEDIT} -// */ -// @Deprecated -// public void saveSchematic(File path) throws IOException, DataException { -// checkNotNull(path); -// SchematicFormat.MCEDIT.save(this, path); -// } -// -// /** -// * Load a .schematic file into a clipboard. -// * -// * @param path the path to the file to load -// * @return a clipboard -// * @throws IOException thrown on I/O error -// * @throws DataException thrown on error writing the data for other reasons -// * @deprecated use {@link SchematicFormat#MCEDIT} -// */ -// @Deprecated -// public static CuboidClipboard loadSchematic(File path) throws DataException, IOException { -// checkNotNull(path); -// return SchematicFormat.MCEDIT.load(path); -// } -// -// /** -// * Get the origin point, which corresponds to where the copy was -// * originally copied from. The origin is the lowest possible X, Y, and -// * Z components of the cuboid region that was copied. -// * -// * @return the origin -// */ -// public Vector getOrigin() { -// return origin; -// } -// -// /** -// * Set the origin point, which corresponds to where the copy was -// * originally copied from. The origin is the lowest possible X, Y, and -// * Z components of the cuboid region that was copied. -// * -// * @param origin the origin to set -// */ -// public void setOrigin(Vector origin) { -// checkNotNull(origin); -// this.origin = origin; -// } -// -// /** -// * Get the offset of the player to the clipboard's minimum point -// * (minimum X, Y, Z coordinates). -// *

-// *

The offset is inverse (multiplied by -1).

-// * -// * @return the offset the offset -// */ -// public Vector getOffset() { -// return offset; -// } -// -// /** -// * Set the offset of the player to the clipboard's minimum point -// * (minimum X, Y, Z coordinates). -// *

-// *

The offset is inverse (multiplied by -1).

-// * -// * @param offset the new offset -// */ -// public void setOffset(Vector offset) { -// this.offset = offset; -// } -// -// /** -// * Get the block distribution inside a clipboard. -// * -// * @return a block distribution -// */ -// public List> getBlockDistribution() { -// List> distribution = new ArrayList>(); -// Map> map = new HashMap>(); -// -// int maxX = getWidth(); -// int maxY = getHeight(); -// int maxZ = getLength(); -// -// for (int x = 0; x < maxX; ++x) { -// for (int y = 0; y < maxY; ++y) { -// for (int z = 0; z < maxZ; ++z) { -// final BaseBlock block = getBlock(x, y, z); -// if (block == null) { -// continue; -// } -// -// int id = block.getId(); -// -// if (map.containsKey(id)) { -// map.get(id).increment(); -// } else { -// Countable c = new Countable(id, 1); -// map.put(id, c); -// distribution.add(c); -// } -// } -// } -// } -// -// Collections.sort(distribution); -// // Collections.reverse(distribution); -// -// return distribution; -// } -// -// /** -// * Get the block distribution inside a clipboard with data values. -// * -// * @return a block distribution -// */ -// // TODO reduce code duplication -// public List> getBlockDistributionWithData() { -// List> distribution = new ArrayList>(); -// Map> map = new HashMap>(); -// -// int maxX = getWidth(); -// int maxY = getHeight(); -// int maxZ = getLength(); -// -// for (int x = 0; x < maxX; ++x) { -// for (int y = 0; y < maxY; ++y) { -// for (int z = 0; z < maxZ; ++z) { -// final BaseBlock block = getBlock(x, y, z); -// if (block == null) { -// continue; -// } -// -// // Strip the block from metadata that is not part of our key -// final BaseBlock bareBlock = new BaseBlock(block.getId(), block.getData()); -// -// if (map.containsKey(bareBlock)) { -// map.get(bareBlock).increment(); -// } else { -// Countable c = new Countable(bareBlock, 1); -// map.put(bareBlock, c); -// distribution.add(c); -// } -// } -// } -// } -// -// Collections.sort(distribution); -// // Collections.reverse(distribution); -// -// return distribution; -// } -// -// /** -// * Stores a copied entity. -// */ -// private class CopiedEntity { -// private final LocalEntity entity; -// private final Vector relativePosition; -// -// private CopiedEntity(LocalEntity entity) { -// this.entity = entity; -// this.relativePosition = entity.getPosition().getPosition().subtract(getOrigin()); -// } -// } + + /** + * An enum of possible flip directions. + */ + public enum FlipDirection { + NORTH_SOUTH(Direction.NORTH), + WEST_EAST(Direction.WEST), + UP_DOWN(Direction.UP), + ; + private final Direction direction; + FlipDirection(Direction direction) { + this.direction = direction; + } + } + + private final BlockArrayClipboard clipboard; + private AffineTransform transform; + public Vector size; + + /** + * Constructs the clipboard. + * + * @param size the dimensions of the clipboard (should be at least 1 on every dimension) + */ + public CuboidClipboard(Vector size) { + checkNotNull(size); + MainUtil.warnDeprecated(BlockArrayClipboard.class, ClipboardFormat.class); + this.size = size; + this.clipboard = this.init(Vector.ZERO, Vector.ZERO); + } + + /** + * Constructs the clipboard. + * + * @param size the dimensions of the clipboard (should be at least 1 on every dimension) + * @param origin the origin point where the copy was made, which must be the + * {@link CuboidRegion#getMinimumPoint()} relative to the copy + */ + public CuboidClipboard(Vector size, Vector origin) { + checkNotNull(size); + checkNotNull(origin); + MainUtil.warnDeprecated(BlockArrayClipboard.class, ClipboardFormat.class); + this.size = size; + this.clipboard = init(Vector.ZERO, origin); + } + + /** + * Constructs the clipboard. + * + * @param size the dimensions of the clipboard (should be at least 1 on every dimension) + * @param origin the origin point where the copy was made, which must be the + * {@link CuboidRegion#getMinimumPoint()} relative to the copy + * @param offset the offset from the minimum point of the copy where the user was + */ + public CuboidClipboard(Vector size, Vector origin, Vector offset) { + checkNotNull(size); + checkNotNull(origin); + checkNotNull(offset); + MainUtil.warnDeprecated(BlockArrayClipboard.class, ClipboardFormat.class); + this.size = size; + this.clipboard = this.init(offset, origin); + } + + /* ------------------------------------------------------------------------------------------------------------- */ + + private BlockArrayClipboard init(Vector offset, Vector min) { + Vector origin = min.subtract(offset); + CuboidRegion region = new CuboidRegion(min, min.add(size).subtract(Vector.ONE)); + BlockArrayClipboard clipboard = new BlockArrayClipboard(region); + clipboard.setOrigin(origin); + return clipboard; + } + + private BaseBlock adapt(BlockState state) { + if (state instanceof BaseBlock) return (BaseBlock) state; + return new BaseBlock(state); + } + + /* ------------------------------------------------------------------------------------------------------------- */ + + public BaseBlock getBlock(Vector position) { + return getBlock(position.getBlockX(), position.getBlockY(), position.getBlockZ()); + } + + + public BaseBlock getBlock(int x, int y, int z) { + return adapt(clipboard.IMP.getBlock(x, y, z)); + } + + public BaseBlock getLazyBlock(Vector position) { + return getBlock(position); + } + + public void setBlock(Vector location, BaseBlock block) { + setBlock(location.getBlockX(), location.getBlockY(), location.getBlockZ(), block); + } + + public boolean setBlock(int x, int y, int z, BaseBlock block) { + return setBlock(x, y, z, (BlockState) block); + } + + public boolean setBlock(int x, int y, int z, BlockState block) { + return clipboard.IMP.setBlock(x, y, z, block); + } + + /** + * Get the width (X-direction) of the clipboard. + * + * @return width + */ + public int getWidth() { + return size.getBlockX(); + } + + /** + * Get the length (Z-direction) of the clipboard. + * + * @return length + */ + public int getLength() { + return size.getBlockZ(); + } + + /** + * Get the height (Y-direction) of the clipboard. + * + * @return height + */ + public int getHeight() { + return size.getBlockY(); + } + + /** + * Rotate the clipboard in 2D. It can only rotate by angles divisible by 90. + * + * @param angle in degrees + */ + @SuppressWarnings("deprecation") + public void rotate2D(int angle) { + AffineTransform newTransform = new AffineTransform().rotateY(-angle); + this.transform = transform == null ? newTransform : newTransform.combine(transform); + } + + /** + * Flip the clipboard. + * + * @param dir direction to flip + */ + public void flip(FlipDirection dir) { + flip(dir, false); + } + + /** + * Flip the clipboard. + * + * @param dir direction to flip + * @param aroundPlayer flip the offset around the player + */ + @SuppressWarnings("deprecation") + public void flip(FlipDirection dir, boolean aroundPlayer) { + checkNotNull(dir); + Direction direction = dir.direction; + AffineTransform newTransform = new AffineTransform().scale(direction.toVector().positive().multiply(-2).add(1, 1, 1)); + this.transform = transform == null ? newTransform : newTransform.combine(transform); + } + + /** + * Copies blocks to the clipboard. + * + * @param editSession the EditSession from which to take the blocks + */ + public void copy(EditSession editSession) { + for (int x = 0; x < size.getBlockX(); ++x) { + for (int y = 0; y < size.getBlockY(); ++y) { + for (int z = 0; z < size.getBlockZ(); ++z) { + setBlock(x, y, z, editSession.getBlock(new Vector(x, y, z).add(getOrigin()))); + } + } + } + } + + /** + * Copies blocks to the clipboard. + * + * @param editSession The EditSession from which to take the blocks + * @param region A region that further constrains which blocks to take. + */ + public void copy(EditSession editSession, Region region) { + for (int x = 0; x < size.getBlockX(); ++x) { + for (int y = 0; y < size.getBlockY(); ++y) { + for (int z = 0; z < size.getBlockZ(); ++z) { + final Vector pt = new Vector(x, y, z).add(getOrigin()); + if (region.contains(pt)) { + setBlock(x, y, z, editSession.getBlock(pt)); + } else { + setBlock(x, y, z, null); + } + } + } + } + } + + /** + * Paste the clipboard at the given location using the given {@code EditSession}. + *

+ *

This method blocks the server/game until the entire clipboard is + * pasted. In the future, {@link ForwardExtentCopy} will be recommended, + * which, if combined with the proposed operation scheduler framework, + * will not freeze the game/server.

+ * + * @param editSession the EditSession to which blocks are to be copied to + * @param newOrigin the new origin point (must correspond to the minimum point of the cuboid) + * @param noAir true to not copy air blocks in the source + * @throws MaxChangedBlocksException thrown if too many blocks were changed + */ + public void paste(EditSession editSession, Vector newOrigin, boolean noAir) throws MaxChangedBlocksException { + paste(editSession, newOrigin, noAir, false); + } + + /** + * Paste the clipboard at the given location using the given {@code EditSession}. + *

+ *

This method blocks the server/game until the entire clipboard is + * pasted. In the future, {@link ForwardExtentCopy} will be recommended, + * which, if combined with the proposed operation scheduler framework, + * will not freeze the game/server.

+ * + * @param editSession the EditSession to which blocks are to be copied to + * @param newOrigin the new origin point (must correspond to the minimum point of the cuboid) + * @param noAir true to not copy air blocks in the source + * @param entities true to copy entities + * @throws MaxChangedBlocksException thrown if too many blocks were changed + */ + public void paste(EditSession editSession, Vector newOrigin, boolean noAir, boolean entities) throws MaxChangedBlocksException { + new Schematic(clipboard).paste(editSession, newOrigin, false, !noAir, entities, transform); + editSession.flushQueue(); + } + + /** + * Paste the clipboard at the given location using the given {@code EditSession}. + *

+ *

This method blocks the server/game until the entire clipboard is + * pasted. In the future, {@link ForwardExtentCopy} will be recommended, + * which, if combined with the proposed operation scheduler framework, + * will not freeze the game/server.

+ * + * @param editSession the EditSession to which blocks are to be copied to + * @param newOrigin the new origin point (must correspond to the minimum point of the cuboid) + * @param noAir true to not copy air blocks in the source + * @throws MaxChangedBlocksException thrown if too many blocks were changed + */ + public void place(EditSession editSession, Vector newOrigin, boolean noAir) throws MaxChangedBlocksException { + paste(editSession, newOrigin, noAir, false); + } + + /** + * Paste the stored entities to the given position. + * + * @param newOrigin the new origin + * @return a list of entities that were pasted + */ + public LocalEntity[] pasteEntities(Vector newOrigin) { + return new LocalEntity[0]; + } + + /** + * Store an entity. + * + * @param entity the entity + */ + public void storeEntity(LocalEntity entity) { + + } + + /** + * Get the block at the given position. + *

+ *

If the position is out of bounds, air will be returned.

+ * + * @param position the point, relative to the origin of the copy (0, 0, 0) and not to the actual copy origin + * @return air, if this block was outside the (non-cuboid) selection while copying + * @throws ArrayIndexOutOfBoundsException if the position is outside the bounds of the CuboidClipboard + * @deprecated use {@link #getBlock(Vector)} instead + */ + @Deprecated + public BaseBlock getPoint(Vector position) throws ArrayIndexOutOfBoundsException { + final BaseBlock block = getBlock(position); + if (block == null) { + return new BaseBlock(BlockTypes.AIR); + } + + return block; + } + + /** + * Get the origin point, which corresponds to where the copy was + * originally copied from. The origin is the lowest possible X, Y, and + * Z components of the cuboid region that was copied. + * + * @return the origin + */ + public Vector getOrigin() { + return clipboard.getMinimumPoint(); + } + + /** + * Set the origin point, which corresponds to where the copy was + * originally copied from. The origin is the lowest possible X, Y, and + * Z components of the cuboid region that was copied. + * + * @param origin the origin to set + */ + public void setOrigin(Vector origin) { + checkNotNull(origin); + setOriginAndOffset(getOffset(), origin); + } + + public void setOriginAndOffset(Vector offset, Vector min) { + Vector origin = min.subtract(offset); + CuboidRegion region = new CuboidRegion(min, min.add(size).subtract(Vector.ONE)); + clipboard.setRegion(region); + clipboard.setOrigin(origin); + } + + /** + * Get the offset of the player to the clipboard's minimum point + * (minimum X, Y, Z coordinates). + *

+ *

The offset is inverse (multiplied by -1).

+ * + * @return the offset the offset + */ + public Vector getOffset() { + Vector min = clipboard.getMinimumPoint(); + Vector origin = clipboard.getOrigin(); + Vector offset = min.subtract(origin); + return offset; + } + + /** + * Set the offset of the player to the clipboard's minimum point + * (minimum X, Y, Z coordinates). + *

+ *

The offset is inverse (multiplied by -1).

+ * + * @param offset the new offset + */ + public void setOffset(Vector offset) { + checkNotNull(offset); + setOriginAndOffset(offset, getOrigin()); + } + + /** + * Get the dimensions of the clipboard. + * + * @return the dimensions, where (1, 1, 1) is 1 wide, 1 across, 1 deep + */ + public Vector getSize() { + return size; + } + + /** + * Saves the clipboard data to a .schematic-format file. + * + * @param path the path to the file to save + * @throws IOException thrown on I/O error + * @throws DataException thrown on error writing the data for other reasons + * @deprecated use {@link SchematicFormat#MCEDIT} + */ + @Deprecated + public void saveSchematic(File path) throws IOException, DataException { + checkNotNull(path); + SchematicFormat.MCEDIT.save(this, path); + } + + /** + * Load a .schematic file into a clipboard. + * + * @param path the path to the file to load + * @return a clipboard + * @throws IOException thrown on I/O error + * @throws DataException thrown on error writing the data for other reasons + * @deprecated use {@link SchematicFormat#MCEDIT} + */ + @Deprecated + public static CuboidClipboard loadSchematic(File path) throws DataException, IOException { + checkNotNull(path); + return SchematicFormat.MCEDIT.load(path); + } + + /** + * Get the block distribution inside a clipboard. + * + * @return a block distribution + */ + public List> getBlockDistribution() { + List> distribution = new ArrayList<>(); + List> distr = clipboard.getBlockDistributionWithData(clipboard.getRegion()); + for (Countable item : distr) { + BlockStateHolder state = item.getID(); + int[] legacyId = LegacyMapper.getInstance().getLegacyFromBlock(state.toImmutableState()); + if (legacyId[0] != 0) distribution.add(new Countable<>(legacyId[0], item.getAmount())); + } + return distribution; + } + + /** + * Get the block distribution inside a clipboard with data values. + * + * @return a block distribution + */ + // TODO reduce code duplication + public List> getBlockDistributionWithData() { + List> distribution = new ArrayList<>(); + List> distr = clipboard.getBlockDistributionWithData(clipboard.getRegion()); + for (Countable item : distr) { + distribution.add(new Countable<>(new BaseBlock(item.getID()), item.getAmount())); + } + return distribution; + } + + /** + * Stores a copied entity. + */ + private class CopiedEntity { + private final LocalEntity entity; + private final Vector relativePosition; + + private CopiedEntity(LocalEntity entity) { + this.entity = entity; + this.relativePosition = entity.getPosition().getPosition().subtract(getOrigin()); + } + } } \ No newline at end of file diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/Extent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/Extent.java index cf347d6f4..a52279768 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/Extent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/Extent.java @@ -23,7 +23,9 @@ import com.boydti.fawe.jnbt.anvil.generator.*; import com.boydti.fawe.object.PseudoRandom; import com.sk89q.worldedit.*; import com.sk89q.worldedit.blocks.BaseBlock; -import com.sk89q.worldedit.world.block.BlockState; +import com.sk89q.worldedit.regions.CuboidRegion; +import com.sk89q.worldedit.util.Countable; +import com.sk89q.worldedit.world.block.*; import com.sk89q.worldedit.entity.BaseEntity; import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.function.mask.Mask; @@ -35,10 +37,9 @@ import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.world.biome.BaseBiome; import com.sk89q.worldedit.world.block.BlockState; -import com.sk89q.worldedit.world.block.BlockStateHolder; -import com.sk89q.worldedit.world.block.BlockTypes; import javax.annotation.Nullable; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.ThreadLocalRandom; @@ -297,6 +298,67 @@ public interface Extent extends InputExtent, OutputExtent { addOre(region, mask, BlockTypes.EMERALD_ORE.getDefaultState(), 5, 1, 100, 4, 31); } + /** + * Get the block distribution inside a region. + * + * @param region a region + * @return the results + */ + default List> getBlockDistribution(final Region region) { + int[] counter = new int[BlockTypes.size()]; + + for (final Vector pt : region) { + BlockType type = getBlockType(pt); + counter[type.getInternalId()]++; + } + List> distribution = new ArrayList<>(); + for (int i = 0; i < counter.length; i++) { + int count = counter[i]; + if (count != 0) { + distribution.add(new Countable<>(BlockTypes.get(i), count)); + } + } + Collections.sort(distribution); + return distribution; + } + + /** + * Get the block distribution (with data values) inside a region. + * + * @param region a region + * @return the results + */ + default List> getBlockDistributionWithData(final Region region) { + int[][] counter = new int[BlockTypes.size()][]; + + for (final Vector pt : region) { + BlockStateHolder blk = this.getBlock(pt); + BlockType type = blk.getBlockType(); + int[] stateCounter = counter[type.getInternalId()]; + if (stateCounter == null) { + counter[type.getInternalId()] = stateCounter = new int[type.getMaxStateId() + 1]; + } + stateCounter[blk.getInternalPropertiesId()]++; + } + List> distribution = new ArrayList<>(); + for (int typeId = 0; typeId < counter.length; typeId++) { + BlockType type = BlockTypes.get(typeId); + int[] stateCount = counter[typeId]; + if (stateCount != null) { + for (int propId = 0; propId < stateCount.length; propId++) { + int count = stateCount[propId]; + if (count != 0) { + BlockStateHolder state = type.withPropertyId(propId); + distribution.add(new Countable<>(state, count)); + } + + } + } + } + // Collections.reverse(distribution); + return distribution; + } + @Nullable @Override default Operation commit() { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/BlockArrayClipboard.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/BlockArrayClipboard.java index 4165ac3b5..f4031bc5e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/BlockArrayClipboard.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/BlockArrayClipboard.java @@ -132,6 +132,10 @@ public class BlockArrayClipboard implements Clipboard, LightingExtent, Closeable return region.clone(); } + public void setRegion(Region region) { + this.region = region; + } + @Override public Vector getOrigin() { return origin;