Search in sources :

Example 6 with Snapshot

use of buildcraft.builders.snapshot.Snapshot in project BuildCraft by BuildCraft.

the class TileElectronicLibrary method writePayload.

// How networking works here:
// down:
// 1. server sends NET_DOWN with snapshot to clients
// 2. clients add snapshot to their local database
// up:
// 1. server sends empty NET_UP to clients
// 2. client who have selected snapshot sends NET_UP with it back to server
// 3. server adds snapshot to its database
@Override
public void writePayload(int id, PacketBufferBC buffer, Side side) {
    super.writePayload(id, buffer, side);
    if (side == Side.SERVER) {
        if (id == NET_RENDER_DATA) {
            buffer.writeBoolean(selected != null);
            if (selected != null) {
                selected.writeToByteBuf(buffer);
            }
        }
        if (id == NET_DOWN) {
            Snapshot.Header header = BCBuildersItems.snapshot.getHeader(invDownIn.getStackInSlot(0));
            if (header != null) {
                Snapshot snapshot = GlobalSavedDataSnapshots.get(world).getSnapshot(header.key);
                if (snapshot != null) {
                    snapshot = snapshot.copy();
                    snapshot.key = new Snapshot.Key(snapshot.key, header);
                    buffer.writeBoolean(true);
                    NbtSquisher.squish(Snapshot.writeToNBT(snapshot), NbtSquishConstants.BUILDCRAFT_V1_COMPRESSED, buffer);
                } else {
                    buffer.writeBoolean(false);
                }
            } else {
                buffer.writeBoolean(false);
            }
        }
        // noinspection StatementWithEmptyBody
        if (id == NET_UP) {
        }
    }
}
Also used : ItemSnapshot(buildcraft.builders.item.ItemSnapshot) Snapshot(buildcraft.builders.snapshot.Snapshot)

Example 7 with Snapshot

use of buildcraft.builders.snapshot.Snapshot in project BuildCraft by BuildCraft.

the class TileElectronicLibrary method readPayload.

