mirror of
https://github.com/EngineHub/WorldEdit.git
synced 2024-12-27 05:00:08 +08:00
Made //copy and //cut copy only the blocks contained in the selection.
This commit is contained in:
parent
77219a8b3e
commit
8ce14dc982
@ -29,7 +29,9 @@
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||||
|
import com.sk89q.worldedit.blocks.BlockID;
|
||||||
import com.sk89q.worldedit.data.DataException;
|
import com.sk89q.worldedit.data.DataException;
|
||||||
|
import com.sk89q.worldedit.regions.Region;
|
||||||
import com.sk89q.worldedit.schematic.SchematicFormat;
|
import com.sk89q.worldedit.schematic.SchematicFormat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -129,29 +131,33 @@ public void rotate2D(int angle) {
|
|||||||
if (angle % 90 != 0) { // Can only rotate 90 degrees at the moment
|
if (angle % 90 != 0) { // Can only rotate 90 degrees at the moment
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
boolean reverse = angle < 0;
|
final boolean reverse = angle < 0;
|
||||||
int numRotations = Math.abs((int) Math.floor(angle / 90.0));
|
final int numRotations = Math.abs((int) Math.floor(angle / 90.0));
|
||||||
|
|
||||||
int width = getWidth();
|
final int width = getWidth();
|
||||||
int length = getLength();
|
final int length = getLength();
|
||||||
int height = getHeight();
|
final int height = getHeight();
|
||||||
Vector sizeRotated = size.transform2D(angle, 0, 0, 0, 0);
|
final Vector sizeRotated = size.transform2D(angle, 0, 0, 0, 0);
|
||||||
int shiftX = sizeRotated.getX() < 0 ? -sizeRotated.getBlockX() - 1 : 0;
|
final int shiftX = sizeRotated.getX() < 0 ? -sizeRotated.getBlockX() - 1 : 0;
|
||||||
int shiftZ = sizeRotated.getZ() < 0 ? -sizeRotated.getBlockZ() - 1 : 0;
|
final int shiftZ = sizeRotated.getZ() < 0 ? -sizeRotated.getBlockZ() - 1 : 0;
|
||||||
|
|
||||||
BaseBlock newData[][][] = new BaseBlock
|
final BaseBlock newData[][][] = new BaseBlock
|
||||||
[Math.abs(sizeRotated.getBlockX())]
|
[Math.abs(sizeRotated.getBlockX())]
|
||||||
[Math.abs(sizeRotated.getBlockY())]
|
[Math.abs(sizeRotated.getBlockY())]
|
||||||
[Math.abs(sizeRotated.getBlockZ())];
|
[Math.abs(sizeRotated.getBlockZ())];
|
||||||
|
|
||||||
for (int x = 0; x < width; ++x) {
|
for (int x = 0; x < width; ++x) {
|
||||||
for (int z = 0; z < length; ++z) {
|
for (int z = 0; z < length; ++z) {
|
||||||
Vector v = (new Vector(x, 0, z)).transform2D(angle, 0, 0, 0, 0);
|
final Vector2D v = new Vector2D(x, z).transform2D(angle, 0, 0, shiftX, shiftZ);
|
||||||
int newX = v.getBlockX();
|
final int newX = v.getBlockX();
|
||||||
int newZ = v.getBlockZ();
|
final int newZ = v.getBlockZ();
|
||||||
for (int y = 0; y < height; ++y) {
|
for (int y = 0; y < height; ++y) {
|
||||||
BaseBlock block = data[x][y][z];
|
final BaseBlock block = data[x][y][z];
|
||||||
newData[shiftX + newX][y][shiftZ + newZ] = block;
|
newData[newX][y][newZ] = block;
|
||||||
|
|
||||||
|
if (block == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (reverse) {
|
if (reverse) {
|
||||||
for (int i = 0; i < numRotations; ++i) {
|
for (int i = 0; i < numRotations; ++i) {
|
||||||
@ -200,10 +206,23 @@ public void flip(FlipDirection dir, boolean aroundPlayer) {
|
|||||||
for (int xs = 0; xs < wid; ++xs) {
|
for (int xs = 0; xs < wid; ++xs) {
|
||||||
for (int z = 0; z < length; ++z) {
|
for (int z = 0; z < length; ++z) {
|
||||||
for (int y = 0; y < height; ++y) {
|
for (int y = 0; y < height; ++y) {
|
||||||
BaseBlock old = data[xs][y][z].flip(dir);
|
// Skip the center plane
|
||||||
if (xs == width - xs - 1) continue;
|
if (xs == width - xs - 1) {
|
||||||
data[xs][y][z] = data[width - xs - 1][y][z].flip(dir);
|
continue;
|
||||||
data[width - xs - 1][y][z] = old;
|
}
|
||||||
|
|
||||||
|
final BaseBlock block1 = data[xs][y][z];
|
||||||
|
if (block1 != null) {
|
||||||
|
block1.flip(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
final BaseBlock block2 = data[width - xs - 1][y][z];
|
||||||
|
if (block2 != null) {
|
||||||
|
block2.flip(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
data[xs][y][z] = block2;
|
||||||
|
data[width - xs - 1][y][z] = block1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,10 +238,23 @@ public void flip(FlipDirection dir, boolean aroundPlayer) {
|
|||||||
for (int zs = 0; zs < len; ++zs) {
|
for (int zs = 0; zs < len; ++zs) {
|
||||||
for (int x = 0; x < width; ++x) {
|
for (int x = 0; x < width; ++x) {
|
||||||
for (int y = 0; y < height; ++y) {
|
for (int y = 0; y < height; ++y) {
|
||||||
BaseBlock old = data[x][y][zs].flip(dir);
|
// Skip the center plane
|
||||||
if (zs == length - zs - 1) continue;
|
if (zs == length - zs - 1) {
|
||||||
data[x][y][zs] = data[x][y][length - zs - 1].flip(dir);
|
continue;
|
||||||
data[x][y][length - zs - 1] = old;
|
}
|
||||||
|
|
||||||
|
final BaseBlock block1 = data[x][y][zs];
|
||||||
|
if (block1 != null) {
|
||||||
|
block1.flip(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
final BaseBlock block2 = data[x][y][length - zs - 1];
|
||||||
|
if (block2 != null) {
|
||||||
|
block2.flip(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
data[x][y][zs] = block2;
|
||||||
|
data[x][y][length - zs - 1] = block1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -238,10 +270,23 @@ public void flip(FlipDirection dir, boolean aroundPlayer) {
|
|||||||
for (int ys = 0; ys < hei; ++ys) {
|
for (int ys = 0; ys < hei; ++ys) {
|
||||||
for (int x = 0; x < width; ++x) {
|
for (int x = 0; x < width; ++x) {
|
||||||
for (int z = 0; z < length; ++z) {
|
for (int z = 0; z < length; ++z) {
|
||||||
BaseBlock old = data[x][ys][z].flip(dir);
|
// Skip the center plane
|
||||||
if (ys == height - ys - 1) continue;
|
if (ys == height - ys - 1) {
|
||||||
data[x][ys][z] = data[x][height - ys - 1][z].flip(dir);
|
continue;
|
||||||
data[x][height - ys - 1][z] = old;
|
}
|
||||||
|
|
||||||
|
final BaseBlock block1 = data[x][ys][z];
|
||||||
|
if (block1 != null) {
|
||||||
|
block1.flip(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
final BaseBlock block2 = data[x][height - ys - 1][z];
|
||||||
|
if (block2 != null) {
|
||||||
|
block2.flip(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
data[x][ys][z] = block2;
|
||||||
|
data[x][height - ys - 1][z] = block1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -255,9 +300,9 @@ public void flip(FlipDirection dir, boolean aroundPlayer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copy to the clipboard.
|
* Copies blocks to the clipboard.
|
||||||
*
|
*
|
||||||
* @param editSession
|
* @param editSession The EditSession from which to take the blocks
|
||||||
*/
|
*/
|
||||||
public void copy(EditSession editSession) {
|
public void copy(EditSession editSession) {
|
||||||
for (int x = 0; x < size.getBlockX(); ++x) {
|
for (int x = 0; x < size.getBlockX(); ++x) {
|
||||||
@ -270,6 +315,27 @@ public void copy(EditSession editSession) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)) {
|
||||||
|
data[x][y][z] = editSession.getBlock(pt);
|
||||||
|
} else {
|
||||||
|
data[x][y][z] = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void paste(EditSession editSession, Vector newOrigin, boolean noAir)
|
public void paste(EditSession editSession, Vector newOrigin, boolean noAir)
|
||||||
throws MaxChangedBlocksException {
|
throws MaxChangedBlocksException {
|
||||||
paste(editSession, newOrigin, noAir, false);
|
paste(editSession, newOrigin, noAir, false);
|
||||||
@ -303,11 +369,16 @@ public void place(EditSession editSession, Vector pos, boolean noAir) throws Max
|
|||||||
for (int x = 0; x < size.getBlockX(); ++x) {
|
for (int x = 0; x < size.getBlockX(); ++x) {
|
||||||
for (int y = 0; y < size.getBlockY(); ++y) {
|
for (int y = 0; y < size.getBlockY(); ++y) {
|
||||||
for (int z = 0; z < size.getBlockZ(); ++z) {
|
for (int z = 0; z < size.getBlockZ(); ++z) {
|
||||||
if (noAir && data[x][y][z].isAir()) {
|
final BaseBlock block = data[x][y][z];
|
||||||
|
if (block == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
editSession.setBlock(new Vector(x, y, z).add(pos), data[x][y][z]);
|
if (noAir && block.isAir()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
editSession.setBlock(new Vector(x, y, z).add(pos), block);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -329,24 +400,38 @@ public void storeEntity(LocalEntity entity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get one point in the copy. The point is relative to the origin
|
* Get one point in the copy.
|
||||||
* of the copy (0, 0, 0) and not to the actual copy origin.
|
|
||||||
*
|
*
|
||||||
* @param pos
|
* @param The point, relative to the origin of the copy (0, 0, 0) and not to the actual copy origin.
|
||||||
* @return null
|
* @return air, if this block was outside the (non-cuboid) selection while copying
|
||||||
* @throws ArrayIndexOutOfBoundsException
|
* @throws ArrayIndexOutOfBoundsException if the position is outside the bounds of the CuboidClipboard
|
||||||
|
* @deprecated Use {@link #getBlock(Vector)} instead
|
||||||
*/
|
*/
|
||||||
public BaseBlock getPoint(Vector pos) throws ArrayIndexOutOfBoundsException {
|
public BaseBlock getPoint(Vector pos) throws ArrayIndexOutOfBoundsException {
|
||||||
|
final BaseBlock block = getBlock(pos);
|
||||||
|
if (block == null) {
|
||||||
|
return new BaseBlock(BlockID.AIR);
|
||||||
|
}
|
||||||
|
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get one point in the copy.
|
||||||
|
*
|
||||||
|
* @param The point, relative to the origin of the copy (0, 0, 0) and not to the actual copy origin.
|
||||||
|
* @return null, if this block was outside the (non-cuboid) selection while copying
|
||||||
|
* @throws ArrayIndexOutOfBoundsException if the position is outside the bounds of the CuboidClipboard
|
||||||
|
*/
|
||||||
|
public BaseBlock getBlock(Vector pos) throws ArrayIndexOutOfBoundsException {
|
||||||
return data[pos.getBlockX()][pos.getBlockY()][pos.getBlockZ()];
|
return data[pos.getBlockX()][pos.getBlockY()][pos.getBlockZ()];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get one point in the copy. The point is relative to the origin
|
* Set one point in the copy. Pass null to remove the block.
|
||||||
* of the copy (0, 0, 0) and not to the actual copy origin.
|
|
||||||
*
|
*
|
||||||
* @param pos
|
* @param The point, relative to the origin of the copy (0, 0, 0) and not to the actual copy origin.
|
||||||
* @return null
|
* @throws ArrayIndexOutOfBoundsException if the position is outside the bounds of the CuboidClipboard
|
||||||
* @throws ArrayIndexOutOfBoundsException
|
|
||||||
*/
|
*/
|
||||||
public void setBlock(Vector pt, BaseBlock block) {
|
public void setBlock(Vector pt, BaseBlock block) {
|
||||||
data[pt.getBlockX()][pt.getBlockY()][pt.getBlockZ()] = block;
|
data[pt.getBlockX()][pt.getBlockY()][pt.getBlockZ()] = block;
|
||||||
@ -440,8 +525,12 @@ public List<Countable<Integer>> getBlockDistribution() {
|
|||||||
for (int x = 0; x < maxX; ++x) {
|
for (int x = 0; x < maxX; ++x) {
|
||||||
for (int y = 0; y < maxY; ++y) {
|
for (int y = 0; y < maxY; ++y) {
|
||||||
for (int z = 0; z < maxZ; ++z) {
|
for (int z = 0; z < maxZ; ++z) {
|
||||||
|
final BaseBlock block = data[x][y][z];
|
||||||
|
if (block == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
int id = data[x][y][z].getId();
|
int id = block.getId();
|
||||||
|
|
||||||
if (map.containsKey(id)) {
|
if (map.containsKey(id)) {
|
||||||
map.get(id).increment();
|
map.get(id).increment();
|
||||||
@ -477,16 +566,19 @@ public List<Countable<BaseBlock>> getBlockDistributionWithData() {
|
|||||||
for (int x = 0; x < maxX; ++x) {
|
for (int x = 0; x < maxX; ++x) {
|
||||||
for (int y = 0; y < maxY; ++y) {
|
for (int y = 0; y < maxY; ++y) {
|
||||||
for (int z = 0; z < maxZ; ++z) {
|
for (int z = 0; z < maxZ; ++z) {
|
||||||
|
final BaseBlock block = data[x][y][z];
|
||||||
|
if (block == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
int id = data[x][y][z].getId();
|
// Strip the block from metadata that is not part of our key
|
||||||
int meta = data[x][y][z].getData();
|
final BaseBlock bareBlock = new BaseBlock(block.getId(), block.getData());
|
||||||
BaseBlock blk = new BaseBlock(id, meta);
|
|
||||||
|
|
||||||
if (map.containsKey(blk)) {
|
if (map.containsKey(bareBlock)) {
|
||||||
map.get(blk).increment();
|
map.get(bareBlock).increment();
|
||||||
} else {
|
} else {
|
||||||
Countable<BaseBlock> c = new Countable<BaseBlock>(blk, 1);
|
Countable<BaseBlock> c = new Countable<BaseBlock>(bareBlock, 1);
|
||||||
map.put(blk, c);
|
map.put(bareBlock, c);
|
||||||
distribution.add(c);
|
distribution.add(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
import com.sk89q.worldedit.*;
|
import com.sk89q.worldedit.*;
|
||||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||||
import com.sk89q.worldedit.blocks.BlockID;
|
import com.sk89q.worldedit.blocks.BlockID;
|
||||||
|
import com.sk89q.worldedit.regions.CuboidRegion;
|
||||||
import com.sk89q.worldedit.regions.Region;
|
import com.sk89q.worldedit.regions.Region;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,7 +67,13 @@ public void copy(CommandContext args, LocalSession session, LocalPlayer player,
|
|||||||
CuboidClipboard clipboard = new CuboidClipboard(
|
CuboidClipboard clipboard = new CuboidClipboard(
|
||||||
max.subtract(min).add(Vector.ONE),
|
max.subtract(min).add(Vector.ONE),
|
||||||
min, min.subtract(pos));
|
min, min.subtract(pos));
|
||||||
clipboard.copy(editSession);
|
|
||||||
|
if (region instanceof CuboidRegion) {
|
||||||
|
clipboard.copy(editSession);
|
||||||
|
} else {
|
||||||
|
clipboard.copy(editSession, region);
|
||||||
|
}
|
||||||
|
|
||||||
if (args.hasFlag('e')) {
|
if (args.hasFlag('e')) {
|
||||||
for (LocalEntity entity : player.getWorld().getEntities(region)) {
|
for (LocalEntity entity : player.getWorld().getEntities(region)) {
|
||||||
clipboard.storeEntity(entity);
|
clipboard.storeEntity(entity);
|
||||||
@ -109,7 +116,13 @@ public void cut(CommandContext args, LocalSession session, LocalPlayer player,
|
|||||||
CuboidClipboard clipboard = new CuboidClipboard(
|
CuboidClipboard clipboard = new CuboidClipboard(
|
||||||
max.subtract(min).add(Vector.ONE),
|
max.subtract(min).add(Vector.ONE),
|
||||||
min, min.subtract(pos));
|
min, min.subtract(pos));
|
||||||
clipboard.copy(editSession);
|
|
||||||
|
if (region instanceof CuboidRegion) {
|
||||||
|
clipboard.copy(editSession);
|
||||||
|
} else {
|
||||||
|
clipboard.copy(editSession, region);
|
||||||
|
}
|
||||||
|
|
||||||
if (args.hasFlag('e')) {
|
if (args.hasFlag('e')) {
|
||||||
LocalEntity[] entities = world.getEntities(region);
|
LocalEntity[] entities = world.getEntities(region);
|
||||||
for (LocalEntity entity : entities) {
|
for (LocalEntity entity : entities) {
|
||||||
@ -119,7 +132,7 @@ public void cut(CommandContext args, LocalSession session, LocalPlayer player,
|
|||||||
}
|
}
|
||||||
session.setClipboard(clipboard);
|
session.setClipboard(clipboard);
|
||||||
|
|
||||||
editSession.setBlocks(session.getSelection(world), block);
|
editSession.setBlocks(region, block);
|
||||||
player.print("Block(s) cut.");
|
player.print("Block(s) cut.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user