Search in sources :

Example 1 with NBTInputStream

use of com.sk89q.jnbt.NBTInputStream in project PlotSquared by IntellectualSites.

the class WorldUtil method upload.

public void upload(@NonNull final Plot plot, @Nullable final UUID uuid, @Nullable final String file, @NonNull final RunnableVal<URL> whenDone) {
    plot.getHome(home -> SchematicHandler.upload(uuid, file, "zip", new RunnableVal<>() {

        @Override
        public void run(OutputStream output) {
            try (final ZipOutputStream zos = new ZipOutputStream(output)) {
                File dat = getDat(plot.getWorldName());
                Location spawn = getSpawn(plot.getWorldName());
                if (dat != null) {
                    ZipEntry ze = new ZipEntry("world" + File.separator + dat.getName());
                    zos.putNextEntry(ze);
                    try (NBTInputStream nis = new NBTInputStream(new GZIPInputStream(new FileInputStream(dat)))) {
                        Map<String, Tag> tag = ((CompoundTag) nis.readNamedTag().getTag()).getValue();
                        Map<String, Tag> newMap = new HashMap<>();
                        for (Map.Entry<String, Tag> entry : tag.entrySet()) {
                            if (!entry.getKey().equals("Data")) {
                                newMap.put(entry.getKey(), entry.getValue());
                                continue;
                            }
                            Map<String, Tag> data = new HashMap<>(((CompoundTag) entry.getValue()).getValue());
                            data.put("SpawnX", new IntTag(home.getX()));
                            data.put("SpawnY", new IntTag(home.getY()));
                            data.put("SpawnZ", new IntTag(home.getZ()));
                            newMap.put("Data", new CompoundTag(data));
                        }
                        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                            try (NBTOutputStream out = new NBTOutputStream(new GZIPOutputStream(baos, true))) {
                                // TODO Find what this should be called
                                out.writeNamedTag("Schematic????", new CompoundTag(newMap));
                            }
                            zos.write(baos.toByteArray());
                        }
                    }
                }
                setSpawn(spawn);
                byte[] buffer = new byte[1024];
                Set<BlockVector2> added = new HashSet<>();
                for (Plot current : plot.getConnectedPlots()) {
                    Location bot = current.getBottomAbs();
                    Location top = current.getTopAbs();
                    int brx = bot.getX() >> 9;
                    int brz = bot.getZ() >> 9;
                    int trx = top.getX() >> 9;
                    int trz = top.getZ() >> 9;
                    Set<BlockVector2> files = getChunkChunks(bot.getWorldName());
                    for (BlockVector2 mca : files) {
                        if (mca.getX() >= brx && mca.getX() <= trx && mca.getZ() >= brz && mca.getZ() <= trz && !added.contains(mca)) {
                            final File file = getMcr(plot.getWorldName(), mca.getX(), mca.getZ());
                            if (file != null) {
                                // final String name = "r." + (x - cx) + "." + (z - cz) + ".mca";
                                String name = file.getName();
                                final ZipEntry ze = new ZipEntry("world" + File.separator + "region" + File.separator + name);
                                zos.putNextEntry(ze);
                                added.add(mca);
                                try (FileInputStream in = new FileInputStream(file)) {
                                    int len;
                                    while ((len = in.read(buffer)) > 0) {
                                        zos.write(buffer, 0, len);
                                    }
                                }
                                zos.closeEntry();
                            }
                        }
                    }
                }
                zos.closeEntry();
                zos.flush();
                zos.finish();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, whenDone));
}
Also used : HashMap(java.util.HashMap) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ZipEntry(java.util.zip.ZipEntry) GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) CompoundTag(com.sk89q.jnbt.CompoundTag) IntTag(com.sk89q.jnbt.IntTag) HashSet(java.util.HashSet) Plot(com.plotsquared.core.plot.Plot) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) BlockVector2(com.sk89q.worldedit.math.BlockVector2) FileInputStream(java.io.FileInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) RunnableVal(com.plotsquared.core.util.task.RunnableVal) NBTInputStream(com.sk89q.jnbt.NBTInputStream) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Location(com.plotsquared.core.location.Location)

Example 2 with NBTInputStream

use of com.sk89q.jnbt.NBTInputStream in project FastAsyncWorldEdit by IntellectualSites.

the class FaweDelegateSchematicHandler method getSchematic.

