Compress nbt when sending it over the network

This commit is contained in:
Sollace 2024-05-25 15:03:26 +01:00
parent 1e3560fc04
commit 81db96d8a2
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
3 changed files with 18 additions and 7 deletions

View file

@ -29,7 +29,7 @@ public class DataTracker {
}
public <T extends TrackableObject> Entry<NbtCompound> startTracking(T value) {
Entry<NbtCompound> entry = startTracking(TrackableDataType.NBT, value.toTrackedNbt());
Entry<NbtCompound> entry = startTracking(TrackableDataType.COMPRESSED_NBT, value.toTrackedNbt());
persistentObjects.put(entry.id(), value);
return entry;
}
@ -136,6 +136,7 @@ public class DataTracker {
tracker.set(this, t);
}
}
static class Pair<T> {
private final TrackableDataType<T> type;
public final int id;

View file

@ -50,7 +50,7 @@ public record MsgTrackedValues(
this(
buffer.readInt(),
buffer.readCollection(HashSet::new, PacketByteBuf::readUuid),
buffer.readMap(HashMap::new, PacketByteBuf::readUuid, PacketCodec.NBT::read)
buffer.readMap(HashMap::new, PacketByteBuf::readUuid, PacketCodec.COMPRESSED_NBT::read)
);
}
@ -58,7 +58,7 @@ public record MsgTrackedValues(
public void write(PacketByteBuf buffer) {
buffer.writeInt(id);
buffer.writeCollection(removedValues, PacketByteBuf::writeUuid);
buffer.writeMap(values, PacketByteBuf::writeUuid, PacketCodec.NBT::write);
buffer.writeMap(values, PacketByteBuf::writeUuid, PacketCodec.COMPRESSED_NBT::write);
}
}

View file

@ -2,7 +2,6 @@ package com.minelittlepony.unicopia.util.serialization;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiConsumer;
@ -12,6 +11,7 @@ import java.util.function.Supplier;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtIo;
import net.minecraft.network.PacketByteBuf;
@ -33,16 +33,26 @@ public record PacketCodec<T>(PacketByteBuf.PacketReader<T> reader, PacketByteBuf
public static final PacketCodec<Identifier> IDENTIFIER = STRING.xMap(Identifier::new, Identifier::toString);
public static final PacketCodec<NbtCompound> NBT = new PacketCodec<>(PacketByteBuf::readNbt, PacketByteBuf::writeNbt);
public static final PacketCodec<NbtCompound> COMPRESSED_NBT = new PacketCodec<>(buffer -> {
public static final PacketCodec<PacketByteBuf> RAW_BYTES = new PacketCodec<>(
buffer -> new PacketByteBuf(buffer.readBytes(buffer.readInt())),
(buffer, bytes) -> {
buffer.writeInt(bytes.writerIndex());
buffer.writeBytes(bytes);
});
public static final PacketCodec<NbtCompound> COMPRESSED_NBT = RAW_BYTES.xMap(buffer -> {
try (InputStream in = new ByteBufInputStream(buffer)) {
return NbtIo.readCompressed(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
}, (buffer, nbt) -> {
try (OutputStream out = new ByteBufOutputStream(buffer)) {
}, nbt -> {
var buffer = new PacketByteBuf(Unpooled.buffer());
try (ByteBufOutputStream out = new ByteBufOutputStream(buffer)) {
NbtIo.writeCompressed(nbt, out);
return buffer;
} catch (IOException e) {
throw new RuntimeException(e);
}
});