Skip to content

Commit

Permalink
Update chunk system to work with latest Via again
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Oct 20, 2023
1 parent 6d2ae22 commit 5ab8093
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.CustomByteType;
import com.viaversion.viaversion.api.type.types.FixedByteArrayType;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.data.BlockList1_6;
import net.raphimc.vialegacy.api.model.ChunkCoord;
Expand Down Expand Up @@ -220,8 +220,8 @@ public void register() {
final ClassicLevel level = levelStorage.getClassicLevel();

final int count = wrapper.read(Type.UNSIGNED_BYTE) + 1; // count
final byte[] indices = wrapper.read(new CustomByteType(1024)); // indices
final byte[] blocks = wrapper.read(new CustomByteType(256)); // blocks
final byte[] indices = wrapper.read(new FixedByteArrayType(1024)); // indices
final byte[] blocks = wrapper.read(new FixedByteArrayType(256)); // blocks

if (wrapper.user().getProtocolInfo().getPipeline().contains(Protocola1_0_16_2toa1_0_15.class)) {
final Map<ChunkCoord, List<BlockChangeRecord>> records = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.*;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.CustomByteType;
import com.viaversion.viaversion.api.type.types.chunk.BaseChunkType;
import com.viaversion.viaversion.api.type.types.FixedByteArrayType;
import io.netty.buffer.ByteBuf;
import net.raphimc.vialegacy.api.model.IdAndData;
import net.raphimc.vialegacy.protocols.release.protocol1_2_1_3to1_1.chunks.NibbleArray1_1;
Expand All @@ -38,11 +37,6 @@ public ChunkType1_1() {
super(Chunk.class);
}

@Override
public Class<? extends Type> getBaseClass() {
return BaseChunkType.class;
}

@Override
public Chunk read(ByteBuf byteBuf) throws Exception {
final int xPosition = byteBuf.readInt();
Expand All @@ -52,7 +46,7 @@ public Chunk read(ByteBuf byteBuf) throws Exception {
final int ySize = byteBuf.readUnsignedByte() + 1;
final int zSize = byteBuf.readUnsignedByte() + 1;
final int chunkSize = byteBuf.readInt();
final byte[] compressedData = new CustomByteType(chunkSize).read(byteBuf);
final byte[] compressedData = new FixedByteArrayType(chunkSize).read(byteBuf);
final byte[] uncompressedData = new byte[(xSize * ySize * zSize * 5) / 2];
final Inflater inflater = new Inflater();
inflater.setInput(compressedData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public void register() {

if (!load) {
final Chunk chunk = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], null, new ArrayList<>());
wrapper.write(new ChunkType1_7_6(wrapper.user().get(ClientWorld.class)), chunk);
wrapper.write(ChunkType1_7_6.forEnvironment(wrapper.user().get(ClientWorld.class).getEnvironment()), chunk);
} else {
wrapper.cancel();
}
Expand All @@ -470,7 +470,7 @@ public void register() {
@Override
public void register() {
handler(wrapper -> {
final ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
final Environment environment = wrapper.user().get(ClientWorld.class).getEnvironment();
Chunk chunk = wrapper.read(Types1_2_4.CHUNK);

wrapper.user().get(ChestStateTracker.class).unload(chunk.getX(), chunk.getZ());
Expand All @@ -481,22 +481,22 @@ public void register() {
for (int i = 0; i < chunk.getSections().length; i++) {
final ChunkSection section = chunk.getSections()[i] = new ChunkSectionImpl(true);
section.palette(PaletteType.BLOCKS).addId(0);
if (clientWorld.getEnvironment() == Environment.NORMAL) {
if (environment == Environment.NORMAL) {
final byte[] skyLight = new byte[2048];
Arrays.fill(skyLight, (byte) 255);
section.getLight().setSkyLight(skyLight);
}
}
}

if (clientWorld.getEnvironment() != Environment.NORMAL) {
if (environment != Environment.NORMAL) {
for (ChunkSection section : chunk.getSections()) {
if (section != null) {
section.getLight().setSkyLight(null);
}
}
}
wrapper.write(new ChunkType1_7_6(clientWorld), chunk);
wrapper.write(ChunkType1_7_6.forEnvironment(environment), chunk);
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,36 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_3_1_2to1_2_4_5.types;

import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.Environment;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection;
import io.netty.buffer.ByteBuf;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkType1_7_6;

public class ChunkType1_2_4 extends ChunkType1_7_6 {

private static final ClientWorld OVERWORLD = new ClientWorld(null);

static {
OVERWORLD.setEnvironment(Environment.NORMAL.id());
}

public ChunkType1_2_4() {
super(OVERWORLD);
super(true);
}

@Override
protected void readUnusedInt(ByteBuf byteBuf, ClientWorld clientWorld) {
protected void readUnusedInt(ByteBuf byteBuf) {
byteBuf.readInt();
}

@Override
protected void writeUnusedInt(ByteBuf byteBuf, ClientWorld clientWorld, Chunk chunk) {
protected void writeUnusedInt(ByteBuf byteBuf, Chunk chunk) {
byteBuf.writeInt(0);
}

@Override
public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk chunk) throws Exception {
public void write(ByteBuf byteBuf, Chunk chunk) throws Exception {
for (ChunkSection section : chunk.getSections()) {
if (section != null && !section.getLight().hasSkyLight()) {
throw new IllegalStateException("Chunk section does not have skylight");
}
}

super.write(byteBuf, clientWorld, chunk);
super.write(byteBuf, chunk);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.Lists;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.Environment;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
Expand All @@ -30,13 +31,13 @@
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter;
import net.raphimc.vialegacy.api.splitter.PreNettySplitter;
import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.rewriter.ItemRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types.ChunkBulkType1_4_4;
import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types.BulkChunkType1_4_4;
import net.raphimc.vialegacy.protocols.release.protocol1_5_0_1to1_4_6_7.ClientboundPackets1_4_6;
import net.raphimc.vialegacy.protocols.release.protocol1_6_1to1_5_2.ServerboundPackets1_5_2;
import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.types.MetaType1_6_4;
import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.types.Types1_6_4;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.metadata.MetaIndex1_8to1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6;

public class Protocol1_4_6_7to1_4_4_5 extends StatelessProtocol<ClientboundPackets1_4_4, ClientboundPackets1_4_6, ServerboundPackets1_5_2, ServerboundPackets1_5_2> {
Expand Down Expand Up @@ -127,10 +128,7 @@ public void register() {
this.registerClientbound(ClientboundPackets1_4_4.MAP_BULK_CHUNK, new PacketHandlers() {
@Override
public void register() {
handler(wrapper -> {
final ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
wrapper.write(new ChunkBulkType1_7_6(clientWorld), wrapper.read(new ChunkBulkType1_4_4(clientWorld)));
});
map(BulkChunkType1_7_6.TYPE, BulkChunkType1_4_4.TYPE);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,18 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types;

import com.viaversion.viaversion.api.minecraft.ClientWorld;
import io.netty.buffer.ByteBuf;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6;

public class ChunkBulkType1_4_4 extends ChunkBulkType1_7_6 {

public ChunkBulkType1_4_4(ClientWorld clientWorld) {
super(clientWorld);
}
public class BulkChunkType1_4_4 extends BulkChunkType1_7_6 {

@Override
protected boolean readHasSkyLight(ByteBuf byteBuf, ClientWorld clientWorld) {
protected boolean readHasSkyLight(ByteBuf byteBuf) {
return true;
}

@Override
protected void writeHasSkyLight(ByteBuf byteBuf, ClientWorld clientWorld, boolean hasSkyLight) {
protected void writeHasSkyLight(ByteBuf byteBuf, boolean hasSkyLight) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.GameProfile;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.providers.GameProfileFetcher;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.MetaType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6;

Expand Down Expand Up @@ -421,7 +421,7 @@ public void register() {
@Override
public void register() {
handler(wrapper -> {
final Chunk chunk = wrapper.passthrough(new ChunkType1_7_6(wrapper.user().get(ClientWorld.class)));
final Chunk chunk = wrapper.passthrough(ChunkType1_7_6.forEnvironment(wrapper.user().get(ClientWorld.class).getEnvironment()));
wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk);
});
}
Expand Down Expand Up @@ -486,7 +486,7 @@ public void register() {
@Override
public void register() {
handler(wrapper -> {
final Chunk[] chunks = wrapper.passthrough(new ChunkBulkType1_7_6(wrapper.user().get(ClientWorld.class)));
final Chunk[] chunks = wrapper.passthrough(BulkChunkType1_7_6.TYPE);
for (Chunk chunk : chunks) {
wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.Environment;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10;
Expand Down Expand Up @@ -63,7 +64,7 @@
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.ItemRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.TranslationRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.storage.*;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6;

Expand Down Expand Up @@ -633,9 +634,11 @@ public void register() {
@Override
public void register() {
handler(wrapper -> {
final Chunk chunk = wrapper.read(new ChunkType1_7_6(wrapper.user().get(ClientWorld.class)));
final Environment environment = wrapper.user().get(ClientWorld.class).getEnvironment();

final Chunk chunk = wrapper.read(ChunkType1_7_6.forEnvironment(environment));
wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk);
wrapper.write(new ChunkType1_8(wrapper.user().get(ClientWorld.class)), chunk);
wrapper.write(ChunkType1_8.forEnvironment(environment), chunk);
});
}
});
Expand Down Expand Up @@ -696,11 +699,11 @@ public void register() {
@Override
public void register() {
handler(wrapper -> {
final Chunk[] chunks = wrapper.read(new ChunkBulkType1_7_6(wrapper.user().get(ClientWorld.class)));
final Chunk[] chunks = wrapper.read(BulkChunkType1_7_6.TYPE);
for (Chunk chunk : chunks) {
wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk);
}
wrapper.write(new BulkChunkType1_8(wrapper.user().get(ClientWorld.class)), chunks);
wrapper.write(BulkChunkType1_8.TYPE, chunks);
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types;

import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.type.PartialType;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.CustomByteType;
import com.viaversion.viaversion.api.type.types.chunk.BaseChunkBulkType;
import com.viaversion.viaversion.api.type.types.FixedByteArrayType;
import com.viaversion.viaversion.util.Pair;
import io.netty.buffer.ByteBuf;

Expand All @@ -32,45 +29,40 @@
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class ChunkBulkType1_7_6 extends PartialType<Chunk[], ClientWorld> {
public class BulkChunkType1_7_6 extends Type<Chunk[]> {

public ChunkBulkType1_7_6(final ClientWorld clientWorld) {
super(clientWorld, Chunk[].class);
}
public static final BulkChunkType1_7_6 TYPE = new BulkChunkType1_7_6();

@Override
public Class<? extends Type> getBaseClass() {
return BaseChunkBulkType.class;
public BulkChunkType1_7_6() {
super(Chunk[].class);
}

/**
* This method is here to allow overriding the code for 1.4.5 -{@literal >} 1.4.7
*
* @param byteBuf The buffer
* @param clientWorld The ClientWorld
* @param byteBuf The buffer
* @return Read skylight array or not
*/
protected boolean readHasSkyLight(final ByteBuf byteBuf, final ClientWorld clientWorld) {
protected boolean readHasSkyLight(final ByteBuf byteBuf) {
return byteBuf.readBoolean();
}

/**
* This method is here to allow overriding the code for 1.4.5 -{@literal >} 1.4.7
*
* @param byteBuf The buffer
* @param clientWorld The ClientWorld
* @param hasSkyLight Has skylight
*/
protected void writeHasSkyLight(final ByteBuf byteBuf, final ClientWorld clientWorld, final boolean hasSkyLight) {
protected void writeHasSkyLight(final ByteBuf byteBuf, final boolean hasSkyLight) {
byteBuf.writeBoolean(hasSkyLight);
}

@Override
public Chunk[] read(ByteBuf byteBuf, ClientWorld clientWorld) throws Exception {
public Chunk[] read(ByteBuf byteBuf) throws Exception {
final short chunkCount = byteBuf.readShort();
final int compressedSize = byteBuf.readInt();
final boolean hasSkyLight = this.readHasSkyLight(byteBuf, clientWorld);
final byte[] data = new CustomByteType(compressedSize).read(byteBuf);
final boolean hasSkyLight = this.readHasSkyLight(byteBuf);
final byte[] data = new FixedByteArrayType(compressedSize).read(byteBuf);
final int[] chunkX = new int[chunkCount];
final int[] chunkZ = new int[chunkCount];
final short[] primaryBitMask = new short[chunkCount];
Expand Down Expand Up @@ -106,7 +98,7 @@ public Chunk[] read(ByteBuf byteBuf, ClientWorld clientWorld) throws Exception {
}

@Override
public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk[] chunks) throws Exception {
public void write(ByteBuf byteBuf, Chunk[] chunks) throws Exception {
final int chunkCount = chunks.length;
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final int[] chunkX = new int[chunkCount];
Expand Down Expand Up @@ -139,7 +131,7 @@ public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk[] chunks) thro

byteBuf.writeShort(chunkCount);
byteBuf.writeInt(compressedSize);
this.writeHasSkyLight(byteBuf, clientWorld, true);
this.writeHasSkyLight(byteBuf, true);
byteBuf.writeBytes(compressedData, 0, compressedSize);

for (int i = 0; i < chunkCount; i++) {
Expand Down
Loading

0 comments on commit 5ab8093

Please sign in to comment.