public Schematic getSchematic(@Nonnull InputStream is) {
    try {
        FastSchematicReader schematicReader = new FastSchematicReader(new NBTInputStream(new BufferedInputStream(new GZIPInputStream(new BufferedInputStream(is)))));
        Clipboard clip = schematicReader.read();
        return new Schematic(clip);
    } catch (IOException e) {
        if (e instanceof EOFException) {
            e.printStackTrace();
            return null;
        }
        try {
            SpongeSchematicReader schematicReader = new SpongeSchematicReader(new NBTInputStream(new GZIPInputStream(is)));
            Clipboard clip = schematicReader.read();
            return new Schematic(clip);
        } catch (IOException e2) {
            if (e2 instanceof EOFException) {
                e.printStackTrace();
                return null;
            }
            try {
                MCEditSchematicReader schematicReader = new MCEditSchematicReader(new NBTInputStream(new GZIPInputStream(is)));
                Clipboard clip = schematicReader.read();
                return new Schematic(clip);
            } catch (IOException e3) {
                LOGGER.warn("{} | {} : {}", is, is.getClass().getCanonicalName(), e.getMessage());
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) SpongeSchematicReader(com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicReader) BufferedInputStream(java.io.BufferedInputStream) FastSchematicReader(com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicReader) NBTInputStream(com.sk89q.jnbt.NBTInputStream) EOFException(java.io.EOFException) MCEditSchematicReader(com.sk89q.worldedit.extent.clipboard.io.MCEditSchematicReader) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) IOException(java.io.IOException) Schematic(com.plotsquared.core.plot.schematic.Schematic)

Example 3 with NBTInputStream

use of com.sk89q.jnbt.NBTInputStream in project FunnyGuilds by FunnyGuilds.

the class WorldEdit6Hook method pasteSchematic.

@Override
public boolean pasteSchematic(File schematicFile, Location location, boolean withAir) {
    try {
        Object pasteLocation = vectorConstructor.newInstance(location.getX(), location.getY(), location.getZ());
        com.sk89q.worldedit.world.World pasteWorld = new BukkitWorld(location.getWorld());
        Object pasteWorldData = getWorldData.invoke(pasteWorld);
        NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(new FileInputStream(schematicFile)));
        Object reader = schematicReaderConstructor.newInstance(nbtStream);
        Object clipboard = readSchematic.invoke(reader, pasteWorldData);
        EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(pasteWorld, -1);
        ClipboardHolder clipboardHolder = (ClipboardHolder) clipboardHolderConstructor.newInstance(clipboard, pasteWorldData);
        PasteBuilder builder = ((PasteBuilder) pasteConstructor.newInstance(clipboardHolder, editSession, pasteWorldData));
        builder = (PasteBuilder) pasteBuilderSetTo.invoke(builder, pasteLocation);
        builder = builder.ignoreAirBlocks(!withAir);
        Operations.completeLegacy(builder.build());
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException | MaxChangedBlocksException | IOException ex) {
        throw new RuntimeException("Could not paste schematic: " + schematicFile.getAbsolutePath(), ex);
    }
    return true;
}
Also used : ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) MaxChangedBlocksException(com.sk89q.worldedit.MaxChangedBlocksException) GZIPInputStream(java.util.zip.GZIPInputStream) PasteBuilder(com.sk89q.worldedit.session.PasteBuilder) NBTInputStream(com.sk89q.jnbt.NBTInputStream) EditSession(com.sk89q.worldedit.EditSession)

Example 4 with NBTInputStream

use of com.sk89q.jnbt.NBTInputStream in project FastAsyncWorldEdit by IntellectualSites.

the class CompressedCompoundTag method decompress.

private void decompress() {
    try (NBTInputStream nbtIn = new NBTInputStream(adapt(in))) {
        in = null;
        CompoundTag tag = (CompoundTag) nbtIn.readTag();
        Map<String, Tag> value = tag.getValue();
        Map<String, Tag> raw = super.getValue();
        raw.putAll(value);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : NBTInputStream(com.sk89q.jnbt.NBTInputStream) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) IOException(java.io.IOException) CompoundTag(com.sk89q.jnbt.CompoundTag)

Example 5 with NBTInputStream

use of com.sk89q.jnbt.NBTInputStream in project PlotSquared by IntellectualSites.

the class SchematicHandler method getSchematic.

public Schematic getSchematic(@NonNull InputStream is) {
    try {
        SpongeSchematicReader schematicReader = new SpongeSchematicReader(new NBTInputStream(new GZIPInputStream(is)));
        Clipboard clip = schematicReader.read();
        return new Schematic(clip);
    } catch (IOException ignored) {
        try {
            MCEditSchematicReader schematicReader = new MCEditSchematicReader(new NBTInputStream(new GZIPInputStream(is)));
            Clipboard clip = schematicReader.read();
            return new Schematic(clip);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) SpongeSchematicReader(com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicReader) NBTInputStream(com.sk89q.jnbt.NBTInputStream) MCEditSchematicReader(com.sk89q.worldedit.extent.clipboard.io.MCEditSchematicReader) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) IOException(java.io.IOException) Schematic(com.plotsquared.core.plot.schematic.Schematic)

Aggregations

NBTInputStream (com.sk89q.jnbt.NBTInputStream)5 IOException (java.io.IOException)5 GZIPInputStream (java.util.zip.GZIPInputStream)4 Schematic (com.plotsquared.core.plot.schematic.Schematic)2 CompoundTag (com.sk89q.jnbt.CompoundTag)2 Tag (com.sk89q.jnbt.Tag)2 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)2 MCEditSchematicReader (com.sk89q.worldedit.extent.clipboard.io.MCEditSchematicReader)2 SpongeSchematicReader (com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicReader)2 FileInputStream (java.io.FileInputStream)2 FastSchematicReader (com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicReader)1 Location (com.plotsquared.core.location.Location)1 Plot (com.plotsquared.core.plot.Plot)1 RunnableVal (com.plotsquared.core.util.task.RunnableVal)1 IntTag (com.sk89q.jnbt.IntTag)1 NBTOutputStream (com.sk89q.jnbt.NBTOutputStream)1 EditSession (com.sk89q.worldedit.EditSession)1 MaxChangedBlocksException (com.sk89q.worldedit.MaxChangedBlocksException)1 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)1 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)1