Skip to content

Commit

Permalink
Merge pull request retrooper#894 from booky10/fix/chunk-codec-issues
Browse files Browse the repository at this point in the history
Fix chunk codec issues
  • Loading branch information
retrooper committed Jul 23, 2024
2 parents 6a69d1b + 66ca952 commit ef2d24c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.stream.NetStreamInput;
import com.github.retrooper.packetevents.protocol.stream.NetStreamOutput;
import com.github.retrooper.packetevents.protocol.world.chunk.BaseChunk;
import com.github.retrooper.packetevents.protocol.world.chunk.NibbleArray3d;
import com.github.retrooper.packetevents.protocol.world.chunk.palette.DataPalette;
import com.github.retrooper.packetevents.protocol.world.chunk.palette.PaletteType;
import com.github.retrooper.packetevents.protocol.world.states.WrappedBlockState;

public class Chunk_v1_9 implements BaseChunk {
private static final int AIR = 0;
Expand Down Expand Up @@ -56,7 +54,8 @@ public Chunk_v1_9(NetStreamInput in, boolean hasBlockLight, boolean hasSkyLight)
}

if (isSixteen) {
dataPalette = DataPalette.read(in, PaletteType.CHUNK);
// singleton palette got added with 1.18 which isn't supported by this chunk section implementation
dataPalette = DataPalette.read(in, PaletteType.CHUNK, false);
} else {
dataPalette = DataPalette.readLegacy(in);
}
Expand Down Expand Up @@ -117,4 +116,20 @@ public boolean isEmpty() {
// 1.14+, we can rely on the value
return this.blockCount == 0;
}
}

public NibbleArray3d getSkyLight() {
return this.skyLight;
}

public void setSkyLight(NibbleArray3d skyLight) {
this.skyLight = skyLight;
}

public NibbleArray3d getBlockLight() {
return this.blockLight;
}

public void setBlockLight(NibbleArray3d blockLight) {
this.blockLight = blockLight;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ public DataPalette(@NotNull Palette palette, @Nullable BaseStorage storage, Pale
}

public static DataPalette read(NetStreamInput in, PaletteType paletteType) {
return read(in, paletteType, true);
}

public static DataPalette read(NetStreamInput in, PaletteType paletteType, boolean allowSingletonPalette) {
int bitsPerEntry = in.readByte();
Palette palette = readPalette(paletteType, bitsPerEntry, in);
Palette palette = readPalette(paletteType, bitsPerEntry, in, allowSingletonPalette);
BitStorage storage;
if (!(palette instanceof SingletonPalette)) {
int length = in.readVarInt();
Expand Down Expand Up @@ -94,7 +98,7 @@ public static void write(NetStreamOutput out, DataPalette palette) {

public static DataPalette readLegacy(NetStreamInput in) {
int bitsPerEntry = in.readByte() & 0xff;
Palette palette = readPalette(PaletteType.CHUNK, bitsPerEntry, in);
Palette palette = readPalette(PaletteType.CHUNK, bitsPerEntry, in, false);
BaseStorage storage;
if (!(palette instanceof SingletonPalette)) {
int length = in.readVarInt();
Expand Down Expand Up @@ -137,11 +141,16 @@ public int set(int x, int y, int z, int state) {
}
}

private static Palette readPalette(PaletteType paletteType, int bitsPerEntry, NetStreamInput in) {
private static Palette readPalette(
PaletteType paletteType,
int bitsPerEntry,
NetStreamInput in,
boolean allowSingletonPalette
) {
if (bitsPerEntry > paletteType.getMaxBitsPerEntry()) {
return new GlobalPalette();
}
if (bitsPerEntry == 0) {
if (bitsPerEntry == 0 && allowSingletonPalette) {
return new SingletonPalette(in);
}
if (bitsPerEntry <= paletteType.getMinBitsPerEntry()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ public void read() {

boolean hasBlocklight = (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_16) || serverVersion.isOlderThan(ServerVersion.V_1_14))
&& !serverVersion.isOlderThanOrEquals(ServerVersion.V_1_8_8);
boolean checkForSky = serverVersion.isNewerThanOrEquals(ServerVersion.V_1_16) || serverVersion.isOlderThanOrEquals(ServerVersion.V_1_8_8) || user.getDimension().getId() == 0;
boolean checkForSky = serverVersion.isNewerThanOrEquals(ServerVersion.V_1_16) || serverVersion.isOlderThanOrEquals(ServerVersion.V_1_8_8)
|| (user.getDimension().getId() == 0 && serverVersion.isOlderThan(ServerVersion.V_1_14));

// 1.7/1.8 don't use this NetStreamInput
NetStreamInput dataIn = serverVersion.isNewerThanOrEquals(ServerVersion.V_1_9) ? new NetStreamInput(new ByteArrayInputStream(data)) : null;
Expand Down Expand Up @@ -433,4 +434,4 @@ private ChunkReader getChunkReader() {
return chunkReader_v1_7;
}
}
}
}

0 comments on commit ef2d24c

Please sign in to comment.