Skip to content

Instantly share code, notes, and snippets.

@mcSw4p
Last active February 26, 2018 00:09
Show Gist options
  • Save mcSw4p/a75ed285f2e2be3ca84cf7e3d1b848e1 to your computer and use it in GitHub Desktop.
Save mcSw4p/a75ed285f2e2be3ca84cf7e3d1b848e1 to your computer and use it in GitHub Desktop.
Basic NBT Schematic load/pasting
package com.desive.schematic;
import net.minecraft.server.v1_12_R1.NBTCompressedStreamTools;
import net.minecraft.server.v1_12_R1.NBTTagCompound;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/*
Created by Jack DeSive on 2/23/2018 at 10:00 PM
*/
public class Schematic {
private short[] blocks;
private byte[] data;
private short width;
private short length;
private short height;
public Schematic(short[] blocks, byte[] data, short width, short length, short height) {
this.blocks = blocks;
this.data = data;
this.width = width;
this.length = length;
this.height = height;
}
public short[] getBlocks() {
return blocks;
}
public byte[] getData() {
return data;
}
public short getWidth() {
return width;
}
public short getLength() {
return length;
}
public short getHeight() {
return height;
}
public static void pasteSchematic(World world, Location loc, Schematic schematic) {
short[] blocks = schematic.getBlocks();
byte[] blockData = schematic.getData();
short length = schematic.getLength();
short width = schematic.getWidth();
short height = schematic.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
for (int z = 0; z < length; ++z) {
int index = y * width * length + z * width + x;
Block block = new Location(world, x + loc.getX(), y + loc.getY(), z + loc.getZ()).getBlock();
block.setTypeIdAndData(blocks[index], blockData[index], true);
}
}
}
}
public static Schematic loadSchematic(File file) throws IOException {
InputStream fis = new FileInputStream(file);
NBTTagCompound nbtData = NBTCompressedStreamTools.a(fis);
short width = nbtData.getShort("Width");
short length = nbtData.getShort("Length");
short height = nbtData.getShort("Height");
byte[] blockId = nbtData.getByteArray("Blocks");
byte[] blockData = nbtData.getByteArray("Data");
byte[] addId = new byte[0];
short[] blocks = new short[blockId.length];
if (nbtData.hasKey("AddBlocks")) {
addId = nbtData.getByteArray("AddBlocks");
}
for (int index = 0; index < blockId.length; index++) {
if ((index >> 1) >= addId.length) {
blocks[index] = (short) (blockId[index] & 0xFF);
} else {
if ((index & 1) == 0) {
blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (blockId[index] & 0xFF));
} else {
blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (blockId[index] & 0xFF));
}
}
}
return new Schematic(blocks, blockData, width, length, height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment