2014-09-22 13:02:14 +02:00
|
|
|
package com.intellectualcrafters.plot;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
|
2014-09-24 22:21:43 +10:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
|
|
|
import com.sk89q.jnbt.ByteArrayTag;
|
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
|
|
|
import com.sk89q.jnbt.NBTInputStream;
|
|
|
|
import com.sk89q.jnbt.ShortTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
|
|
|
import com.sk89q.worldedit.CuboidClipboard;
|
|
|
|
import com.sk89q.worldedit.EditSession;
|
|
|
|
import com.sk89q.worldedit.Vector;
|
|
|
|
import com.sk89q.worldedit.bukkit.BukkitWorld;
|
|
|
|
|
2014-09-22 13:02:14 +02:00
|
|
|
/**
|
|
|
|
* Created by Citymonstret on 2014-09-15.
|
|
|
|
*/
|
|
|
|
public class SchematicHandler {
|
|
|
|
|
2014-10-11 00:33:10 -07:00
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public boolean paste(Location location, Schematic schematic, Plot plot) {
|
|
|
|
if (schematic == null) {
|
|
|
|
PlotMain.sendConsoleSenderMessage("Schematic == null :|");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
2014-10-12 00:37:36 -07:00
|
|
|
EditSession session = new EditSession(new BukkitWorld(location.getWorld()), 999999999);
|
|
|
|
CuboidClipboard clipboard = CuboidClipboard.loadSchematic(schematic.getFile());
|
|
|
|
Location l1 = PlotHelper.getPlotBottomLoc(plot.getWorld(), plot.getId());
|
2014-10-11 00:33:10 -07:00
|
|
|
PlotHelper.getPlotTopLoc(plot.getWorld(), plot.getId());
|
|
|
|
int x = l1.getBlockX() + 1;
|
|
|
|
int z = l1.getBlockZ() + 1;
|
|
|
|
int y = location.getWorld().getHighestBlockYAt(x, z);
|
|
|
|
Vector v1 = new Vector(x, y + 1, z);
|
|
|
|
clipboard.paste(session, v1, true);
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2014-10-11 00:33:10 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Schematic getSchematic(String name) {
|
|
|
|
{
|
2014-10-12 00:37:36 -07:00
|
|
|
File parent =
|
|
|
|
new File(JavaPlugin.getPlugin(PlotMain.class).getDataFolder() + File.separator + "schematics");
|
2014-10-11 00:33:10 -07:00
|
|
|
if (!parent.exists()) {
|
|
|
|
parent.mkdir();
|
|
|
|
}
|
|
|
|
}
|
2014-10-12 00:37:36 -07:00
|
|
|
File file =
|
|
|
|
new File(JavaPlugin.getPlugin(PlotMain.class).getDataFolder() + File.separator + "schematics"
|
|
|
|
+ File.separator + name + ".schematic");
|
2014-10-11 00:33:10 -07:00
|
|
|
if (!file.exists()) {
|
2014-10-12 00:37:36 -07:00
|
|
|
PlotMain.sendConsoleSenderMessage(file.toString() + " doesn't exist");
|
2014-10-11 00:33:10 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Schematic schematic = null;
|
|
|
|
try {
|
|
|
|
InputStream iStream = new FileInputStream(file);
|
2014-10-12 00:37:36 -07:00
|
|
|
NBTInputStream stream = new NBTInputStream(new GZIPInputStream(iStream));
|
2014-10-11 00:33:10 -07:00
|
|
|
CompoundTag tag = (CompoundTag) stream.readTag();
|
|
|
|
Map<String, Tag> tagMap = tag.getValue();
|
|
|
|
|
|
|
|
byte[] addId = new byte[0];
|
|
|
|
if (tagMap.containsKey("AddBlocks")) {
|
2014-10-12 00:37:36 -07:00
|
|
|
addId = ByteArrayTag.class.cast(tagMap.get("AddBlocks")).getValue();
|
2014-10-11 00:33:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-10-12 00:37:36 -07:00
|
|
|
// AddBlocks index
|
2014-10-11 00:33:10 -07:00
|
|
|
blocks[index] = (short) (b[index] & 0xFF);
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
|
|
|
else {
|
2014-10-11 00:33:10 -07:00
|
|
|
if ((index & 1) == 0) {
|
|
|
|
blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (b[index] & 0xFF));
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
|
|
|
else {
|
2014-10-11 00:33:10 -07:00
|
|
|
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);
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2014-10-11 00:33:10 -07:00
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
|
|
|
finally {
|
2014-10-11 00:33:10 -07:00
|
|
|
return schematic;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Schematic {
|
|
|
|
private DataCollection[] blockCollection;
|
|
|
|
private Dimension schematicDimension;
|
|
|
|
private File file;
|
|
|
|
|
2014-10-12 00:37:36 -07:00
|
|
|
public Schematic(DataCollection[] blockCollection, Dimension schematicDimension, File file) {
|
2014-10-11 00:33:10 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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
|
|
|
}
|