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);
}
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
}
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);
}
}
}
}
}
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);
}
}
}
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;
}
Aggregations