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