Created
February 8, 2019 15:18
-
-
Save Cryptite/be602089180e9b7f7e23c8241830ec06 to your computer and use it in GitHub Desktop.
WorldEdit Schematic Loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final BuildingPreviewExtent customExtent; | |
private abstract static class BuildingPreviewExtent implements Extent { | |
@Override | |
public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 position, T block) { | |
return false; | |
} | |
} | |
static { | |
customExtent = new BuildingPreviewExtent() { | |
@Override | |
public boolean setBiome(BlockVector2 position, BaseBiome biome) { | |
return false; | |
} | |
@Nullable | |
@Override | |
public Operation commit() { | |
return null; | |
} | |
@Override | |
public BlockState getBlock(BlockVector3 position) { | |
return null; | |
} | |
@Override | |
public BaseBlock getFullBlock(BlockVector3 position) { | |
return null; | |
} | |
@Override | |
public BaseBiome getBiome(BlockVector2 position) { | |
return null; | |
} | |
@Override | |
public BlockVector3 getMinimumPoint() { | |
return null; | |
} | |
@Override | |
public BlockVector3 getMaximumPoint() { | |
return null; | |
} | |
@Override | |
public List<? extends Entity> getEntities(Region region) { | |
return null; | |
} | |
@Override | |
public List<? extends Entity> getEntities() { | |
return null; | |
} | |
@Nullable | |
@Override | |
public Entity createEntity(com.sk89q.worldedit.util.Location location, BaseEntity entity) { | |
return null; | |
} | |
}; | |
} | |
public static BuildingPreview loadSchematic(File saveFile, Location loc, int angle) throws IOException, WorldEditException { | |
ClipboardFormat format = ClipboardFormats.findByFile(saveFile); | |
try (ClipboardReader reader = format.getReader(new FileInputStream(saveFile))) { | |
Clipboard clipboard = reader.read(); | |
ClipboardHolder clipboardHolder = new ClipboardHolder(clipboard); | |
clipboardHolder.setTransform(new AffineTransform().rotateY(angle)); | |
Operation operation = clipboardHolder | |
.createPaste(customExtent) | |
.to(BukkitAdapter.asBlockVector(loc)) | |
.build(); | |
Operations.complete(operation); | |
List<StringBlock> blocks = new ArrayList<>(); | |
BuildingPreview pv = new BuildingPreview(blocks); | |
Region region = clipboard.getRegion(); | |
BlockVector3 minimumPoint = region.getMinimumPoint(); | |
BlockVector3 maximumPoint = region.getMaximumPoint(); | |
pv.p1 = new StringLocation(loc.getWorld(), minimumPoint); | |
pv.p2 = new StringLocation(loc.getWorld(), maximumPoint); | |
for (int y = minimumPoint.getY(); y <= maximumPoint.getY(); y++) { | |
for (int x = minimumPoint.getX(); x <= maximumPoint.getX(); x++) { | |
for (int z = minimumPoint.getZ(); z <= maximumPoint.getZ(); z++) { | |
final BaseBlock fullBlock = clipboard.getFullBlock(Vector3.toBlockPoint(x, y, z)); | |
if (fullBlock == null) continue; | |
blocks.add(new StringBlock(loc.getWorld(), x, y, z, BukkitAdapter.adapt(fullBlock))); | |
} | |
} | |
} | |
return pv; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment