Skip to content

Commit

Permalink
BlockStorage17 refactor + rare 1.8 warning fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hevav committed Jan 27, 2023
1 parent 09d726f commit 2d7ebd8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public interface BlockStorage {

void write(Object byteBufObject, ProtocolVersion version);
void write(Object byteBufObject, ProtocolVersion version, int pass);

void set(int posX, int posY, int posZ, @NonNull VirtualBlock block);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class BlockStorage17 implements BlockStorage {

private final VirtualBlock[] blocks;

private int pass;

public BlockStorage17() {
this(new VirtualBlock[SimpleChunk.MAX_BLOCKS_PER_SECTION]);
}
Expand All @@ -43,10 +41,10 @@ private BlockStorage17(VirtualBlock[] blocks) {
}

@Override
public void write(Object byteBufObject, ProtocolVersion version) {
public void write(Object byteBufObject, ProtocolVersion version, int pass) {
Preconditions.checkArgument(byteBufObject instanceof ByteBuf);
ByteBuf buf = (ByteBuf) byteBufObject;
if (this.pass == 0) {
if (pass == 0) {
if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) < 0) {
byte[] raw = new byte[this.blocks.length];
for (int i = 0; i < this.blocks.length; ++i) {
Expand All @@ -66,17 +64,14 @@ public void write(Object byteBufObject, ProtocolVersion version) {
buf.writeShortLE(s);
}
}

++this.pass;
} else if (this.pass == 1) {
} else if (pass == 1 && version.compareTo(ProtocolVersion.MINECRAFT_1_8) < 0) {
NibbleArray3D metadata = new NibbleArray3D(SimpleChunk.MAX_BLOCKS_PER_SECTION);
for (int i = 0; i < this.blocks.length; ++i) {
VirtualBlock block = this.blocks[i];
metadata.set(i, block == null ? 0 : block.getBlockStateID(ProtocolVersion.MINECRAFT_1_7_2) & 0xFFFF);
}

buf.writeBytes(metadata.getData());
this.pass = 0;
}
}

Expand Down Expand Up @@ -106,7 +101,6 @@ public BlockStorage copy() {
public String toString() {
return "BlockStorage17{"
+ "blocks=" + Arrays.toString(this.blocks)
+ ", pass=" + this.pass
+ "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private BlockStorage19(ProtocolVersion version, List<VirtualBlock> palette, Map<
}

@Override
public void write(Object byteBufObject, ProtocolVersion version) {
public void write(Object byteBufObject, ProtocolVersion version, int pass) {
Preconditions.checkArgument(byteBufObject instanceof ByteBuf);
ByteBuf buf = (ByteBuf) byteBufObject;
buf.writeByte(this.storage.getBitsPerEntry());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,13 @@ public int getDataLength(ProtocolVersion version) {
public void writeData(ByteBuf buf, int pass, ProtocolVersion version) {
if (version.compareTo(ProtocolVersion.MINECRAFT_1_9) < 0) {
BlockStorage storage = this.ensureStorageCreated(version);
if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) < 0) {
this.write17Data(buf, pass, storage);
} else {
this.write18Data(buf, pass, storage);
}
this.write17Data(buf, storage, version, pass);
} else if (pass == 0) {
BlockStorage storage = this.ensureStorageCreated(version);
if (version.compareTo(ProtocolVersion.MINECRAFT_1_14) < 0) {
this.write19Data(buf, storage, version);
this.write19Data(buf, storage, version, pass);
} else {
this.write114Data(buf, storage, version);
this.write114Data(buf, storage, version, pass);

if (version.compareTo(ProtocolVersion.MINECRAFT_1_17_1) > 0) {
this.write118Biomes(buf, version);
Expand Down Expand Up @@ -113,39 +109,27 @@ private BlockStorage createStorage(ProtocolVersion version) {
}
}

private void write17Data(ByteBuf buf, int pass, BlockStorage storage) {
if (pass == 0) {
storage.write(buf, ProtocolVersion.MINECRAFT_1_7_2);
} else if (pass == 1) {
storage.write(buf, ProtocolVersion.MINECRAFT_1_7_2);
private void write17Data(ByteBuf buf, BlockStorage storage, ProtocolVersion version, int pass) {
if (pass == 0 || pass == 1) {
storage.write(buf, version, pass);
} else if (pass == 2) {
buf.writeBytes(this.blockLight.getData());
} else if (pass == 3 && this.skyLight != null) {
buf.writeBytes(this.skyLight.getData());
}
}

private void write18Data(ByteBuf buf, int pass, BlockStorage storage) {
if (pass == 0) {
storage.write(buf, ProtocolVersion.MINECRAFT_1_8);
} else if (pass == 1) {
buf.writeBytes(this.blockLight.getData());
} else if (pass == 2 && this.skyLight != null) {
buf.writeBytes(this.skyLight.getData());
}
}

private void write19Data(ByteBuf buf, BlockStorage storage, ProtocolVersion version) {
storage.write(buf, version);
private void write19Data(ByteBuf buf, BlockStorage storage, ProtocolVersion version, int pass) {
storage.write(buf, version, pass);
buf.writeBytes(this.blockLight.getData());
if (this.skyLight != null) {
buf.writeBytes(this.skyLight.getData());
}
}

private void write114Data(ByteBuf buf, BlockStorage storage, ProtocolVersion version) {
private void write114Data(ByteBuf buf, BlockStorage storage, ProtocolVersion version, int pass) {
buf.writeShort(this.blockCount);
storage.write(buf, version);
storage.write(buf, version, pass);
}

private void write118Biomes(ByteBuf buf, ProtocolVersion version) {
Expand Down

0 comments on commit 2d7ebd8

Please sign in to comment.