Search in sources :

Example 26 with PacketBufferBC

use of buildcraft.lib.net.PacketBufferBC in project BuildCraft by BuildCraft.

the class PacketBufferBcTest method testInvalidEnum_0_read.

@Test(expected = IllegalArgumentException.class)
public void testInvalidEnum_0_read() {
    PacketBufferBC buffer = new PrintingByteBuf(Unpooled.buffer());
    /* Attempting to write out an enum value that doesn't have any values is definitely a bug */
    buffer.readEnumValue(Enum_0.class);
}
Also used : PacketBufferBC(buildcraft.lib.net.PacketBufferBC) Test(org.junit.Test)

Example 27 with PacketBufferBC

use of buildcraft.lib.net.PacketBufferBC in project BuildCraft by BuildCraft.

the class PacketBufferBcTest method testSizedEnums.

@Test
public void testSizedEnums() {
    PacketBufferBC buffer = new PrintingByteBuf(Unpooled.buffer());
    buffer.writeEnumValue(Enum_1.A);
    buffer.writeEnumValue(Enum_2.A);
    buffer.writeEnumValue(Enum_2.B);
    // @formatter:off
    for (Enum_3 e : Enum_3.values()) buffer.writeEnumValue(e);
    for (Enum_4 e : Enum_4.values()) buffer.writeEnumValue(e);
    for (Enum_5 e : Enum_5.values()) buffer.writeEnumValue(e);
    for (Enum_6 e : Enum_6.values()) buffer.writeEnumValue(e);
    for (Enum_7 e : Enum_7.values()) buffer.writeEnumValue(e);
    for (Enum_8 e : Enum_8.values()) buffer.writeEnumValue(e);
    for (Enum_9 e : Enum_9.values()) buffer.writeEnumValue(e);
    // @formatter:on
    Assert.assertEquals(Enum_1.A, buffer.readEnumValue(Enum_1.class));
    Assert.assertEquals(Enum_2.A, buffer.readEnumValue(Enum_2.class));
    Assert.assertEquals(Enum_2.B, buffer.readEnumValue(Enum_2.class));
    // @formatter:off
    for (Enum_3 e : Enum_3.values()) Assert.assertEquals(e, buffer.readEnumValue(Enum_3.class));
    for (Enum_4 e : Enum_4.values()) Assert.assertEquals(e, buffer.readEnumValue(Enum_4.class));
    for (Enum_5 e : Enum_5.values()) Assert.assertEquals(e, buffer.readEnumValue(Enum_5.class));
    for (Enum_6 e : Enum_6.values()) Assert.assertEquals(e, buffer.readEnumValue(Enum_6.class));
    for (Enum_7 e : Enum_7.values()) Assert.assertEquals(e, buffer.readEnumValue(Enum_7.class));
    for (Enum_8 e : Enum_8.values()) Assert.assertEquals(e, buffer.readEnumValue(Enum_8.class));
    for (Enum_9 e : Enum_9.values()) Assert.assertEquals(e, buffer.readEnumValue(Enum_9.class));
// @formatter:on
}
Also used : PacketBufferBC(buildcraft.lib.net.PacketBufferBC) Test(org.junit.Test)

Example 28 with PacketBufferBC

use of buildcraft.lib.net.PacketBufferBC 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)

Example 29 with PacketBufferBC

use of buildcraft.lib.net.PacketBufferBC in project BuildCraft by BuildCraft.

the class PipeFlowItems method readPayload.

// Network
@Override
public void readPayload(int id, PacketBuffer bufIn, Side msgSide) throws IOException {
    PacketBufferBC buffer = PacketBufferBC.asPacketBufferBc(bufIn);
    if (msgSide == Side.CLIENT) {
        if (id == NET_CREATE_ITEM) {
            int stackId = buffer.readInt();
            Supplier<ItemStack> link = BuildCraftObjectCaches.retrieveItemStack(stackId);
            int count = buffer.readUnsignedShort();
            TravellingItem item = new TravellingItem(link, count);
            item.toCenter = buffer.readBoolean();
            item.side = buffer.readEnumValue(EnumFacing.class);
            item.colour = MessageUtil.readEnumOrNull(buffer, EnumDyeColor.class);
            item.timeToDest = buffer.readUnsignedShort();
            item.tickStarted = pipe.getHolder().getPipeWorld().getTotalWorldTime() + 1;
            item.tickFinished = item.tickStarted + item.timeToDest;
            items.add(item.timeToDest + 1, item);
        }
    }
}
Also used : PacketBufferBC(buildcraft.lib.net.PacketBufferBC) EnumFacing(net.minecraft.util.EnumFacing) ItemStack(net.minecraft.item.ItemStack) EnumDyeColor(net.minecraft.item.EnumDyeColor)

Example 30 with PacketBufferBC

use of buildcraft.lib.net.PacketBufferBC in project BuildCraft by BuildCraft.

the class MessageUtil method readEnumSet.

public static <E extends Enum<E>> EnumSet<E> readEnumSet(ByteBuf buffer, Class<E> clazz) {
    PacketBufferBC buf = PacketBufferBC.asPacketBufferBc(buffer);
    E[] constants = clazz.getEnumConstants();
    if (constants == null)
        throw new IllegalArgumentException("Not an enum type " + clazz);
    EnumSet<E> set = EnumSet.noneOf(clazz);
    for (E e : constants) {
        if (buf.readBoolean()) {
            set.add(e);
        }
    }
    return set;
}
Also used : PacketBufferBC(buildcraft.lib.net.PacketBufferBC)

Aggregations

PacketBufferBC (buildcraft.lib.net.PacketBufferBC)31 Test (org.junit.Test)6 EnumPipePart (buildcraft.api.core.EnumPipePart)4 IOException (java.io.IOException)4 List (java.util.List)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 NBTUtilBC (buildcraft.lib.misc.NBTUtilBC)3 ArrayList (java.util.ArrayList)3 UUID (java.util.UUID)3 IntStream (java.util.stream.IntStream)3 EnumDyeColor (net.minecraft.item.EnumDyeColor)3 EnumFacing (net.minecraft.util.EnumFacing)3 BlockPos (net.minecraft.util.math.BlockPos)3 World (net.minecraft.world.World)3 Side (net.minecraftforge.fml.relauncher.Side)3 Box (buildcraft.lib.misc.data.Box)2 ByteBuf (io.netty.buffer.ByteBuf)2 ItemStack (net.minecraft.item.ItemStack)2 NBTUtil (net.minecraft.nbt.NBTUtil)2 PacketBuffer (net.minecraft.network.PacketBuffer)2