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) { 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); persistentObjects.put(entry.id(), value);
return entry; return entry;
} }
@ -136,6 +136,7 @@ public class DataTracker {
tracker.set(this, t); tracker.set(this, t);
} }
} }
static class Pair<T> { static class Pair<T> {
private final TrackableDataType<T> type; private final TrackableDataType<T> type;
public final int id; public final int id;

View file

@ -50,7 +50,7 @@ public record MsgTrackedValues(
this( this(
buffer.readInt(), buffer.readInt(),
buffer.readCollection(HashSet::new, PacketByteBuf::readUuid), 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) { public void write(PacketByteBuf buffer) {
buffer.writeInt(id); buffer.writeInt(id);
buffer.writeCollection(removedValues, PacketByteBuf::writeUuid); 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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@ -12,6 +11,7 @@ import java.util.function.Supplier;
import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream; import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtIo; import net.minecraft.nbt.NbtIo;
import net.minecraft.network.PacketByteBuf; 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<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> 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)) { try (InputStream in = new ByteBufInputStream(buffer)) {
return NbtIo.readCompressed(in); return NbtIo.readCompressed(in);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}, (buffer, nbt) -> { }, nbt -> {
try (OutputStream out = new ByteBufOutputStream(buffer)) { var buffer = new PacketByteBuf(Unpooled.buffer());
try (ByteBufOutputStream out = new ByteBufOutputStream(buffer)) {
NbtIo.writeCompressed(nbt, out); NbtIo.writeCompressed(nbt, out);
return buffer;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e);
} }
}); });