2010-10-02 23:11:44 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
2012-01-05 21:38:23 +00:00
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
|
2010-10-02 23:11:44 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-01-02 05:50:31 +00:00
|
|
|
package com.sk89q.worldedit;
|
|
|
|
|
2011-03-10 07:10:59 +00:00
|
|
|
import com.sk89q.jnbt.*;
|
2010-10-20 09:12:16 +00:00
|
|
|
import com.sk89q.worldedit.blocks.*;
|
|
|
|
import com.sk89q.worldedit.data.*;
|
2011-12-25 23:36:23 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
2010-10-03 17:44:52 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.ArrayList;
|
2010-10-14 08:31:05 +00:00
|
|
|
import java.util.List;
|
2011-03-10 07:10:59 +00:00
|
|
|
import java.util.zip.GZIPInputStream;
|
2010-10-02 23:11:44 +00:00
|
|
|
|
|
|
|
/**
|
2010-10-13 17:08:53 +00:00
|
|
|
* The clipboard remembers the state of a cuboid region.
|
2010-10-02 23:11:44 +00:00
|
|
|
*
|
2010-10-13 17:08:53 +00:00
|
|
|
* @author sk89q
|
2010-10-02 23:11:44 +00:00
|
|
|
*/
|
2010-10-11 08:22:47 +00:00
|
|
|
public class CuboidClipboard {
|
2010-11-27 06:15:21 +00:00
|
|
|
/**
|
|
|
|
* Flip direction.
|
|
|
|
*/
|
|
|
|
public enum FlipDirection {
|
|
|
|
NORTH_SOUTH,
|
|
|
|
WEST_EAST,
|
|
|
|
UP_DOWN
|
|
|
|
}
|
|
|
|
|
2010-10-13 23:49:35 +00:00
|
|
|
private BaseBlock[][][] data;
|
2010-10-13 17:08:53 +00:00
|
|
|
private Vector offset;
|
2010-10-13 01:03:56 +00:00
|
|
|
private Vector origin;
|
2010-10-13 17:08:53 +00:00
|
|
|
private Vector size;
|
2010-10-02 23:11:44 +00:00
|
|
|
|
|
|
|
/**
|
2010-10-13 17:08:53 +00:00
|
|
|
* Constructs the clipboard.
|
|
|
|
*
|
|
|
|
* @param size
|
|
|
|
*/
|
|
|
|
public CuboidClipboard(Vector size) {
|
|
|
|
this.size = size;
|
2010-10-13 23:49:35 +00:00
|
|
|
data = new BaseBlock[size.getBlockX()][size.getBlockY()][size.getBlockZ()];
|
2010-10-13 17:08:53 +00:00
|
|
|
origin = new Vector();
|
|
|
|
offset = new Vector();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs the clipboard.
|
|
|
|
*
|
|
|
|
* @param size
|
|
|
|
* @param origin
|
|
|
|
*/
|
|
|
|
public CuboidClipboard(Vector size, Vector origin) {
|
|
|
|
this.size = size;
|
2010-10-13 23:49:35 +00:00
|
|
|
data = new BaseBlock[size.getBlockX()][size.getBlockY()][size.getBlockZ()];
|
2010-10-13 17:08:53 +00:00
|
|
|
this.origin = origin;
|
|
|
|
offset = new Vector();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs the clipboard.
|
|
|
|
*
|
|
|
|
* @param size
|
2010-10-02 23:11:44 +00:00
|
|
|
* @param origin
|
2011-09-24 19:32:03 +00:00
|
|
|
* @param offset
|
2010-10-02 23:11:44 +00:00
|
|
|
*/
|
2010-10-13 17:08:53 +00:00
|
|
|
public CuboidClipboard(Vector size, Vector origin, Vector offset) {
|
|
|
|
this.size = size;
|
2010-10-13 23:49:35 +00:00
|
|
|
data = new BaseBlock[size.getBlockX()][size.getBlockY()][size.getBlockZ()];
|
2010-10-02 23:11:44 +00:00
|
|
|
this.origin = origin;
|
2010-10-13 17:08:53 +00:00
|
|
|
this.offset = offset;
|
2010-10-02 23:11:44 +00:00
|
|
|
}
|
|
|
|
|
2010-10-03 17:44:52 +00:00
|
|
|
/**
|
|
|
|
* Get the width (X-direction) of the clipboard.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return width
|
2010-10-03 17:44:52 +00:00
|
|
|
*/
|
|
|
|
public int getWidth() {
|
2010-10-13 17:08:53 +00:00
|
|
|
return size.getBlockX();
|
2010-10-03 17:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the length (Z-direction) of the clipboard.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return length
|
2010-10-03 17:44:52 +00:00
|
|
|
*/
|
|
|
|
public int getLength() {
|
2010-10-13 17:08:53 +00:00
|
|
|
return size.getBlockZ();
|
2010-10-03 17:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the height (Y-direction) of the clipboard.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return height
|
2010-10-03 17:44:52 +00:00
|
|
|
*/
|
|
|
|
public int getHeight() {
|
2010-10-13 17:08:53 +00:00
|
|
|
return size.getBlockY();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotate the clipboard in 2D. It can only rotate by angles divisible by 90.
|
2010-11-27 06:15:21 +00:00
|
|
|
*
|
2010-10-13 17:08:53 +00:00
|
|
|
* @param angle in degrees
|
|
|
|
*/
|
|
|
|
public void rotate2D(int angle) {
|
|
|
|
angle = angle % 360;
|
|
|
|
if (angle % 90 != 0) { // Can only rotate 90 degrees at the moment
|
|
|
|
return;
|
|
|
|
}
|
2011-01-30 07:10:12 +00:00
|
|
|
boolean reverse = angle < 0;
|
2011-11-23 01:29:48 +00:00
|
|
|
int numRotations = Math.abs((int) Math.floor(angle / 90.0));
|
2010-10-13 17:08:53 +00:00
|
|
|
|
|
|
|
int width = getWidth();
|
|
|
|
int length = getLength();
|
|
|
|
int height = getHeight();
|
|
|
|
Vector sizeRotated = size.transform2D(angle, 0, 0, 0, 0);
|
2010-10-17 22:17:24 +00:00
|
|
|
int shiftX = sizeRotated.getX() < 0 ? -sizeRotated.getBlockX() - 1 : 0;
|
|
|
|
int shiftZ = sizeRotated.getZ() < 0 ? -sizeRotated.getBlockZ() - 1 : 0;
|
2010-10-13 17:08:53 +00:00
|
|
|
|
2010-10-17 22:17:24 +00:00
|
|
|
BaseBlock newData[][][] = new BaseBlock
|
|
|
|
[Math.abs(sizeRotated.getBlockX())]
|
|
|
|
[Math.abs(sizeRotated.getBlockY())]
|
|
|
|
[Math.abs(sizeRotated.getBlockZ())];
|
2010-10-13 17:08:53 +00:00
|
|
|
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
for (int z = 0; z < length; ++z) {
|
2010-10-17 22:17:24 +00:00
|
|
|
Vector v = (new Vector(x, 0, z)).transform2D(angle, 0, 0, 0, 0);
|
|
|
|
int newX = v.getBlockX();
|
|
|
|
int newZ = v.getBlockZ();
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int y = 0; y < height; ++y) {
|
2011-01-30 07:10:12 +00:00
|
|
|
BaseBlock block = data[x][y][z];
|
|
|
|
newData[shiftX + newX][y][shiftZ + newZ] = block;
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2011-01-30 07:10:12 +00:00
|
|
|
if (reverse) {
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int i = 0; i < numRotations; ++i) {
|
2011-01-30 07:10:12 +00:00
|
|
|
block.rotate90Reverse();
|
|
|
|
}
|
|
|
|
} else {
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int i = 0; i < numRotations; ++i) {
|
2011-01-30 07:10:12 +00:00
|
|
|
block.rotate90();
|
|
|
|
}
|
|
|
|
}
|
2010-10-13 17:08:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data = newData;
|
2010-10-17 22:17:24 +00:00
|
|
|
size = new Vector(Math.abs(sizeRotated.getBlockX()),
|
|
|
|
Math.abs(sizeRotated.getBlockY()),
|
|
|
|
Math.abs(sizeRotated.getBlockZ()));
|
2010-10-13 17:08:53 +00:00
|
|
|
offset = offset.transform2D(angle, 0, 0, 0, 0)
|
2011-08-26 02:57:05 +00:00
|
|
|
.subtract(shiftX, 0, shiftZ);
|
2010-10-03 17:44:52 +00:00
|
|
|
}
|
|
|
|
|
2011-09-03 16:54:20 +00:00
|
|
|
/**
|
|
|
|
* Flip the clipboard.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2011-09-03 16:54:20 +00:00
|
|
|
* @param dir direction to flip
|
|
|
|
*/
|
|
|
|
public void flip(FlipDirection dir) {
|
|
|
|
flip(dir, false);
|
|
|
|
}
|
|
|
|
|
2010-11-27 06:15:21 +00:00
|
|
|
/**
|
|
|
|
* Flip the clipboard.
|
|
|
|
*
|
2011-09-03 16:54:20 +00:00
|
|
|
* @param dir direction to flip
|
|
|
|
* @param aroundPlayer flip the offset around the player
|
2010-11-27 06:15:21 +00:00
|
|
|
*/
|
2011-08-26 02:57:05 +00:00
|
|
|
public void flip(FlipDirection dir, boolean aroundPlayer) {
|
2011-08-28 10:28:31 +00:00
|
|
|
final int width = getWidth();
|
|
|
|
final int length = getLength();
|
|
|
|
final int height = getHeight();
|
2010-11-27 06:15:21 +00:00
|
|
|
|
2011-08-26 02:57:05 +00:00
|
|
|
switch (dir) {
|
|
|
|
case NORTH_SOUTH:
|
2011-08-28 10:28:31 +00:00
|
|
|
final int wid = (int) Math.ceil(width / 2.0f);
|
2011-08-26 02:57:05 +00:00
|
|
|
for (int xs = 0; xs < wid; ++xs) {
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int z = 0; z < length; ++z) {
|
|
|
|
for (int y = 0; y < height; ++y) {
|
2011-09-03 16:54:20 +00:00
|
|
|
BaseBlock old = data[xs][y][z].flip(dir);
|
2011-09-04 00:25:20 +00:00
|
|
|
if (xs == width - xs - 1) continue;
|
2011-09-03 16:54:20 +00:00
|
|
|
data[xs][y][z] = data[width - xs - 1][y][z].flip(dir);
|
|
|
|
data[width - xs - 1][y][z] = old;
|
2010-11-27 06:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 02:57:05 +00:00
|
|
|
|
2011-09-03 16:54:20 +00:00
|
|
|
if (aroundPlayer) {
|
2011-08-26 02:57:05 +00:00
|
|
|
offset = offset.setX(1 - offset.getX() - width);
|
2011-09-03 16:54:20 +00:00
|
|
|
}
|
2011-08-26 02:57:05 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WEST_EAST:
|
2011-08-28 10:28:31 +00:00
|
|
|
final int len = (int) Math.ceil(length / 2.0f);
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int zs = 0; zs < len; ++zs) {
|
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
for (int y = 0; y < height; ++y) {
|
2011-09-03 16:54:20 +00:00
|
|
|
BaseBlock old = data[x][y][zs].flip(dir);
|
2011-09-04 00:25:20 +00:00
|
|
|
if (zs == length - zs - 1) continue;
|
2011-09-03 16:54:20 +00:00
|
|
|
data[x][y][zs] = data[x][y][length - zs - 1].flip(dir);
|
|
|
|
data[x][y][length - zs - 1] = old;
|
2010-11-27 06:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 02:57:05 +00:00
|
|
|
|
2011-09-03 16:54:20 +00:00
|
|
|
if (aroundPlayer) {
|
2011-08-26 02:57:05 +00:00
|
|
|
offset = offset.setZ(1 - offset.getZ() - length);
|
2011-09-03 16:54:20 +00:00
|
|
|
}
|
2011-08-26 02:57:05 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UP_DOWN:
|
2011-08-28 10:28:31 +00:00
|
|
|
final int hei = (int) Math.ceil(height / 2.0f);
|
2011-08-26 02:57:05 +00:00
|
|
|
for (int ys = 0; ys < hei; ++ys) {
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
for (int z = 0; z < length; ++z) {
|
2011-09-03 16:54:20 +00:00
|
|
|
BaseBlock old = data[x][ys][z].flip(dir);
|
2011-09-04 00:25:20 +00:00
|
|
|
if (ys == height - ys - 1) continue;
|
2011-09-03 16:54:20 +00:00
|
|
|
data[x][ys][z] = data[x][height - ys - 1][z].flip(dir);
|
|
|
|
data[x][height - ys - 1][z] = old;
|
2010-11-27 06:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 02:57:05 +00:00
|
|
|
|
2011-09-03 16:54:20 +00:00
|
|
|
if (aroundPlayer) {
|
2011-08-26 02:57:05 +00:00
|
|
|
offset = offset.setY(1 - offset.getY() - height);
|
2011-09-03 16:54:20 +00:00
|
|
|
}
|
2011-08-26 02:57:05 +00:00
|
|
|
|
|
|
|
break;
|
2010-11-27 06:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-02 23:11:44 +00:00
|
|
|
/**
|
|
|
|
* Copy to the clipboard.
|
|
|
|
*
|
|
|
|
* @param editSession
|
|
|
|
*/
|
|
|
|
public void copy(EditSession editSession) {
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int x = 0; x < size.getBlockX(); ++x) {
|
|
|
|
for (int y = 0; y < size.getBlockY(); ++y) {
|
|
|
|
for (int z = 0; z < size.getBlockZ(); ++z) {
|
2010-10-13 17:08:53 +00:00
|
|
|
data[x][y][z] =
|
2011-11-23 01:29:48 +00:00
|
|
|
editSession.getBlock(new Vector(x, y, z).add(getOrigin()));
|
2010-10-02 23:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Paste from the clipboard.
|
|
|
|
*
|
|
|
|
* @param editSession
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param newOrigin Position to paste it from
|
2010-10-02 23:11:44 +00:00
|
|
|
* @param noAir True to not paste air
|
2010-10-04 23:39:35 +00:00
|
|
|
* @throws MaxChangedBlocksException
|
2010-10-02 23:11:44 +00:00
|
|
|
*/
|
2010-10-13 01:03:56 +00:00
|
|
|
public void paste(EditSession editSession, Vector newOrigin, boolean noAir)
|
2010-10-05 20:46:31 +00:00
|
|
|
throws MaxChangedBlocksException {
|
2010-10-13 17:08:53 +00:00
|
|
|
place(editSession, newOrigin.add(offset), noAir);
|
2010-10-05 20:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Places the blocks in a position from the minimum corner.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2010-10-05 20:46:31 +00:00
|
|
|
* @param editSession
|
2010-10-13 17:08:53 +00:00
|
|
|
* @param pos
|
2010-10-05 20:46:31 +00:00
|
|
|
* @param noAir
|
|
|
|
* @throws MaxChangedBlocksException
|
|
|
|
*/
|
2011-11-23 01:29:48 +00:00
|
|
|
public void place(EditSession editSession, Vector pos, boolean noAir) throws MaxChangedBlocksException {
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int x = 0; x < size.getBlockX(); ++x) {
|
|
|
|
for (int y = 0; y < size.getBlockY(); ++y) {
|
|
|
|
for (int z = 0; z < size.getBlockZ(); ++z) {
|
2011-11-23 01:29:48 +00:00
|
|
|
if (noAir && data[x][y][z].isAir()) {
|
2010-10-13 23:49:35 +00:00
|
|
|
continue;
|
2011-11-23 01:29:48 +00:00
|
|
|
}
|
2010-10-17 22:17:24 +00:00
|
|
|
|
2011-11-23 01:29:48 +00:00
|
|
|
editSession.setBlock(new Vector(x, y, z).add(pos), data[x][y][z]);
|
2010-10-02 23:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2011-01-30 09:01:11 +00:00
|
|
|
/**
|
|
|
|
* Get one point in the copy. The point is relative to the origin
|
|
|
|
* of the copy (0, 0, 0) and not to the actual copy origin.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2011-01-30 09:01:11 +00:00
|
|
|
* @param pos
|
|
|
|
* @return null
|
|
|
|
* @throws ArrayIndexOutOfBoundsException
|
|
|
|
*/
|
|
|
|
public BaseBlock getPoint(Vector pos) throws ArrayIndexOutOfBoundsException {
|
|
|
|
return data[pos.getBlockX()][pos.getBlockY()][pos.getBlockZ()];
|
|
|
|
}
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2011-01-30 08:34:13 +00:00
|
|
|
/**
|
|
|
|
* Get the size of the copy.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2011-01-30 08:34:13 +00:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Vector getSize() {
|
|
|
|
return size;
|
|
|
|
}
|
2010-10-03 17:44:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the clipboard data to a .schematic-format file.
|
|
|
|
*
|
|
|
|
* @param path
|
|
|
|
* @throws IOException
|
2010-10-20 09:12:16 +00:00
|
|
|
* @throws DataException
|
2010-10-03 17:44:52 +00:00
|
|
|
*/
|
2011-01-27 09:51:34 +00:00
|
|
|
public void saveSchematic(File path) throws IOException, DataException {
|
2010-10-13 17:08:53 +00:00
|
|
|
int width = getWidth();
|
|
|
|
int height = getHeight();
|
|
|
|
int length = getLength();
|
2010-10-03 17:44:52 +00:00
|
|
|
|
2010-10-13 17:08:53 +00:00
|
|
|
if (width > 65535) {
|
2010-10-20 09:12:16 +00:00
|
|
|
throw new DataException("Width of region too large for a .schematic");
|
2010-10-05 00:00:54 +00:00
|
|
|
}
|
2010-10-13 17:08:53 +00:00
|
|
|
if (height > 65535) {
|
2010-10-20 09:12:16 +00:00
|
|
|
throw new DataException("Height of region too large for a .schematic");
|
2010-10-05 00:00:54 +00:00
|
|
|
}
|
2010-10-13 17:08:53 +00:00
|
|
|
if (length > 65535) {
|
2010-10-20 09:12:16 +00:00
|
|
|
throw new DataException("Length of region too large for a .schematic");
|
2010-10-05 00:00:54 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 01:29:48 +00:00
|
|
|
HashMap<String, Tag> schematic = new HashMap<String, Tag>();
|
|
|
|
schematic.put("Width", new ShortTag("Width", (short) width));
|
|
|
|
schematic.put("Length", new ShortTag("Length", (short) length));
|
|
|
|
schematic.put("Height", new ShortTag("Height", (short) height));
|
2010-10-03 17:44:52 +00:00
|
|
|
schematic.put("Materials", new StringTag("Materials", "Alpha"));
|
2010-11-07 05:23:43 +00:00
|
|
|
schematic.put("WEOriginX", new IntTag("WEOriginX", getOrigin().getBlockX()));
|
|
|
|
schematic.put("WEOriginY", new IntTag("WEOriginY", getOrigin().getBlockY()));
|
|
|
|
schematic.put("WEOriginZ", new IntTag("WEOriginZ", getOrigin().getBlockZ()));
|
2010-11-17 06:34:18 +00:00
|
|
|
schematic.put("WEOffsetX", new IntTag("WEOffsetX", getOffset().getBlockX()));
|
|
|
|
schematic.put("WEOffsetY", new IntTag("WEOffsetY", getOffset().getBlockY()));
|
|
|
|
schematic.put("WEOffsetZ", new IntTag("WEOffsetZ", getOffset().getBlockZ()));
|
2010-10-03 17:44:52 +00:00
|
|
|
|
2010-10-14 08:31:05 +00:00
|
|
|
// Copy
|
2010-10-13 17:08:53 +00:00
|
|
|
byte[] blocks = new byte[width * height * length];
|
2010-10-13 23:49:35 +00:00
|
|
|
byte[] blockData = new byte[width * height * length];
|
2010-10-14 08:31:05 +00:00
|
|
|
ArrayList<Tag> tileEntities = new ArrayList<Tag>();
|
|
|
|
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
for (int y = 0; y < height; ++y) {
|
|
|
|
for (int z = 0; z < length; ++z) {
|
2010-10-13 17:08:53 +00:00
|
|
|
int index = y * width * length + z * width + x;
|
2011-11-23 01:29:48 +00:00
|
|
|
blocks[index] = (byte) data[x][y][z].getType();
|
|
|
|
blockData[index] = (byte) data[x][y][z].getData();
|
2010-10-14 08:31:05 +00:00
|
|
|
|
|
|
|
// Store TileEntity data
|
|
|
|
if (data[x][y][z] instanceof TileEntityBlock) {
|
|
|
|
TileEntityBlock tileEntityBlock =
|
2011-11-23 01:29:48 +00:00
|
|
|
(TileEntityBlock) data[x][y][z];
|
2010-10-14 08:31:05 +00:00
|
|
|
|
|
|
|
// Get the list of key/values from the block
|
2011-11-23 01:29:48 +00:00
|
|
|
Map<String, Tag> values = tileEntityBlock.toTileEntityNBT();
|
2010-10-14 08:31:05 +00:00
|
|
|
if (values != null) {
|
|
|
|
values.put("id", new StringTag("id",
|
|
|
|
tileEntityBlock.getTileEntityID()));
|
|
|
|
values.put("x", new IntTag("x", x));
|
|
|
|
values.put("y", new IntTag("y", y));
|
|
|
|
values.put("z", new IntTag("z", z));
|
|
|
|
CompoundTag tileEntityTag =
|
|
|
|
new CompoundTag("TileEntity", values);
|
|
|
|
tileEntities.add(tileEntityTag);
|
|
|
|
}
|
|
|
|
}
|
2010-10-03 17:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2010-10-03 17:44:52 +00:00
|
|
|
schematic.put("Blocks", new ByteArrayTag("Blocks", blocks));
|
2010-10-13 23:49:35 +00:00
|
|
|
schematic.put("Data", new ByteArrayTag("Data", blockData));
|
2010-10-03 17:44:52 +00:00
|
|
|
schematic.put("Entities", new ListTag("Entities", CompoundTag.class, new ArrayList<Tag>()));
|
2010-10-14 08:31:05 +00:00
|
|
|
schematic.put("TileEntities", new ListTag("TileEntities", CompoundTag.class, tileEntities));
|
2010-10-03 17:44:52 +00:00
|
|
|
|
|
|
|
// Build and output
|
|
|
|
CompoundTag schematicTag = new CompoundTag("Schematic", schematic);
|
|
|
|
NBTOutputStream stream = new NBTOutputStream(new FileOutputStream(path));
|
|
|
|
stream.writeTag(schematicTag);
|
|
|
|
stream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a .schematic file into a clipboard.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2010-10-03 17:44:52 +00:00
|
|
|
* @param path
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return clipboard
|
2010-10-20 09:12:16 +00:00
|
|
|
* @throws DataException
|
2010-10-03 17:44:52 +00:00
|
|
|
* @throws IOException
|
|
|
|
*/
|
2011-01-27 09:51:34 +00:00
|
|
|
public static CuboidClipboard loadSchematic(File path)
|
2010-10-20 09:12:16 +00:00
|
|
|
throws DataException, IOException {
|
2010-10-03 17:44:52 +00:00
|
|
|
FileInputStream stream = new FileInputStream(path);
|
2011-03-10 07:10:59 +00:00
|
|
|
NBTInputStream nbtStream = new NBTInputStream(
|
|
|
|
new GZIPInputStream(stream));
|
2010-10-14 08:31:05 +00:00
|
|
|
|
2010-11-07 05:23:43 +00:00
|
|
|
Vector origin = new Vector();
|
2010-11-17 06:34:18 +00:00
|
|
|
Vector offset = new Vector();
|
2010-11-07 05:23:43 +00:00
|
|
|
|
2010-10-14 08:31:05 +00:00
|
|
|
// Schematic tag
|
2011-11-23 01:29:48 +00:00
|
|
|
CompoundTag schematicTag = (CompoundTag) nbtStream.readTag();
|
2010-10-03 17:44:52 +00:00
|
|
|
if (!schematicTag.getName().equals("Schematic")) {
|
2010-10-20 09:12:16 +00:00
|
|
|
throw new DataException("Tag \"Schematic\" does not exist or is not first");
|
2010-10-03 17:44:52 +00:00
|
|
|
}
|
2010-10-14 08:31:05 +00:00
|
|
|
|
|
|
|
// Check
|
2011-11-23 01:29:48 +00:00
|
|
|
Map<String, Tag> schematic = schematicTag.getValue();
|
2010-10-03 17:44:52 +00:00
|
|
|
if (!schematic.containsKey("Blocks")) {
|
2010-10-20 09:12:16 +00:00
|
|
|
throw new DataException("Schematic file is missing a \"Blocks\" tag");
|
2010-10-03 17:44:52 +00:00
|
|
|
}
|
2010-10-14 08:31:05 +00:00
|
|
|
|
|
|
|
// Get information
|
2012-03-04 00:47:12 +00:00
|
|
|
short width = getChildTag(schematic, "Width", ShortTag.class).getValue();
|
|
|
|
short length = getChildTag(schematic, "Length", ShortTag.class).getValue();
|
|
|
|
short height = getChildTag(schematic, "Height", ShortTag.class).getValue();
|
2010-10-14 08:31:05 +00:00
|
|
|
|
2010-11-07 05:23:43 +00:00
|
|
|
try {
|
2012-03-04 00:47:12 +00:00
|
|
|
int originX = getChildTag(schematic, "WEOriginX", IntTag.class).getValue();
|
|
|
|
int originY = getChildTag(schematic, "WEOriginY", IntTag.class).getValue();
|
|
|
|
int originZ = getChildTag(schematic, "WEOriginZ", IntTag.class).getValue();
|
2010-11-07 05:23:43 +00:00
|
|
|
origin = new Vector(originX, originY, originZ);
|
|
|
|
} catch (DataException e) {
|
|
|
|
// No origin data
|
|
|
|
}
|
|
|
|
|
2010-11-17 06:34:18 +00:00
|
|
|
try {
|
2012-03-04 00:47:12 +00:00
|
|
|
int offsetX = getChildTag(schematic, "WEOffsetX", IntTag.class).getValue();
|
|
|
|
int offsetY = getChildTag(schematic, "WEOffsetY", IntTag.class).getValue();
|
|
|
|
int offsetZ = getChildTag(schematic, "WEOffsetZ", IntTag.class).getValue();
|
2010-11-17 06:34:18 +00:00
|
|
|
offset = new Vector(offsetX, offsetY, offsetZ);
|
|
|
|
} catch (DataException e) {
|
|
|
|
// No offset data
|
|
|
|
}
|
|
|
|
|
2010-10-14 08:31:05 +00:00
|
|
|
// Check type of Schematic
|
2012-03-04 00:47:12 +00:00
|
|
|
String materials = getChildTag(schematic, "Materials", StringTag.class).getValue();
|
2010-10-03 17:44:52 +00:00
|
|
|
if (!materials.equals("Alpha")) {
|
2010-10-20 09:12:16 +00:00
|
|
|
throw new DataException("Schematic file is not an Alpha schematic");
|
2010-10-03 17:44:52 +00:00
|
|
|
}
|
2010-10-14 08:31:05 +00:00
|
|
|
|
|
|
|
// Get blocks
|
2012-03-04 00:47:12 +00:00
|
|
|
byte[] blocks = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
|
|
|
|
byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
|
2010-10-03 17:44:52 +00:00
|
|
|
|
2010-10-14 08:31:05 +00:00
|
|
|
// Need to pull out tile entities
|
2012-03-04 00:47:12 +00:00
|
|
|
List<Tag> tileEntities = (List<Tag>)getChildTag(schematic, "TileEntities", ListTag.class)
|
2010-10-15 07:24:25 +00:00
|
|
|
.getValue();
|
2011-11-23 01:29:48 +00:00
|
|
|
Map<BlockVector, Map<String, Tag>> tileEntitiesMap =
|
|
|
|
new HashMap<BlockVector, Map<String, Tag>>();
|
2010-10-14 08:31:05 +00:00
|
|
|
|
|
|
|
for (Tag tag : tileEntities) {
|
|
|
|
if (!(tag instanceof CompoundTag)) continue;
|
2011-11-23 01:29:48 +00:00
|
|
|
CompoundTag t = (CompoundTag) tag;
|
2010-10-03 17:44:52 +00:00
|
|
|
|
2010-10-14 08:31:05 +00:00
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
int z = 0;
|
|
|
|
|
2011-11-23 01:29:48 +00:00
|
|
|
Map<String, Tag> values = new HashMap<String, Tag>();
|
2010-10-14 08:31:05 +00:00
|
|
|
|
2011-11-23 01:29:48 +00:00
|
|
|
for (Map.Entry<String, Tag> entry : t.getValue().entrySet()) {
|
2010-10-14 08:31:05 +00:00
|
|
|
if (entry.getKey().equals("x")) {
|
|
|
|
if (entry.getValue() instanceof IntTag) {
|
2011-11-23 01:29:48 +00:00
|
|
|
x = ((IntTag) entry.getValue()).getValue();
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
|
|
|
} else if (entry.getKey().equals("y")) {
|
|
|
|
if (entry.getValue() instanceof IntTag) {
|
2011-11-23 01:29:48 +00:00
|
|
|
y = ((IntTag) entry.getValue()).getValue();
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
|
|
|
} else if (entry.getKey().equals("z")) {
|
|
|
|
if (entry.getValue() instanceof IntTag) {
|
2011-11-23 01:29:48 +00:00
|
|
|
z = ((IntTag) entry.getValue()).getValue();
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
values.put(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockVector vec = new BlockVector(x, y, z);
|
|
|
|
tileEntitiesMap.put(vec, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector size = new Vector(width, height, length);
|
2010-10-13 17:08:53 +00:00
|
|
|
CuboidClipboard clipboard = new CuboidClipboard(size);
|
2010-11-07 05:23:43 +00:00
|
|
|
clipboard.setOrigin(origin);
|
2010-11-17 06:34:18 +00:00
|
|
|
clipboard.setOffset(offset);
|
2010-10-13 17:08:53 +00:00
|
|
|
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
for (int y = 0; y < height; ++y) {
|
|
|
|
for (int z = 0; z < length; ++z) {
|
2010-10-13 17:08:53 +00:00
|
|
|
int index = y * width * length + z * width + x;
|
2010-10-14 08:31:05 +00:00
|
|
|
BlockVector pt = new BlockVector(x, y, z);
|
|
|
|
BaseBlock block;
|
2010-10-25 06:42:56 +00:00
|
|
|
|
2011-09-05 01:53:39 +00:00
|
|
|
switch (blocks[index]) {
|
|
|
|
case BlockID.WALL_SIGN:
|
|
|
|
case BlockID.SIGN_POST:
|
2010-10-14 08:31:05 +00:00
|
|
|
block = new SignBlock(blocks[index], blockData[index]);
|
2011-09-05 01:53:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BlockID.CHEST:
|
2011-01-23 08:36:26 +00:00
|
|
|
block = new ChestBlock(blockData[index]);
|
2011-09-05 01:53:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BlockID.FURNACE:
|
|
|
|
case BlockID.BURNING_FURNACE:
|
2011-01-23 08:36:26 +00:00
|
|
|
block = new FurnaceBlock(blocks[index], blockData[index]);
|
2011-09-05 01:53:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BlockID.DISPENSER:
|
2011-01-23 08:36:26 +00:00
|
|
|
block = new DispenserBlock(blockData[index]);
|
2011-09-05 01:53:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BlockID.MOB_SPAWNER:
|
2011-01-23 08:36:26 +00:00
|
|
|
block = new MobSpawnerBlock(blockData[index]);
|
2011-09-05 01:53:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BlockID.NOTE_BLOCK:
|
2011-01-23 08:36:26 +00:00
|
|
|
block = new NoteBlock(blockData[index]);
|
2011-09-05 01:53:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-10-14 08:31:05 +00:00
|
|
|
block = new BaseBlock(blocks[index], blockData[index]);
|
2011-09-05 01:53:39 +00:00
|
|
|
break;
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
2011-09-24 19:32:03 +00:00
|
|
|
|
|
|
|
if (block instanceof TileEntityBlock
|
2011-01-23 08:36:26 +00:00
|
|
|
&& tileEntitiesMap.containsKey(pt)) {
|
2011-11-23 01:29:48 +00:00
|
|
|
((TileEntityBlock) block).fromTileEntityNBT(
|
2011-01-23 08:36:26 +00:00
|
|
|
tileEntitiesMap.get(pt));
|
|
|
|
}
|
2010-10-14 08:31:05 +00:00
|
|
|
|
|
|
|
clipboard.data[x][y][z] = block;
|
2010-10-03 17:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clipboard;
|
|
|
|
}
|
|
|
|
|
2010-10-13 17:08:53 +00:00
|
|
|
/**
|
|
|
|
* Get child tag of a NBT structure.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2010-10-13 17:08:53 +00:00
|
|
|
* @param items
|
|
|
|
* @param key
|
|
|
|
* @param expected
|
|
|
|
* @return child tag
|
2010-10-20 09:12:16 +00:00
|
|
|
* @throws DataException
|
2010-10-13 17:08:53 +00:00
|
|
|
*/
|
2012-03-04 00:47:12 +00:00
|
|
|
private static <T extends Tag> T getChildTag(Map<String, Tag> items, String key,
|
|
|
|
Class<T> expected) throws DataException {
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2010-10-03 17:44:52 +00:00
|
|
|
if (!items.containsKey(key)) {
|
2010-10-20 09:12:16 +00:00
|
|
|
throw new DataException("Schematic file is missing a \"" + key + "\" tag");
|
2010-10-03 17:44:52 +00:00
|
|
|
}
|
|
|
|
Tag tag = items.get(key);
|
|
|
|
if (!expected.isInstance(tag)) {
|
2010-10-20 09:12:16 +00:00
|
|
|
throw new DataException(
|
2010-10-03 17:44:52 +00:00
|
|
|
key + " tag is not of tag type " + expected.getName());
|
|
|
|
}
|
2012-03-04 00:47:12 +00:00
|
|
|
return expected.cast(tag);
|
2010-10-03 17:44:52 +00:00
|
|
|
}
|
2010-11-07 05:23:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the origin
|
|
|
|
*/
|
|
|
|
public Vector getOrigin() {
|
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param origin the origin to set
|
|
|
|
*/
|
|
|
|
public void setOrigin(Vector origin) {
|
|
|
|
this.origin = origin;
|
|
|
|
}
|
2010-11-17 06:34:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the offset
|
|
|
|
*/
|
|
|
|
public Vector getOffset() {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-24 19:32:03 +00:00
|
|
|
* @param offset
|
2010-11-17 06:34:18 +00:00
|
|
|
*/
|
|
|
|
public void setOffset(Vector offset) {
|
|
|
|
this.offset = offset;
|
|
|
|
}
|
2010-10-02 23:11:44 +00:00
|
|
|
}
|