Skip to content

Commit fd442c3

Browse files
committed
chore: disable direct palette replacement for now
1 parent ca347c3 commit fd442c3

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

src/main/java/net/hollowcube/polar/StreamingPolarLoader.java

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import net.minestom.server.command.builder.arguments.minecraft.ArgumentBlockState;
88
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
99
import net.minestom.server.coordinate.CoordConversion;
10-
import net.minestom.server.instance.Chunk;
1110
import net.minestom.server.instance.InstanceContainer;
1211
import net.minestom.server.instance.Section;
1312
import net.minestom.server.network.NetworkBuffer;
@@ -22,8 +21,7 @@
2221
import static net.hollowcube.polar.PolarLoader.*;
2322
import static net.hollowcube.polar.PolarReader.*;
2423
import static net.hollowcube.polar.UnsafeOps.*;
25-
import static net.minestom.server.instance.palette.PolarPaletteAccessWidener.directReplaceInnerPaletteBiome;
26-
import static net.minestom.server.instance.palette.PolarPaletteAccessWidener.directReplaceInnerPaletteBlock;
24+
import static net.minestom.server.instance.Chunk.CHUNK_SECTION_SIZE;
2725
import static net.minestom.server.network.NetworkBuffer.*;
2826
import static net.minestom.server.network.PolarBufferAccessWidener.networkBufferAddress;
2927
import static net.minestom.server.network.PolarBufferAccessWidener.networkBufferView;
@@ -66,7 +64,7 @@ public void loadAllSequential(@NotNull ReadableByteChannel channel, long fileSiz
6664
// Chunk data
6765
int chunkCount = buffer.read(VAR_INT);
6866
for (int i = 0; i < chunkCount; i++) {
69-
unsafeCacheChunk(instance, readChunk(buffer, minSection, maxSection));
67+
readChunk(buffer, minSection, maxSection);
7068
}
7169

7270
Check.stateCondition(buffer.readableBytes() > 0, "Unexpected extra data at end of buffer");
@@ -114,7 +112,7 @@ private NetworkBuffer readHeader(@NotNull ReadableByteChannel channel, long file
114112
};
115113
}
116114

117-
private @NotNull Chunk readChunk(@NotNull NetworkBuffer buffer, int minSection, int maxSection) {
115+
private void readChunk(@NotNull NetworkBuffer buffer, int minSection, int maxSection) {
118116
final var chunkX = buffer.read(VAR_INT);
119117
final var chunkZ = buffer.read(VAR_INT);
120118
final var chunk = instance.getChunkSupplier().createChunk(instance, chunkX, chunkZ);
@@ -147,19 +145,19 @@ private NetworkBuffer readHeader(@NotNull ReadableByteChannel channel, long file
147145
int[][] heightmaps = readHeightmapData(buffer, worldAccess == null);
148146
if (worldAccess != null) worldAccess.loadHeightmaps(chunk, heightmaps);
149147
else unsafeSetNeedsCompleteHeightmapRefresh(chunk, true);
148+
}
150149

151-
// Load user data
152-
if (version > PolarWorld.VERSION_USERDATA_OPT_BLOCK_ENT_NBT) {
153-
int userDataLength = buffer.read(VAR_INT);
154-
if (worldAccess != null) {
155-
var chunkDataView = networkBufferView(buffer, buffer.readIndex(), userDataLength);
156-
worldAccess.loadChunkData(chunk, chunkDataView);
157-
}
158-
buffer.advanceRead(userDataLength);
150+
unsafeCacheChunk(instance, chunk);
151+
152+
// Load user data
153+
if (version > PolarWorld.VERSION_USERDATA_OPT_BLOCK_ENT_NBT) {
154+
int userDataLength = buffer.read(VAR_INT);
155+
if (worldAccess != null) {
156+
var chunkDataView = networkBufferView(buffer, buffer.readIndex(), userDataLength);
157+
worldAccess.loadChunkData(chunk, chunkDataView);
159158
}
159+
buffer.advanceRead(userDataLength);
160160
}
161-
162-
return chunk;
163161
}
164162

165163
private void readSection(@NotNull NetworkBuffer buffer, @NotNull Section section) {
@@ -172,24 +170,49 @@ private void readSection(@NotNull NetworkBuffer buffer, @NotNull Section section
172170
section.blockPalette().fill(blockPalette[0]);
173171
}
174172
} else {
173+
var blockData = new int[PolarSection.BLOCK_PALETTE_SIZE];
174+
175175
var rawBlockData = buffer.read(LONG_ARRAY);
176176
var bitsPerEntry = (int) Math.ceil(Math.log(blockPalette.length) / Math.log(2));
177+
PaletteUtil.unpack(blockData, rawBlockData, bitsPerEntry);
178+
179+
section.blockPalette().setAll((x, y, z) -> {
180+
int index = y * CHUNK_SECTION_SIZE * CHUNK_SECTION_SIZE + z * CHUNK_SECTION_SIZE + x;
181+
return blockPalette[blockData[index]];
182+
});
177183

178-
int count = computeCount(blockPalette, rawBlockData, bitsPerEntry);
179-
directReplaceInnerPaletteBlock(section.blockPalette(), (byte) bitsPerEntry, count,
180-
blockPalette, rawBlockData);
184+
// Below was some previous logic, leaving it around for now I would like to fix it up.
185+
// System.out.println(Arrays.toString(blockPalette));
186+
// var rawBlockData = buffer.read(LONG_ARRAY);
187+
// var bitsPerEntry = (int) Math.ceil(Math.log(blockPalette.length) / Math.log(2));
188+
//
189+
//// int count = computeCount(blockPalette, rawBlockData, bitsPerEntry);
190+
// int count = 16 * 16 * 16;
191+
// directReplaceInnerPaletteBlock(section.blockPalette(), (byte) bitsPerEntry, count,
192+
// blockPalette, rawBlockData);
181193
}
182194

183195
int[] biomePalette = readBiomePalette(buffer);
184196
if (biomePalette.length == 1) {
185197
section.biomePalette().fill(biomePalette[0]);
186198
} else {
199+
var biomeData = new int[PolarSection.BIOME_PALETTE_SIZE];
200+
187201
var rawBiomeData = buffer.read(LONG_ARRAY);
188202
var bitsPerEntry = (int) Math.ceil(Math.log(biomePalette.length) / Math.log(2));
189-
// Biome count is irrelevant to the client. Though it might be worth computing it anyway here
190-
// in case a server implementation uses it for anything.
191-
directReplaceInnerPaletteBiome(section.biomePalette(), (byte) bitsPerEntry, 4 * 4 * 4,
192-
biomePalette, rawBiomeData);
203+
PaletteUtil.unpack(biomeData, rawBiomeData, bitsPerEntry);
204+
205+
section.biomePalette().setAll((x, y, z) -> {
206+
int index = x / 4 + (z / 4) * 4 + (y / 4) * 16;
207+
return biomePalette[biomeData[index]];
208+
});
209+
210+
// var rawBiomeData = buffer.read(LONG_ARRAY);
211+
// var bitsPerEntry = (int) Math.ceil(Math.log(biomePalette.length) / Math.log(2));
212+
// // Biome count is irrelevant to the client. Though it might be worth computing it anyway here
213+
// // in case a server implementation uses it for anything.
214+
// directReplaceInnerPaletteBiome(section.biomePalette(), (byte) bitsPerEntry, 4 * 4 * 4,
215+
// biomePalette, rawBiomeData);
193216
}
194217

195218
if (version > PolarWorld.VERSION_UNIFIED_LIGHT) {

0 commit comments

Comments
 (0)