Skip to content

Commit

Permalink
Update BlueNBT
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Sep 13, 2024
1 parent 57e0e5e commit 79fd4d0
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
package de.bluecolored.bluemap.core.map.renderstate;

import com.flowpowered.math.vector.Vector2i;
import com.google.gson.reflect.TypeToken;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.storage.GridStorage;
import de.bluecolored.bluemap.core.storage.compression.CompressedInputStream;
import de.bluecolored.bluemap.core.util.Key;
import de.bluecolored.bluemap.core.util.PalettedArrayAdapter;
import de.bluecolored.bluemap.core.util.RegistryAdapter;
import de.bluecolored.bluenbt.BlueNBT;
import de.bluecolored.bluenbt.TypeToken;
import lombok.Getter;

import java.io.IOException;
Expand All @@ -44,8 +44,8 @@ abstract class CellStorage<T extends CellStorage.Cell> {

private static final BlueNBT BLUE_NBT = new BlueNBT();
static {
BLUE_NBT.register(TypeToken.get(TileState.class), new RegistryAdapter<>(TileState.REGISTRY, Key.BLUEMAP_NAMESPACE, TileState.UNKNOWN));
BLUE_NBT.register(TypeToken.get(TileState[].class), new PalettedArrayAdapter<>(BLUE_NBT, TileState.class));
BLUE_NBT.register(TypeToken.of(TileState.class), new RegistryAdapter<>(TileState.REGISTRY, Key.BLUEMAP_NAMESPACE, TileState.UNKNOWN));
BLUE_NBT.register(TypeToken.of(TileState[].class), new PalettedArrayAdapter<>(BLUE_NBT, TileState.class));
}

private static final int CACHE_SIZE = 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
*/
package de.bluecolored.bluemap.core.map.renderstate;

import com.google.gson.reflect.TypeToken;
import de.bluecolored.bluemap.core.util.Key;
import de.bluecolored.bluemap.core.util.RegistryAdapter;
import de.bluecolored.bluenbt.BlueNBT;
import de.bluecolored.bluenbt.NBTName;
import de.bluecolored.bluenbt.NBTPostDeserialize;
import de.bluecolored.bluenbt.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
Expand Down Expand Up @@ -132,7 +129,7 @@ private static class PaletteOnly {

private final static BlueNBT BLUE_NBT = new BlueNBT();
static {
BLUE_NBT.register(TypeToken.get(TileState.class), new RegistryAdapter<>(TileState.REGISTRY, Key.BLUEMAP_NAMESPACE, TileState.UNKNOWN));
BLUE_NBT.register(TypeToken.of(TileState.class), new RegistryAdapter<>(TileState.REGISTRY, Key.BLUEMAP_NAMESPACE, TileState.UNKNOWN));
}

@NBTName("tile-states")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
*/
package de.bluecolored.bluemap.core.util;

import com.google.gson.reflect.TypeToken;
import de.bluecolored.bluenbt.BlueNBT;
import de.bluecolored.bluenbt.NBTReader;
import de.bluecolored.bluenbt.NBTWriter;
import de.bluecolored.bluenbt.TypeAdapter;
import de.bluecolored.bluenbt.*;
import de.bluecolored.bluenbt.adapter.ArrayAdapterFactory;
import lombok.RequiredArgsConstructor;

Expand All @@ -45,7 +41,7 @@ public class PalettedArrayAdapter<T> implements TypeAdapter<T[]> {
@SuppressWarnings("unchecked")
public PalettedArrayAdapter(BlueNBT blueNBT, Class<T> type) {
this.type = type;
this.paletteAdapter = ArrayAdapterFactory.INSTANCE.create((TypeToken<T[]>) TypeToken.getArray(type), blueNBT).orElseThrow();
this.paletteAdapter = ArrayAdapterFactory.INSTANCE.create((TypeToken<T[]>) TypeToken.array(type), blueNBT).orElseThrow();
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@
*/
package de.bluecolored.bluemap.core.world.block.entity;

import com.google.gson.reflect.TypeToken;
import de.bluecolored.bluemap.core.util.Key;
import de.bluecolored.bluenbt.BlueNBT;
import de.bluecolored.bluenbt.NBTDeserializer;
import de.bluecolored.bluenbt.NBTReader;
import de.bluecolored.bluenbt.TypeDeserializer;
import de.bluecolored.bluenbt.*;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -65,7 +61,7 @@ public static class BlockEntityDeserializer implements TypeDeserializer<BlockEnt
@Override
@SuppressWarnings("unchecked")
public @Nullable BlockEntity read(NBTReader reader) throws IOException {
Map<String, Object> raw = (Map<String, Object>) blueNBT.read(reader, TypeToken.getParameterized(Map.class, String.class, Object.class));
Map<String, Object> raw = (Map<String, Object>) blueNBT.read(reader, TypeToken.of(Map.class, String.class, Object.class));

String id = (String) raw.get("id");
if (id == null) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
*/
package de.bluecolored.bluemap.core.world.mca;

import com.google.gson.reflect.TypeToken;
import de.bluecolored.bluemap.core.util.Key;
import de.bluecolored.bluemap.core.world.BlockState;
import de.bluecolored.bluemap.core.world.block.entity.BlockEntity;
import de.bluecolored.bluemap.core.world.mca.data.BlockStateDeserializer;
import de.bluecolored.bluemap.core.world.mca.data.KeyDeserializer;
import de.bluecolored.bluenbt.BlueNBT;
import de.bluecolored.bluenbt.NamingStrategy;
import de.bluecolored.bluenbt.TypeToken;
import org.jetbrains.annotations.Contract;

public class MCAUtil {
Expand All @@ -39,26 +40,13 @@ public class MCAUtil {

@Contract(value = "_ -> param1", mutates = "param1")
public static BlueNBT addCommonNbtAdapters(BlueNBT nbt) {
nbt.register(TypeToken.get(BlockState.class), new BlockStateDeserializer());
nbt.register(TypeToken.get(Key.class), new KeyDeserializer());
nbt.register(TypeToken.get(BlockEntity.class), new BlockEntity.BlockEntityDeserializer(nbt));
nbt.setNamingStrategy(NamingStrategy.lowerCaseWithDelimiter("_"));
nbt.register(TypeToken.of(BlockState.class), new BlockStateDeserializer());
nbt.register(TypeToken.of(Key.class), new KeyDeserializer());
nbt.register(TypeToken.of(BlockEntity.class), new BlockEntity.BlockEntityDeserializer(nbt));
return nbt;
}

/**
* Having a long array where each long contains as many values as fit in it without overflowing, returning the "valueIndex"-th value when each value has "bitsPerValue" bits.
*/
public static long getValueFromLongArray(long[] data, int valueIndex, int bitsPerValue) {
int valuesPerLong = 64 / bitsPerValue;
int longIndex = valueIndex / valuesPerLong;
int bitIndex = (valueIndex % valuesPerLong) * bitsPerValue;

if (longIndex >= data.length) return 0;
long value = data[longIndex] >>> bitIndex;

return value & (0xFFFFFFFFFFFFFFFFL >>> -bitsPerValue);
}

/**
* Treating the long array "data" as a continuous stream of bits, returning the "valueIndex"-th value when each value has "bitsPerValue" bits.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.flowpowered.math.vector.Vector3i;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.gson.reflect.TypeToken;
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.resources.pack.datapack.DataPack;
Expand All @@ -43,6 +42,7 @@
import de.bluecolored.bluemap.core.world.mca.data.LevelData;
import de.bluecolored.bluemap.core.world.mca.region.RegionType;
import de.bluecolored.bluenbt.BlueNBT;
import de.bluecolored.bluenbt.TypeToken;
import lombok.Getter;
import lombok.ToString;

Expand Down Expand Up @@ -286,7 +286,7 @@ public static Path resolveDimensionFolder(Path worldFolder, Key dimension) {

private static BlueNBT createBlueNBTForDataPack(DataPack dataPack) {
BlueNBT blueNBT = MCAUtil.addCommonNbtAdapters(new BlueNBT());
blueNBT.register(TypeToken.get(DimensionType.class), new DimensionTypeDeserializer(blueNBT, dataPack));
blueNBT.register(TypeToken.of(DimensionType.class), new DimensionTypeDeserializer(blueNBT, dataPack));
return blueNBT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,38 +292,68 @@ public int getSectionY() {
@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class Data extends MCAChunk.Data {

@NBTName("Level")
private Level level = new Level();

}

@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class Level {

@NBTName("Status")
private Key status = STATUS_EMPTY;

@NBTName("InhabitedTime")
private long inhabitedTime = 0;

@NBTName("Heightmaps")
private HeightmapsData heightmaps = new HeightmapsData();

@NBTName("Sections")
private SectionData @Nullable [] sections = null;

@NBTName("Biomes")
private int[] biomes = EMPTY_INT_ARRAY;

@NBTName("TileEntities")
@NBTDeserializer(LenientBlockEntityArrayDeserializer.class)
private @Nullable BlockEntity [] blockEntities = EMPTY_BLOCK_ENTITIES_ARRAY;

}

@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class HeightmapsData {
@NBTName("WORLD_SURFACE") private long[] worldSurface = EMPTY_LONG_ARRAY;
@NBTName("OCEAN_FLOOR") private long[] oceanFloor = EMPTY_LONG_ARRAY;

@NBTName("WORLD_SURFACE")
private long[] worldSurface = EMPTY_LONG_ARRAY;

@NBTName("OCEAN_FLOOR")
private long[] oceanFloor = EMPTY_LONG_ARRAY;

}

@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class SectionData {

@NBTName("Y")
private int y = 0;

@NBTName("BlockLight")
private byte[] blockLight = EMPTY_BYTE_ARRAY;

@NBTName("SkyLight")
private byte[] skyLight = EMPTY_BYTE_ARRAY;

@NBTName("Palette")
private BlockState[] palette = EMPTY_BLOCKSTATE_ARRAY;

@NBTName("BlockStates")
private long[] blockStates = EMPTY_LONG_ARRAY;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -279,38 +279,68 @@ public int getSectionY() {
@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class Data extends MCAChunk.Data {

@NBTName("Level")
private Level level = new Level();

}

@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class Level {

@NBTName("Status")
private Key status = STATUS_EMPTY;

@NBTName("InhabitedTime")
private long inhabitedTime = 0;

@NBTName("Heightmaps")
private HeightmapsData heightmaps = new HeightmapsData();

@NBTName("Sections")
private SectionData @Nullable [] sections = null;

@NBTName("Biomes")
private int[] biomes = EMPTY_INT_ARRAY;

@NBTName("TileEntities")
@NBTDeserializer(LenientBlockEntityArrayDeserializer.class)
private @Nullable BlockEntity [] blockEntities = EMPTY_BLOCK_ENTITIES_ARRAY;

}

@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class HeightmapsData {
@NBTName("WORLD_SURFACE") private long[] worldSurface = EMPTY_LONG_ARRAY;
@NBTName("OCEAN_FLOOR") private long[] oceanFloor = EMPTY_LONG_ARRAY;

@NBTName("WORLD_SURFACE")
private long[] worldSurface = EMPTY_LONG_ARRAY;

@NBTName("OCEAN_FLOOR")
private long[] oceanFloor = EMPTY_LONG_ARRAY;

}

@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class SectionData {

@NBTName("Y")
private int y = 0;

@NBTName("BlockLight")
private byte[] blockLight = EMPTY_BYTE_ARRAY;

@NBTName("SkyLight")
private byte[] skyLight = EMPTY_BYTE_ARRAY;

@NBTName("Palette")
private BlockState[] palette = EMPTY_BLOCKSTATE_ARRAY;

@NBTName("BlockStates")
private long[] blockStates = EMPTY_LONG_ARRAY;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -294,45 +294,72 @@ public int getSectionY() {
@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class Data extends MCAChunk.Data {

@NBTName("Status")
private Key status = STATUS_EMPTY;

@NBTName("InhabitedTime")
private long inhabitedTime = 0;

@NBTName("Heightmaps")
private HeightmapsData heightmaps = new HeightmapsData();

private SectionData @Nullable [] sections = null;

@NBTName("block_entities")
@NBTDeserializer(LenientBlockEntityArrayDeserializer.class)
private @Nullable BlockEntity [] blockEntities = EMPTY_BLOCK_ENTITIES_ARRAY;

}

@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class HeightmapsData {
@NBTName("WORLD_SURFACE") private long[] worldSurface = EMPTY_LONG_ARRAY;
@NBTName("OCEAN_FLOOR") private long[] oceanFloor = EMPTY_LONG_ARRAY;

@NBTName("WORLD_SURFACE")
private long[] worldSurface = EMPTY_LONG_ARRAY;

@NBTName("OCEAN_FLOOR")
private long[] oceanFloor = EMPTY_LONG_ARRAY;

}

@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class SectionData {

@NBTName("Y")
private int y = 0;

@NBTName("BlockLight")
private byte[] blockLight = EMPTY_BYTE_ARRAY;

@NBTName("SkyLight")
private byte[] skyLight = EMPTY_BYTE_ARRAY;
@NBTName("block_states") private BlockStatesData blockStates = new BlockStatesData();

private BlockStatesData blockStates = new BlockStatesData();

private BiomesData biomes = new BiomesData();

}

@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class BlockStatesData {

private BlockState[] palette = EMPTY_BLOCKSTATE_ARRAY;

private long[] data = EMPTY_LONG_ARRAY;

}

@Getter
@SuppressWarnings("FieldMayBeFinal")
public static class BiomesData {

private Key[] palette = EMPTY_KEY_ARRAY;

private long[] data = EMPTY_LONG_ARRAY;

}

}
Loading

0 comments on commit 79fd4d0

Please sign in to comment.