456 lines
16 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// 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, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-09-22 13:02:14 +02:00
2014-11-16 10:48:18 +01:00
package com.intellectualcrafters.plot.util;
2014-09-22 13:02:14 +02:00
2015-01-14 03:38:15 +11:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
2014-12-17 20:15:11 -06:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
2014-12-16 16:03:20 +11:00
2015-01-14 03:38:15 +11:00
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.Block;
import com.intellectualcrafters.jnbt.ByteArrayTag;
import com.intellectualcrafters.jnbt.CompoundTag;
import com.intellectualcrafters.jnbt.IntTag;
import com.intellectualcrafters.jnbt.ListTag;
import com.intellectualcrafters.jnbt.NBTInputStream;
import com.intellectualcrafters.jnbt.NBTOutputStream;
import com.intellectualcrafters.jnbt.ShortTag;
import com.intellectualcrafters.jnbt.StringTag;
import com.intellectualcrafters.jnbt.Tag;
2015-02-19 19:51:10 +11:00
import com.intellectualcrafters.plot.PlotSquared;
2015-02-19 21:47:05 +11:00
import com.intellectualcrafters.plot.object.Location;
2015-01-14 03:38:15 +11:00
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
2015-02-19 22:06:27 +11:00
import com.intellectualcrafters.plot.util.bukkit.BukkitUtil;
2015-01-14 03:38:15 +11:00
2014-09-22 13:02:14 +02:00
/**
2014-10-28 01:57:14 +01:00
* Schematic Handler
2014-11-05 14:42:08 +11:00
*
2014-10-28 01:57:14 +01:00
* @author Citymonstret
* @author Empire92
2014-09-22 13:02:14 +02:00
*/
public class SchematicHandler {
2014-10-28 01:57:14 +01:00
/**
* Paste a schematic
2014-11-05 14:42:08 +11:00
*
2014-12-17 20:15:11 -06:00
* @param location origin
* @param schematic schematic to paste
* @param plot plot to paste in
*
2014-10-28 01:57:14 +01:00
* @return true if succeeded
*/
2014-11-05 14:42:08 +11:00
public static boolean paste(final Location location, final Schematic schematic, final Plot plot, final int x_offset, final int z_offset) {
if (schematic == null) {
2015-02-19 21:23:36 +11:00
PlotSquared.log("Schematic == null :|");
2014-11-05 14:42:08 +11:00
return false;
}
try {
final Dimension demensions = schematic.getSchematicDimension();
final int WIDTH = demensions.getX();
final int LENGTH = demensions.getZ();
final int HEIGHT = demensions.getY();
final DataCollection[] blocks = schematic.getBlockCollection();
2015-02-19 21:47:05 +11:00
Location l1 = PlotHelper.getPlotBottomLoc(plot.world, plot.getId());
2014-11-05 14:42:08 +11:00
2015-02-19 22:06:27 +11:00
final int sy = BukkitUtil.getHeighestBlock(location.getWorld(), l1.getX() + 1, l1.getZ() + 1);
2014-11-05 14:42:08 +11:00
l1 = l1.add(1, sy - 1, 1);
int y_offset;
2015-02-19 22:06:27 +11:00
if (HEIGHT == BukkitUtil.getMaxHeight(location.getWorld())) {
2014-11-05 14:42:08 +11:00
y_offset = 0;
2014-12-17 20:15:11 -06:00
} else {
2015-02-19 21:47:05 +11:00
y_offset = l1.getY();
2014-11-05 14:42:08 +11:00
}
2015-02-19 22:06:27 +11:00
int[] xl = new int[blocks.length];
int[] yl = new int[blocks.length];
int[] zl = new int[blocks.length];
int[] ids = new int[blocks.length];
byte[] data = new byte[blocks.length];
2014-11-05 14:42:08 +11:00
for (int x = 0; x < WIDTH; x++) {
for (int z = 0; z < LENGTH; z++) {
for (int y = 0; y < HEIGHT; y++) {
final int index = (y * WIDTH * LENGTH) + (z * WIDTH) + x;
final DataCollection block = blocks[index];
2015-02-19 22:06:27 +11:00
xl[index] = x;
yl[index] = y;
zl[index] = z;
ids[index] = block.block;
data[index] = block.data;
2014-11-05 14:42:08 +11:00
}
}
2014-10-15 20:43:20 +11:00
}
2015-02-19 22:06:27 +11:00
BlockManager.setBlocks(plot.world, xl, yl, zl, ids, data);
2014-12-17 20:15:11 -06:00
} catch (final Exception e) {
2014-11-05 14:42:08 +11:00
return false;
}
return true;
}
2014-12-28 21:38:11 +11:00
public static Schematic getSchematic(final CompoundTag tag, File file) {
final Map<String, Tag> tagMap = tag.getValue();
byte[] addId = new byte[0];
if (tagMap.containsKey("AddBlocks")) {
addId = ByteArrayTag.class.cast(tagMap.get("AddBlocks")).getValue();
}
final short width = ShortTag.class.cast(tagMap.get("Width")).getValue();
final short length = ShortTag.class.cast(tagMap.get("Length")).getValue();
final short height = ShortTag.class.cast(tagMap.get("Height")).getValue();
final byte[] b = ByteArrayTag.class.cast(tagMap.get("Blocks")).getValue();
final byte[] d = ByteArrayTag.class.cast(tagMap.get("Data")).getValue();
final short[] blocks = new short[b.length];
final Dimension dimension = new Dimension(width, height, length);
for (int index = 0; index < b.length; index++) {
if ((index >> 1) >= addId.length) { // No corresponding
// AddBlocks index
blocks[index] = (short) (b[index] & 0xFF);
} else {
if ((index & 1) == 0) {
blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (b[index] & 0xFF));
} else {
blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (b[index] & 0xFF));
}
}
}
final DataCollection[] collection = new DataCollection[b.length];
for (int x = 0; x < b.length; x++) {
collection[x] = new DataCollection(blocks[x], d[x]);
}
return new Schematic(collection, dimension, file);
}
2014-10-28 01:57:14 +01:00
/**
* Get a schematic
2014-11-05 14:42:08 +11:00
*
2014-12-17 20:15:11 -06:00
* @param name to check
*
2014-10-28 01:57:14 +01:00
* @return schematic if found, else null
*/
2014-11-05 14:42:08 +11:00
public static Schematic getSchematic(final String name) {
{
2015-02-19 22:06:27 +11:00
final File parent = new File(PlotSquared.IMP.getDirectory() + File.separator + "schematics");
2014-11-05 14:42:08 +11:00
if (!parent.exists()) {
2014-11-20 00:00:38 +01:00
if (!parent.mkdir()) {
throw new RuntimeException("Could not create schematic parent directory");
}
2014-11-05 14:42:08 +11:00
}
}
2015-02-19 22:06:27 +11:00
final File file = new File(PlotSquared.IMP.getDirectory() + File.separator + "schematics" + File.separator + name + ".schematic");
2014-11-05 14:42:08 +11:00
if (!file.exists()) {
2015-02-19 21:23:36 +11:00
PlotSquared.log(file.toString() + " doesn't exist");
2014-11-05 14:42:08 +11:00
return null;
}
try {
final InputStream iStream = new FileInputStream(file);
final NBTInputStream stream = new NBTInputStream(new GZIPInputStream(iStream));
final CompoundTag tag = (CompoundTag) stream.readTag();
stream.close();
2014-12-28 21:38:11 +11:00
return getSchematic(tag, file);
2014-11-05 14:42:08 +11:00
2014-12-17 20:15:11 -06:00
} catch (final Exception e) {
2015-02-19 21:23:36 +11:00
PlotSquared.log(file.toString() + " is not in GZIP format");
2014-11-05 14:42:08 +11:00
return null;
}
}
2014-11-05 14:42:08 +11:00
/**
* Saves a schematic to a file path
*
2014-12-17 20:15:11 -06:00
* @param tag to save
* @param path to save in
*
2014-11-05 14:42:08 +11:00
* @return true if succeeded
*/
public static boolean save(final CompoundTag tag, final String path) {
if (tag == null) {
2015-02-19 21:23:36 +11:00
PlotSquared.log("&cCannot save empty tag");
2014-11-05 14:42:08 +11:00
return false;
}
2014-10-22 19:46:41 +11:00
try {
2014-12-28 21:38:11 +11:00
File tmp = new File(path);
tmp.getParentFile().mkdirs();
2014-11-05 14:42:08 +11:00
final OutputStream stream = new FileOutputStream(path);
final NBTOutputStream output = new NBTOutputStream(new GZIPOutputStream(stream));
2014-10-22 19:46:41 +11:00
output.writeTag(tag);
output.close();
stream.close();
2014-12-17 20:15:11 -06:00
} catch (final IOException e) {
2014-10-22 19:46:41 +11:00
e.printStackTrace();
return false;
}
return true;
2014-11-05 14:42:08 +11:00
}
/**
* Gets the schematic of a plot
*
2014-12-17 20:15:11 -06:00
* @param world to check
* @param id plot
*
2014-11-05 14:42:08 +11:00
* @return tag
*/
2014-12-28 21:38:11 +11:00
public static CompoundTag getCompoundTag(final World world, PlotId id) {
2015-02-19 19:51:10 +11:00
if (!PlotSquared.getPlots(world).containsKey(id)) {
2014-11-05 14:42:08 +11:00
return null;
}
2014-12-28 21:38:11 +11:00
2014-11-05 14:42:08 +11:00
final Location pos1 = PlotHelper.getPlotBottomLoc(world, id).add(1, 0, 1);
2014-10-25 15:27:12 +11:00
final Location pos2 = PlotHelper.getPlotTopLoc(world, id);
2014-12-28 21:38:11 +11:00
return getCompoundTag(world, pos1, pos2);
}
@SuppressWarnings("deprecation")
2015-02-19 22:06:27 +11:00
public static CompoundTag getCompoundTag(final String world, Location pos1, Location pos2) {
2014-12-28 21:38:11 +11:00
// loading chunks
int i = 0;
int j = 0;
try {
2015-02-19 21:47:05 +11:00
for (i = (pos1.getX() / 16) * 16; i < (16 + ((pos2.getX() / 16) * 16)); i += 16) {
for (j = (pos1.getZ() / 16) * 16; j < (16 + ((pos2.getZ() / 16) * 16)); j += 16) {
2015-02-19 22:06:27 +11:00
final Chunk chunk = BukkitUtil.getChunkAt(world, i, j);
2014-11-05 14:42:08 +11:00
final boolean result = chunk.load(false);
if (!result) {
// Plot is not even generated
return null;
}
}
2014-10-22 19:46:41 +11:00
}
2014-12-17 20:15:11 -06:00
} catch (final Exception e) {
2015-02-19 21:23:36 +11:00
PlotSquared.log("&7 - Cannot save: corrupt chunk at " + (i / 16) + ", " + (j / 16));
return null;
}
2015-02-19 21:47:05 +11:00
final int width = (pos2.getX() - pos1.getX()) + 1;
final int height = pos2.getY() - pos1.getY() + 1;
final int length = (pos2.getZ() - pos1.getZ()) + 1;
2014-10-22 23:22:00 +11:00
2014-11-05 14:42:08 +11:00
final HashMap<String, Tag> schematic = new HashMap<>();
2014-10-22 23:22:00 +11:00
schematic.put("Width", new ShortTag("Width", (short) width));
schematic.put("Length", new ShortTag("Length", (short) length));
schematic.put("Height", new ShortTag("Height", (short) height));
schematic.put("Materials", new StringTag("Materials", "Alpha"));
schematic.put("WEOriginX", new IntTag("WEOriginX", 0));
schematic.put("WEOriginY", new IntTag("WEOriginY", 0));
schematic.put("WEOriginZ", new IntTag("WEOriginZ", 0));
schematic.put("WEOffsetX", new IntTag("WEOffsetX", 0));
schematic.put("WEOffsetY", new IntTag("WEOffsetY", 0));
schematic.put("WEOffsetZ", new IntTag("WEOffsetZ", 0));
2014-11-05 14:42:08 +11:00
final byte[] blocks = new byte[width * height * length];
2014-10-22 23:22:00 +11:00
byte[] addBlocks = null;
2014-11-05 14:42:08 +11:00
final byte[] blockData = new byte[width * height * length];
2015-02-19 21:47:05 +11:00
int sx = pos1.getX();
int ex = pos2.getX();
2014-12-28 21:38:11 +11:00
2015-02-19 21:47:05 +11:00
int sz = pos1.getZ();
int ez = pos2.getZ();
2014-12-28 21:38:11 +11:00
2015-02-19 21:47:05 +11:00
int sy = pos1.getY();
int ey = pos2.getY();
2014-12-28 21:38:11 +11:00
for (int x = 0; x < width; x++) {
for (int z = 0; z < length; z++) {
for (int y = 0; y < height; y++) {
2014-11-05 14:42:08 +11:00
final int index = (y * width * length) + (z * width) + x;
2015-02-19 22:06:27 +11:00
block = BukkitUtil.getBlock(new Location(world, sx + x, sy + y, sz + z));
2014-12-17 20:15:11 -06:00
@SuppressWarnings("deprecation") final int id2 = block.getTypeId();
2014-10-28 01:57:14 +01:00
2014-10-25 15:27:12 +11:00
if (id2 > 255) {
if (addBlocks == null) {
addBlocks = new byte[(blocks.length >> 1) + 1];
}
2014-11-05 14:42:08 +11:00
addBlocks[index >> 1] = (byte) (((index & 1) == 0) ? (addBlocks[index >> 1] & 0xF0) | ((id2 >> 8) & 0xF) : (addBlocks[index >> 1] & 0xF) | (((id2 >> 8) & 0xF) << 4));
}
2014-10-25 15:27:12 +11:00
blocks[index] = (byte) id2;
2014-10-28 01:57:14 +01:00
blockData[index] = block.getData();
2014-11-05 14:42:08 +11:00
// We need worldedit to save tileentity data or entities
2014-11-05 14:42:08 +11:00
// - it uses NMS and CB internal code, which changes every
// update
2014-10-22 23:22:00 +11:00
}
}
}
schematic.put("Blocks", new ByteArrayTag("Blocks", blocks));
schematic.put("Data", new ByteArrayTag("Data", blockData));
schematic.put("Entities", new ListTag("Entities", CompoundTag.class, new ArrayList<Tag>()));
schematic.put("TileEntities", new ListTag("TileEntities", CompoundTag.class, new ArrayList<Tag>()));
2014-11-05 14:42:08 +11:00
2014-10-22 23:22:00 +11:00
if (addBlocks != null) {
schematic.put("AddBlocks", new ByteArrayTag("AddBlocks", addBlocks));
}
2014-10-28 01:57:14 +01:00
return new CompoundTag("Schematic", schematic);
2014-10-22 19:46:41 +11:00
}
2015-02-19 21:47:05 +11:00
public static boolean pastePart(final String world, final DataCollection[] blocks, final Location l1, final int x_offset, final int z_offset, final int i1, final int i2, final int WIDTH, final int LENGTH) {
2015-02-19 22:06:27 +11:00
int length = 0;
for (int i = i1; i <= i2; i++) {
if (blocks[i].block == 0) {
length++;
}
}
length = i2 - length;
int[] xl = new int[length];
int[] yl = new int[length];
int[] zl = new int[length];
int[] ids = new int[length];
byte[] data = new byte[length];
int count = 0;
2014-11-05 14:42:08 +11:00
for (int i = i1; i <= i2; i++) {
2015-02-19 21:47:05 +11:00
final short id = blocks[i].block;
2014-11-05 14:42:08 +11:00
if (id == 0) {
continue;
}
2015-02-19 22:06:27 +11:00
count++;
2015-02-19 21:47:05 +11:00
2014-11-05 14:42:08 +11:00
final int area = WIDTH * LENGTH;
final int r = i % (area);
final int x = r % WIDTH;
final int y = i / area;
final int z = r / WIDTH;
2015-02-19 21:47:05 +11:00
2015-02-19 22:06:27 +11:00
xl[count] = x;
yl[count] = y;
zl[count] = z;
2015-02-19 21:47:05 +11:00
2015-02-19 22:06:27 +11:00
ids[count] = id;
data[count] = blocks[i].data;
2014-11-05 14:42:08 +11:00
if (y > 256) {
break;
}
}
2015-02-19 21:47:05 +11:00
BlockManager.setBlocks(world, xl, yl, zl, ids, data);
return true;
}
2014-11-16 10:48:18 +01:00
/**
* Schematic Class
*
* @author Citymonstret
*/
public static class Schematic {
private final DataCollection[] blockCollection;
2014-12-17 20:15:11 -06:00
private final Dimension schematicDimension;
private final File file;
2014-11-16 10:48:18 +01:00
public Schematic(final DataCollection[] blockCollection, final Dimension schematicDimension, final File file) {
this.blockCollection = blockCollection;
this.schematicDimension = schematicDimension;
this.file = file;
}
public File getFile() {
return this.file;
}
public Dimension getSchematicDimension() {
return this.schematicDimension;
}
public DataCollection[] getBlockCollection() {
return this.blockCollection;
}
}
/**
* Schematic Dimensions
*
* @author Citymonstret
*/
public static class Dimension {
private final int x;
private final int y;
private final int z;
public Dimension(final int x, final int y, final int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getZ() {
return this.z;
}
}
/**
* Schematic Data Collection
*
* @author Citymonstret
*/
public static class DataCollection {
private final short block;
2014-12-17 20:15:11 -06:00
private final byte data;
2014-11-16 10:48:18 +01:00
// public CompoundTag tag;
public DataCollection(final short block, final byte data) {
this.block = block;
this.data = data;
}
public short getBlock() {
return this.block;
}
public byte getData() {
return this.data;
}
}
2014-09-22 13:02:14 +02:00
}