@Override
public void readPayload(int id, PacketBufferBC buffer, Side side, MessageContext ctx) throws IOException {
    super.readPayload(id, buffer, side, ctx);
    if (side == Side.CLIENT) {
        if (id == NET_RENDER_DATA) {
            if (buffer.readBoolean()) {
                selected = new Snapshot.Key(buffer);
            } else {
                selected = null;
            }
        }
        if (id == NET_DOWN) {
            if (buffer.readBoolean()) {
                Snapshot snapshot = Snapshot.readFromNBT(NbtSquisher.expand(buffer));
                snapshot.computeKey();
                GlobalSavedDataSnapshots.get(world).addSnapshot(snapshot);
            }
        }
        if (id == NET_UP) {
            if (selected != null) {
                Snapshot snapshot = GlobalSavedDataSnapshots.get(world).getSnapshot(selected);
                if (snapshot != null) {
                    try (OutputStream outputStream = new OutputStream() {

                        private byte[] buf = new byte[4 * 1024];

                        private int pos = 0;

                        private boolean closed = false;

                        private void write(boolean last) throws IOException {
                            MessageManager.sendToServer(createMessage(NET_UP, localBuffer -> {
                                localBuffer.writeUniqueId(ctx.getClientHandler().getGameProfile().getId());
                                selected.writeToByteBuf(localBuffer);
                                localBuffer.writeBoolean(last);
                                localBuffer.writeByteArray(buf);
                            }));
                        }

                        @Override
                        public void write(int b) throws IOException {
                            buf[pos++] = (byte) b;
                            if (pos >= buf.length) {
                                write(false);
                                buf = new byte[buf.length];
                                pos = 0;
                            }
                        }

                        @Override
                        public void close() throws IOException {
                            if (closed) {
                                return;
                            }
                            closed = true;
                            buf = Arrays.copyOf(buf, pos);
                            pos = 0;
                            write(true);
                        }
                    }) {
                        NbtSquisher.squish(Snapshot.writeToNBT(snapshot), NbtSquishConstants.BUILDCRAFT_V1_COMPRESSED, outputStream);
                    }
                }
            }
        }
    }
    if (side == Side.SERVER) {
        if (id == NET_UP) {
            UUID playerId = buffer.readUniqueId();
            Snapshot.Key key = new Snapshot.Key(buffer);
            Pair<UUID, Snapshot.Key> pair = Pair.of(playerId, key);
            boolean last = buffer.readBoolean();
            upSnapshotsParts.computeIfAbsent(pair, localPair -> new ArrayList<>()).add(buffer.readByteArray());
            if (last && upSnapshotsParts.containsKey(pair)) {
                try {
                    Snapshot snapshot = Snapshot.readFromNBT(NbtSquisher.expand(Bytes.concat(upSnapshotsParts.get(pair).toArray(new byte[0][]))));
                    Snapshot.Header header = snapshot.key.header;
                    snapshot = snapshot.copy();
                    snapshot.key = new Snapshot.Key(snapshot.key, (Snapshot.Header) null);
                    snapshot.computeKey();
                    GlobalSavedDataSnapshots.get(world).addSnapshot(snapshot);
                    invUpOut.setStackInSlot(0, BCBuildersItems.snapshot.getUsed(snapshot.getType(), header));
                    invUpIn.setStackInSlot(0, StackUtil.EMPTY);
                } finally {
                    upSnapshotsParts.remove(pair);
                }
            }
        }
    }
}
Also used : EnumAccess(buildcraft.lib.tile.item.ItemHandlerManager.EnumAccess) Arrays(java.util.Arrays) ItemSnapshot(buildcraft.builders.item.ItemSnapshot) Snapshot(buildcraft.builders.snapshot.Snapshot) ItemHandlerSimple(buildcraft.lib.tile.item.ItemHandlerSimple) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) Pair(org.apache.commons.lang3.tuple.Pair) Side(net.minecraftforge.fml.relauncher.Side) NbtSquishConstants(buildcraft.api.data.NbtSquishConstants) Map(java.util.Map) ITickable(net.minecraft.util.ITickable) IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) MessageManager(buildcraft.lib.net.MessageManager) DeltaInt(buildcraft.lib.delta.DeltaInt) Nonnull(javax.annotation.Nonnull) OutputStream(java.io.OutputStream) DeltaManager(buildcraft.lib.delta.DeltaManager) PacketBufferBC(buildcraft.lib.net.PacketBufferBC) IdAllocator(buildcraft.lib.misc.data.IdAllocator) BCBuildersItems(buildcraft.builders.BCBuildersItems) NbtSquisher(buildcraft.lib.nbt.NbtSquisher) TileBC_Neptune(buildcraft.lib.tile.TileBC_Neptune) IOException(java.io.IOException) UUID(java.util.UUID) Bytes(com.google.common.primitives.Bytes) List(java.util.List) MessageContext(net.minecraftforge.fml.common.network.simpleimpl.MessageContext) GlobalSavedDataSnapshots(buildcraft.builders.snapshot.GlobalSavedDataSnapshots) StackUtil(buildcraft.lib.misc.StackUtil) EnumPipePart(buildcraft.api.core.EnumPipePart) StackInsertionFunction(buildcraft.lib.tile.item.StackInsertionFunction) ItemSnapshot(buildcraft.builders.item.ItemSnapshot) Snapshot(buildcraft.builders.snapshot.Snapshot) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) UUID(java.util.UUID)

Aggregations

Snapshot (buildcraft.builders.snapshot.Snapshot)7 ItemSnapshot (buildcraft.builders.item.ItemSnapshot)6 Blueprint (buildcraft.builders.snapshot.Blueprint)3 ItemStack (net.minecraft.item.ItemStack)3 EnumPipePart (buildcraft.api.core.EnumPipePart)2 BCBuildersItems (buildcraft.builders.BCBuildersItems)2 GlobalSavedDataSnapshots (buildcraft.builders.snapshot.GlobalSavedDataSnapshots)2 Header (buildcraft.builders.snapshot.Snapshot.Header)2 SnapshotBuilder (buildcraft.builders.snapshot.SnapshotBuilder)2 Template (buildcraft.builders.snapshot.Template)2 IdAllocator (buildcraft.lib.misc.data.IdAllocator)2 MessageManager (buildcraft.lib.net.MessageManager)2 PacketBufferBC (buildcraft.lib.net.PacketBufferBC)2 Date (java.util.Date)2 EnumFacing (net.minecraft.util.EnumFacing)2 IPathProvider (buildcraft.api.core.IPathProvider)1 InvalidInputDataException (buildcraft.api.core.InvalidInputDataException)1 NbtSquishConstants (buildcraft.api.data.NbtSquishConstants)1 EnumOptionalSnapshotType (buildcraft.api.enums.EnumOptionalSnapshotType)1 EnumSnapshotType (buildcraft.api.enums.EnumSnapshotType)1