Skip to content

Commit

Permalink
Implemented sodium fix for classic worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Mar 24, 2024
1 parent a55de5e commit 0a8e70e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.*;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.util.ChunkUtil;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.model.ChunkCoord;
import net.raphimc.vialegacy.api.util.ChunkCoordSpiral;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void addDataPart(final byte[] part, final int partSize) {
this.netBuffer.write(part, 0, partSize);
}

public void finish(final int sizeX, final int sizeY, final int sizeZ) {
public void finish(final int sizeX, final int sizeY, final int sizeZ) throws Exception {
try {
final DataInputStream dis = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(this.netBuffer.toByteArray()), 64 * 1024));
final byte[] blocks = new byte[dis.readInt()];
Expand All @@ -95,6 +96,20 @@ public void finish(final int sizeX, final int sizeY, final int sizeZ) {
this.subChunkZLength = Math.min(16, sizeZ);
this.sectionBitmask = 0;
for (int i = 0; i < this.sectionYCount; i++) this.sectionBitmask = (this.sectionBitmask << 1) | 1;

{ // Sodium fix (Sodium requires a ring of empty chunks around the loaded chunks)
for (int chunkX = -1; chunkX <= this.chunkXCount; chunkX++) {
for (int chunkZ = -1; chunkZ <= this.chunkZCount; chunkZ++) {
if (chunkX < 0 || chunkX >= this.chunkXCount || chunkZ < 0 || chunkZ >= this.chunkZCount) {
final Chunk chunk = ChunkUtil.createEmptyChunk(chunkX, chunkZ, Math.max(8, this.sectionYCount), this.sectionBitmask);
ChunkUtil.setDummySkylight(chunk, true);
final PacketWrapper chunkData = PacketWrapper.create(ClientboundPacketsa1_0_15.CHUNK_DATA, this.getUser());
chunkData.write(Types1_1.CHUNK, chunk);
chunkData.send(Protocola1_0_15toc0_30.class);
}
}
}
}
}

public void tick() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.Environment;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.*;
import com.viaversion.viaversion.api.minecraft.chunks.BaseChunk;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
Expand All @@ -36,6 +38,7 @@
import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.util.ChunkUtil;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.data.BlockList1_6;
import net.raphimc.vialegacy.api.model.IdAndData;
Expand Down Expand Up @@ -68,7 +71,6 @@
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
Expand Down Expand Up @@ -454,15 +456,9 @@ public void register() {

if (chunk.isFullChunk() && chunk.getBitmask() == 0) { // Remap to empty chunk
ViaLegacy.getPlatform().getLogger().warning("Received empty 1.2.5 chunk packet");
chunk = new BaseChunk(chunk.getX(), chunk.getZ(), true, false, 65535, new ChunkSection[16], new int[256], new ArrayList<>());
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 (dimension == Environment.NORMAL) {
final byte[] skyLight = new byte[2048];
Arrays.fill(skyLight, (byte) 255);
section.getLight().setSkyLight(skyLight);
}
chunk = ChunkUtil.createEmptyChunk(chunk.getX(), chunk.getZ());
if (dimension == Environment.NORMAL) {
ChunkUtil.setDummySkylight(chunk, true);
}
}

Expand Down

0 comments on commit 0a8e70e

Please sign in to comment.