Search in sources :

Example 6 with LibraryId

use of buildcraft.core.blueprints.LibraryId in project BuildCraft by BuildCraft.

the class TileBlueprintLibrary method receiveCommand.

@Override
public void receiveCommand(String command, Side side, Object sender, ByteBuf stream) {
    if (side.isClient()) {
        if ("requestSelectedBlueprint".equals(command)) {
            if (isOutputConsistent()) {
                if (selected > -1 && selected < entries.size()) {
                    // Work around 32k max limit on client->server
                    final NBTTagCompound compound = BuildCraftBuilders.clientDB.load(entries.get(selected));
                    compound.setString("__filename", entries.get(selected).name);
                    final byte[] bptData = NBTUtilBC.save(compound);
                    final int chunks = (bptData.length + CHUNK_SIZE - 1) / CHUNK_SIZE;
                    BuildCraftCore.instance.sendToServer(new PacketCommand(this, "uploadServerBegin", new CommandWriter() {

                        @Override
                        public void write(ByteBuf data) {
                            entries.get(selected).writeData(data);
                            data.writeShort(chunks);
                        }
                    }));
                    for (int i = 0; i < chunks; i++) {
                        final int chunk = i;
                        final int start = CHUNK_SIZE * chunk;
                        final int length = Math.min(CHUNK_SIZE, bptData.length - start);
                        BuildCraftCore.instance.sendToServer(new PacketCommand(this, "uploadServerChunk", new CommandWriter() {

                            @Override
                            public void write(ByteBuf data) {
                                data.writeShort(chunk);
                                data.writeShort(length);
                                data.writeBytes(bptData, start, length);
                            }
                        }));
                    }
                    BuildCraftCore.instance.sendToServer(new PacketCommand(this, "uploadServerEnd", null));
                } else {
                    BuildCraftCore.instance.sendToServer(new PacketCommand(this, "uploadNothingToServer", null));
                }
            }
        } else if ("downloadBlueprintToClient".equals(command)) {
            LibraryId id = new LibraryId();
            id.readData(stream);
            byte[] data = NetworkUtils.readByteArray(stream);
            try {
                LibraryTypeHandler handler = LibraryAPI.getHandlerFor(id.extension);
                if (handler == null) {
                    return;
                }
                NBTTagCompound nbt = CompressedStreamTools.readCompressed(new ByteArrayInputStream(data));
                BuildCraftBuilders.clientDB.add(id, nbt);
                entries = BuildCraftBuilders.clientDB.getBlueprintIds();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else if (side.isServer()) {
        if ("uploadNothingToServer".equals(command)) {
            setInventorySlotContents(3, getStackInSlot(2));
            setInventorySlotContents(2, null);
            downloadingPlayer = null;
        } else if ("uploadServerBegin".equals(command)) {
            blueprintDownloadId = new LibraryId();
            blueprintDownloadId.readData(stream);
            blueprintDownload = new byte[CHUNK_SIZE * stream.readUnsignedShort()];
        } else if ("uploadServerChunk".equals(command)) {
            int start = stream.readUnsignedShort() * CHUNK_SIZE;
            int length = stream.readUnsignedShort();
            if (blueprintDownload != null) {
                stream.readBytes(blueprintDownload, start, length);
            } else {
                stream.skipBytes(length);
            }
        } else if ("uploadServerEnd".equals(command)) {
            try {
                LibraryTypeHandler handler = LibraryAPI.getHandlerFor(blueprintDownloadId.extension);
                if (handler != null) {
                    ItemStack output = null;
                    if (handler instanceof LibraryTypeHandlerNBT) {
                        NBTTagCompound nbt = CompressedStreamTools.readCompressed(new ByteArrayInputStream(blueprintDownload));
                        output = ((LibraryTypeHandlerNBT) handler).load(getStackInSlot(2), nbt);
                    } else if (handler instanceof LibraryTypeHandlerByteArray) {
                        output = ((LibraryTypeHandlerByteArray) handler).load(getStackInSlot(2), blueprintDownload);
                    }
                    if (output != null) {
                        setInventorySlotContents(3, output);
                        setInventorySlotContents(2, null);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            blueprintDownloadId = null;
            blueprintDownload = null;
            downloadingPlayer = null;
        } else if ("selectBlueprint".equals(command)) {
            selected = stream.readInt();
        }
    }
}
Also used : NBTTagCompound(net.minecraft.nbt.NBTTagCompound) PacketCommand(buildcraft.core.lib.network.command.PacketCommand) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) LibraryId(buildcraft.core.blueprints.LibraryId) LibraryTypeHandlerByteArray(buildcraft.api.library.LibraryTypeHandlerByteArray) ByteArrayInputStream(java.io.ByteArrayInputStream) LibraryTypeHandlerNBT(buildcraft.api.library.LibraryTypeHandlerNBT) LibraryTypeHandler(buildcraft.api.library.LibraryTypeHandler) CommandWriter(buildcraft.core.lib.network.command.CommandWriter) ItemStack(net.minecraft.item.ItemStack)

Example 7 with LibraryId

use of buildcraft.core.blueprints.LibraryId in project BuildCraft by BuildCraft.

the class ItemBlueprint method getId.

public static LibraryId getId(ItemStack stack) {
    NBTTagCompound nbt = NBTUtilBC.getItemData(stack);
    if (nbt == null) {
        return null;
    }
    LibraryId id = new LibraryId();
    id.read(nbt);
    if (BuildCraftBuilders.serverDB.exists(id)) {
        return id;
    } else {
        return null;
    }
}
Also used : LibraryId(buildcraft.core.blueprints.LibraryId) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Aggregations

LibraryId (buildcraft.core.blueprints.LibraryId)7 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)5 LibraryTypeHandler (buildcraft.api.library.LibraryTypeHandler)2 LibraryTypeHandlerByteArray (buildcraft.api.library.LibraryTypeHandlerByteArray)2 LibraryTypeHandlerNBT (buildcraft.api.library.LibraryTypeHandlerNBT)2 CommandWriter (buildcraft.core.lib.network.command.CommandWriter)2 PacketCommand (buildcraft.core.lib.network.command.PacketCommand)2 ByteBuf (io.netty.buffer.ByteBuf)2 IBlueprintItem (buildcraft.api.items.IBlueprintItem)1 Blueprint (buildcraft.core.blueprints.Blueprint)1 BlueprintBase (buildcraft.core.blueprints.BlueprintBase)1 BptBuilderBlueprint (buildcraft.core.blueprints.BptBuilderBlueprint)1 BptContext (buildcraft.core.blueprints.BptContext)1 Template (buildcraft.core.blueprints.Template)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ItemStack (net.minecraft.item.ItemStack)1 Vec3d (net.minecraft.util.math.Vec3d)1