Make LightData record

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-12-01 21:26:51 +01:00
parent 9e24aaab77
commit 9d26fa8137
2 changed files with 19 additions and 40 deletions

View File

@ -8,36 +8,15 @@ import org.jetbrains.annotations.NotNull;
import java.util.BitSet;
import java.util.List;
public final class LightData implements Writeable {
private final boolean trustEdges;
private final BitSet skyMask;
private final BitSet blockMask;
private final BitSet emptySkyMask;
private final BitSet emptyBlockMask;
private final List<byte[]> skyLight;
private final List<byte[]> blockLight;
public LightData(boolean trustEdges,
BitSet skyMask, BitSet blockMask,
BitSet emptySkyMask, BitSet emptyBlockMask,
List<byte[]> skyLight, List<byte[]> blockLight) {
this.trustEdges = trustEdges;
this.skyMask = skyMask;
this.blockMask = blockMask;
this.emptySkyMask = emptySkyMask;
this.emptyBlockMask = emptyBlockMask;
this.skyLight = skyLight;
this.blockLight = blockLight;
}
public record LightData(boolean trustEdges,
@NotNull BitSet skyMask, @NotNull BitSet blockMask,
@NotNull BitSet emptySkyMask, @NotNull BitSet emptyBlockMask,
@NotNull List<byte[]> skyLight, @NotNull List<byte[]> blockLight) implements Writeable {
public LightData(BinaryReader reader) {
this.trustEdges = reader.readBoolean();
this.skyMask = BitSet.valueOf(reader.readLongArray());
this.blockMask = BitSet.valueOf(reader.readLongArray());
this.emptySkyMask = BitSet.valueOf(reader.readLongArray());
this.emptyBlockMask = BitSet.valueOf(reader.readLongArray());
this.skyLight = reader.readVarIntList(r -> r.readBytes(r.readVarInt()));
this.blockLight = reader.readVarIntList(r -> r.readBytes(r.readVarInt()));
this(reader.readBoolean(),
BitSet.valueOf(reader.readLongArray()), BitSet.valueOf(reader.readLongArray()),
BitSet.valueOf(reader.readLongArray()), BitSet.valueOf(reader.readLongArray()),
reader.readVarIntList(r -> r.readBytes(r.readVarInt())), reader.readVarIntList(r -> r.readBytes(r.readVarInt())));
}
@Override
@ -50,16 +29,7 @@ public final class LightData implements Writeable {
writer.writeLongArray(emptySkyMask.toLongArray());
writer.writeLongArray(emptyBlockMask.toLongArray());
writer.writeVarInt(skyLight.size());
for (byte[] bytes : skyLight) {
writer.writeVarInt(2048); // Always 2048 length
writer.writeBytes(bytes);
}
writer.writeVarInt(blockLight.size());
for (byte[] bytes : blockLight) {
writer.writeVarInt(2048); // Always 2048 length
writer.writeBytes(bytes);
}
writer.writeVarIntList(skyLight, BinaryWriter::writeByteArray);
writer.writeVarIntList(blockLight, BinaryWriter::writeByteArray);
}
}

View File

@ -231,6 +231,15 @@ public class BinaryWriter extends OutputStream {
}
}
public void writeByteArray(byte[] array) {
if (array == null) {
writeVarInt(0);
return;
}
writeVarInt(array.length);
writeBytes(array);
}
/**
* Writes a byte array.
* <p>