@Override public String getName() { return name(); }
@Override public Set<String> getFileExtensions() { return ImmutableSet.of(getPrimaryFileExtension()); }
/** * Detect the format of given a file. * * @param file * the file * @return the format, otherwise null if one cannot be detected */ @Nullable public static ClipboardFormat findByFile(File file) { checkNotNull(file); for (ClipboardFormat format : registeredFormats) { if (format.isFormat(file)) { return format; } } return null; }
File f = worldEdit.getSafeOpenFile(player, dir, filename, BuiltInClipboardFormat.SPONGE_SCHEMATIC.getPrimaryFileExtension(), ClipboardFormats.getFileExtensionArray()); ClipboardFormat format = ClipboardFormats.findByFile(f); if (format == null) { format = ClipboardFormats.findByAlias(formatName); FileInputStream fis = closer.register(new FileInputStream(f)); BufferedInputStream bis = closer.register(new BufferedInputStream(fis)); ClipboardReader reader = closer.register(format.getReader(bis)); Clipboard clipboard = reader.read(); session.setClipboard(new ClipboardHolder(clipboard));
ClipboardFormat format = ClipboardFormats.findByAlias(formatName); if (format == null) { player.printError("Unknown schematic format: " + formatName); File f = worldEdit.getSafeSaveFile(player, dir, filename, format.getPrimaryFileExtension()); ClipboardWriter writer = closer.register(format.getWriter(bos)); writer.write(target); log.info(player.getName() + " saved " + f.getCanonicalPath()); player.print(filename + " saved.");
String materials = requireTag(schematic, "Materials", StringTag.class).getValue(); if (!materials.equals("Alpha")) { throw new IOException("Schematic file is not an Alpha schematic"); short width = requireTag(schematic, "Width", ShortTag.class).getValue(); short height = requireTag(schematic, "Height", ShortTag.class).getValue(); short length = requireTag(schematic, "Length", ShortTag.class).getValue(); int originX = requireTag(schematic, "WEOriginX", IntTag.class).getValue(); int originY = requireTag(schematic, "WEOriginY", IntTag.class).getValue(); int originZ = requireTag(schematic, "WEOriginZ", IntTag.class).getValue(); BlockVector3 min = BlockVector3.at(originX, originY, originZ); int offsetX = requireTag(schematic, "WEOffsetX", IntTag.class).getValue(); int offsetY = requireTag(schematic, "WEOffsetY", IntTag.class).getValue(); int offsetZ = requireTag(schematic, "WEOffsetZ", IntTag.class).getValue(); BlockVector3 offset = BlockVector3.at(offsetX, offsetY, offsetZ); byte[] blockId = requireTag(schematic, "Blocks", ByteArrayTag.class).getValue(); byte[] blockData = requireTag(schematic, "Data", ByteArrayTag.class).getValue(); byte[] addId = new byte[0]; short[] blocks = new short[blockId.length]; // Have to later combine IDs addId = requireTag(schematic, "AddBlocks", ByteArrayTag.class).getValue(); List<Tag> tileEntities = requireTag(schematic, "TileEntities", ListTag.class).getValue(); Map<BlockVector3, Map<String, Tag>> tileEntitiesMap = new HashMap<>(); if (block != null) {
public static boolean pasteSchematic(File schematicFile, Location location, boolean withAir) { try { Vector pasteLocation = new Vector(location.getX(), location.getY(), location.getZ()); World pasteWorld = new BukkitWorld(location.getWorld()); WorldData pasteWorldData = pasteWorld.getWorldData(); Clipboard clipboard = ClipboardFormat.SCHEMATIC.getReader(new FileInputStream(schematicFile)).read(pasteWorldData); ClipboardHolder clipboardHolder = new ClipboardHolder(clipboard, pasteWorldData); EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(pasteWorld, -1); Operation operation = clipboardHolder .createPaste(editSession, pasteWorldData) .to(pasteLocation) .ignoreAirBlocks(!withAir) .build(); Operations.completeLegacy(operation); return true; } catch (IOException | MaxChangedBlocksException e) { FunnyGuildsLogger.exception(e); return false; } }
@Command( aliases = {"formats", "listformats", "f"}, desc = "List available formats", max = 0 ) @CommandPermissions("worldedit.schematic.formats") public void formats(Actor actor) throws WorldEditException { actor.print("Available clipboard formats (Name: Lookup names)"); StringBuilder builder; boolean first = true; for (ClipboardFormat format : ClipboardFormats.getAll()) { builder = new StringBuilder(); builder.append(format.getName()).append(": "); for (String lookupName : format.getAliases()) { if (!first) { builder.append(", "); } builder.append(lookupName); first = false; } first = true; actor.print(builder.toString()); } }
Map<String, Tag> metadata = requireTag(schematic, "Metadata", CompoundTag.class).getValue(); int width = requireTag(schematic, "Width", ShortTag.class).getValue(); int height = requireTag(schematic, "Height", ShortTag.class).getValue(); int length = requireTag(schematic, "Length", ShortTag.class).getValue(); int[] offsetParts = requireTag(schematic, "Offset", IntArrayTag.class).getValue(); if (offsetParts.length != 3) { throw new IOException("Invalid offset specified in schematic."); int offsetX = requireTag(metadata, "WEOffsetX", IntTag.class).getValue(); int offsetY = requireTag(metadata, "WEOffsetY", IntTag.class).getValue(); int offsetZ = requireTag(metadata, "WEOffsetZ", IntTag.class).getValue(); BlockVector3 offset = BlockVector3.at(offsetX, offsetY, offsetZ); origin = min.subtract(offset); int paletteMax = requireTag(schematic, "PaletteMax", IntTag.class).getValue(); Map<String, Tag> paletteObject = requireTag(schematic, "Palette", CompoundTag.class).getValue(); if (paletteObject.size() != paletteMax) { throw new IOException("Differing given palette size to actual size"); int id = requireTag(paletteObject, palettePart, IntTag.class).getValue(); BlockState state; try { byte[] blocks = requireTag(schematic, "BlockData", ByteArrayTag.class).getValue(); List<Map<String, Tag>> tileEntityTags = requireTag(schematic, "TileEntities", ListTag.class).getValue().stream() .map(tag -> (CompoundTag) tag)
private List<String> listFiles(String prefix, File[] files) { if (prefix == null) prefix = ""; List<String> result = new ArrayList<>(); for (File file : files) { StringBuilder build = new StringBuilder(); build.append("\u00a72"); ClipboardFormat format = ClipboardFormats.findByFile(file); boolean inRoot = file.getParentFile().getName().equals(prefix); build.append(inRoot ? file.getName() : file.getPath().split(Pattern.quote(prefix + File.separator))[1]) .append(": ").append(format == null ? "Unknown" : format.getName()); result.add(build.toString()); } return result; } }
public static void registerClipboardFormat(ClipboardFormat format) { checkNotNull(format); for (String key : format.getAliases()) { String lowKey = key.toLowerCase(Locale.ENGLISH); ClipboardFormat old = aliasMap.put(lowKey, format); if (old != null) { aliasMap.put(lowKey, old); WorldEdit.logger.warning(format.getClass().getName() + " cannot override existing alias '" + lowKey + "' used by " + old.getClass().getName()); } } for (String ext : format.getFileExtensions()) { String lowExt = ext.toLowerCase(Locale.ENGLISH); fileExtensionMap.put(lowExt, format); } registeredFormats.add(format); }
@Override public Clipboard read() throws IOException { NamedTag rootTag = inputStream.readNamedTag(); if (!rootTag.getName().equals("Schematic")) { throw new IOException("Tag 'Schematic' does not exist or is not first"); } CompoundTag schematicTag = (CompoundTag) rootTag.getTag(); // Check Map<String, Tag> schematic = schematicTag.getValue(); int version = requireTag(schematic, "Version", IntTag.class).getValue(); switch (version) { case 1: return readVersion1(schematic); default: throw new IOException("This schematic version is currently not supported"); } }
@Override public ClipboardWriter getWriter(OutputStream outputStream) throws IOException { NBTOutputStream nbtStream = new NBTOutputStream(new GZIPOutputStream(outputStream)); return new SpongeSchematicWriter(nbtStream); }
@Override public ClipboardReader getReader(InputStream inputStream) throws IOException { NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(inputStream)); return new MCEditSchematicReader(nbtStream); }
@Override public ClipboardReader getReader(InputStream inputStream) throws IOException { NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(inputStream)); return new SpongeSchematicReader(nbtStream); }
@Override public void write(Clipboard clipboard) throws IOException { // For now always write the latest version. Maybe provide support for earlier if more appear. outputStream.writeNamedTag("Schematic", new CompoundTag(write1(clipboard))); }