358 lines
11 KiB
Java
Raw Normal View History

2014-09-22 13:02:14 +02:00
package com.intellectualcrafters.plot;
2014-10-22 19:46:41 +11:00
import org.bukkit.Chunk;
import org.bukkit.Location;
2014-10-15 20:43:20 +11:00
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.plugin.java.JavaPlugin;
2014-10-15 20:43:20 +11:00
import com.intellectualcrafters.jnbt.*;
2014-09-22 13:02:14 +02:00
import java.io.File;
import java.io.FileInputStream;
2014-10-22 19:46:41 +11:00
import java.io.FileOutputStream;
import java.io.IOException;
2014-09-22 13:02:14 +02:00
import java.io.InputStream;
2014-10-22 19:46:41 +11:00
import java.io.OutputStream;
import java.util.ArrayList;
2014-10-22 19:46:41 +11:00
import java.util.HashMap;
2014-09-22 13:02:14 +02:00
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
2014-09-22 13:02:14 +02:00
/**
2014-10-28 01:57:14 +01:00
* Schematic Handler
* @author Citymonstret
* @author Empire92
2014-09-22 13:02:14 +02:00
*/
2014-10-28 01:57:14 +01:00
@SuppressWarnings({"all"})
2014-09-22 13:02:14 +02:00
public class SchematicHandler {
2014-10-28 01:57:14 +01:00
/**
* Paste a schematic
* @param location origin
* @param schematic schematic to paste
* @param plot plot to paste in
* @return true if succeeded
*/
2014-10-25 15:27:12 +11:00
public static boolean paste(Location location, Schematic schematic, Plot plot) {
if (schematic == null) {
PlotMain.sendConsoleSenderMessage("Schematic == null :|");
return false;
}
try {
2014-10-15 20:43:20 +11:00
Dimension demensions = schematic.getSchematicDimension();
int WIDTH = demensions.getX();
int LENGTH = demensions.getZ();
int HEIGHT = demensions.getY();
DataCollection[] blocks = schematic.getBlockCollection();
2014-10-15 21:36:23 +11:00
2014-10-15 20:43:20 +11:00
Location l1 = PlotHelper.getPlotBottomLoc(plot.getWorld(), plot.getId());
2014-10-15 21:36:23 +11:00
int sy = location.getWorld().getHighestBlockYAt(l1.getBlockX()+1, l1.getBlockZ()+1);
l1 = l1.add(1, sy-1, 1);
2014-10-15 20:43:20 +11:00
World world = location.getWorld();
2014-10-15 21:36:23 +11:00
for (int x = 0; x < WIDTH; x++) {
for (int z = 0; z < LENGTH; z++) {
2014-10-15 20:43:20 +11:00
for (int y = 0; y < HEIGHT; y++) {
int index = y * WIDTH * LENGTH + z * WIDTH + x;
short id = blocks[index].getBlock();
byte data = blocks[index].getData();
2014-10-15 21:36:23 +11:00
Block block = world.getBlockAt(l1.getBlockX()+x, l1.getBlockY()+y, l1.getBlockZ()+z);
2014-10-15 20:43:20 +11:00
PlotBlock plotblock = new PlotBlock(id, data);
PlotHelper.setBlock(block, plotblock);
}
}
}
}
catch (Exception e) {
return false;
}
return true;
}
2014-10-28 01:57:14 +01:00
/**
* Get a schematic
* @param name to check
* @return schematic if found, else null
*/
2014-10-25 15:27:12 +11:00
public static Schematic getSchematic(String name) {
{
File parent =
new File(JavaPlugin.getPlugin(PlotMain.class).getDataFolder() + File.separator + "schematics");
if (!parent.exists()) {
parent.mkdir();
}
}
File file =
new File(JavaPlugin.getPlugin(PlotMain.class).getDataFolder() + File.separator + "schematics"
+ File.separator + name + ".schematic");
if (!file.exists()) {
PlotMain.sendConsoleSenderMessage(file.toString() + " doesn't exist");
return null;
}
2014-10-28 01:57:14 +01:00
Schematic schematic;
try {
InputStream iStream = new FileInputStream(file);
NBTInputStream stream = new NBTInputStream(new GZIPInputStream(iStream));
CompoundTag tag = (CompoundTag) stream.readTag();
2014-10-12 23:08:48 +11:00
stream.close();
Map<String, Tag> tagMap = tag.getValue();
byte[] addId = new byte[0];
if (tagMap.containsKey("AddBlocks")) {
addId = ByteArrayTag.class.cast(tagMap.get("AddBlocks")).getValue();
}
short width = ShortTag.class.cast(tagMap.get("Width")).getValue();
short length = ShortTag.class.cast(tagMap.get("Length")).getValue();
short height = ShortTag.class.cast(tagMap.get("Height")).getValue();
byte[] b = ByteArrayTag.class.cast(tagMap.get("Blocks")).getValue();
byte[] d = ByteArrayTag.class.cast(tagMap.get("Data")).getValue();
short[] blocks = new short[b.length];
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));
}
}
}
DataCollection[] collection = new DataCollection[b.length];
for (int x = 0; x < b.length; x++) {
collection[x] = new DataCollection(blocks[x], d[x]);
}
schematic = new Schematic(collection, dimension, file);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
2014-10-12 23:08:48 +11:00
return schematic;
}
2014-10-28 01:57:14 +01:00
/**
* Schematic Class
* @author Citymonstret
*/
public static class Schematic {
private DataCollection[] blockCollection;
private Dimension schematicDimension;
private File file;
public Schematic(DataCollection[] blockCollection, Dimension schematicDimension, 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;
}
}
2014-10-28 01:57:14 +01:00
/**
* Schematic Dimensions
* @author Citymonstret
*/
2014-10-25 15:27:12 +11:00
public static class Dimension {
private int x;
private int y;
private int z;
public Dimension(int x, int y, 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;
}
}
/**
* Saves a schematic to a file path
2014-10-28 01:57:14 +01:00
* @param tag to save
* @param path to save in
* @return true if succeeded
*/
2014-10-25 15:27:12 +11:00
public static boolean save(CompoundTag tag, String path) {
if (tag==null) {
PlotMain.sendConsoleSenderMessage("&cCannot save empty tag");
return false;
}
2014-10-22 19:46:41 +11:00
try {
OutputStream stream = new FileOutputStream(path);
NBTOutputStream output = new NBTOutputStream(new GZIPOutputStream(stream));
2014-10-22 19:46:41 +11:00
output.writeTag(tag);
output.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* Gets the schematic of a plot
2014-10-28 01:57:14 +01:00
* @param world to check
* @param id plot
* @return tag
*/
2014-10-25 15:27:12 +11:00
public static CompoundTag getCompoundTag(World world, PlotId id) {
2014-10-22 19:46:41 +11:00
2014-10-25 15:27:12 +11:00
if (!PlotMain.getPlots(world).containsKey(id)) {
return null;
// Plot is empty
}
2014-10-22 19:46:41 +11:00
// loading chunks
2014-10-25 15:27:12 +11:00
final Location pos1 = PlotHelper.getPlotBottomLoc(world, id).add(1, 0, 1);
final Location pos2 = PlotHelper.getPlotTopLoc(world, id);
int i = 0;
int j = 0;
try {
for (i = (pos1.getBlockX() / 16) * 16; i < (16 + ((pos2.getBlockX() / 16) * 16)); i += 16) {
for (j = (pos1.getBlockZ() / 16) * 16; j < (16 + ((pos2.getBlockZ() / 16) * 16)); j += 16) {
2014-10-22 19:46:41 +11:00
Chunk chunk = world.getChunkAt(i, j);
boolean result = chunk.load(false);
if (!result) {
// Plot is not even generated
return null;
}
2014-10-22 19:46:41 +11:00
}
}
}
catch (Exception e) {
PlotMain.sendConsoleSenderMessage("&7 - Cannot save: corrupt chunk at "+(i/16)+", "+(j/16));
return null;
}
int width = pos2.getBlockX()-pos1.getBlockX();
int height = 256;
int length = pos2.getBlockZ()-pos1.getBlockZ();
2014-10-22 23:22:00 +11:00
2014-10-28 01:57:14 +01:00
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-10-22 23:22:00 +11:00
byte[] blocks = new byte[width * height * length];
byte[] addBlocks = null;
byte[] blockData = new byte[width * height * length];
for (int x = 0; x < width; x++) {
for (int z = 0; z < length; z++) {
for (int y = 0; y < height; y++) {
int index = y * width * length + z * width + x;
2014-10-28 01:57:14 +01:00
Block block = world.getBlockAt(new Location(world, pos1.getBlockX() + x, y, pos1.getBlockZ() + z));
2014-10-25 15:27:12 +11:00
int id2 = block.getTypeId();
2014-10-25 15:27:12 +11:00
if (id2 > 255) {
if (addBlocks == null) {
addBlocks = new byte[(blocks.length >> 1) + 1];
}
addBlocks[index >> 1] = (byte) (((index & 1) == 0) ?
2014-10-25 15:27:12 +11:00
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();
// We need worldedit to save tileentity data or entities
// - 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-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
}
2014-10-28 01:57:14 +01:00
/**
* Schematic Data Collection
* @author Citymonstret
*/
2014-10-25 15:27:12 +11:00
public static class DataCollection {
private short block;
private byte data;
public DataCollection(short block, 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
}